File: volume_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1625 lines) | stat: -rw-r--r-- 64,838 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_manager/volume_manager.h"

#include <stddef.h>

#include <algorithm>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include "ash/constants/ash_switches.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_running_on_chromeos.h"
#include "chrome/browser/ash/arc/fileapi/arc_file_system_operation_runner.h"
#include "chrome/browser/ash/arc/fileapi/arc_media_view_util.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_manager/volume.h"
#include "chrome/browser/ash/file_manager/volume_manager_observer.h"
#include "chrome/browser/ash/file_system_provider/fake_extension_provider.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/download/download_dir_util.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "chromeos/ash/components/disks/disk.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/disks/fake_disk_mount_manager.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "chromeos/ash/experiences/arc/test/connection_holder_util.h"
#include "chromeos/ash/experiences/arc/test/fake_file_system_instance.h"
#include "chromeos/components/disks/disks_prefs.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/storage_monitor/storage_info.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/extension_registry.h"
#include "services/device/public/mojom/mtp_storage_info.mojom.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace file_manager {
namespace {

using ::ash::MountError;
using ::ash::MountType;
using ::ash::disks::Disk;
using ::ash::disks::DiskMountManager;
using ::ash::disks::FakeDiskMountManager;
using base::FilePath;
using ::testing::UnorderedElementsAre;

std::vector<std::string> arc_volume_ids = {
    arc::kImagesRootId, arc::kVideosRootId, arc::kAudioRootId,
    arc::kDocumentsRootId, "android_files:0"};

const char kAllowlistedVendorId[] = "A123";
const char kAllowlistedProductId[] = "456B";
const policy::DeviceId kAllowlistedDeviceId{0xA123, 0x456B};

// Adds `kAllowlistedDeviceId` to ExternalStorageAllowlist.
void SetExternalStorageAllowlist(PrefService* pref_service) {
  pref_service->SetList(
      disks::prefs::kExternalStorageAllowlist,
      base::Value::List().Append(kAllowlistedDeviceId.ToDict()));
}

std::unique_ptr<Disk> CreateAllowlistedDisk(const std::string& disk_path) {
  return Disk::Builder()
      .SetDevicePath(disk_path)
      .SetVendorId(kAllowlistedVendorId)
      .SetProductId(kAllowlistedProductId)
      .SetHasMedia(true)
      .Build();
}

device::mojom::MtpStorageInfoPtr CreateAllowlistedMtpStorageInfo(
    std::string_view storage_name) {
  auto mtp_storage_info = device::mojom::MtpStorageInfo::New();
  mtp_storage_info->vendor_id = kAllowlistedDeviceId.vid;
  mtp_storage_info->product_id = kAllowlistedDeviceId.pid;
  mtp_storage_info->storage_name = storage_name;
  return mtp_storage_info;
}

class LoggingObserver : public VolumeManagerObserver {
 public:
  class Event {
   public:
    enum EventType {
      DISK_ADDED,
      DISK_ADD_BLOCKED_BY_POLICY,
      DISK_REMOVED,
      DEVICE_ADDED,
      DEVICE_REMOVED,
      VOLUME_MOUNTED,
      VOLUME_UNMOUNTED,
      FORMAT_STARTED,
      FORMAT_COMPLETED,
      PARTITION_STARTED,
      PARTITION_COMPLETED,
      RENAME_STARTED,
      RENAME_COMPLETED
    };

    EventType type() const { return type_.value(); }
    std::string device_path() const { return device_path_.value(); }
    std::string device_label() const { return device_label_.value(); }
    std::string volume_id() const { return volume_id_.value(); }
    bool mounting() const { return mounting_.value(); }
    ash::MountError mount_error() const { return mount_error_.value(); }
    bool success() const { return success_.value(); }

   private:
    friend class LoggingObserver;
    std::optional<EventType> type_;
    std::optional<std::string> device_path_;
    std::optional<std::string> device_label_;
    std::optional<std::string> volume_id_;
    std::optional<bool> mounting_;
    std::optional<ash::MountError> mount_error_;
    std::optional<bool> success_;
  };

  LoggingObserver() = default;

  LoggingObserver(const LoggingObserver&) = delete;
  LoggingObserver& operator=(const LoggingObserver&) = delete;

  ~LoggingObserver() override = default;

  const std::vector<Event>& events() const { return events_; }

  // VolumeManagerObserver overrides.
  void OnDiskAdded(const Disk& disk, bool mounting) override {
    Event event;
    event.type_ = Event::DISK_ADDED;
    event.device_path_ = disk.device_path();  // Keep only device_path.
    event.mounting_ = mounting;
    events_.push_back(event);
  }

  void OnDiskAddBlockedByPolicy(const std::string& device_path) override {
    Event event;
    event.type_ = Event::DISK_ADD_BLOCKED_BY_POLICY;
    event.device_path_ = device_path;
    events_.push_back(event);
  }

  void OnDiskRemoved(const Disk& disk) override {
    Event event;
    event.type_ = Event::DISK_REMOVED;
    event.device_path_ = disk.device_path();  // Keep only device_path.
    events_.push_back(event);
  }

  void OnDeviceAdded(const std::string& device_path) override {
    Event event;
    event.type_ = Event::DEVICE_ADDED;
    event.device_path_ = device_path;
    events_.push_back(event);
  }

  void OnDeviceRemoved(const std::string& device_path) override {
    Event event;
    event.type_ = Event::DEVICE_REMOVED;
    event.device_path_ = device_path;
    events_.push_back(event);
  }

  void OnVolumeMounted(ash::MountError error_code,
                       const Volume& volume) override {
    Event event;
    event.type_ = Event::VOLUME_MOUNTED;
    event.device_path_ = volume.source_path().AsUTF8Unsafe();
    event.volume_id_ = volume.volume_id();
    event.mount_error_ = error_code;
    events_.push_back(event);
  }

  void OnVolumeUnmounted(ash::MountError error_code,
                         const Volume& volume) override {
    Event event;
    event.type_ = Event::VOLUME_UNMOUNTED;
    event.device_path_ = volume.source_path().AsUTF8Unsafe();
    event.volume_id_ = volume.volume_id();
    event.mount_error_ = error_code;
    events_.push_back(event);
  }

  void OnFormatStarted(const std::string& device_path,
                       const std::string& device_label,
                       bool success) override {
    Event event;
    event.type_ = Event::FORMAT_STARTED;
    event.device_path_ = device_path;
    event.device_label_ = device_label;
    event.success_ = success;
    events_.push_back(event);
  }

  void OnFormatCompleted(const std::string& device_path,
                         const std::string& device_label,
                         bool success) override {
    Event event;
    event.type_ = Event::FORMAT_COMPLETED;
    event.device_path_ = device_path;
    event.device_label_ = device_label;
    event.success_ = success;
    events_.push_back(event);
  }

  void OnPartitionStarted(const std::string& device_path,
                          const std::string& device_label,
                          bool success) override {
    Event event;
    event.type_ = Event::PARTITION_STARTED;
    event.device_path_ = device_path;
    event.device_label_ = device_label;
    event.success_ = success;
    events_.push_back(event);
  }

  void OnPartitionCompleted(const std::string& device_path,
                            const std::string& device_label,
                            bool success) override {
    Event event;
    event.type_ = Event::PARTITION_COMPLETED;
    event.device_path_ = device_path;
    event.device_label_ = device_label;
    event.success_ = success;
    events_.push_back(event);
  }

  void OnRenameStarted(const std::string& device_path,
                       const std::string& device_label,
                       bool success) override {
    Event event;
    event.type_ = Event::RENAME_STARTED;
    event.device_path_ = device_path;
    event.device_label_ = device_label;
    event.success_ = success;
    events_.push_back(event);
  }

  void OnRenameCompleted(const std::string& device_path,
                         const std::string& device_label,
                         bool success) override {
    Event event;
    event.type_ = Event::RENAME_COMPLETED;
    event.device_path_ = device_path;
    event.device_label_ = device_label;
    event.success_ = success;
    events_.push_back(event);
  }

  void OnShutdownStart(VolumeManager* volume_manager) override {
    // Each test should remove its observer manually, so that they're all gone
    // by the time VolumeManager shuts down, and this handler is never reached.
    // In fact, it's more likely for UAF crash to happen before this code is
    // reached.
    NOTREACHED();
  }

 private:
  std::vector<Event> events_;
};

class ScopedLoggingObserver {
 public:
  explicit ScopedLoggingObserver(VolumeManager* volume_manager)
      : volume_manager_(volume_manager) {
    volume_manager_->AddObserver(&logging_observer_);
  }

  ~ScopedLoggingObserver() {
    volume_manager_->RemoveObserver(&logging_observer_);
  }

  const std::vector<LoggingObserver::Event>& events() const {
    return logging_observer_.events();
  }

 private:
  const raw_ptr<VolumeManager> volume_manager_;
  LoggingObserver logging_observer_;
};

}  // namespace

std::unique_ptr<KeyedService> CreateFileSystemOperationRunnerForTesting(
    content::BrowserContext* context) {
  return arc::ArcFileSystemOperationRunner::CreateForTesting(
      context, arc::ArcServiceManager::Get()->arc_bridge_service());
}

class VolumeManagerTest : public testing::Test {
 protected:
  // Helper class that contains per-profile objects.
  class ProfileEnvironment {
   public:
    ProfileEnvironment(TestingProfile* profile, DiskMountManager* disk_manager)
        : profile_(profile),
          extension_registry_(
              std::make_unique<extensions::ExtensionRegistry>(profile_)),
          file_system_provider_service_(
              std::make_unique<ash::file_system_provider::Service>(
                  profile_,
                  extension_registry_.get())),
          drive_integration_service_(
              std::make_unique<drive::DriveIntegrationService>(
                  TestingBrowserProcess::GetGlobal()->local_state(),
                  profile_,
                  std::string(),
                  base::FilePath())),
          volume_manager_(std::make_unique<VolumeManager>(
              profile_,
              drive_integration_service_.get(),  // DriveIntegrationService
              chromeos::PowerManagerClient::Get(),
              disk_manager,
              file_system_provider_service_.get(),
              base::BindRepeating(&ProfileEnvironment::GetFakeMtpStorageInfo,
                                  base::Unretained(this)))) {}

    ~ProfileEnvironment() {
      // In production, KeyedServices have Shutdown() called before destruction.
      volume_manager_->Shutdown();
      drive_integration_service_->Shutdown();
      file_system_provider_service_->Shutdown();
      extension_registry_->Shutdown();
    }

    TestingProfile* profile() const { return profile_; }
    VolumeManager* volume_manager() const { return volume_manager_.get(); }

    void SetFakeMtpStorageInfo(
        device::mojom::MtpStorageInfoPtr fake_mtp_storage_info) {
      fake_mtp_storage_info_ = std::move(fake_mtp_storage_info);
    }

   private:
    void GetFakeMtpStorageInfo(
        const std::string& storage_name,
        device::mojom::MtpManager::GetStorageInfoCallback callback) {
      if (!fake_mtp_storage_info_) {
        fake_mtp_storage_info_ = device::mojom::MtpStorageInfo::New();
      }
      std::move(callback).Run(std::move(fake_mtp_storage_info_));
    }

    const raw_ptr<TestingProfile> profile_;
    std::unique_ptr<extensions::ExtensionRegistry> extension_registry_;
    std::unique_ptr<ash::file_system_provider::Service>
        file_system_provider_service_;
    std::unique_ptr<drive::DriveIntegrationService> drive_integration_service_;
    std::unique_ptr<VolumeManager> volume_manager_;
    device::mojom::MtpStorageInfoPtr fake_mtp_storage_info_;
  };

  void SetUp() override {
    // Some test cases exercises the "MyFiles" directory.
    scoped_command_line_.GetProcessCommandLine()->AppendSwitch(
        ash::switches::kUseMyFilesInUserDataDirForTesting);

    chromeos::PowerManagerClient::InitializeFake();
    disk_mount_manager_ = std::make_unique<FakeDiskMountManager>();
    fake_user_manager_.Reset(std::make_unique<ash::FakeChromeUserManager>());

    testing_profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(testing_profile_manager_->SetUp());

    primary_profile_ = std::make_unique<ProfileEnvironment>(
        AddLoggedInUser(AccountId::FromUserEmail("primary@test")),
        disk_mount_manager_.get());
  }

  void TearDown() override {
    task_environment_.RunUntilIdle();
    primary_profile_.reset();
    testing_profile_manager_->DeleteAllTestingProfiles();

    disk_mount_manager_.reset();
    chromeos::PowerManagerClient::Shutdown();

    // ExternalMountPoints instance for the system is global singleton,
    // so some states can be leaked to another test. Revoke all of them
    // explicitly.
    storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();
  }

  virtual TestingProfile* AddLoggedInUser(const AccountId& account_id) {
    fake_user_manager_->AddUser(account_id);
    fake_user_manager_->LoginUser(account_id);
    TestingProfile* profile = testing_profile_manager_->CreateTestingProfile(
        account_id.GetUserEmail());
    ash::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
        fake_user_manager_->FindUserAndModify(account_id), profile);
    return profile;
  }

  // Accessors to the primary profile.
  TestingProfile* profile() const { return primary_profile_->profile(); }
  VolumeManager* volume_manager() const {
    return primary_profile_->volume_manager();
  }
  ProfileEnvironment* primary_profile() { return primary_profile_.get(); }

  base::test::ScopedCommandLine scoped_command_line_;
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<FakeDiskMountManager> disk_mount_manager_;
  user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
      fake_user_manager_;
  std::unique_ptr<ProfileEnvironment> primary_profile_;
  std::unique_ptr<TestingProfileManager> testing_profile_manager_;
};

TEST(VolumeTest, CreateForRemovable) {
  const std::unique_ptr<Volume> volume = Volume::CreateForRemovable(
      {"/source/path", "/mount/path", MountType::kDevice,
       MountError::kUnknownFilesystem},
      nullptr);
  ASSERT_TRUE(volume);
  EXPECT_EQ(volume->source_path(), FilePath("/source/path"));
  EXPECT_EQ(volume->mount_path(), FilePath("/mount/path"));
  EXPECT_EQ(volume->type(), VOLUME_TYPE_REMOVABLE_DISK_PARTITION);
  EXPECT_EQ(volume->mount_condition(), MountError::kUnknownFilesystem);
  EXPECT_EQ(volume->volume_id(), "removable:path");
  EXPECT_EQ(volume->volume_label(), "path");
  EXPECT_EQ(volume->source(), SOURCE_DEVICE);
  EXPECT_FALSE(volume->is_read_only());
  EXPECT_TRUE(volume->watchable());
}

TEST_F(VolumeManagerTest, OnDriveFileSystemMountAndUnmount) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnFileSystemMounted();

  ASSERT_EQ(1U, observer.events().size());
  LoggingObserver::Event event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type());
  EXPECT_EQ(drive::DriveIntegrationServiceFactory::GetForProfile(profile())
                ->GetMountPointPath()
                .AsUTF8Unsafe(),
            event.device_path());
  EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());

  volume_manager()->OnFileSystemBeingUnmounted();

  ASSERT_EQ(2U, observer.events().size());
  event = observer.events()[1];
  EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type());
  EXPECT_EQ(drive::DriveIntegrationServiceFactory::GetForProfile(profile())
                ->GetMountPointPath()
                .AsUTF8Unsafe(),
            event.device_path());
  EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
}

TEST_F(VolumeManagerTest, OnDriveFileSystemUnmountWithoutMount) {
  ScopedLoggingObserver observer(volume_manager());
  volume_manager()->OnFileSystemBeingUnmounted();

  // Unmount event for non-mounted volume is not reported.
  ASSERT_EQ(0U, observer.events().size());
}

TEST_F(VolumeManagerTest, OnBootDeviceDiskEvent) {
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> disk =
      Disk::Builder().SetDevicePath("device1").SetOnBootDevice(true).Build();

  volume_manager()->OnBootDeviceDiskEvent(DiskMountManager::DISK_ADDED, *disk);
  EXPECT_EQ(0U, observer.events().size());

  volume_manager()->OnBootDeviceDiskEvent(DiskMountManager::DISK_REMOVED,
                                          *disk);
  EXPECT_EQ(0U, observer.events().size());

  volume_manager()->OnBootDeviceDiskEvent(DiskMountManager::DISK_CHANGED,
                                          *disk);
  EXPECT_EQ(0U, observer.events().size());
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_Hidden) {
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> disk =
      Disk::Builder().SetDevicePath("device1").SetIsHidden(true).Build();

  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                             *disk);
  EXPECT_EQ(0U, observer.events().size());

  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_REMOVED,
                                             *disk);
  EXPECT_EQ(0U, observer.events().size());

  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_CHANGED,
                                             *disk);
  EXPECT_EQ(0U, observer.events().size());
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_Added) {
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> empty_device_path_disk = Disk::Builder().Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                             *empty_device_path_disk);
  EXPECT_EQ(0U, observer.events().size());

  std::unique_ptr<const Disk> media_disk =
      Disk::Builder().SetDevicePath("device1").SetHasMedia(true).Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                             *media_disk);
  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_TRUE(event.mounting());

  ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
  const FakeDiskMountManager::MountRequest& mount_request =
      disk_mount_manager_->mount_requests()[0];
  EXPECT_EQ("device1", mount_request.source_path);
  EXPECT_EQ("", mount_request.source_format);
  EXPECT_EQ("", mount_request.mount_label);
  EXPECT_EQ(ash::MountType::kDevice, mount_request.type);
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_AddedNonMounting) {
  // Device which is already mounted.
  {
    ScopedLoggingObserver observer(volume_manager());

    std::unique_ptr<const Disk> mounted_media_disk =
        Disk::Builder()
            .SetDevicePath("device1")
            .SetMountPath("mounted")
            .SetHasMedia(true)
            .Build();
    volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                               *mounted_media_disk);
    ASSERT_EQ(1U, observer.events().size());
    const LoggingObserver::Event& event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type());
    EXPECT_EQ("device1", event.device_path());
    EXPECT_FALSE(event.mounting());

    ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
  }

  // Device without media.
  {
    ScopedLoggingObserver observer(volume_manager());

    std::unique_ptr<const Disk> no_media_disk =
        Disk::Builder().SetDevicePath("device1").Build();
    volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                               *no_media_disk);
    ASSERT_EQ(1U, observer.events().size());
    const LoggingObserver::Event& event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type());
    EXPECT_EQ("device1", event.device_path());
    EXPECT_FALSE(event.mounting());

    ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
  }
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_ExternalStoragePolicy) {
  std::unique_ptr<const Disk> media_disk = CreateAllowlistedDisk("device1");

  // Disable external storage by policy.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageDisabled,
                                    true);

  // Disk mounting is blocked by policy.
  {
    ScopedLoggingObserver observer(volume_manager());
    volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                               *media_disk);
    ASSERT_EQ(1U, observer.events().size());
    const LoggingObserver::Event& event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::DISK_ADD_BLOCKED_BY_POLICY, event.type());
    EXPECT_EQ("device1", event.device_path());
    ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
  }

  // Set the external storage allowlist.
  SetExternalStorageAllowlist(profile()->GetPrefs());

  // Disk mounting is not blocked because of the allowlist.
  {
    ScopedLoggingObserver observer(volume_manager());
    volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                               *media_disk);
    ASSERT_EQ(1U, observer.events().size());
    const LoggingObserver::Event& event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type());
    EXPECT_EQ("device1", event.device_path());
    EXPECT_TRUE(event.mounting());
    ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
  }
}

TEST_F(VolumeManagerTest, OnDiskAutoMountableEvent_Removed) {
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> mounted_disk = Disk::Builder()
                                                 .SetDevicePath("device1")
                                                 .SetMountPath("mount_path")
                                                 .Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_REMOVED,
                                             *mounted_disk);

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type());
  EXPECT_EQ("device1", event.device_path());

  ASSERT_EQ(1U, disk_mount_manager_->unmount_requests().size());
  EXPECT_EQ("mount_path", disk_mount_manager_->unmount_requests()[0]);
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_RemovedNotMounted) {
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> not_mounted_disk =
      Disk::Builder().SetDevicePath("device1").Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_REMOVED,
                                             *not_mounted_disk);

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type());
  EXPECT_EQ("device1", event.device_path());

  ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_Changed) {
  // Changed event should cause mounting (if possible).
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> disk =
      Disk::Builder().SetDevicePath("device1").SetHasMedia(true).Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_CHANGED,
                                             *disk);

  EXPECT_EQ(1U, observer.events().size());
  EXPECT_EQ(1U, disk_mount_manager_->mount_requests().size());
  EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
  // Read-write mode by default.
  EXPECT_EQ(ash::MountAccessMode::kReadWrite,
            disk_mount_manager_->mount_requests()[0].access_mode);
}

TEST_F(VolumeManagerTest, OnAutoMountableDiskEvent_ChangedInReadonly) {
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageReadOnly,
                                    true);

  // Changed event should cause mounting (if possible).
  ScopedLoggingObserver observer(volume_manager());

  std::unique_ptr<const Disk> disk =
      Disk::Builder().SetDevicePath("device1").SetHasMedia(true).Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_CHANGED,
                                             *disk);

  EXPECT_EQ(1U, observer.events().size());
  EXPECT_EQ(1U, disk_mount_manager_->mount_requests().size());
  EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
  // Should mount a disk in read-only mode.
  EXPECT_EQ(ash::MountAccessMode::kReadOnly,
            disk_mount_manager_->mount_requests()[0].access_mode);
}

TEST_F(VolumeManagerTest, OnDeviceEvent_Added) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnDeviceEvent(DiskMountManager::DEVICE_ADDED, "device1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::DEVICE_ADDED, event.type());
  EXPECT_EQ("device1", event.device_path());
}

TEST_F(VolumeManagerTest, OnDeviceEvent_Removed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnDeviceEvent(DiskMountManager::DEVICE_REMOVED, "device1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, event.type());
  EXPECT_EQ("device1", event.device_path());
}

TEST_F(VolumeManagerTest, OnDeviceEvent_Scanned) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnDeviceEvent(DiskMountManager::DEVICE_SCANNED, "device1");

  // SCANNED event is just ignored.
  EXPECT_EQ(0U, observer.events().size());
}

TEST_F(VolumeManagerTest, OnMountEvent_MountingAndUnmounting) {
  ScopedLoggingObserver observer(volume_manager());

  const DiskMountManager::MountPoint kMountPoint{"device1", "mount1",
                                                 ash::MountType::kDevice};

  volume_manager()->OnMountEvent(DiskMountManager::MOUNTING,
                                 ash::MountError::kSuccess, kMountPoint);

  ASSERT_EQ(1U, observer.events().size());
  LoggingObserver::Event event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());

  volume_manager()->OnMountEvent(DiskMountManager::UNMOUNTING,
                                 ash::MountError::kSuccess, kMountPoint);

  ASSERT_EQ(2U, observer.events().size());
  event = observer.events()[1];
  EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
}

TEST_F(VolumeManagerTest, OnMountEvent_ExternalStoragePolicy) {
  disk_mount_manager_->AddDiskForTest(CreateAllowlistedDisk("device1"));
  const DiskMountManager::MountPoint kMountPoint{"device1", "mount1",
                                                 ash::MountType::kDevice};

  // Disable external storage by policy.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageDisabled,
                                    true);

  // Disk mounting is blocked by policy.
  {
    ScopedLoggingObserver observer(volume_manager());
    volume_manager()->OnMountEvent(DiskMountManager::MOUNTING,
                                   ash::MountError::kSuccess, kMountPoint);
    ASSERT_EQ(1U, observer.events().size());
    LoggingObserver::Event event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::DISK_ADD_BLOCKED_BY_POLICY, event.type());
    EXPECT_EQ("device1", event.device_path());
  }

  // Set the external storage allowlist.
  SetExternalStorageAllowlist(profile()->GetPrefs());

  // Disk mounting is not blocked because of the allowlist.
  {
    ScopedLoggingObserver observer(volume_manager());
    volume_manager()->OnMountEvent(DiskMountManager::MOUNTING,
                                   ash::MountError::kSuccess, kMountPoint);
    ASSERT_EQ(1U, observer.events().size());
    LoggingObserver::Event event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type());
    EXPECT_EQ("device1", event.device_path());
    EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
  }
}

TEST_F(VolumeManagerTest, OnMountEvent_Remounting) {
  std::unique_ptr<Disk> disk = Disk::Builder()
                                   .SetDevicePath("device1")
                                   .SetFileSystemUUID("uuid1")
                                   .Build();
  disk_mount_manager_->AddDiskForTest(std::move(disk));
  disk_mount_manager_->MountPath("device1", "", "", {}, ash::MountType::kDevice,
                                 ash::MountAccessMode::kReadWrite,
                                 base::DoNothing());

  const DiskMountManager::MountPoint kMountPoint{"device1", "mount1",
                                                 ash::MountType::kDevice};

  volume_manager()->OnMountEvent(DiskMountManager::MOUNTING,
                                 ash::MountError::kSuccess, kMountPoint);

  // Emulate system suspend and then resume.
  chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
      power_manager::SuspendImminent_Reason_OTHER);
  chromeos::FakePowerManagerClient::Get()->SendSuspendDone();

  // After resume, the device is unmounted and then mounted.
  volume_manager()->OnMountEvent(DiskMountManager::UNMOUNTING,
                                 ash::MountError::kSuccess, kMountPoint);

  // Observe what happened for the mount event.
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnMountEvent(DiskMountManager::MOUNTING,
                                 ash::MountError::kSuccess, kMountPoint);

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
}

TEST_F(VolumeManagerTest, OnMountEvent_UnmountingWithoutMounting) {
  ScopedLoggingObserver observer(volume_manager());

  const DiskMountManager::MountPoint kMountPoint{"device1", "mount1",
                                                 ash::MountType::kDevice};

  volume_manager()->OnMountEvent(DiskMountManager::UNMOUNTING,
                                 ash::MountError::kSuccess, kMountPoint);

  // Unmount event for a disk not mounted in this manager is not reported.
  ASSERT_EQ(0U, observer.events().size());
}

TEST_F(VolumeManagerTest, OnFormatEvent_Started) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnFormatEvent(DiskMountManager::FORMAT_STARTED,
                                  ash::FormatError::kSuccess, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_TRUE(event.success());
}

TEST_F(VolumeManagerTest, OnFormatEvent_StartFailed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnFormatEvent(DiskMountManager::FORMAT_STARTED,
                                  ash::FormatError::kUnknownError, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_FALSE(event.success());
}

TEST_F(VolumeManagerTest, OnFormatEvent_Completed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
                                  ash::FormatError::kSuccess, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_TRUE(event.success());

  // When "format" is done, VolumeManager requests to mount it.
  ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
  const FakeDiskMountManager::MountRequest& mount_request =
      disk_mount_manager_->mount_requests()[0];
  EXPECT_EQ("device1", mount_request.source_path);
  EXPECT_EQ("", mount_request.source_format);
  EXPECT_EQ("", mount_request.mount_label);
  EXPECT_EQ(ash::MountType::kDevice, mount_request.type);
}

TEST_F(VolumeManagerTest, OnFormatEvent_CompletedFailed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
                                  ash::FormatError::kUnknownError, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_FALSE(event.success());

  // When "format" is done, VolumeManager requests to mount it.
  ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
  const FakeDiskMountManager::MountRequest& mount_request =
      disk_mount_manager_->mount_requests()[0];
  EXPECT_EQ("device1", mount_request.source_path);
  EXPECT_EQ("", mount_request.source_format);
  EXPECT_EQ("", mount_request.mount_label);
  EXPECT_EQ(ash::MountType::kDevice, mount_request.type);
}

TEST_F(VolumeManagerTest, OnPartitionEvent_Started) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnPartitionEvent(DiskMountManager::PARTITION_STARTED,
                                     ash::PartitionError::kSuccess, "device1",
                                     "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::PARTITION_STARTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_TRUE(event.success());
}

TEST_F(VolumeManagerTest, OnPartitionEvent_StartFailed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnPartitionEvent(DiskMountManager::PARTITION_STARTED,
                                     ash::PartitionError::kUnknownError,
                                     "device1", "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::PARTITION_STARTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_FALSE(event.success());
}

TEST_F(VolumeManagerTest, OnPartitionEvent_Completed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnPartitionEvent(DiskMountManager::PARTITION_COMPLETED,
                                     ash::PartitionError::kSuccess, "device1",
                                     "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::PARTITION_COMPLETED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_TRUE(event.success());
}

TEST_F(VolumeManagerTest, OnPartitionEvent_CompletedFailed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnPartitionEvent(DiskMountManager::PARTITION_COMPLETED,
                                     ash::PartitionError::kUnknownError,
                                     "device1", "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::PARTITION_COMPLETED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_FALSE(event.success());

  // When "partitioning" fails, VolumeManager requests to mount it for retry.
  ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
  const FakeDiskMountManager::MountRequest& mount_request =
      disk_mount_manager_->mount_requests()[0];
  EXPECT_EQ("device1", mount_request.source_path);
  EXPECT_EQ("", mount_request.source_format);
  EXPECT_EQ("", mount_request.mount_label);
  EXPECT_EQ(ash::MountType::kDevice, mount_request.type);
}

TEST_F(VolumeManagerTest, OnExternalStorageDisabledChanged) {
  // Set up ExternalStorageAllowlist.
  disk_mount_manager_->AddDiskForTest(CreateAllowlistedDisk("mount1"));
  SetExternalStorageAllowlist(profile()->GetPrefs());

  // Subscribe to pref changes.
  volume_manager()->Initialize();

  // Create four mount points (first one is allowlisted).
  disk_mount_manager_->MountPath("mount1", "", "", {}, ash::MountType::kDevice,
                                 ash::MountAccessMode::kReadWrite,
                                 base::DoNothing());
  disk_mount_manager_->MountPath("mount2", "", "", {}, ash::MountType::kDevice,
                                 ash::MountAccessMode::kReadOnly,
                                 base::DoNothing());
  disk_mount_manager_->MountPath(
      "mount3", "", "", {}, ash::MountType::kNetworkStorage,
      ash::MountAccessMode::kReadOnly, base::DoNothing());
  disk_mount_manager_->MountPath(
      "failed_unmount", "", "", {}, ash::MountType::kDevice,
      ash::MountAccessMode::kReadWrite, base::DoNothing());
  disk_mount_manager_->FailUnmountRequest("failed_unmount",
                                          ash::MountError::kUnknownError);

  // Initially, there are four mount points.
  ASSERT_EQ(4U, disk_mount_manager_->mount_points().size());
  ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());

  // Set kExternalStorageDisabled to false and expect no effects.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageDisabled,
                                    false);
  EXPECT_EQ(4U, disk_mount_manager_->mount_points().size());
  EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());

  // Set kExternalStorageDisabled to true.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageDisabled,
                                    true);

  // Wait until all unmount request finishes, so that callback chain to unmount
  // all the mount points will be invoked.
  disk_mount_manager_->FinishAllUnmountPathRequests();

  // External media mount points which are not allowlisted should be unmounted.
  // Other mount point types should remain. The failing unmount should also
  // remain.
  EXPECT_EQ(3U, disk_mount_manager_->mount_points().size());
  EXPECT_THAT(disk_mount_manager_->unmount_requests(),
              UnorderedElementsAre("mount2", "failed_unmount"));
}

TEST_F(VolumeManagerTest, ExternalStorageDisabledPolicyMultiProfile) {
  auto secondary = std::make_unique<ProfileEnvironment>(
      AddLoggedInUser(AccountId::FromUserEmail("secondary@test")),
      disk_mount_manager_.get());
  volume_manager()->Initialize();
  secondary->volume_manager()->Initialize();

  // Simulates the case that the main profile has kExternalStorageDisabled set
  // as false, and the secondary profile has the config set to true.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageDisabled,
                                    false);
  secondary->profile()->GetPrefs()->SetBoolean(
      disks::prefs::kExternalStorageDisabled, true);

  ScopedLoggingObserver main_observer(volume_manager());
  ScopedLoggingObserver secondary_observer(secondary->volume_manager());

  // Add 1 disk.
  std::unique_ptr<const Disk> media_disk =
      Disk::Builder().SetDevicePath("device1").SetHasMedia(true).Build();
  volume_manager()->OnAutoMountableDiskEvent(DiskMountManager::DISK_ADDED,
                                             *media_disk);
  secondary->volume_manager()->OnAutoMountableDiskEvent(
      DiskMountManager::DISK_ADDED, *media_disk);

  // The profile with external storage enabled should have mounted the volume.
  auto is_volume_mounted = [](const auto& event) {
    return event.type() == LoggingObserver::Event::VOLUME_MOUNTED;
  };
  EXPECT_TRUE(std::ranges::any_of(main_observer.events(), is_volume_mounted));

  // The other profiles with external storage disabled should have not.
  EXPECT_FALSE(
      std::ranges::any_of(secondary_observer.events(), is_volume_mounted));
}

TEST_F(VolumeManagerTest, OnExternalStorageReadOnlyChanged) {
  // This subscribes to pref changes.
  volume_manager()->Initialize();

  // Set up some disks (first one is allowlisted).
  disk_mount_manager_->AddDiskForTest(CreateAllowlistedDisk("device1"));
  disk_mount_manager_->AddDiskForTest(
      Disk::Builder().SetDevicePath("device2").Build());

  // Trigger pref updates.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageReadOnly,
                                    true);
  SetExternalStorageAllowlist(profile()->GetPrefs());
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageReadOnly,
                                    false);

  // Verify that removable disk remounts are triggered.
  using ash::MountAccessMode;
  std::vector<FakeDiskMountManager::RemountRequest> expected = {
      // ExternalStorageReadOnly set to true.
      {"device1", MountAccessMode::kReadOnly},
      {"device2", MountAccessMode::kReadOnly},
      // ExternalStorageAllowlist set to device1.
      {"device1", MountAccessMode::kReadWrite},
      {"device2", MountAccessMode::kReadOnly},
      // ExternalStorageReadOnly set to false.
      {"device1", MountAccessMode::kReadWrite},
      {"device2", MountAccessMode::kReadWrite},
  };
  EXPECT_EQ(expected, disk_mount_manager_->remount_requests());
}

TEST_F(VolumeManagerTest, GetVolumeList) {
  volume_manager()->Initialize();  // Adds "Downloads"
  std::vector<base::WeakPtr<Volume>> volume_list =
      volume_manager()->GetVolumeList();
  ASSERT_GT(volume_list.size(), 0u);
}

TEST_F(VolumeManagerTest, VolumeManagerInitializeMyFilesVolume) {
  // Emulate running inside ChromeOS.
  base::test::ScopedRunningOnChromeOS running_on_chromeos;
  volume_manager()->Initialize();  // Adds "Downloads"
  std::vector<base::WeakPtr<Volume>> volume_list =
      volume_manager()->GetVolumeList();
  ASSERT_GT(volume_list.size(), 0u);
  auto volume =
      std::ranges::find(volume_list, "downloads:MyFiles", &Volume::volume_id);
  EXPECT_FALSE(volume == volume_list.end());
  EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, (*volume)->type());
}

TEST_F(VolumeManagerTest, FindVolumeById) {
  volume_manager()->Initialize();  // Adds "Downloads"
  base::WeakPtr<Volume> bad_volume =
      volume_manager()->FindVolumeById("nonexistent");
  ASSERT_FALSE(bad_volume.get());
  base::WeakPtr<Volume> good_volume =
      volume_manager()->FindVolumeById("downloads:MyFiles");
  ASSERT_TRUE(good_volume.get());
  EXPECT_EQ("downloads:MyFiles", good_volume->volume_id());
  EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, good_volume->type());
}

TEST_F(VolumeManagerTest, VolumeManagerInitializeShareCacheVolume) {
  volume_manager()->Initialize();
  base::WeakPtr<Volume> share_cache_volume =
      volume_manager()->FindVolumeById("system_internal:ShareCache");
  ASSERT_TRUE(share_cache_volume.get());
  EXPECT_EQ("system_internal:ShareCache", share_cache_volume->volume_id());
  EXPECT_EQ(VOLUME_TYPE_SYSTEM_INTERNAL, share_cache_volume->type());
}

TEST_F(VolumeManagerTest, FindVolumeFromPath) {
  volume_manager()->Initialize();  // Adds "Downloads"
  base::WeakPtr<Volume> downloads_volume = volume_manager()->GetVolumeList()[0];
  EXPECT_EQ("downloads:MyFiles", downloads_volume->volume_id());
  base::FilePath downloads_mount_path = downloads_volume->mount_path();
  // FindVolumeFromPath(downloads_mount_path.DirName()) should return null
  // because the path is the parent folder of the Downloads mount path.
  base::WeakPtr<Volume> volume_from_path =
      volume_manager()->FindVolumeFromPath(downloads_mount_path.DirName());
  ASSERT_FALSE(volume_from_path);
  // FindVolumeFromPath("MyFiles") should return null because it's only the last
  // component of the Downloads mount path.
  volume_from_path =
      volume_manager()->FindVolumeFromPath(downloads_mount_path.BaseName());
  ASSERT_FALSE(volume_from_path);
  // FindVolumeFromPath(<Downloads mount path>) should point to the Downloads
  // volume.
  volume_from_path = volume_manager()->FindVolumeFromPath(downloads_mount_path);
  ASSERT_TRUE(volume_from_path);
  EXPECT_EQ("downloads:MyFiles", volume_from_path->volume_id());
  // FindVolumeFromPath(<Downloads mount path>/folder) is on the Downloads
  // volume, it should also point to the Downloads volume, even if the folder
  // doesn't exist.
  volume_from_path = volume_manager()->FindVolumeFromPath(
      downloads_mount_path.Append("folder"));
  ASSERT_TRUE(volume_from_path);
  EXPECT_EQ("downloads:MyFiles", volume_from_path->volume_id());
}

TEST_F(VolumeManagerTest, ArchiveSourceFiltering) {
  ScopedLoggingObserver observer(volume_manager());

  // Mount a USB stick.
  volume_manager()->OnMountEvent(
      DiskMountManager::MOUNTING, ash::MountError::kSuccess,
      {"/removable/usb", "/removable/usb", ash::MountType::kDevice});

  // Mount a zip archive in the stick.
  volume_manager()->OnMountEvent(
      DiskMountManager::MOUNTING, ash::MountError::kSuccess,
      {"/removable/usb/1.zip", "/archive/1", ash::MountType::kArchive});
  base::WeakPtr<Volume> volume = volume_manager()->FindVolumeById("archive:1");
  ASSERT_TRUE(volume.get());
  EXPECT_EQ("/archive/1", volume->mount_path().AsUTF8Unsafe());
  EXPECT_EQ(2u, observer.events().size());

  // Mount a zip archive in the previous zip archive.
  volume_manager()->OnMountEvent(
      DiskMountManager::MOUNTING, ash::MountError::kSuccess,
      {"/archive/1/2.zip", "/archive/2", ash::MountType::kArchive});
  base::WeakPtr<Volume> second_volume =
      volume_manager()->FindVolumeById("archive:2");
  ASSERT_TRUE(second_volume.get());
  EXPECT_EQ("/archive/2", second_volume->mount_path().AsUTF8Unsafe());
  EXPECT_EQ(3u, observer.events().size());

  // A zip file is mounted from other profile. It must be ignored in the current
  // VolumeManager.
  volume_manager()->OnMountEvent(DiskMountManager::MOUNTING,
                                 ash::MountError::kSuccess,
                                 {"/other/profile/drive/folder/3.zip",
                                  "/archive/3", ash::MountType::kArchive});
  base::WeakPtr<Volume> third_volume =
      volume_manager()->FindVolumeById("archive:3");
  ASSERT_FALSE(third_volume.get());
  EXPECT_EQ(3u, observer.events().size());
}

TEST_F(VolumeManagerTest, MTPPlugAndUnplug) {
  ScopedLoggingObserver observer(volume_manager());

  storage_monitor::StorageInfo info(
      storage_monitor::StorageInfo::MakeDeviceId(
          storage_monitor::StorageInfo::MTP_OR_PTP, "dummy-device-id"),
      FILE_PATH_LITERAL("/dummy/device/location"), u"label", u"vendor",
      u"model", 12345 /* size */);

  storage_monitor::StorageInfo non_mtp_info(
      storage_monitor::StorageInfo::MakeDeviceId(
          storage_monitor::StorageInfo::FIXED_MASS_STORAGE, "dummy-device-id2"),
      FILE_PATH_LITERAL("/dummy/device/location2"), u"label2", u"vendor2",
      u"model2", 12345 /* size */);

  // Attach: expect mount events for the MTP and fusebox MTP volumes.
  volume_manager()->OnRemovableStorageAttached(info);
  ASSERT_EQ(2u, observer.events().size());
  EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED,
            observer.events()[0].type());
  EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED,
            observer.events()[1].type());

  // The MTP volume should be mounted.
  base::WeakPtr<Volume> volume = volume_manager()->FindVolumeById("mtp:model");
  ASSERT_TRUE(volume);
  EXPECT_EQ("", volume->file_system_type());
  EXPECT_EQ(VOLUME_TYPE_MTP, volume->type());

  // The fusebox MTP volume should be mounted.
  const auto fusebox_volume_id = base::StrCat({util::kFuseBox, "mtp:model"});
  base::WeakPtr<Volume> fusebox_volume =
      volume_manager()->FindVolumeById(fusebox_volume_id);
  ASSERT_TRUE(fusebox_volume);
  EXPECT_EQ(util::kFuseBox, fusebox_volume->file_system_type());
  EXPECT_EQ(VOLUME_TYPE_MTP, fusebox_volume->type());

  // Non MTP attach events from storage monitor are ignored.
  volume_manager()->OnRemovableStorageAttached(non_mtp_info);
  EXPECT_EQ(2u, observer.events().size());

  // Detach: there should be two more events, bringing the total to four.
  volume_manager()->OnRemovableStorageDetached(info);
  ASSERT_EQ(4u, observer.events().size());
  EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED,
            observer.events()[2].type());
  EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED,
            observer.events()[3].type());

  // The unmount events should remove the MTP and fusebox MTP volumes.
  EXPECT_FALSE(volume);
  EXPECT_FALSE(fusebox_volume);
}

TEST_F(VolumeManagerTest, MTP_ExternalStoragePolicy) {
  storage_monitor::StorageInfo info(
      storage_monitor::StorageInfo::MakeDeviceId(
          storage_monitor::StorageInfo::MTP_OR_PTP, "dummy-device-id"),
      FILE_PATH_LITERAL("/dummy/device/location"), u"label", u"vendor",
      u"model", 12345 /* size */);

  // Disable external storage by policy.
  profile()->GetPrefs()->SetBoolean(disks::prefs::kExternalStorageDisabled,
                                    true);

  // Attach is blocked by policy.
  {
    ScopedLoggingObserver observer(volume_manager());
    primary_profile()->SetFakeMtpStorageInfo(
        CreateAllowlistedMtpStorageInfo("dummy/device/location"));
    volume_manager()->OnRemovableStorageAttached(info);
    ASSERT_EQ(1u, observer.events().size());
    const LoggingObserver::Event& event = observer.events()[0];
    EXPECT_EQ(LoggingObserver::Event::DISK_ADD_BLOCKED_BY_POLICY, event.type());
    EXPECT_EQ("/dummy/device/location", event.device_path());
  }

  // Set the external storage allowlist.
  SetExternalStorageAllowlist(profile()->GetPrefs());

  // Attach is not blocked because of the allowlist.
  {
    ScopedLoggingObserver observer(volume_manager());
    primary_profile()->SetFakeMtpStorageInfo(
        CreateAllowlistedMtpStorageInfo("dummy/device/location"));
    volume_manager()->OnRemovableStorageAttached(info);
    ASSERT_EQ(2u, observer.events().size());
    EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED,
              observer.events()[0].type());
    EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED,
              observer.events()[1].type());
  }

  // Cleanup. Detach storage, otherwise crashes in ~MTPDeviceMapService.
  volume_manager()->OnRemovableStorageDetached(info);
}

TEST_F(VolumeManagerTest, OnRenameEvent_Started) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnRenameEvent(DiskMountManager::RENAME_STARTED,
                                  ash::RenameError::kSuccess, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::RENAME_STARTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_TRUE(event.success());
}

TEST_F(VolumeManagerTest, OnRenameEvent_StartFailed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnRenameEvent(DiskMountManager::RENAME_STARTED,
                                  ash::RenameError::kUnknownError, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::RENAME_STARTED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_FALSE(event.success());
}

TEST_F(VolumeManagerTest, OnRenameEvent_Completed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnRenameEvent(DiskMountManager::RENAME_COMPLETED,
                                  ash::RenameError::kSuccess, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::RENAME_COMPLETED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_TRUE(event.success());

  // When "rename" is successfully done, VolumeManager requests to mount it.
  ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
  const FakeDiskMountManager::MountRequest& mount_request =
      disk_mount_manager_->mount_requests()[0];
  EXPECT_EQ("device1", mount_request.source_path);
  EXPECT_EQ("", mount_request.source_format);
  EXPECT_EQ(ash::MountType::kDevice, mount_request.type);
}

TEST_F(VolumeManagerTest, OnRenameEvent_CompletedFailed) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnRenameEvent(DiskMountManager::RENAME_COMPLETED,
                                  ash::RenameError::kUnknownError, "device1",
                                  "label1");

  ASSERT_EQ(1U, observer.events().size());
  const LoggingObserver::Event& event = observer.events()[0];
  EXPECT_EQ(LoggingObserver::Event::RENAME_COMPLETED, event.type());
  EXPECT_EQ("device1", event.device_path());
  EXPECT_EQ("label1", event.device_label());
  EXPECT_FALSE(event.success());

  EXPECT_EQ(1U, disk_mount_manager_->mount_requests().size());
}

TEST_F(VolumeManagerTest, VolumeManagerInitializeForMultiProfiles) {
  auto secondary_profile = std::make_unique<ProfileEnvironment>(
      AddLoggedInUser(AccountId::FromUserEmail("secondary@test")),
      disk_mount_manager_.get());

  volume_manager()->Initialize();
  secondary_profile->volume_manager()->Initialize();

  // Different profiles' shared cache and download volumes
  // should have different `mount_name`, see crbug.com/365173555.
  std::vector<storage::MountPoints::MountPointInfo> mount_point_infos;
  storage::ExternalMountPoints::GetSystemInstance()->AddMountPointInfosTo(
      &mount_point_infos);

  std::unordered_set<std::string> mount_point_names;
  for (const auto& mount_point_info : mount_point_infos) {
    mount_point_names.insert(mount_point_info.name);
  }

  ASSERT_THAT(mount_point_names, testing::SizeIs(4));
  EXPECT_THAT(
      mount_point_names,
      testing::UnorderedElementsAre(
          util::GetDownloadsMountPointName(profile()),
          util::GetDownloadsMountPointName(secondary_profile->profile()),
          util::GetShareCacheMountPointName(profile()),
          util::GetShareCacheMountPointName(secondary_profile->profile())));
}

// Test fixture for VolumeManager tests with ARC enabled.
class VolumeManagerArcTest : public VolumeManagerTest {
 protected:
  void SetUp() override {
    scoped_command_line_.GetProcessCommandLine()->AppendSwitchASCII(
        ash::switches::kArcAvailability, "officially-supported");
    VolumeManagerTest::SetUp();
  }

  void TearDown() override {
    arc_service_manager_->arc_bridge_service()->file_system()->CloseInstance(
        &file_system_instance_);
    arc_service_manager_->set_browser_context(nullptr);
    VolumeManagerTest::TearDown();
  }

  TestingProfile* AddLoggedInUser(const AccountId& account_id) override {
    TestingProfile* profile = VolumeManagerTest::AddLoggedInUser(account_id);

    // Set up an Arc service manager with a fake file system. This must be done
    // before initializing VolumeManager() to make its dependency
    // DocumentsProviderRootManager work.
    CHECK(!arc_service_manager_);
    arc_service_manager_ = std::make_unique<arc::ArcServiceManager>();
    arc_service_manager_->set_browser_context(profile);
    arc::ArcFileSystemOperationRunner::GetFactory()->SetTestingFactoryAndUse(
        profile,
        base::BindRepeating(&CreateFileSystemOperationRunnerForTesting));
    arc_service_manager_->arc_bridge_service()->file_system()->SetInstance(
        &file_system_instance_);
    arc::WaitForInstanceReady(
        arc_service_manager_->arc_bridge_service()->file_system());
    EXPECT_TRUE(file_system_instance_.InitCalled());
    return profile;
  }

 private:
  arc::FakeFileSystemInstance file_system_instance_;
  std::unique_ptr<arc::ArcServiceManager> arc_service_manager_;
};

TEST_F(VolumeManagerArcTest, OnArcPlayStoreEnabledChanged_Enabled) {
  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnArcPlayStoreEnabledChanged(true);

  ASSERT_EQ(5U, observer.events().size());

  size_t index = 0;
  for (const auto& event : observer.events()) {
    EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type());
    EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
    if (index < 4) {
      EXPECT_EQ(arc::GetMediaViewVolumeId(arc_volume_ids[index]),
                event.volume_id());
    } else {
      EXPECT_EQ(arc_volume_ids[index], event.volume_id());
    }
    index++;
  }
}

TEST_F(VolumeManagerArcTest, OnArcPlayStoreEnabledChanged_Disabled) {
  // Need to enable it first before disabling it, otherwise
  // it will be no-op.
  volume_manager()->OnArcPlayStoreEnabledChanged(true);

  ScopedLoggingObserver observer(volume_manager());

  volume_manager()->OnArcPlayStoreEnabledChanged(false);

  ASSERT_EQ(5U, observer.events().size());

  size_t index = 0;
  for (const auto& event : observer.events()) {
    EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type());
    EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
    if (index < 4) {
      EXPECT_EQ(arc::GetMediaViewVolumeId(arc_volume_ids[index]),
                event.volume_id());
    } else {
      EXPECT_EQ(arc_volume_ids[index], event.volume_id());
    }
    index++;
  }
}

TEST_F(VolumeManagerArcTest, ShouldAlwaysMountAndroidVolumesInFilesForTesting) {
  base::test::ScopedCommandLine command_line;
  command_line.GetProcessCommandLine()->AppendSwitch(
      ash::switches::kArcForceMountAndroidVolumesInFiles);

  ScopedLoggingObserver observer(volume_manager());

  // Volumes are mounted even when Play Store is not enabled for the profile.
  volume_manager()->OnArcPlayStoreEnabledChanged(false);

  ASSERT_EQ(5U, observer.events().size());

  size_t index = 0;
  for (const auto& event : observer.events()) {
    EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type());
    EXPECT_EQ(ash::MountError::kSuccess, event.mount_error());
    if (index < 4) {
      EXPECT_EQ(arc::GetMediaViewVolumeId(arc_volume_ids[index]),
                event.volume_id());
    } else {
      EXPECT_EQ(arc_volume_ids[index], event.volume_id());
    }
    index++;
  }

  // No volume-related event happens after Play Store preference changes,
  // because volumes are just kept being mounted.
  volume_manager()->OnArcPlayStoreEnabledChanged(true);
  volume_manager()->OnArcPlayStoreEnabledChanged(false);
  ASSERT_EQ(5U, observer.events().size());
}

// Tests VolumeManager with the LocalUserFilesAllowed policy.
class VolumeManagerLocalUserFilesTest : public VolumeManagerArcTest {
 public:
  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        {features::kSkyVault, features::kSkyVaultV2}, {});
    VolumeManagerArcTest::SetUp();
  }

  void TearDown() override { VolumeManagerArcTest::TearDown(); }

  void SetLocalUserFilesPolicy(bool allowed) {
    testing_profile_manager_->local_state()->Get()->SetBoolean(
        prefs::kLocalUserFilesAllowed, allowed);
  }

  void SetLocalUserFilesMigrationPolicy(const std::string& destination) {
    testing_profile_manager_->local_state()->Get()->SetString(
        prefs::kLocalUserFilesMigrationDestination, destination);
    volume_manager()->OnMigrationSucceededForTesting();
  }

  bool ContainsDownloads() {
    std::vector<base::WeakPtr<Volume>> volume_list =
        volume_manager()->GetVolumeList();
    if (volume_list.size() == 0u) {
      return false;
    }
    auto volume =
        std::ranges::find(volume_list, "downloads:MyFiles", &Volume::volume_id);
    return volume != volume_list.end() &&
           (*volume)->type() == VOLUME_TYPE_DOWNLOADS_DIRECTORY;
  }

  bool ContainsPlayFiles() {
    std::vector<base::WeakPtr<Volume>> volume_list =
        volume_manager()->GetVolumeList();
    if (volume_list.size() == 0u) {
      return false;
    }
    auto volume =
        std::ranges::find(volume_list, "android_files:0", &Volume::volume_id);
    return volume != volume_list.end() &&
           (*volume)->type() == VOLUME_TYPE_ANDROID_FILES;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests that VolumeManager removes local volumes when the policy is set to
// false, and adds them when set to true.
TEST_F(VolumeManagerLocalUserFilesTest, DisableEnable) {
  // Enable ARC.
  profile()->GetPrefs()->SetBoolean(arc::prefs::kArcEnabled, true);
  // Emulate running inside ChromeOS.
  base::test::ScopedRunningOnChromeOS running_on_chromeos;
  volume_manager()->Initialize();  // Adds "Downloads" and "Play Files"
  EXPECT_TRUE(ContainsDownloads());
  EXPECT_TRUE(ContainsPlayFiles());

  // Setting the policy to false removes only "Play Files".
  SetLocalUserFilesPolicy(/*allowed=*/false);
  EXPECT_TRUE(ContainsDownloads());
  EXPECT_FALSE(ContainsPlayFiles());

  // Setting the migration policy removes also "Downloads".
  SetLocalUserFilesMigrationPolicy(download_dir_util::kLocationGoogleDrive);
  EXPECT_FALSE(ContainsDownloads());
  EXPECT_FALSE(ContainsPlayFiles());

  // Setting the policy to true adds local volumes.
  SetLocalUserFilesPolicy(/*allowed=*/true);
  EXPECT_TRUE(ContainsDownloads());
  EXPECT_TRUE(ContainsPlayFiles());

  // Another update with the same value shouldn't do anything.
  SetLocalUserFilesPolicy(/*allowed=*/true);
  EXPECT_TRUE(ContainsDownloads());
  EXPECT_TRUE(ContainsPlayFiles());
}

}  // namespace file_manager