-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice_test.go
365 lines (328 loc) · 9.71 KB
/
device_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package browser
import (
"fmt"
"log"
"os"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/yaml.v3"
)
// testDevices is a map of user agent strings for each device from the devices.yml file
var testDevices map[string]string
// initTestDevices reads the devices.yml file and
// unmarshals it into the testDevices map.
func initTestDevices() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory: %v", err)
}
wd = strings.Split(wd, "/browser")[0]
yamlFile, err := os.ReadFile(fmt.Sprintf("%s/browser/assets/test/devices.yml", wd))
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
var data map[string]string
if err := yaml.Unmarshal(yamlFile, &data); err != nil {
log.Fatalf("failed to unmarshal: %v", err)
}
testDevices = data
}
func TestNewDevice(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When NewDevice is called", func() {
Convey("It returns a device", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d, ShouldHaveSameTypeAs, &Device{})
})
})
Convey("When NewDevice is called with a user agent string larger than the limit", func() {
Convey("It returns ErrUserAgentSizeExceeded error", func() {
// generate a string that is larger than the limit of 2k
largeString := strings.Repeat("a", 2049)
_, err := NewDevice(largeString)
So(err, ShouldEqual, ErrUserAgentSizeExceeded)
})
})
})
}
func TestDeviceName(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When NewDevice is called", func() {
Convey("Then it should return the correct device name", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d.Name(), ShouldEqual, "iPhone")
})
})
})
}
func TestDeviceIsBlackberryPlaybook(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Blackberry Playbook", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["bb-playbook-1"])
So(d.IsBlackberryPlaybook(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsIPad(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is an iPad", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["ipad-4"])
So(d.IsIPad(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsIPhone(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is an iPhone", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d.IsIPhone(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsIPodTouch(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is an iPod touch", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["ipod-touch-1"])
So(d.IsIPodTouch(), ShouldBeTrue)
})
})
Convey("When the device is not an iPod touch", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d.IsIPodTouch(), ShouldBeFalse)
})
})
})
}
func TestDeviceIsPlayStation3(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a PlayStation 3", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["ps3"])
So(d.IsPs3(), ShouldBeTrue)
So(d.IsPlayStation(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsPlayStation4(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a PlayStation 4", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["ps4"])
So(d.IsPs4(), ShouldBeTrue)
So(d.IsPlayStation(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsPlayStation5(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a PlayStation 5", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["ps5"])
So(d.IsPs5(), ShouldBeTrue)
So(d.IsPlayStation(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsXbox360(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is an Xbox 360", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["xbox360-1"])
So(d.IsXbox360(), ShouldBeTrue)
So(d.IsXbox(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsXboxOne(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is an Xbox One", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["xbox-one-1"])
So(d.IsXboxOne(), ShouldBeTrue)
So(d.IsXbox(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsWii(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Wii", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["wii-1"])
So(d.IsWii(), ShouldBeTrue)
So(d.IsNintendoWii(), ShouldBeTrue)
So(d.IsNintendo(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsWiiU(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Wii U", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["wiiu-1"])
So(d.IsWiiU(), ShouldBeTrue)
So(d.IsNintendoWiiU(), ShouldBeTrue)
So(d.IsNintendo(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsSwitch(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Switch", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["switch-1"])
So(d.IsSwitch(), ShouldBeTrue)
So(d.IsNintendoSwitch(), ShouldBeTrue)
So(d.IsNintendo(), ShouldBeTrue)
So(d.IsConsole(), ShouldBeTrue)
})
})
Convey("When the device is not a Switch", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["wiiu-1"])
So(d.IsSwitch(), ShouldBeFalse)
})
})
})
}
func TestDeviceIsSurface(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Surface", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["surface-1"])
So(d.IsSurface(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsKindle(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Kindle or Kindle fire", func() {
Convey("It should return true", func() {
kindleUserAgents := []string{testDevices["kindle-1"], testDevices["kindle-fire-1"]}
for _, ua := range kindleUserAgents {
d, _ := NewDevice(ua)
So(d.IsKindle(), ShouldBeTrue)
}
})
})
})
}
func TestDeviceIsKindleFire(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Kindle Fire", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["kindle-fire-1"])
So(d.IsKindleFire(), ShouldBeTrue)
})
})
Convey("When the device is not a Kindle Fire", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["kindle-1"])
So(d.IsKindleFire(), ShouldBeFalse)
})
})
})
}
func TestDeviceIsPSP(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a PSP", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["psp-1"])
So(d.IsPSP(), ShouldBeTrue)
})
})
})
}
func TestDeviceIsSamsung(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a Samsung", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["samsung-1"])
So(d.IsSamsung(), ShouldBeTrue)
})
})
Convey("When the device is not a Samsung", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d.IsSamsung(), ShouldBeFalse)
})
})
})
}
func TestDeviceIsTV(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a TV", func() {
Convey("It should return true", func() {
d, _ := NewDevice(testDevices["tv-1"])
So(d.IsTV(), ShouldBeTrue)
})
})
Convey("When the device is not a TV", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d.IsTV(), ShouldBeFalse)
})
})
})
}
func TestDeviceIsMobile(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a mobile", func() {
Convey("It should return true", func() {
mobiles := []string{testDevices["iphone-7"], testDevices["android-froyo-2-2"], testDevices["mobile-1"], testDevices["mobile-2"]}
for _, ua := range mobiles {
d, _ := NewDevice(ua)
So(d.IsMobile(), ShouldBeTrue)
}
})
})
Convey("When the device is not a mobile", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["ipad-1"])
So(d.IsMobile(), ShouldBeFalse)
})
})
})
}
func TestDeviceIsTablet(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the device is a tablet", func() {
Convey("It should return true", func() {
tablets := []string{testDevices["ipad-4"], testDevices["surface-1"], testDevices["bb-playbook-1"]}
for _, ua := range tablets {
d, _ := NewDevice(ua)
So(d.IsTablet(), ShouldBeTrue)
}
})
})
Convey("When the device is not a tablet", func() {
Convey("It should return false", func() {
d, _ := NewDevice(testDevices["iphone-7"])
So(d.IsTablet(), ShouldBeFalse)
})
})
})
}