-
Notifications
You must be signed in to change notification settings - Fork 1
/
platform_test.go
579 lines (516 loc) · 16.2 KB
/
platform_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package browser
import (
"fmt"
"log"
"os"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/yaml.v3"
)
// testPlatforms is a map of user agent strings for each platform from the platforms.yml file
var testPlatforms map[string]string
// initTestPlatforms reads the platforms.yml file and
// unmarshals it into the testPlatforms map.
func initTestPlatforms() {
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/platforms.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)
}
testPlatforms = data
}
func TestNewPlatform(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When NewPlatform is called", func() {
platform, _ := NewPlatform(testPlatforms["adobe-air"])
Convey("It returns a platform", func() {
So(platform, ShouldHaveSameTypeAs, &Platform{})
})
})
Convey("When NewPlatform 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 := NewPlatform(largeString)
So(err, ShouldEqual, ErrUserAgentSizeExceeded)
})
})
})
}
func TestPlatformName(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When NewPlatform is called", func() {
p, _ := NewPlatform(testPlatforms["adobe-air"])
Convey("It returns the correct platform name", func() {
So(p.Name(), ShouldEqual, "Adobe AIR")
})
})
})
}
func TestPlatformIsAdobeAir(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Adobe AIR", func() {
p, _ := NewPlatform(testPlatforms["adobe-air"])
Convey("It returns true", func() {
So(p.IsAdobeAir(), ShouldBeTrue)
})
})
Convey("When the platform is not Adobe AIR", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsAdobeAir(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsAdobeAirVersionCompatible(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Adobe AIR", func() {
p, _ := NewPlatform(testPlatforms["adobe-air"])
Convey("And the version is compatible", func() {
Convey("It returns true", func() {
So(p.IsAdobeAirVersionCompatible("17"), ShouldBeTrue)
})
})
Convey("And the version is not compatible", func() {
Convey("It returns false", func() {
So(p.IsAdobeAirVersionCompatible("19"), ShouldBeFalse)
})
})
})
})
}
func TestPlatformIsAndroid(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Android", func() {
p, _ := NewPlatform(testPlatforms["android-jelly-bean-4.1"])
Convey("It returns true", func() {
So(p.IsAndroid(), ShouldBeTrue)
})
})
Convey("When the platform is not Android", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsAndroid(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsAndroidApp(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Android App", func() {
Convey("It returns true", func() {
apps := []string{"android-app-1", "android-app-2"}
for _, app := range apps {
p, _ := NewPlatform(testPlatforms[app])
So(p.IsAndroidApp(), ShouldBeTrue)
So(p.IsAndroidWebview(), ShouldBeTrue)
}
})
})
Convey("When the platform is not Android App", func() {
p, _ := NewPlatform(testPlatforms["android-jelly-bean-4.1"])
Convey("It returns false", func() {
So(p.IsAndroidApp(), ShouldBeFalse)
So(p.IsAndroidWebview(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsChromeOS(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Chrome OS", func() {
p, _ := NewPlatform(testPlatforms["chrome-os-armv7i"])
Convey("It returns true", func() {
So(p.IsChromeOS(), ShouldBeTrue)
})
})
Convey("When the platform is not Chrome OS", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsChromeOS(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsChromeOSVersionCompatible(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Chrome OS", func() {
p, _ := NewPlatform(testPlatforms["chrome-os-armv7i"])
Convey("And the version is compatible", func() {
Convey("It returns true", func() {
So(p.IsChromeOSVersionCompatible("6157"), ShouldBeTrue)
})
})
Convey("And the version is not compatible", func() {
Convey("It returns false", func() {
So(p.IsChromeOSVersionCompatible("6158.50.0"), ShouldBeFalse)
})
})
})
})
}
func TestPlatformIsIOS(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is iOS", func() {
p, _ := NewPlatform(testPlatforms["ios-12"])
Convey("It returns true", func() {
So(p.IsIOS(), ShouldBeTrue)
})
})
Convey("When the platform is not iOS", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsIOS(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsWatchOS(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is watchOS", func() {
p, _ := NewPlatform(testPlatforms["apple-watch-6"])
Convey("It returns true", func() {
So(p.IsWatchOS(), ShouldBeTrue)
})
})
Convey("When the platform is not watchOS", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsWatchOS(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsKaiOS(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is KaiOS", func() {
p, _ := NewPlatform(testPlatforms["kai-os-2"])
Convey("It returns true", func() {
So(p.IsKaiOS(), ShouldBeTrue)
})
})
Convey("When the platform is not KaiOS", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsKaiOS(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsLinux(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Linux", func() {
p, _ := NewPlatform(testPlatforms["linux"])
Convey("It returns true", func() {
So(p.IsLinux(), ShouldBeTrue)
})
})
Convey("When the platform is not Linux", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns false", func() {
So(p.IsLinux(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsBlackberry(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is BlackBerry", func() {
p, _ := NewPlatform(testPlatforms["blackberry"])
Convey("It returns true", func() {
So(p.IsBlackBerry(), ShouldBeTrue)
})
})
Convey("When the platform is not BlackBerry", func() {
p, _ := NewPlatform(testPlatforms["linux"])
Convey("It returns false", func() {
So(p.IsBlackBerry(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsWindowsMobile(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows Mobile", func() {
p, _ := NewPlatform(testPlatforms["windows-ce"])
Convey("It returns true", func() {
So(p.IsWindowsMobile(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows Mobile", func() {
p, _ := NewPlatform(testPlatforms["linux"])
Convey("It returns false", func() {
So(p.IsWindowsMobile(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsWindowsPhone(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows Phone", func() {
p, _ := NewPlatform(testPlatforms["windows-phone"])
Convey("It returns true", func() {
So(p.IsWindowsPhone(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows Phone", func() {
p, _ := NewPlatform(testPlatforms["linux"])
Convey("It returns false", func() {
So(p.IsWindowsPhone(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsIOSApp(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is iOS App", func() {
p, _ := NewPlatform(testPlatforms["ios-app"])
Convey("It returns true", func() {
So(p.IsIOSApp(), ShouldBeTrue)
So(p.IsIOSWebview(), ShouldBeTrue)
})
})
Convey("When the platform is not iOS App", func() {
p, _ := NewPlatform(testPlatforms["ios-3"])
Convey("It returns false", func() {
So(p.IsIOSApp(), ShouldBeFalse)
So(p.IsIOSWebview(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsWindows(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows", func() {
p, _ := NewPlatform(testPlatforms["windows-10"])
Convey("It returns true", func() {
So(p.IsWindows(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows", func() {
p, _ := NewPlatform(testPlatforms["linux"])
Convey("It returns false", func() {
So(p.IsWindows(), ShouldBeFalse)
})
})
})
}
func TestPlatformIsWindowsXP(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows XP", func() {
p, _ := NewPlatform(testPlatforms["windows-xp"])
Convey("It returns true", func() {
So(p.IsWindowsXP(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows XP", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8-1", "windows-8", "windows-7", "windows-vista"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsXP(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindowsVista(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows Vista", func() {
p, _ := NewPlatform(testPlatforms["windows-vista"])
Convey("It returns true", func() {
So(p.IsWindowsVista(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows Vista", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8-1", "windows-8", "windows-7", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsVista(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindows7(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows 7", func() {
p, _ := NewPlatform(testPlatforms["windows-7"])
Convey("It returns true", func() {
So(p.IsWindows7(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows 7", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8-1", "windows-8", "windows-vista", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindows7(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindows8(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows 8", func() {
p, _ := NewPlatform(testPlatforms["windows-8"])
Convey("It returns true", func() {
So(p.IsWindows8(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows 8", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-7", "windows-vista", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindows8(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindows8_1(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows 8.1", func() {
p, _ := NewPlatform(testPlatforms["windows-8-1"])
Convey("It returns true", func() {
So(p.IsWindows8_1(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows 8.1", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-7", "windows-vista", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindows8_1(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindows10(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows 10", func() {
p, _ := NewPlatform(testPlatforms["windows-10"])
Convey("It returns true", func() {
So(p.IsWindows10(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows 10", func() {
Convey("It returns false", func() {
platforms := []string{"windows-8-1", "windows-8", "windows-7", "windows-vista", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindows10(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindowsRT(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows RT", func() {
p, _ := NewPlatform(testPlatforms["windows-rt"])
Convey("It returns true", func() {
So(p.IsWindowsRT(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows RT", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8-1", "windows-8", "windows-7", "windows-vista", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsRT(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindowsX64(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows x64", func() {
p, _ := NewPlatform(testPlatforms["windows-8-1"])
Convey("It returns true", func() {
So(p.IsWindowsX64(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows x64", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8", "windows-7", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsX64(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindowsWOW64(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows WOW64", func() {
p, _ := NewPlatform(testPlatforms["windows-10"])
Convey("It returns true", func() {
So(p.IsWindowsWOW64(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows WOW64", func() {
Convey("It returns false", func() {
platforms := []string{"windows-vista", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsWOW64(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindowsX64Inclusive(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows X64 Inclusive", func() {
p, _ := NewPlatform(testPlatforms["windows-inclusive"])
Convey("It returns true", func() {
So(p.IsWindowsX64Inclusive(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows X64 Inclusive", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8", "windows-7", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsX64(), ShouldBeFalse)
}
})
})
})
}
func TestPlatformIsWindowsTouchScreenDesktop(t *testing.T) {
Convey("Given a user agent string", t, func() {
Convey("When the platform is Windows Touch Screen Desktop", func() {
p, _ := NewPlatform(testPlatforms["windows-touch"])
Convey("It returns true", func() {
So(p.IsWindowsTouchScreenDesktop(), ShouldBeTrue)
})
})
Convey("When the platform is not Windows Touch Screen Desktop", func() {
Convey("It returns false", func() {
platforms := []string{"windows-10", "windows-8", "windows-7", "windows-xp"}
for _, platform := range platforms {
p, _ := NewPlatform(testPlatforms[platform])
So(p.IsWindowsTouchScreenDesktop(), ShouldBeFalse)
}
})
})
})
}