summaryrefslogtreecommitdiff
path: root/sdklib/src/test/java/com/android/sdklib/devices/DeviceManagerTest.java
blob: 30687b4c09b13cc8662e4c6bf53003d42a05b6a9 (plain)
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.sdklib.devices;

import static com.google.common.truth.Truth.assertThat;

import com.android.repository.impl.meta.TypeDetails;
import com.android.repository.testframework.FakePackage;
import com.android.repository.testframework.FakeProgressIndicator;
import com.android.resources.Navigation;
import com.android.resources.ScreenRound;
import com.android.sdklib.TempSdkManager;
import com.android.sdklib.devices.Device.Builder;
import com.android.sdklib.devices.DeviceManager.DeviceFilter;
import com.android.sdklib.devices.DeviceManager.DeviceStatus;
import com.android.sdklib.repository.AndroidSdkHandler;
import com.android.sdklib.repository.IdDisplay;
import com.android.sdklib.repository.meta.DetailsTypes;
import com.android.sdklib.repository.targets.SystemImage;
import com.android.testutils.NoErrorsOrWarningsLogger;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class DeviceManagerTest {

    static final String WSVGA_HASH             = "MD5:176ce220cc833bcb6dc60ff13b82c716";
    static final String WSVGA_TRACKBALL_HASH   = "MD5:e964715d73bc8b2cc7ca2fadcb12fa41";
    static final String NEXUS_ONE_HASH         = "MD5:ef39e456bf2cab397201c2ac251f35fc";
    static final String NEXUS_ONE_PLUGGED_HASH = "MD5:474a72646a55e61e94f69bdd94758ceb";

    @Rule public final TempSdkManager sdkManager =
            new TempSdkManager("sdk_" + getClass().getSimpleName());

    private DeviceManager dm;

    @Before
    public void setUp() {
        dm = createDeviceManager();
    }

    private DeviceManager createDeviceManager() {
        NoErrorsOrWarningsLogger log = new NoErrorsOrWarningsLogger();
        AndroidSdkHandler sdkHandler = sdkManager.getSdkHandler();
        return DeviceManager.createInstance(
                sdkHandler,
                log);
    }

    /**
     * Returns a list of just the devices' display names, for unit test comparisons.
     */
    private static List<String> listDisplayNames(Collection<Device> devices) {
        return devices.stream().map(Device::getDisplayName).collect(Collectors.toList());
    }

    @Test
    public final void testGetDevices_Default() {
        // no user devices defined in the test's custom .android home folder
        assertThat(dm.getDevices(DeviceFilter.USER)).isEmpty();

        // no system-images devices defined in the SDK by default
        assertThat(dm.getDevices(DeviceFilter.SYSTEM_IMAGES)).isEmpty();

        // this list comes from devices.xml bundled in the JAR
        // cf /sdklib/src/main/java/com/android/sdklib/devices/devices.xml
        assertThat(listDisplayNames(dm.getDevices(DeviceFilter.DEFAULT)))
                .containsExactly(
                        "10.1\" WXGA (Tablet)",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" FWVGA slider",
                        "3.7\" WVGA (Nexus One)",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "6.7\" Horizontal Fold-in",
                        "7\" WSVGA (Tablet)",
                        "7.4\" Rollable",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "13.5\" Freeform",
                        "Resizable (Experimental)");

        assertThat(dm.getDevice("2.7in QVGA", "Generic").getDisplayName()).isEqualTo("2.7\" QVGA");

        // this list comes from the nexus.xml bundled in the JAR
        // cf /sdklib/src/main/java/com/android/sdklib/devices/nexus.xml
        assertThat(listDisplayNames(dm.getDevices(DeviceFilter.VENDOR)))
                .containsExactly(
                        "Android TV (4K)",
                        "Android TV (1080p)",
                        "Android TV (720p)",
                        "Large Desktop",
                        "Medium Desktop",
                        "Small Desktop",
                        "Wear OS Rectangular",
                        "Wear OS Small Round",
                        "Wear OS Square",
                        "Wear OS Large Round",
                        "Automotive (1024p landscape)",
                        "Galaxy Nexus",
                        "Nexus 10",
                        "Nexus 4",
                        "Nexus 5",
                        "Nexus 5X",
                        "Nexus 6",
                        "Nexus 6P",
                        "Nexus 7",
                        "Nexus 7 (2012)",
                        "Nexus 9",
                        "Nexus One",
                        "Nexus S",
                        "Pixel C",
                        "Pixel",
                        "Pixel XL",
                        "Pixel 2",
                        "Pixel 2 XL",
                        "Pixel 3",
                        "Pixel 3 XL",
                        "Pixel 3a",
                        "Pixel 3a XL",
                        "Pixel 4",
                        "Pixel 4 XL",
                        "Pixel 4a",
                        "Pixel 5",
                        "Pixel 6",
                        "Pixel 6 Pro",
                        "Pixel 6a",
                        "Pixel 7",
                        "Pixel 7 Pro");

        assertThat(dm.getDevice("Nexus One", "Google").getDisplayName()).isEqualTo("Nexus One");

        assertThat(listDisplayNames(dm.getDevices(DeviceManager.ALL_DEVICES)))
                .containsExactly(
                        "10.1\" WXGA (Tablet)",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" FWVGA slider",
                        "3.7\" WVGA (Nexus One)",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "6.7\" Horizontal Fold-in",
                        "7\" WSVGA (Tablet)",
                        "7.4\" Rollable",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "13.5\" Freeform",
                        "Resizable (Experimental)",
                        "Android TV (4K)",
                        "Android TV (1080p)",
                        "Android TV (720p)",
                        "Large Desktop",
                        "Medium Desktop",
                        "Small Desktop",
                        "Wear OS Rectangular",
                        "Wear OS Small Round",
                        "Wear OS Square",
                        "Wear OS Large Round",
                        "Automotive (1024p landscape)",
                        "Galaxy Nexus",
                        "Nexus 10",
                        "Nexus 4",
                        "Nexus 5",
                        "Nexus 5X",
                        "Nexus 6",
                        "Nexus 6P",
                        "Nexus 7",
                        "Nexus 7 (2012)",
                        "Nexus 9",
                        "Nexus One",
                        "Nexus S",
                        "Pixel C",
                        "Pixel",
                        "Pixel XL",
                        "Pixel 2",
                        "Pixel 2 XL",
                        "Pixel 3",
                        "Pixel 3 XL",
                        "Pixel 3a",
                        "Pixel 3a XL",
                        "Pixel 4",
                        "Pixel 4 XL",
                        "Pixel 4a",
                        "Pixel 5",
                        "Pixel 6",
                        "Pixel 6 Pro",
                        "Pixel 6a",
                        "Pixel 7",
                        "Pixel 7 Pro");
    }

    @Test
    public final void testGetDevice() {
        // get a definition from the bundled devices.xml file
        Device d1 = dm.getDevice("7in WSVGA (Tablet)", "Generic");
        assertThat(d1.getDisplayName()).isEqualTo("7\" WSVGA (Tablet)");
        // get a definition from the bundled nexus.xml file
        Device d2 = dm.getDevice("Nexus One", "Google");
        assertThat(d2.getDisplayName()).isEqualTo("Nexus One");
    }

    @Test
    public final void testGetDevices_UserDevice() {

        Device d1 = dm.getDevice("7in WSVGA (Tablet)", "Generic");

        Builder b = new Device.Builder(d1);
        b.setId("MyCustomTablet");
        b.setName("My Custom Tablet");
        b.setManufacturer("OEM");

        Device d2 = b.build();

        dm.addUserDevice(d2);
        dm.saveUserDevices();

        assertThat(dm.getDevice("MyCustomTablet", "OEM").getDisplayName())
                .isEqualTo("My Custom Tablet");

        // create a new device manager, forcing it reload all files
        dm = null;
        DeviceManager dm2 = createDeviceManager();

        assertThat(dm2.getDevice("MyCustomTablet", "OEM").getDisplayName())
                .isEqualTo("My Custom Tablet");

        // 1 user device defined in the test's custom .android home folder
        assertThat(listDisplayNames(dm2.getDevices(DeviceFilter.USER)))
                .containsExactly("My Custom Tablet");

        // no system-images devices defined in the SDK by default
        assertThat(dm2.getDevices(DeviceFilter.SYSTEM_IMAGES)).isEmpty();

        // this list comes from devices.xml bundled in the JAR
        // cf /sdklib/src/main/java/com/android/sdklib/devices/devices.xml
        assertThat(listDisplayNames(dm2.getDevices(DeviceFilter.DEFAULT)))
                .containsExactly(
                        "10.1\" WXGA (Tablet)",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" FWVGA slider",
                        "3.7\" WVGA (Nexus One)",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "6.7\" Horizontal Fold-in",
                        "7\" WSVGA (Tablet)",
                        "7.4\" Rollable",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "13.5\" Freeform",
                        "Resizable (Experimental)");

        // this list comes from the nexus.xml bundled in the JAR
        // cf /sdklib/src/main/java/com/android/sdklib/devices/nexus.xml
        assertThat(listDisplayNames(dm2.getDevices(DeviceFilter.VENDOR)))
                .containsExactly(
                        "Android TV (4K)",
                        "Android TV (1080p)",
                        "Android TV (720p)",
                        "Large Desktop",
                        "Medium Desktop",
                        "Small Desktop",
                        "Wear OS Rectangular",
                        "Wear OS Small Round",
                        "Wear OS Square",
                        "Wear OS Large Round",
                        "Automotive (1024p landscape)",
                        "Galaxy Nexus",
                        "Nexus 10",
                        "Nexus 4",
                        "Nexus 5",
                        "Nexus 5X",
                        "Nexus 6",
                        "Nexus 6P",
                        "Nexus 7",
                        "Nexus 7 (2012)",
                        "Nexus 9",
                        "Nexus One",
                        "Nexus S",
                        "Pixel C",
                        "Pixel",
                        "Pixel XL",
                        "Pixel 2",
                        "Pixel 2 XL",
                        "Pixel 3",
                        "Pixel 3 XL",
                        "Pixel 3a",
                        "Pixel 3a XL",
                        "Pixel 4",
                        "Pixel 4 XL",
                        "Pixel 4a",
                        "Pixel 5",
                        "Pixel 6",
                        "Pixel 6 Pro",
                        "Pixel 6a",
                        "Pixel 7",
                        "Pixel 7 Pro");

        assertThat(listDisplayNames(dm2.getDevices(DeviceManager.ALL_DEVICES)))
                .containsExactly(
                        "10.1\" WXGA (Tablet)",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" FWVGA slider",
                        "3.7\" WVGA (Nexus One)",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "6.7\" Horizontal Fold-in",
                        "7\" WSVGA (Tablet)",
                        "7.4\" Rollable",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "13.5\" Freeform",
                        "Resizable (Experimental)",
                        "Android TV (4K)",
                        "Android TV (1080p)",
                        "Android TV (720p)",
                        "Large Desktop",
                        "Medium Desktop",
                        "Small Desktop",
                        "Wear OS Rectangular",
                        "Wear OS Small Round",
                        "Wear OS Square",
                        "Wear OS Large Round",
                        "Automotive (1024p landscape)",
                        "Galaxy Nexus",
                        "My Custom Tablet",
                        "Nexus 10",
                        "Nexus 4",
                        "Nexus 5",
                        "Nexus 5X",
                        "Nexus 6",
                        "Nexus 6P",
                        "Nexus 7",
                        "Nexus 7 (2012)",
                        "Nexus 9",
                        "Nexus One",
                        "Nexus S",
                        "Pixel C",
                        "Pixel",
                        "Pixel XL",
                        "Pixel 2",
                        "Pixel 2 XL",
                        "Pixel 3",
                        "Pixel 3 XL",
                        "Pixel 3a",
                        "Pixel 3a XL",
                        "Pixel 4",
                        "Pixel 4 XL",
                        "Pixel 4a",
                        "Pixel 5",
                        "Pixel 6",
                        "Pixel 6 Pro",
                        "Pixel 6a",
                        "Pixel 7",
                        "Pixel 7 Pro");
    }

    @Test
    public final void testGetDevices_SysImgDevice() throws Exception {

        AndroidSdkHandler handler = sdkManager.getSdkHandler();
        Path sdkPath = handler.getLocation();
        FakePackage.FakeLocalPackage p = new FakePackage.FakeLocalPackage("sample");

        // Create a system image directory with one device
        DetailsTypes.AddonDetailsType details = AndroidSdkHandler.getAddonModule()
                .createLatestFactory().createAddonDetailsType();
        details.setApiLevel(22);
        details.setVendor(SystemImage.DEFAULT_TAG);
        p.setTypeDetails((TypeDetails) details);
        SystemImage imageWithDevice =
                new SystemImage(
                        sdkPath.resolve("system-images/android-22/tag-1/x86"),
                        IdDisplay.create("tag-1", "tag-1"),
                        IdDisplay.create("OEM", "Tag 1 OEM"),
                        "x86",
                        new Path[] {},
                        p);

        sdkManager.makeSystemImageFolder(imageWithDevice, "tag-1");

        // no user devices defined in the test's custom .android home folder
        assertThat(dm.getDevices(DeviceFilter.USER)).isEmpty();

        // find the system-images specific device added by makeSystemImageFolder above
        // using both the getDevices() API and the device-specific getDevice() API.
        assertThat(listDisplayNames(dm.getDevices(DeviceFilter.SYSTEM_IMAGES)))
                .containsExactly("Mock Tag 1 Device Name");

        assertThat(dm.getDevice("tag-1", "OEM").getDisplayName())
                .isEqualTo("Mock Tag 1 Device Name");

        // this list comes from devices.xml bundled in the JAR
        // cf /sdklib/src/main/java/com/android/sdklib/devices/devices.xml
        assertThat(listDisplayNames(dm.getDevices(DeviceFilter.DEFAULT)))
                .containsExactly(
                        "10.1\" WXGA (Tablet)",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" FWVGA slider",
                        "3.7\" WVGA (Nexus One)",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "6.7\" Horizontal Fold-in",
                        "7\" WSVGA (Tablet)",
                        "7.4\" Rollable",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "13.5\" Freeform",
                        "Resizable (Experimental)");

        // this list comes from the nexus.xml bundled in the JAR
        // cf /sdklib/src/main/java/com/android/sdklib/devices/nexus.xml
        assertThat(listDisplayNames(dm.getDevices(DeviceFilter.VENDOR)))
                .containsExactly(
                        "Android TV (4K)",
                        "Android TV (1080p)",
                        "Android TV (720p)",
                        "Large Desktop",
                        "Medium Desktop",
                        "Small Desktop",
                        "Wear OS Small Round",
                        "Wear OS Rectangular",
                        "Wear OS Square",
                        "Wear OS Large Round",
                        "Automotive (1024p landscape)",
                        "Galaxy Nexus",
                        "Nexus 10",
                        "Nexus 4",
                        "Nexus 5",
                        "Nexus 5X",
                        "Nexus 6",
                        "Nexus 6P",
                        "Nexus 7",
                        "Nexus 7 (2012)",
                        "Nexus 9",
                        "Nexus One",
                        "Nexus S",
                        "Pixel C",
                        "Pixel",
                        "Pixel XL",
                        "Pixel 2",
                        "Pixel 2 XL",
                        "Pixel 3",
                        "Pixel 3 XL",
                        "Pixel 3a",
                        "Pixel 3a XL",
                        "Pixel 4",
                        "Pixel 4 XL",
                        "Pixel 4a",
                        "Pixel 5",
                        "Pixel 6",
                        "Pixel 6 Pro",
                        "Pixel 6a",
                        "Pixel 7",
                        "Pixel 7 Pro");

        assertThat(listDisplayNames(dm.getDevices(DeviceManager.ALL_DEVICES)))
                .containsExactly(
                        "10.1\" WXGA (Tablet)",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" FWVGA slider",
                        "3.7\" WVGA (Nexus One)",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "6.7\" Horizontal Fold-in",
                        "7\" WSVGA (Tablet)",
                        "7.4\" Rollable",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "13.5\" Freeform",
                        "Resizable (Experimental)",
                        "Android TV (4K)",
                        "Android TV (1080p)",
                        "Android TV (720p)",
                        "Large Desktop",
                        "Medium Desktop",
                        "Small Desktop",
                        "Wear OS Rectangular",
                        "Wear OS Small Round",
                        "Wear OS Square",
                        "Wear OS Large Round",
                        "Automotive (1024p landscape)",
                        "Galaxy Nexus",
                        "Mock Tag 1 Device Name",
                        "Nexus 10",
                        "Nexus 4",
                        "Nexus 5",
                        "Nexus 5X",
                        "Nexus 6",
                        "Nexus 6P",
                        "Nexus 7",
                        "Nexus 7 (2012)",
                        "Nexus 9",
                        "Nexus One",
                        "Nexus S",
                        "Pixel C",
                        "Pixel",
                        "Pixel XL",
                        "Pixel 2",
                        "Pixel 2 XL",
                        "Pixel 3",
                        "Pixel 3 XL",
                        "Pixel 3a",
                        "Pixel 3a XL",
                        "Pixel 4",
                        "Pixel 4 XL",
                        "Pixel 4a",
                        "Pixel 5",
                        "Pixel 6",
                        "Pixel 6 Pro",
                        "Pixel 6a",
                        "Pixel 7",
                        "Pixel 7 Pro");
    }

    @Test
    public void getDevicesCanonicalDeviceDefinitionsEnabled() {
        // Arrange
        EnumSet<DeviceFilter> filters = EnumSet.of(DeviceFilter.DEFAULT);

        // Act
        Collection<Device> actualDevices = dm.getDevices(filters, property -> true);

        // Assert
        Object expectedNames =
                Arrays.asList(
                        "Resizable (Experimental)",
                        "13.5\" Freeform",
                        "7.6\" Fold-in with outer display",
                        "8\" Fold-out",
                        "6.7\" Horizontal Fold-in",
                        "7.4\" Rollable",
                        "2.7\" QVGA",
                        "2.7\" QVGA slider",
                        "3.2\" HVGA slider (ADP1)",
                        "3.2\" QVGA (ADP2)",
                        "3.3\" WQVGA",
                        "3.4\" WQVGA",
                        "3.7\" WVGA (Nexus One)",
                        "3.7\" FWVGA slider",
                        "4\" WVGA (Nexus S)",
                        "4.65\" 720p (Galaxy Nexus)",
                        "4.7\" WXGA",
                        "5.1\" WVGA",
                        "5.4\" FWVGA",
                        "7\" WSVGA (Tablet)",
                        "10.1\" WXGA (Tablet)",
                        "Small Phone",
                        "Medium Phone",
                        "Medium Tablet");

        assertThat(listDisplayNames(actualDevices)).isEqualTo(expectedNames);
    }

    @Test
    public final void testGetDeviceStatus() {
        // get a definition from the bundled devices.xml file
        assertThat(dm.getDeviceStatus("7in WSVGA (Tablet)", "Generic"))
                .isEqualTo(DeviceStatus.EXISTS);

        // get a definition from the bundled oem file
        assertThat(dm.getDeviceStatus("Nexus One", "Google")).isEqualTo(DeviceStatus.EXISTS);

        // try a device that does not exist
        assertThat(dm.getDeviceStatus("My Device", "Custom OEM")).isEqualTo(DeviceStatus.MISSING);
    }

    @Test
    public final void testGetHardwareProperties() {
        final Device pixelDevice = dm.getDevice("pixel", "Google");

        Map<String, String> devProperties = DeviceManager.getHardwareProperties(pixelDevice);
        assertThat(devProperties.get("hw.lcd.density")).isEqualTo("420");
        assertThat(devProperties.get("hw.lcd.width")).isEqualTo("1080");
        assertThat(devProperties.get("hw.ramSize")).isEqualTo("4096"); // In MB, without units
    }

    @Test
    public final void testHasHardwarePropHashChanged_Generic() {
        final Device d1 = dm.getDevice("7in WSVGA (Tablet)", "Generic");

        assertThat(DeviceManager.hasHardwarePropHashChanged(d1, "invalid"))
                .isEqualTo(WSVGA_HASH);

        assertThat(DeviceManager.hasHardwarePropHashChanged(
                d1, WSVGA_HASH))
                .isNull();

        // change the device hardware props, this should change the hash
        d1.getDefaultHardware().setNav(Navigation.TRACKBALL);

        assertThat(DeviceManager.hasHardwarePropHashChanged(
                d1, WSVGA_HASH))
                .isEqualTo(WSVGA_TRACKBALL_HASH);

        // change the property back, should revert its hash to the previous one
        d1.getDefaultHardware().setNav(Navigation.NONAV);

        assertThat(DeviceManager.hasHardwarePropHashChanged(
                d1, WSVGA_HASH))
                .isNull();
    }

    @Test
    public final void testHasHardwarePropHashChanged_Oem() {
        final Device d2 = dm.getDevice("Nexus One", "Google");

        assertThat(DeviceManager.hasHardwarePropHashChanged(d2, "invalid"))
                .isEqualTo(NEXUS_ONE_HASH);

        assertThat(DeviceManager.hasHardwarePropHashChanged(
                d2, NEXUS_ONE_HASH))
                .isNull();

        // change the device hardware props, this should change the hash
        d2.getDefaultHardware().setChargeType(PowerType.PLUGGEDIN);

        assertThat(DeviceManager.hasHardwarePropHashChanged(
                d2, NEXUS_ONE_HASH))
                .isEqualTo(NEXUS_ONE_PLUGGED_HASH);

        // change the property back, should revert its hash to the previous one
        d2.getDefaultHardware().setChargeType(PowerType.BATTERY);

        assertThat(DeviceManager.hasHardwarePropHashChanged(
                d2, NEXUS_ONE_HASH))
                .isNull();
    }

    @Test
    public final void testDeviceOverrides() throws Exception {
        AndroidSdkHandler handler = sdkManager.getSdkHandler();
        Path sdkPath = handler.getLocation();
        FakePackage.FakeLocalPackage p =
                new FakePackage.FakeLocalPackage(
                        "sample", sdkManager.getSdkHandler().getLocation().resolve("sample"));

        // Create a local DeviceManager, get the number of devices, and verify one device
        DeviceManager localDeviceManager = createDeviceManager();
        int count = localDeviceManager.getDevices(EnumSet.allOf(DeviceFilter.class)).size();
        Device localDevice = localDeviceManager.getDevice("wearos_small_round", "Google");
        assertThat(localDevice.getDisplayName()).isEqualTo("Wear OS Small Round");

        // Create two system image directories with different definitions of the
        // device that we just checked. The version in android-25 should override
        // the other two versions.
        DetailsTypes.AddonDetailsType details22 = AndroidSdkHandler.getAddonModule()
                .createLatestFactory().createAddonDetailsType();
        details22.setApiLevel(22);
        details22.setVendor(SystemImage.DEFAULT_TAG);
        p.setTypeDetails((TypeDetails) details22);
        SystemImage imageWithDevice22 =
                new SystemImage(
                        sdkPath.resolve("system-images/android-22/android-wear/x86"),
                        IdDisplay.create("android-wear", "android-wear"),
                        IdDisplay.create("Google", "Google"),
                        "x86",
                        new Path[] {},
                        p);
        sdkManager.makeSystemImageFolder(imageWithDevice22, "wearos_small_round");

        DetailsTypes.AddonDetailsType details25 = AndroidSdkHandler.getAddonModule()
          .createLatestFactory().createAddonDetailsType();
        details25.setApiLevel(25);
        details25.setVendor(SystemImage.DEFAULT_TAG);
        p.setTypeDetails((TypeDetails) details25);
        SystemImage imageWithDevice25 =
                new SystemImage(
                        sdkPath.resolve("system-images/android-25/android-wear/x86"),
                        IdDisplay.create("android-wear", "android-wear"),
                        IdDisplay.create("Google", "Google"),
                        "arm",
                        new Path[] {},
                        p);
        sdkManager.makeSystemImageFolder(imageWithDevice25, "wearos_small_round");

        // Re-create the local DeviceManager using the new directory,
        // fetch the device, and verify that it is the right one.
        FakeProgressIndicator progress = new FakeProgressIndicator();
        sdkManager.getSdkHandler().getSdkManager(progress).markLocalCacheInvalid();
        localDeviceManager = createDeviceManager();

        localDevice = localDeviceManager.getDevice("wearos_small_round", "Google");
        // (The "Android wear" part comes from the tag "android-wear")
        assertThat(localDevice.getDisplayName()).isEqualTo("Mock Android wear Device Name");
        assertThat(localDevice.getDefaultState().getHardware().getCpu()).isEqualTo("arm");

        // Change the name of that device and add it to our local DeviceManager again
        Device dmDevice = dm.getDevice("wearos_small_round", "Google");
        Builder b = new Device.Builder(dmDevice);
        b.setName("Custom");
        localDeviceManager.addUserDevice(b.build());
        localDeviceManager.saveUserDevices();

        // Fetch the device from our local DeviceManager and verify
        // that it has the updated name
        localDevice = localDeviceManager.getDevice("wearos_small_round", "Google");
        assertThat(localDevice.getDisplayName()).isEqualTo("Custom");

        // Verify that the total number of devices is unchanged
        assertThat(localDeviceManager.getDevices(EnumSet.allOf(DeviceFilter.class)).size()).isEqualTo(count);
    }

    @Test
    public final void testWriteUserDevice() {
        Device testDeviceBefore = dm.getDevice("Test Round User Wear Device", "User");
        assertThat(testDeviceBefore).isNull();

        Device squareDevice = dm.getDevice("wearos_square", "Google");
        String squareName = squareDevice.getDisplayName();
        assertThat(squareName).isEqualTo("Wear OS Square");
        assertThat(squareDevice.isScreenRound()).isFalse();
        assertThat(squareDevice.getBootProps().get(DeviceParser.ROUND_BOOT_PROP)).isNull();

        {
            Device.Builder devBuilder = new Device.Builder(squareDevice);
            devBuilder.setId("test_round_dev");
            devBuilder.setName("Test Round User Wear Device");
            devBuilder.setManufacturer("User");

            State defaultState = squareDevice.getDefaultState().deepCopy();
            defaultState.getHardware().getScreen().setScreenRound(ScreenRound.ROUND);
            devBuilder.addState(defaultState);

            Device roundDevice = devBuilder.build();
            assertThat(roundDevice).isNotNull();
            assertThat(roundDevice.getBootProps().get(DeviceParser.ROUND_BOOT_PROP))
                    .isEqualTo("true");

            dm.addUserDevice(roundDevice);
        }

        Device testDeviceMid = dm.getDevice("test_round_dev", "User");
        assertThat(testDeviceMid).isNotNull();

        // Write the user-defined device definitions to devices.xml
        dm.saveUserDevices();
        dm.removeUserDevice(testDeviceMid);

        // Create a new DeviceManager. It will read the newly-written
        // devices.xml file, so we can check the contents.
        DeviceManager newDM = createDeviceManager();

        Device testDeviceAfter = newDM.getDevice("test_round_dev", "User");
        assertThat(testDeviceAfter).isNotNull();
        String afterName = testDeviceAfter.getDisplayName();
        assertThat(afterName).isEqualTo("Test Round User Wear Device");
        assertThat(testDeviceAfter.isScreenRound()).isTrue();
        assertThat(testDeviceAfter.getBootProps().get(DeviceParser.ROUND_BOOT_PROP))
                .isEqualTo("true");
    }
}