-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathAdbRunnerTests.cs
More file actions
1438 lines (1197 loc) · 45.4 KB
/
AdbRunnerTests.cs
File metadata and controls
1438 lines (1197 loc) · 45.4 KB
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
namespace Xamarin.Android.Tools.Tests;
/// <summary>
/// Tests for AdbRunner parsing, formatting, and merging logic.
/// Ported from dotnet/android GetAvailableAndroidDevicesTests.
///
/// API consumer reference:
/// - ParseAdbDevicesOutput: used by dotnet/android GetAvailableAndroidDevices task
/// - BuildDeviceDescription: used by dotnet/android GetAvailableAndroidDevices task
/// - FormatDisplayName: used by dotnet/android GetAvailableAndroidDevices tests
/// - MergeDevicesAndEmulators: used by dotnet/android GetAvailableAndroidDevices task
/// - MapAdbStateToStatus: used internally by ParseAdbDevicesOutput, public for extensibility
/// - ListDevicesAsync: used by MAUI DevTools Adb provider (Providers/Android/Adb.cs)
/// - WaitForDeviceAsync: used by MAUI DevTools Adb provider
/// - StopEmulatorAsync: used by MAUI DevTools Adb provider
/// - GetEmulatorAvdNameAsync: internal, used by ListDevicesAsync only
/// </summary>
[TestFixture]
public class AdbRunnerTests
{
// --- ParseAdbDevicesOutput tests ---
// Consumer: dotnet/android GetAvailableAndroidDevices.cs, MAUI DevTools (via ListDevicesAsync)
[Test]
public void ParseAdbDevicesOutput_RealWorldData ()
{
var output =
"List of devices attached\n" +
"0A041FDD400327 device product:redfin model:Pixel_5 device:redfin transport_id:2\n" +
"emulator-5554 device product:sdk_gphone64_x86_64 model:sdk_gphone64_x86_64 device:emu64xa transport_id:1\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (2, devices.Count);
// Physical device
Assert.AreEqual ("0A041FDD400327", devices [0].Serial);
Assert.AreEqual (AdbDeviceType.Device, devices [0].Type);
Assert.AreEqual (AdbDeviceStatus.Online, devices [0].Status);
Assert.AreEqual ("Pixel 5", devices [0].Description);
Assert.AreEqual ("Pixel_5", devices [0].Model);
Assert.AreEqual ("redfin", devices [0].Product);
Assert.AreEqual ("redfin", devices [0].Device);
Assert.AreEqual ("2", devices [0].TransportId);
Assert.IsFalse (devices [0].IsEmulator);
// Emulator
Assert.AreEqual ("emulator-5554", devices [1].Serial);
Assert.AreEqual (AdbDeviceType.Emulator, devices [1].Type);
Assert.AreEqual (AdbDeviceStatus.Online, devices [1].Status);
Assert.AreEqual ("sdk gphone64 x86 64", devices [1].Description); // model with underscores replaced
Assert.AreEqual ("sdk_gphone64_x86_64", devices [1].Model);
Assert.AreEqual ("1", devices [1].TransportId);
Assert.IsTrue (devices [1].IsEmulator);
}
[Test]
public void ParseAdbDevicesOutput_EmptyOutput ()
{
var output = "List of devices attached\n\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (0, devices.Count);
}
[Test]
public void ParseAdbDevicesOutput_SingleEmulator ()
{
var output =
"List of devices attached\n" +
"emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("emulator-5554", devices [0].Serial);
Assert.AreEqual (AdbDeviceType.Emulator, devices [0].Type);
Assert.AreEqual (AdbDeviceStatus.Online, devices [0].Status);
Assert.AreEqual ("sdk_gphone64_arm64", devices [0].Model);
Assert.AreEqual ("1", devices [0].TransportId);
}
[Test]
public void ParseAdbDevicesOutput_SinglePhysicalDevice ()
{
var output =
"List of devices attached\n" +
"0A041FDD400327 device usb:1-1 product:raven model:Pixel_6_Pro device:raven transport_id:2\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("0A041FDD400327", devices [0].Serial);
Assert.AreEqual (AdbDeviceType.Device, devices [0].Type);
Assert.AreEqual (AdbDeviceStatus.Online, devices [0].Status);
Assert.AreEqual ("Pixel 6 Pro", devices [0].Description);
Assert.AreEqual ("Pixel_6_Pro", devices [0].Model);
Assert.AreEqual ("raven", devices [0].Product);
Assert.AreEqual ("2", devices [0].TransportId);
}
[Test]
public void ParseAdbDevicesOutput_OfflineDevice ()
{
var output =
"List of devices attached\n" +
"emulator-5554 offline product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual (AdbDeviceStatus.Offline, devices [0].Status);
}
[Test]
public void ParseAdbDevicesOutput_UnauthorizedDevice ()
{
var output =
"List of devices attached\n" +
"0A041FDD400327 unauthorized usb:1-1\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("0A041FDD400327", devices [0].Serial);
Assert.AreEqual (AdbDeviceStatus.Unauthorized, devices [0].Status);
Assert.AreEqual (AdbDeviceType.Device, devices [0].Type);
}
[Test]
public void ParseAdbDevicesOutput_NoPermissionsDevice ()
{
var output =
"List of devices attached\n" +
"???????????????? no permissions usb:1-1\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("????????????????", devices [0].Serial);
Assert.AreEqual (AdbDeviceStatus.NoPermissions, devices [0].Status);
}
[Test]
public void ParseAdbDevicesOutput_DeviceWithMinimalMetadata ()
{
var output =
"List of devices attached\n" +
"ABC123 device\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("ABC123", devices [0].Serial);
Assert.AreEqual (AdbDeviceType.Device, devices [0].Type);
Assert.AreEqual (AdbDeviceStatus.Online, devices [0].Status);
Assert.AreEqual ("ABC123", devices [0].Description, "Should fall back to serial");
}
[Test]
public void ParseAdbDevicesOutput_InvalidLines ()
{
var output =
"List of devices attached\n" +
"\n" +
" \n" +
"Some random text\n" +
"* daemon not running; starting now at tcp:5037\n" +
"* daemon started successfully\n" +
"emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count, "Should only return valid device lines");
Assert.AreEqual ("emulator-5554", devices [0].Serial);
}
[Test]
public void ParseAdbDevicesOutput_MixedDeviceStates ()
{
var output =
"List of devices attached\n" +
"emulator-5554 device product:sdk_gphone64_arm64 model:Pixel_7 device:emu64a\n" +
"emulator-5556 offline\n" +
"0A041FDD400327 device usb:1-1 product:raven model:Pixel_6_Pro\n" +
"0B123456789ABC unauthorized usb:1-2\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (4, devices.Count);
Assert.AreEqual (AdbDeviceStatus.Online, devices [0].Status);
Assert.AreEqual (AdbDeviceStatus.Offline, devices [1].Status);
Assert.AreEqual (AdbDeviceStatus.Online, devices [2].Status);
Assert.AreEqual (AdbDeviceStatus.Unauthorized, devices [3].Status);
}
[Test]
public void ParseAdbDevicesOutput_WindowsNewlines ()
{
var output =
"List of devices attached\r\n" +
"emulator-5554 device transport_id:1\r\n" +
"\r\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("emulator-5554", devices [0].Serial);
Assert.IsTrue (devices [0].IsEmulator);
}
[Test]
public void ParseAdbDevicesOutput_TabSeparator ()
{
var output =
"List of devices attached\n" +
"emulator-5554\tdevice\n" +
"R5CR10YZQPJ\tdevice\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (2, devices.Count);
Assert.AreEqual ("emulator-5554", devices [0].Serial);
Assert.AreEqual ("R5CR10YZQPJ", devices [1].Serial);
}
[Test]
public void ParseAdbDevicesOutput_IpPortDevice ()
{
var output =
"List of devices attached\n" +
"192.168.1.100:5555 device product:sdk_gphone64_arm64 model:Remote_Device\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("192.168.1.100:5555", devices [0].Serial);
Assert.AreEqual (AdbDeviceType.Device, devices [0].Type, "IP devices should be Device");
Assert.AreEqual ("Remote Device", devices [0].Description);
}
[Test]
public void ParseAdbDevicesOutput_AdbDaemonStarting ()
{
var output =
"* daemon not running; starting now at tcp:5037\n" +
"* daemon started successfully\n" +
"List of devices attached\n" +
"emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1\n" +
"0A041FDD400327 device usb:1-1 product:raven model:Pixel_6_Pro device:raven transport_id:2\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (2, devices.Count, "Should parse devices even with daemon startup messages");
}
// --- BuildDeviceDescription tests ---
// Consumer: dotnet/android GetAvailableAndroidDevices.cs
[Test]
public void DescriptionPriorityOrder ()
{
// Model has highest priority
var output1 = "List of devices attached\ndevice1 device product:product_name model:model_name device:device_name\n";
var devices1 = AdbRunner.ParseAdbDevicesOutput (output1.Split ('\n'));
Assert.AreEqual ("model name", devices1 [0].Description, "Model should have highest priority");
// Product has second priority
var output2 = "List of devices attached\ndevice2 device product:product_name device:device_name\n";
var devices2 = AdbRunner.ParseAdbDevicesOutput (output2.Split ('\n'));
Assert.AreEqual ("product name", devices2 [0].Description, "Product should have second priority");
// Device code name has third priority
var output3 = "List of devices attached\ndevice3 device device:device_name\n";
var devices3 = AdbRunner.ParseAdbDevicesOutput (output3.Split ('\n'));
Assert.AreEqual ("device name", devices3 [0].Description, "Device should have third priority");
}
[Test]
public void BuildDeviceDescription_EmulatorWithUppercaseAvdName ()
{
// Mirrors dotnet/android ParseMixedDeviceStates: offline emulator gets AVD name "Pixel_9_Pro_XL"
// BuildDeviceDescription should format it preserving "XL" uppercase
var device = new AdbDeviceInfo {
Serial = "emulator-5556",
Type = AdbDeviceType.Emulator,
Status = AdbDeviceStatus.Offline,
AvdName = "Pixel_9_Pro_XL",
};
var description = AdbRunner.BuildDeviceDescription (device);
Assert.AreEqual ("Pixel 9 Pro XL", description, "Offline emulator should still get AVD name with uppercase preserved");
}
// --- FormatDisplayName tests ---
// Consumer: dotnet/android GetAvailableAndroidDevicesTests, BuildDeviceDescription (via AVD name formatting)
[Test]
public void FormatDisplayName_ReplacesUnderscoresWithSpaces ()
{
Assert.AreEqual ("Pixel 7 Pro", AdbRunner.FormatDisplayName ("pixel_7_pro"));
}
[Test]
public void FormatDisplayName_AppliesTitleCase ()
{
Assert.AreEqual ("Pixel 7 Pro", AdbRunner.FormatDisplayName ("pixel 7 pro"));
}
[Test]
public void FormatDisplayName_ReplacesApiWithAPIUppercase ()
{
Assert.AreEqual ("Pixel 5 API 34", AdbRunner.FormatDisplayName ("pixel_5_api_34"));
}
[Test]
public void FormatDisplayName_HandlesMultipleApiOccurrences ()
{
Assert.AreEqual ("Test API Device API 35", AdbRunner.FormatDisplayName ("test_api_device_api_35"));
}
[Test]
public void FormatDisplayName_HandlesMixedCaseInput ()
{
Assert.AreEqual ("Pixel 7 API 35", AdbRunner.FormatDisplayName ("PiXeL_7_API_35"));
}
[Test]
public void FormatDisplayName_HandlesComplexNames ()
{
// Lowercase input: "xl" gets title-cased to "Xl"
Assert.AreEqual ("Pixel 9 Pro Xl API 36", AdbRunner.FormatDisplayName ("pixel_9_pro_xl_api_36"));
}
[Test]
public void FormatDisplayName_PreservesUppercaseSegments ()
{
// Fully-uppercase segments like "XL", "SE", "FE" are preserved
Assert.AreEqual ("Pixel 9 Pro XL", AdbRunner.FormatDisplayName ("Pixel_9_Pro_XL"));
Assert.AreEqual ("Galaxy S24 FE", AdbRunner.FormatDisplayName ("Galaxy_S24_FE"));
}
[Test]
public void FormatDisplayName_PreservesNumbersAndSpecialChars ()
{
Assert.AreEqual ("Pixel 7-Pro API 35", AdbRunner.FormatDisplayName ("pixel_7-pro_api_35"));
}
[Test]
public void FormatDisplayName_HandlesEmptyString ()
{
Assert.AreEqual ("", AdbRunner.FormatDisplayName (""));
}
[Test]
public void FormatDisplayName_HandlesSingleWord ()
{
Assert.AreEqual ("Pixel", AdbRunner.FormatDisplayName ("pixel"));
}
[Test]
public void FormatDisplayName_DoesNotReplaceApiInsideWords ()
{
Assert.AreEqual ("Erapidevice", AdbRunner.FormatDisplayName ("erapidevice"));
}
// --- MapAdbStateToStatus tests ---
// Consumer: ParseAdbDevicesOutput (internal mapping), public for custom consumers
[Test]
public void MapAdbStateToStatus_AllStates ()
{
Assert.AreEqual (AdbDeviceStatus.Online, AdbRunner.MapAdbStateToStatus ("device"));
Assert.AreEqual (AdbDeviceStatus.Offline, AdbRunner.MapAdbStateToStatus ("offline"));
Assert.AreEqual (AdbDeviceStatus.Unauthorized, AdbRunner.MapAdbStateToStatus ("unauthorized"));
Assert.AreEqual (AdbDeviceStatus.NoPermissions, AdbRunner.MapAdbStateToStatus ("no permissions"));
Assert.AreEqual (AdbDeviceStatus.Unknown, AdbRunner.MapAdbStateToStatus ("something-else"));
}
// --- MergeDevicesAndEmulators tests ---
// Consumer: dotnet/android GetAvailableAndroidDevices.cs
[Test]
public void MergeDevicesAndEmulators_NoEmulators_ReturnsAdbDevicesOnly ()
{
var adbDevices = new List<AdbDeviceInfo> {
new AdbDeviceInfo { Serial = "0A041FDD400327", Description = "Pixel 5", Type = AdbDeviceType.Device, Status = AdbDeviceStatus.Online },
};
var result = AdbRunner.MergeDevicesAndEmulators (adbDevices, new List<string> ());
Assert.AreEqual (1, result.Count);
Assert.AreEqual ("0A041FDD400327", result [0].Serial);
}
[Test]
public void MergeDevicesAndEmulators_NoRunningEmulators_AddsAllAvailable ()
{
var adbDevices = new List<AdbDeviceInfo> {
new AdbDeviceInfo { Serial = "0A041FDD400327", Description = "Pixel 5", Type = AdbDeviceType.Device, Status = AdbDeviceStatus.Online },
};
var available = new List<string> { "pixel_7_api_35", "pixel_9_api_36" };
var result = AdbRunner.MergeDevicesAndEmulators (adbDevices, available);
Assert.AreEqual (3, result.Count);
// Online first
Assert.AreEqual ("0A041FDD400327", result [0].Serial);
// Non-running sorted alphabetically
Assert.AreEqual ("pixel_7_api_35", result [1].Serial);
Assert.AreEqual (AdbDeviceStatus.NotRunning, result [1].Status);
Assert.AreEqual ("pixel_7_api_35", result [1].AvdName);
Assert.AreEqual ("Pixel 7 API 35 (Not Running)", result [1].Description);
Assert.AreEqual ("pixel_9_api_36", result [2].Serial);
Assert.AreEqual (AdbDeviceStatus.NotRunning, result [2].Status);
Assert.AreEqual ("Pixel 9 API 36 (Not Running)", result [2].Description);
}
[Test]
public void MergeDevicesAndEmulators_RunningEmulator_NoDuplicate ()
{
var adbDevices = new List<AdbDeviceInfo> {
new AdbDeviceInfo {
Serial = "emulator-5554", Description = "Pixel 7 API 35",
Type = AdbDeviceType.Emulator, Status = AdbDeviceStatus.Online,
AvdName = "pixel_7_api_35"
},
};
var available = new List<string> { "pixel_7_api_35" };
var result = AdbRunner.MergeDevicesAndEmulators (adbDevices, available);
Assert.AreEqual (1, result.Count, "Should not duplicate running emulator");
Assert.AreEqual ("emulator-5554", result [0].Serial);
Assert.AreEqual (AdbDeviceStatus.Online, result [0].Status);
}
[Test]
public void MergeDevicesAndEmulators_MixedRunningAndNotRunning ()
{
var adbDevices = new List<AdbDeviceInfo> {
new AdbDeviceInfo {
Serial = "emulator-5554", Description = "Pixel 7 API 35",
Type = AdbDeviceType.Emulator, Status = AdbDeviceStatus.Online,
AvdName = "pixel_7_api_35"
},
new AdbDeviceInfo {
Serial = "0A041FDD400327", Description = "Pixel 5",
Type = AdbDeviceType.Device, Status = AdbDeviceStatus.Online,
},
};
var available = new List<string> { "pixel_7_api_35", "pixel_9_api_36", "nexus_5_api_30" };
var result = AdbRunner.MergeDevicesAndEmulators (adbDevices, available);
Assert.AreEqual (4, result.Count);
// Online devices first, sorted alphabetically
Assert.AreEqual ("0A041FDD400327", result [0].Serial);
Assert.AreEqual (AdbDeviceStatus.Online, result [0].Status);
Assert.AreEqual ("emulator-5554", result [1].Serial);
Assert.AreEqual (AdbDeviceStatus.Online, result [1].Status);
// Non-running emulators second, sorted alphabetically
Assert.AreEqual ("nexus_5_api_30", result [2].Serial);
Assert.AreEqual (AdbDeviceStatus.NotRunning, result [2].Status);
Assert.AreEqual ("Nexus 5 API 30 (Not Running)", result [2].Description);
Assert.AreEqual ("pixel_9_api_36", result [3].Serial);
Assert.AreEqual (AdbDeviceStatus.NotRunning, result [3].Status);
}
[Test]
public void MergeDevicesAndEmulators_CaseInsensitiveAvdNameMatching ()
{
var adbDevices = new List<AdbDeviceInfo> {
new AdbDeviceInfo {
Serial = "emulator-5554", Description = "Pixel 7 API 35",
Type = AdbDeviceType.Emulator, Status = AdbDeviceStatus.Online,
AvdName = "Pixel_7_API_35"
},
};
var available = new List<string> { "pixel_7_api_35" }; // lowercase
var result = AdbRunner.MergeDevicesAndEmulators (adbDevices, available);
Assert.AreEqual (1, result.Count, "Should match AVD names case-insensitively");
}
[Test]
public void MergeDevicesAndEmulators_EmptyAdbDevices_ReturnsAllAvailable ()
{
var result = AdbRunner.MergeDevicesAndEmulators (new List<AdbDeviceInfo> (), new List<string> { "pixel_7_api_35", "pixel_9_api_36" });
Assert.AreEqual (2, result.Count);
Assert.AreEqual ("Pixel 7 API 35 (Not Running)", result [0].Description);
Assert.AreEqual ("Pixel 9 API 36 (Not Running)", result [1].Description);
}
// --- AdbPath tests ---
// Consumer: MAUI DevTools Adb provider (AdbPath, IsAvailable properties)
[Test]
public void Constructor_NullPath_ThrowsArgumentException ()
{
Assert.Throws<ArgumentException> (() => new AdbRunner (null!));
}
[Test]
public void Constructor_EmptyPath_ThrowsArgumentException ()
{
Assert.Throws<ArgumentException> (() => new AdbRunner (""));
}
[Test]
public void Constructor_WhitespacePath_ThrowsArgumentException ()
{
Assert.Throws<ArgumentException> (() => new AdbRunner (" "));
}
[Test]
public void ParseAdbDevicesOutput_DeviceWithProductOnly ()
{
var output =
"List of devices attached\n" +
"emulator-5554 device product:aosp_x86_64\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("aosp_x86_64", devices [0].Product);
Assert.AreEqual (AdbDeviceType.Emulator, devices [0].Type);
}
[Test]
public void ParseAdbDevicesOutput_MultipleDevices ()
{
var output =
"List of devices attached\n" +
"emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:1\n" +
"emulator-5556 device product:sdk_gphone64_x86_64 model:sdk_gphone64_x86_64 device:emu64x transport_id:3\n" +
"0A041FDD400327 device usb:1-1 product:raven model:Pixel_6_Pro device:raven transport_id:2\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (3, devices.Count);
Assert.AreEqual ("emulator-5554", devices [0].Serial);
Assert.AreEqual (AdbDeviceType.Emulator, devices [0].Type);
Assert.AreEqual ("emulator-5556", devices [1].Serial);
Assert.AreEqual (AdbDeviceType.Emulator, devices [1].Type);
Assert.AreEqual ("0A041FDD400327", devices [2].Serial);
Assert.AreEqual (AdbDeviceType.Device, devices [2].Type);
Assert.AreEqual ("Pixel_6_Pro", devices [2].Model);
}
[Test]
public void MergeDevicesAndEmulators_AllEmulatorsRunning_NoDuplicate ()
{
var emulator1 = new AdbDeviceInfo {
Serial = "emulator-5554", Description = "Pixel 7 API 35",
Type = AdbDeviceType.Emulator, Status = AdbDeviceStatus.Online,
AvdName = "pixel_7_api_35",
};
var emulator2 = new AdbDeviceInfo {
Serial = "emulator-5556", Description = "Pixel 9 API 36",
Type = AdbDeviceType.Emulator, Status = AdbDeviceStatus.Online,
AvdName = "pixel_9_api_36",
};
var result = AdbRunner.MergeDevicesAndEmulators (
new List<AdbDeviceInfo> { emulator1, emulator2 },
new List<string> { "pixel_7_api_35", "pixel_9_api_36" });
Assert.AreEqual (2, result.Count, "Should not add duplicates when all emulators are running");
Assert.IsTrue (result.All (d => d.Status == AdbDeviceStatus.Online));
}
[Test]
public void MergeDevicesAndEmulators_NonRunningEmulatorHasFormattedDescription ()
{
var result = AdbRunner.MergeDevicesAndEmulators (
new List<AdbDeviceInfo> (),
new List<string> { "pixel_7_pro_api_35" });
Assert.AreEqual (1, result.Count);
Assert.AreEqual ("Pixel 7 Pro API 35 (Not Running)", result [0].Description);
Assert.AreEqual (AdbDeviceStatus.NotRunning, result [0].Status);
}
[Test]
public void ParseAdbDevicesOutput_RecoveryState ()
{
var output = "List of devices attached\n" +
"0A041FDD400327 recovery product:redfin model:Pixel_5 device:redfin transport_id:2\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("0A041FDD400327", devices [0].Serial);
Assert.AreEqual (AdbDeviceStatus.Unknown, devices [0].Status);
}
[Test]
public void ParseAdbDevicesOutput_SideloadState ()
{
var output = "List of devices attached\n" +
"0A041FDD400327 sideload\n";
var devices = AdbRunner.ParseAdbDevicesOutput (output.Split ('\n'));
Assert.AreEqual (1, devices.Count);
Assert.AreEqual ("0A041FDD400327", devices [0].Serial);
Assert.AreEqual (AdbDeviceStatus.Unknown, devices [0].Status);
}
[Test]
public void MapAdbStateToStatus_Recovery_ReturnsUnknown ()
{
Assert.AreEqual (AdbDeviceStatus.Unknown, AdbRunner.MapAdbStateToStatus ("recovery"));
}
[Test]
public void MapAdbStateToStatus_Sideload_ReturnsUnknown ()
{
Assert.AreEqual (AdbDeviceStatus.Unknown, AdbRunner.MapAdbStateToStatus ("sideload"));
}
// --- WaitForDeviceAsync tests ---
// Consumer: MAUI DevTools Adb provider (WaitForDeviceAsync)
[Test]
public void WaitForDeviceAsync_NegativeTimeout_ThrowsArgumentOutOfRange ()
{
var runner = new AdbRunner ("/fake/sdk/platform-tools/adb");
Assert.ThrowsAsync<System.ArgumentOutOfRangeException> (
async () => await runner.WaitForDeviceAsync (timeout: System.TimeSpan.FromSeconds (-1)));
}
[Test]
public void WaitForDeviceAsync_ZeroTimeout_ThrowsArgumentOutOfRange ()
{
var runner = new AdbRunner ("/fake/sdk/platform-tools/adb");
Assert.ThrowsAsync<System.ArgumentOutOfRangeException> (
async () => await runner.WaitForDeviceAsync (timeout: System.TimeSpan.Zero));
}
// --- FirstNonEmptyLine tests ---
[Test]
public void FirstNonEmptyLine_ReturnsFirstLine ()
{
Assert.AreEqual ("hello", AdbRunner.FirstNonEmptyLine ("hello\nworld\n"));
}
[Test]
public void FirstNonEmptyLine_SkipsBlankLines ()
{
Assert.AreEqual ("hello", AdbRunner.FirstNonEmptyLine ("\n\nhello\nworld\n"));
}
[Test]
public void FirstNonEmptyLine_TrimsWhitespace ()
{
Assert.AreEqual ("hello", AdbRunner.FirstNonEmptyLine (" hello \n"));
}
[Test]
public void FirstNonEmptyLine_HandlesWindowsNewlines ()
{
Assert.AreEqual ("hello", AdbRunner.FirstNonEmptyLine ("\r\nhello\r\nworld\r\n"));
}
[Test]
public void FirstNonEmptyLine_EmptyString_ReturnsNull ()
{
Assert.IsNull (AdbRunner.FirstNonEmptyLine (""));
}
[Test]
public void FirstNonEmptyLine_OnlyNewlines_ReturnsNull ()
{
Assert.IsNull (AdbRunner.FirstNonEmptyLine ("\n\n\n"));
}
[Test]
public void FirstNonEmptyLine_SingleValue_ReturnsIt ()
{
Assert.AreEqual ("1", AdbRunner.FirstNonEmptyLine ("1\n"));
}
[Test]
public void FirstNonEmptyLine_TypicalGetpropOutput ()
{
// adb shell getprop sys.boot_completed returns "1\n" or "1\r\n"
Assert.AreEqual ("1", AdbRunner.FirstNonEmptyLine ("1\r\n"));
}
[Test]
public void FirstNonEmptyLine_PmPathOutput ()
{
// adb shell pm path android returns "package:/system/framework/framework-res.apk\n"
var output = "package:/system/framework/framework-res.apk\n";
Assert.AreEqual ("package:/system/framework/framework-res.apk", AdbRunner.FirstNonEmptyLine (output));
}
// --- ParseReverseListOutput tests ---
// Consumer: MAUI DevTools (via ListReversePortsAsync), vscode-maui ServiceHub replacement
[Test]
public void ParseReverseListOutput_SingleRule ()
{
var output = new [] {
"(reverse) tcp:5000 tcp:5000",
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (1, rules.Count);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Remote.Protocol);
Assert.AreEqual (5000, rules [0].Remote.Port);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Local.Protocol);
Assert.AreEqual (5000, rules [0].Local.Port);
}
[Test]
public void ParseReverseListOutput_MultipleRules ()
{
var output = new [] {
"(reverse) tcp:5000 tcp:5000",
"(reverse) tcp:8081 tcp:8081",
"(reverse) tcp:19000 tcp:19001",
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (3, rules.Count);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Remote.Protocol);
Assert.AreEqual (5000, rules [0].Remote.Port);
Assert.AreEqual (5000, rules [0].Local.Port);
Assert.AreEqual (8081, rules [1].Remote.Port);
Assert.AreEqual (8081, rules [1].Local.Port);
Assert.AreEqual (19000, rules [2].Remote.Port);
Assert.AreEqual (19001, rules [2].Local.Port);
}
[Test]
public void ParseReverseListOutput_EmptyOutput ()
{
var output = new [] { "", " " };
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseReverseListOutput_NoLines ()
{
var rules = AdbRunner.ParseReverseListOutput (Array.Empty<string> ());
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseReverseListOutput_IgnoresNonReverseLines ()
{
var output = new [] {
"some random header",
"(reverse) tcp:5000 tcp:5000",
"* daemon started successfully",
"(reverse) tcp:8081 tcp:8081",
"",
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (2, rules.Count);
Assert.AreEqual (5000, rules [0].Remote.Port);
Assert.AreEqual (8081, rules [1].Remote.Port);
}
[Test]
public void ParseReverseListOutput_MalformedLine_InsufficientParts ()
{
var output = new [] {
"(reverse) tcp:5000", // missing local spec
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseReverseListOutput_DifferentRemoteAndLocalPorts ()
{
var output = new [] {
"(reverse) tcp:8080 tcp:3000",
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (1, rules.Count);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Remote.Protocol);
Assert.AreEqual (8080, rules [0].Remote.Port);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Local.Protocol);
Assert.AreEqual (3000, rules [0].Local.Port);
}
[Test]
public void ParseReverseListOutput_NonTcpSpecs_SkipsUnparseable ()
{
var output = new [] {
"(reverse) localabstract:chrome_devtools_remote tcp:9222",
"(reverse) tcp:5000 tcp:5000",
};
var rules = AdbRunner.ParseReverseListOutput (output);
// localabstract:chrome_devtools_remote has a non-numeric port, so it is skipped
Assert.AreEqual (1, rules.Count);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Remote.Protocol);
Assert.AreEqual (5000, rules [0].Remote.Port);
}
[Test]
public void ParseReverseListOutput_WindowsLineEndings ()
{
// Simulate \r\n line endings (split on \n leaves trailing \r)
var output = new [] {
"(reverse) tcp:5000 tcp:5000\r",
"(reverse) tcp:8081 tcp:8081\r",
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (2, rules.Count);
Assert.AreEqual (5000, rules [0].Remote.Port);
Assert.AreEqual (8081, rules [1].Remote.Port);
}
[Test]
public void ParseReverseListOutput_TabSeparated ()
{
var output = new [] {
"(reverse)\ttcp:5000\ttcp:5000",
"(reverse)\ttcp:8081\ttcp:8081",
};
var rules = AdbRunner.ParseReverseListOutput (output);
Assert.AreEqual (2, rules.Count);
Assert.AreEqual (5000, rules [0].Remote.Port);
Assert.AreEqual (8081, rules [1].Remote.Port);
}
// --- ParseForwardListOutput tests ---
// Consumer: MAUI DevTools (via ListForwardPortsAsync — host→device tunnel for JDWP debugger
// attach, perf endpoints, host-side DevFlow agent connect), vscode-maui ServiceHub replacement.
// Output format differs from reverse: "(reverse) <remote> <local>" → "<serial> <local> <remote>".
[Test]
public void ParseForwardListOutput_SingleRule ()
{
var output = new [] {
"emulator-5554 tcp:5000 tcp:6000",
};
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
Assert.AreEqual (1, rules.Count);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Local.Protocol);
Assert.AreEqual (5000, rules [0].Local.Port);
Assert.AreEqual (AdbProtocol.Tcp, rules [0].Remote.Protocol);
Assert.AreEqual (6000, rules [0].Remote.Port);
}
[Test]
public void ParseForwardListOutput_MultipleRulesSameDevice ()
{
var output = new [] {
"emulator-5554 tcp:5000 tcp:6000",
"emulator-5554 tcp:8081 tcp:8081",
"emulator-5554 tcp:9222 tcp:9223",
};
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
Assert.AreEqual (3, rules.Count);
Assert.AreEqual (5000, rules [0].Local.Port);
Assert.AreEqual (6000, rules [0].Remote.Port);
Assert.AreEqual (8081, rules [1].Local.Port);
Assert.AreEqual (8081, rules [1].Remote.Port);
Assert.AreEqual (9222, rules [2].Local.Port);
Assert.AreEqual (9223, rules [2].Remote.Port);
}
[Test]
public void ParseForwardListOutput_FiltersByDeviceSerial ()
{
// adb forward --list returns rules across ALL devices; we must filter.
var output = new [] {
"emulator-5554 tcp:5000 tcp:5000",
"emulator-5556 tcp:5001 tcp:5001",
"emulator-5554 tcp:8081 tcp:8081",
"abcd1234device tcp:9000 tcp:9000",
};
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
Assert.AreEqual (2, rules.Count);
Assert.AreEqual (5000, rules [0].Local.Port);
Assert.AreEqual (8081, rules [1].Local.Port);
}
[Test]
public void ParseForwardListOutput_EmptyOutput ()
{
var output = new [] { "", " " };
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseForwardListOutput_NoLines ()
{
var rules = AdbRunner.ParseForwardListOutput (Array.Empty<string> (), "emulator-5554");
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseForwardListOutput_EmptySerial_ReturnsEmpty ()
{
var output = new [] { "emulator-5554 tcp:5000 tcp:5000" };
var rules = AdbRunner.ParseForwardListOutput (output, "");
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseForwardListOutput_NoMatchingDevice ()
{
var output = new [] {
"emulator-5554 tcp:5000 tcp:5000",
"emulator-5556 tcp:5001 tcp:5001",
};
var rules = AdbRunner.ParseForwardListOutput (output, "missing-device");
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseForwardListOutput_MalformedLine_InsufficientParts ()
{
var output = new [] {
"emulator-5554 tcp:5000", // missing remote spec
};
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
Assert.AreEqual (0, rules.Count);
}
[Test]
public void ParseForwardListOutput_NonTcpSpecs_SkipsUnparseable ()
{
var output = new [] {
"emulator-5554 tcp:9222 localabstract:chrome_devtools_remote",
"emulator-5554 tcp:5000 tcp:5000",
};
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
// localabstract:chrome_devtools_remote has a non-numeric port, so it is skipped
Assert.AreEqual (1, rules.Count);
Assert.AreEqual (5000, rules [0].Local.Port);
Assert.AreEqual (5000, rules [0].Remote.Port);
}
[Test]
public void ParseForwardListOutput_WindowsLineEndings ()
{
var output = new [] {
"emulator-5554 tcp:5000 tcp:6000\r",
"emulator-5554 tcp:8081 tcp:8081\r",
};
var rules = AdbRunner.ParseForwardListOutput (output, "emulator-5554");
Assert.AreEqual (2, rules.Count);
Assert.AreEqual (5000, rules [0].Local.Port);
Assert.AreEqual (6000, rules [0].Remote.Port);
Assert.AreEqual (8081, rules [1].Local.Port);
}