-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathandroid_handler_test.go
139 lines (126 loc) · 3.84 KB
/
android_handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_androidHandler_isPackage(t *testing.T) {
type args struct {
filename string
}
tests := []struct {
name string
args args
want bool
}{
{"filename", args{"AndroidmanifesT.xmL"}, true},
{"filename with path and \\", args{"c:/dev/androidmanifest.xml"}, true},
{"filename with path and /", args{"c:\\dev\\androidmanifest.xml"}, true},
{"filename with path", args{"c:\\dev/androidmanifest.xml"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := new(androidHandler)
if got := handler.isPackage(tt.args.filename); got != tt.want {
t.Errorf("isAndroidPackage() = %v, want %v", got, tt.want)
}
})
}
}
func Test_androidHandler_getPackageInfo(t *testing.T) {
handler := new(androidHandler)
currentVersion, err := handler.getPackageInfo(filePath)
require.NoError(t, err)
assert.Equal(t, "1.0.1", currentVersion.Version)
assert.Equal(t, "1000100", currentVersion.InternalVersion)
}
func Test_androidHandler_changePackageVersion(t *testing.T) {
handler := new(androidHandler)
currentVersion, err := handler.getPackageInfo(filePath)
require.NoError(t, err)
if currentVersion.Version != "1.0.1" {
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.1")
}
handler.changePackageVersion(currentVersion, "1.0.2")
currentVersion, err = handler.getPackageInfo(filePath)
require.NoError(t, err)
if currentVersion.Version != "1.0.2" {
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.2")
}
// some kind of rollback
handler.changePackageVersion(currentVersion, "1.0.1")
}
func Test_androidHandler_applyVersion(t *testing.T) {
handler := new(androidHandler)
processedBytes, err := handler.applyVersion(androidSeed, "1.0.2")
require.NoError(t, err)
xml, _ := handler.read(processedBytes)
if xml.VersionName != "1.0.2" {
t.Errorf("VersionName mismatch; expected %v", "1.0.2")
}
if xml.Code != "1000200" {
t.Errorf("code mismatch; actual %v, expected %v", xml.Code, "1000200")
}
}
func Test_androidHandler_read(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
want string
shouldError bool
}{
{"invalid bytes", args{invalidAndroidSeed}, "", true},
{"valid file", args{readFile("test/AndroidManifest.xml")}, "1.0.1", false},
{"valid bytes", args{androidSeed}, "1.0.1", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := new(androidHandler)
got, err := handler.read(tt.args.data)
if tt.shouldError {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got.VersionName)
})
}
}
var filePath = "test/AndroidManifest.xml"
var androidSeed = []byte(`
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xavtool"
android:versionCode="1"
android:versionName="1.0.1" >
<permission android:name="android"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sample" >
<activity
android:name="com.example.xavtool.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
`)
var invalidAndroidSeed = []byte(`
<?xml version="1.0" encoding="utf-8"?>
<man
`)