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
content / browser / file_system_access / file_system_access_watcher_manager_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/file_system_access/file_system_access_watcher_manager.h"
#include <list>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/run_until.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/test/test_timeouts.h"
#include "build/buildflag.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/file_system_access/file_system_access_change_source.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "content/browser/file_system_access/file_system_access_watch_scope.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_web_contents_factory.h"
#include "content/test/test_web_contents.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/mock_quota_manager.h"
#include "storage/browser/test/mock_quota_manager_proxy.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "storage/browser/test/quota_manager_proxy_sync.h"
#include "storage/browser/test/test_file_system_context.h"
#include "storage/common/file_system/file_system_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_directory_handle.mojom.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_error.mojom-shared.h"
namespace content {
using Change = FileSystemAccessWatcherManager::Observation::Change;
using Observation = FileSystemAccessWatcherManager::Observation;
using ChangeInfo = FileSystemAccessChangeSource::ChangeInfo;
using ChangeType = FileSystemAccessChangeSource::ChangeType;
using FilePathType = FileSystemAccessChangeSource::FilePathType;
namespace {
constexpr char kTestMountPoint[] = "testfs";
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
void SpinEventLoopForABit() {
base::RunLoop loop;
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, loop.QuitClosure(), TestTimeouts::tiny_timeout());
loop.Run();
}
bool ReportsModifiedPathForLocalObservations() {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || \
BUILDFLAG(IS_MAC)
return true;
#else
return false;
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) ||
// BUILDFLAG(IS_WIN)
}
bool ReportsChangeInfoForLocalObservations() {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || \
BUILDFLAG(IS_MAC)
return true;
#else
return false;
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) ||
// BUILDFLAG(IS_MAC)
}
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) &&
// !BUILDFLAG(IS_IOS)
// Accumulates changes it receives from the given `observation`.
class ChangeAccumulator {
public:
explicit ChangeAccumulator(std::unique_ptr<Observation> observation)
: observation_(std::move(observation)) {
observation_->SetCallback(base::BindRepeating(&ChangeAccumulator::OnChanges,
weak_factory_.GetWeakPtr()));
}
ChangeAccumulator(const ChangeAccumulator&) = delete;
ChangeAccumulator& operator=(const ChangeAccumulator&) = delete;
~ChangeAccumulator() = default;
void OnChanges(const std::optional<std::list<Change>>& changes_or_error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (has_error_) {
return;
}
if (!changes_or_error.has_value()) {
received_changes_.clear();
has_error_ = true;
return;
}
for (const auto& change : changes_or_error.value()) {
received_changes_.push_back(change);
}
}
Observation* observation() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return observation_.get();
}
bool has_error() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return has_error_;
}
const std::list<Change>& changes() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return received_changes_;
}
private:
SEQUENCE_CHECKER(sequence_checker_);
std::unique_ptr<Observation> observation_
GUARDED_BY_CONTEXT(sequence_checker_);
std::list<Change> received_changes_ GUARDED_BY_CONTEXT(sequence_checker_);
bool has_error_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
base::WeakPtrFactory<ChangeAccumulator> weak_factory_
GUARDED_BY_CONTEXT(sequence_checker_){this};
};
// Trivial implementation of a change source which allows tests to signal
// changes.
class FakeChangeSource : public FileSystemAccessChangeSource {
public:
explicit FakeChangeSource(
FileSystemAccessWatchScope scope,
scoped_refptr<storage::FileSystemContext> file_system_context)
: FileSystemAccessChangeSource(std::move(scope),
std::move(file_system_context)) {}
FakeChangeSource(const FakeChangeSource&) = delete;
FakeChangeSource& operator=(const FakeChangeSource&) = delete;
~FakeChangeSource() override = default;
// FileSystemAccessChangeSource:
void Initialize(
base::OnceCallback<void(blink::mojom::FileSystemAccessErrorPtr)>
on_initialized) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::move(on_initialized).Run(initialization_result_->Clone());
}
void Signal(base::FilePath relative_path = base::FilePath(),
bool error = false,
ChangeInfo change_info = ChangeInfo()) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
NotifyOfChange(std::move(relative_path), error, change_info);
}
void Signal(const storage::FileSystemURL& changed_url,
bool error = false,
ChangeInfo change_info = ChangeInfo()) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
NotifyOfChange(changed_url, error, change_info);
}
void set_initialization_result(
blink::mojom::FileSystemAccessErrorPtr result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
initialization_result_ = std::move(result);
}
private:
blink::mojom::FileSystemAccessErrorPtr initialization_result_
GUARDED_BY_CONTEXT(sequence_checker_) =
blink::mojom::FileSystemAccessError::New(
blink::mojom::FileSystemAccessStatus::kOk,
base::File::FILE_OK,
"");
};
} // namespace
class FileSystemAccessWatcherManagerTest : public testing::Test {
public:
FileSystemAccessWatcherManagerTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}
void SetUp() override {
#if BUILDFLAG(IS_WIN)
ASSERT_TRUE(dir_.CreateUniqueTempDir());
// Convert path to long format to avoid mixing long and 8.3 formats in test.
ASSERT_TRUE(dir_.Set(base::MakeLongFilePath(dir_.Take())));
#elif BUILDFLAG(IS_MAC)
// Temporary files in Mac are created under /var/, which is a symlink that
// resolves to /private/var/. Set `dir_` directly to the resolved file
// path, given that the expected FSEvents event paths are reported as
// resolved paths.
ASSERT_TRUE(dir_.CreateUniqueTempDir());
base::FilePath resolved_path = base::MakeAbsoluteFilePath(dir_.GetPath());
if (!resolved_path.empty()) {
dir_.Take();
ASSERT_TRUE(dir_.Set(resolved_path));
}
#else
ASSERT_TRUE(dir_.CreateUniqueTempDir());
#endif
web_contents_ = web_contents_factory_.CreateWebContents(&browser_context_);
static_cast<TestWebContents*>(web_contents_)->NavigateAndCommit(kTestUrl);
quota_manager_ = base::MakeRefCounted<storage::MockQuotaManager>(
/*is_incognito=*/false, dir_.GetPath(),
base::SingleThreadTaskRunner::GetCurrentDefault(),
special_storage_policy_);
quota_manager_proxy_ = base::MakeRefCounted<storage::MockQuotaManagerProxy>(
quota_manager_.get(),
base::SingleThreadTaskRunner::GetCurrentDefault().get());
file_system_context_ = storage::CreateFileSystemContextForTesting(
quota_manager_proxy_.get(), dir_.GetPath());
storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
kTestMountPoint, storage::kFileSystemTypeLocal,
storage::FileSystemMountOption(), dir_.GetPath());
chrome_blob_context_ = base::MakeRefCounted<ChromeBlobStorageContext>();
chrome_blob_context_->InitializeOnIOThread(base::FilePath(),
base::FilePath(), nullptr);
manager_ = base::MakeRefCounted<FileSystemAccessManagerImpl>(
file_system_context_, chrome_blob_context_,
/*permission_context=*/nullptr,
/*off_the_record=*/false);
manager_->BindReceiver(kBindingContext,
manager_remote_.BindNewPipeAndPassReceiver());
}
void TearDown() override {
storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
kTestMountPoint);
manager_remote_.reset();
manager_.reset();
file_system_context_.reset();
chrome_blob_context_.reset();
task_environment_.RunUntilIdle();
EXPECT_TRUE(dir_.Delete());
}
bool CreateDirectory(const base::FilePath& full_path) {
bool result = base::CreateDirectory(full_path);
#if BUILDFLAG(IS_MAC)
// Wait so that the event for this operation is received by FSEvents before
// returning.
SpinEventLoopForABit();
#endif
return result;
}
bool WriteFile(const base::FilePath& filename, std::string_view data) {
bool result = base::WriteFile(filename, data);
#if BUILDFLAG(IS_MAC)
// Wait so that the event for this operation is received by FSEvents before
// returning.
SpinEventLoopForABit();
#endif
return result;
}
bool DeleteFile(const base::FilePath& path) {
bool result = base::DeleteFile(path);
#if BUILDFLAG(IS_MAC)
// Wait so that the event for this operation is received by FSEvents before
// returning.
SpinEventLoopForABit();
#endif
return result;
}
FileSystemAccessWatcherManager& watcher_manager() const {
return manager_->watcher_manager();
}
storage::QuotaErrorOr<storage::BucketLocator>
CreateSandboxFileSystemAndGetDefaultBucket() {
base::test::TestFuture<
blink::mojom::FileSystemAccessErrorPtr,
mojo::PendingRemote<blink::mojom::FileSystemAccessDirectoryHandle>>
future;
manager_remote_->GetSandboxedFileSystem(future.GetCallback());
blink::mojom::FileSystemAccessErrorPtr get_fs_result;
mojo::PendingRemote<blink::mojom::FileSystemAccessDirectoryHandle>
directory_remote;
std::tie(get_fs_result, directory_remote) = future.Take();
EXPECT_EQ(get_fs_result->status, blink::mojom::FileSystemAccessStatus::kOk);
mojo::Remote<blink::mojom::FileSystemAccessDirectoryHandle> root(
std::move(directory_remote));
EXPECT_TRUE(root);
storage::QuotaManagerProxySync quota_manager_proxy_sync(
quota_manager_proxy_.get());
// Check default bucket exists.
return quota_manager_proxy_sync
.GetBucket(kTestStorageKey, storage::kDefaultBucketName,
blink::mojom::StorageType::kTemporary)
.transform([&](storage::BucketInfo result) {
EXPECT_EQ(result.name, storage::kDefaultBucketName);
EXPECT_EQ(result.storage_key, kTestStorageKey);
EXPECT_GT(result.id.value(), 0);
return result.ToBucketLocator();
});
}
protected:
const GURL kTestUrl = GURL("http://example.com/foo");
const blink::StorageKey kTestStorageKey =
blink::StorageKey::CreateFromStringForTesting("https://example.com/test");
const int kProcessId = 1;
const int kFrameRoutingId = 2;
const GlobalRenderFrameHostId kFrameId{kProcessId, kFrameRoutingId};
const FileSystemAccessManagerImpl::BindingContext kBindingContext = {
kTestStorageKey, kTestUrl, kFrameId};
BrowserTaskEnvironment task_environment_;
base::ScopedTempDir dir_;
TestBrowserContext browser_context_;
TestWebContentsFactory web_contents_factory_;
scoped_refptr<storage::MockSpecialStoragePolicy> special_storage_policy_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
scoped_refptr<ChromeBlobStorageContext> chrome_blob_context_;
scoped_refptr<storage::MockQuotaManager> quota_manager_;
scoped_refptr<storage::MockQuotaManagerProxy> quota_manager_proxy_;
scoped_refptr<FileSystemAccessManagerImpl> manager_;
mojo::Remote<blink::mojom::FileSystemAccessManager> manager_remote_;
raw_ptr<WebContents> web_contents_ = nullptr;
};
// Watching the local file system is not supported on Android or Fuchsia.
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
TEST_F(FileSystemAccessWatcherManagerTest, BasicRegistration) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
EXPECT_FALSE(watcher_manager().HasObservationsForTesting());
std::optional<FileSystemAccessWatchScope> observation_scope = std::nullopt;
{
base::test::TestFuture<base::expected<
std::unique_ptr<Observation>, blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url,
/*is_recursive=*/false, get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
// An observation should have been created.
auto observation = get_observation_future.Take();
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(observation.value().get()));
// A source should have been created to cover the scope of the observation
observation_scope = observation.value()->scope();
EXPECT_TRUE(watcher_manager().HasSourceContainingScopeForTesting(
*observation_scope));
}
// Destroying an observation unregisters it with the manager and removes the
// respective source.
EXPECT_FALSE(watcher_manager().HasObservationsForTesting());
EXPECT_FALSE(
watcher_manager().HasSourceContainingScopeForTesting(*observation_scope));
}
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) &&
// !BUILDFLAG(IS_IOS)
TEST_F(FileSystemAccessWatcherManagerTest, BasicRegistrationUnownedSource) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
auto scope = FileSystemAccessWatchScope::GetScopeForFileWatch(file_url);
{
FakeChangeSource source(scope, file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
}
// Destroying a source unregisters it with the manager.
EXPECT_FALSE(watcher_manager().HasSourceContainingScopeForTesting(scope));
}
TEST_F(FileSystemAccessWatcherManagerTest, UnownedSource) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
// Attempting to observe a scope covered by `source` will use `source`.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
source.Signal();
std::list<Change> expected_changes = {{file_url, ChangeInfo()}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
}
TEST_F(FileSystemAccessWatcherManagerTest, SourceFailsInitialization) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
source.set_initialization_result(blink::mojom::FileSystemAccessError::New(
blink::mojom::FileSystemAccessStatus::kOperationFailed,
base::File::FILE_OK, ""));
// Attempting to observe a scope covered by `source` will use `source`.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kOperationFailed);
// TODO(crbug.com/341095544): Determine what should happen on failure to
// initialize a source, then add better test coverage.
}
TEST_F(FileSystemAccessWatcherManagerTest, IgnoreSwapFileChanges) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
CreateDirectory(dir_path);
auto swap_file_path = dir_path.AppendASCII("foo.crswap");
WriteFile(swap_file_path, "watch me and then ignore me");
auto non_swap_file_path = dir_path.AppendASCII("bar.noncrswap");
WriteFile(non_swap_file_path, "watch me and then report me");
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url,
/*is_recursive=*/false, get_observation_future.GetCallback());
// Watching the local file system is not supported on Android, iOS, or Fuchsia.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
if (!ReportsChangeInfoForLocalObservations()) {
GTEST_SKIP();
}
ASSERT_TRUE(get_observation_future.Get().has_value());
// Constructing an observation registers it with the manager.
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
// Delete a file in the directory. This should be reported to `accumulator`.
// But it will be ignored because it is a swap file.
DeleteFile(swap_file_path);
SpinEventLoopForABit();
EXPECT_THAT(accumulator.changes(), testing::IsEmpty());
// Delete a non-swap file in the directory. This should be reported to
// `accumulator`.
DeleteFile(non_swap_file_path);
SpinEventLoopForABit();
FilePathType file_path_type = FilePathType::kFile;
#if BUILDFLAG(IS_WIN)
// There is no way to know the correct handle type on Windows in this
// scenario.
//
// Window's content::FilePathWatcher uses base::GetFileInfo to figure out the
// file path type. Since `fileInDir` is deleted, there is nothing to call
// base::GetFileInfo on.
file_path_type = FilePathType::kUnknown;
#endif // BUILDFLAG(IS_WIN)
auto expected_url =
manager_->CreateFileSystemURLFromPath(PathInfo(non_swap_file_path));
const ChangeInfo change_info =
ChangeInfo(file_path_type, ChangeType::kDeleted, expected_url.path());
std::list<Change> expected_changes{{expected_url, change_info}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
TEST_F(FileSystemAccessWatcherManagerTest, RemoveObservation) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
// Attempting to observe a scope covered by `source` will use `source`.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
{
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
source.Signal();
std::list<Change> expected_changes = {{file_url, ChangeInfo()}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
}
// Signaling changes after the observation was removed should not crash.
source.Signal();
EXPECT_FALSE(watcher_manager().HasObservationsForTesting());
}
TEST_F(FileSystemAccessWatcherManagerTest, ObserveBucketFS) {
ASSERT_OK_AND_ASSIGN(auto default_bucket,
CreateSandboxFileSystemAndGetDefaultBucket());
auto test_dir_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("test/foo/bar"));
test_dir_url.SetBucket(default_bucket);
#if BUILDFLAG(IS_MAC)
// Flush setup events before observation begins.
SpinEventLoopForABit();
#endif
// Attempting to observe the given file will fail.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(test_dir_url,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
base::test::TestFuture<base::File::Error> create_file_future;
manager_->DoFileSystemOperation(
FROM_HERE, &storage::FileSystemOperationRunner::CreateDirectory,
create_file_future.GetCallback(), test_dir_url,
/*exclusive=*/false, /*recursive=*/true);
ASSERT_EQ(create_file_future.Get(), base::File::Error::FILE_OK);
// TODO(crbug.com/40283118): Expect changes for recursively-created
// intermediate directories.
ChangeInfo change_info(FilePathType::kDirectory, ChangeType::kCreated,
test_dir_url.path());
Change expected_change{test_dir_url, change_info};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::Contains(expected_change))(
accumulator.changes());
}));
}
TEST_F(FileSystemAccessWatcherManagerTest, UnsupportedScope) {
// TODO(crbug.com/321980129): External backends are not yet supported.
base::FilePath test_external_path =
base::FilePath::FromUTF8Unsafe(kTestMountPoint).AppendASCII("foo");
auto external_url = manager_->CreateFileSystemURLFromPath(
PathInfo(PathType::kExternal, test_external_path));
#if BUILDFLAG(IS_MAC)
// Flush setup events before observation begins.
SpinEventLoopForABit();
#endif
// Attempting to observe the given file will fail.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(external_url,
get_observation_future.GetCallback());
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
}
// TODO(crbug.com/321980367): Add tests covering more edge cases regarding
// overlapping scopes.
TEST_F(FileSystemAccessWatcherManagerTest, OverlappingSourceScopes) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
base::FilePath file_path = dir_path.AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source_for_file(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&source_for_file);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source_for_file));
// Add another source which covers the scope of `source_for_file`, and more.
FakeChangeSource source_for_dir(
FileSystemAccessWatchScope::GetScopeForDirectoryWatch(
dir_url, /*is_recursive=*/true),
file_system_context_);
watcher_manager().RegisterSource(&source_for_dir);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source_for_dir));
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
source_for_file.Signal();
source_for_dir.Signal(/*relative_path=*/file_path.BaseName());
Change expected_change{file_url, ChangeInfo()};
std::list<Change> expected_changes = {expected_change};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
}
TEST_F(FileSystemAccessWatcherManagerTest,
OverlappingObservationScopesForBucketFileSystem) {
ASSERT_OK_AND_ASSIGN(auto default_bucket,
CreateSandboxFileSystemAndGetDefaultBucket());
auto dir_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("dir"));
dir_url.SetBucket(default_bucket);
auto file_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("dir/foo"));
file_url.SetBucket(default_bucket);
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForAllBucketFileSystems(),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_dir_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url, /*is_recursive=*/true, get_dir_observation_future.GetCallback());
EXPECT_TRUE(get_dir_observation_future.Get().has_value());
ChangeAccumulator dir_accumulator(get_dir_observation_future.Take().value());
EXPECT_TRUE(watcher_manager().HasObservationForTesting(
dir_accumulator.observation()));
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_file_observation_future;
watcher_manager().GetFileObservation(
file_url, get_file_observation_future.GetCallback());
EXPECT_TRUE(get_file_observation_future.Get().has_value());
ChangeAccumulator file_accumulator(
get_file_observation_future.Take().value());
EXPECT_TRUE(watcher_manager().HasObservationForTesting(
file_accumulator.observation()));
// Only observed by `dir_accumulator`.
source.Signal(dir_url);
// Observed by both accumulators.
source.Signal(file_url);
std::list<Change> expected_dir_changes = {{dir_url, ChangeInfo()},
{file_url, ChangeInfo()}};
std::list<Change> expected_file_changes = {{file_url, ChangeInfo()}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_dir_changes))(
dir_accumulator.changes()) &&
testing::Matches(testing::ContainerEq(expected_file_changes))(
file_accumulator.changes());
}));
}
TEST_F(FileSystemAccessWatcherManagerTest,
OnlyReceiveChangesWhenSourceAndObservationUrlsMatchForLocalFileSystem) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
base::FilePath file_path = dir_path.AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source(FileSystemAccessWatchScope::GetScopeForDirectoryWatch(
dir_url, /*is_recursive=*/true),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_dir_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url, /*is_recursive=*/true, get_dir_observation_future.GetCallback());
EXPECT_TRUE(get_dir_observation_future.Get().has_value());
ChangeAccumulator dir_accumulator(get_dir_observation_future.Take().value());
EXPECT_TRUE(watcher_manager().HasObservationForTesting(
dir_accumulator.observation()));
FakeChangeSource file_source(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&file_source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&file_source));
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_file_observation_future;
watcher_manager().GetFileObservation(
file_url, get_file_observation_future.GetCallback());
EXPECT_TRUE(get_file_observation_future.Get().has_value());
ChangeAccumulator file_accumulator(
get_file_observation_future.Take().value());
EXPECT_TRUE(watcher_manager().HasObservationForTesting(
file_accumulator.observation()));
// Only observed by `dir_accumulator`.
source.Signal();
// Only observed by the `file_accumulator`.
file_source.Signal();
// The directory accumulator should receive changes for both the directory
// and the file url. No duplicate changes should be reported.
std::list<Change> expected_dir_changes = {{dir_url, ChangeInfo()}};
std::list<Change> expected_file_changes = {{file_url, ChangeInfo()}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_dir_changes))(
dir_accumulator.changes()) &&
testing::Matches(testing::ContainerEq(expected_file_changes))(
file_accumulator.changes());
}));
}
TEST_F(FileSystemAccessWatcherManagerTest, ErroredChange) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
// Attempting to observe a scope covered by `source` will use `source`.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
source.Signal(/*relative_path=*/base::FilePath(), /*error=*/true);
EXPECT_THAT(accumulator.has_error(), testing::IsTrue());
EXPECT_THAT(accumulator.changes(), testing::IsEmpty());
}
TEST_F(FileSystemAccessWatcherManagerTest,
ErroredChangeOnBucketFileSystemWhenSourceHasLargerScopeThanObservation) {
ASSERT_OK_AND_ASSIGN(auto default_bucket,
CreateSandboxFileSystemAndGetDefaultBucket());
auto dir_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("dir"));
dir_url.SetBucket(default_bucket);
auto sub_dir_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("dir/sub_dir"));
sub_dir_url.SetBucket(default_bucket);
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForAllBucketFileSystems(),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
sub_dir_url, /*is_recursive=*/false,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
// `accumulator` should receive this error.
source.Signal(dir_url, /*error=*/true);
EXPECT_THAT(accumulator.has_error(), testing::IsTrue());
EXPECT_THAT(accumulator.changes(), testing::IsEmpty());
}
TEST_F(FileSystemAccessWatcherManagerTest, ChangeAtRelativePath) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("foo");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
FakeChangeSource source(FileSystemAccessWatchScope::GetScopeForDirectoryWatch(
dir_url, /*is_recursive=*/true),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
// Attempting to observe a scope covered by `source` will use `source`.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url, /*is_recursive=*/true, get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
auto relative_path =
base::FilePath::FromASCII("nested").AppendASCII("subdir");
source.Signal(relative_path);
std::list<Change> expected_changes = {
{manager_->CreateFileSystemURLFromPath(
PathInfo(dir_path.Append(relative_path))),
ChangeInfo()}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
}
TEST_F(FileSystemAccessWatcherManagerTest, ChangeType) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
FakeChangeSource source(
FileSystemAccessWatchScope::GetScopeForFileWatch(file_url),
file_system_context_);
watcher_manager().RegisterSource(&source);
EXPECT_TRUE(watcher_manager().HasSourceForTesting(&source));
// Attempting to observe a scope covered by `source` will use `source`.
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
base::FilePath path;
ChangeInfo change_info(FilePathType::kUnknown, ChangeType::kCreated, path);
source.Signal(/*relative_path=*/path, /*error=*/false, change_info);
std::list<Change> expected_changes = {{file_url, change_info}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
}
// TODO(crbug.com/321980129): Consider parameterizing these tests once
// observing changes to other backends is supported.
TEST_F(FileSystemAccessWatcherManagerTest, WatchLocalDirectory) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
CreateDirectory(dir_path);
auto file_path = dir_path.AppendASCII("foo");
WriteFile(file_path, "watch me");
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url,
/*is_recursive=*/false, get_observation_future.GetCallback());
// Watching the local file system is not supported on Android, iOS, or Fuchsia.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
ASSERT_TRUE(get_observation_future.Get().has_value());
// Constructing an observation registers it with the manager.
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
// Move a file in the directory. This should be reported to `accumulator`.
auto new_file_path = dir_path.AppendASCII("bar");
base::Move(file_path, new_file_path);
auto expected_url =
ReportsModifiedPathForLocalObservations()
? manager_->CreateFileSystemURLFromPath(PathInfo(new_file_path))
: dir_url;
ChangeInfo change_info =
ReportsChangeInfoForLocalObservations()
? ChangeInfo(FilePathType::kFile, ChangeType::kMoved, new_file_path,
file_path)
: ChangeInfo();
std::list<Change> expected_changes = {{expected_url, change_info}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
// TODO(crbug.com/41490258): Failing on Windows. Re-enable this test.
TEST_F(FileSystemAccessWatcherManagerTest,
DISABLED_WatchLocalDirectoryNonRecursivelyDoesNotSeeRecursiveChanges) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
// Create a file within a subdirectory of the directory being watched.
CreateDirectory(dir_path);
CreateDirectory(dir_path.AppendASCII("subdir"));
auto file_path = dir_path.AppendASCII("subdir").AppendASCII("foo");
WriteFile(file_path, "watch me");
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url,
/*is_recursive=*/false, get_observation_future.GetCallback());
// Watching the local file system is not supported on Android, iOS, or
// Fuchsia.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
EXPECT_TRUE(watcher_manager().HasSourceContainingScopeForTesting(
accumulator.observation()->scope()));
// Delete a file in the sub-directory. This should _not_ be reported to
// `accumulator`.
DeleteFile(file_path);
// No events should be received, since this change falls outside the scope
// of this observation.
SpinEventLoopForABit();
EXPECT_THAT(accumulator.changes(), testing::IsEmpty());
#endif // BUILDFLAG(IS_ANDROID)|| BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
TEST_F(FileSystemAccessWatcherManagerTest, WatchLocalDirectoryRecursively) {
base::FilePath dir_path = dir_.GetPath().AppendASCII("dir");
auto dir_url = manager_->CreateFileSystemURLFromPath(PathInfo(dir_path));
// Create a file within a subdirectory of the directory being watched.
CreateDirectory(dir_path);
CreateDirectory(dir_path.AppendASCII("subdir"));
auto file_path = dir_path.AppendASCII("subdir").AppendASCII("foo");
WriteFile(file_path, "watch me");
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetDirectoryObservation(
dir_url,
/*is_recursive=*/true, get_observation_future.GetCallback());
// Watching the local file system is not supported on Android or Fuchsia.
// Recursive watching of the local file system is not supported on iOS.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
EXPECT_TRUE(watcher_manager().HasSourceContainingScopeForTesting(
accumulator.observation()->scope()));
// TODO(crbug.com/343801378): Ensure that no events are reported by this
// point.
// Delete a file in the sub-directory. This should be reported to
// `accumulator`.
DeleteFile(file_path);
// TODO(crbug.com/40263777): Check values of expected changes, depending
// on platform availability for change types.
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::Not(testing::IsEmpty()))(
accumulator.changes());
}));
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
TEST_F(FileSystemAccessWatcherManagerTest, WatchLocalFile) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
// Create the file to be watched.
WriteFile(file_path, "watch me");
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
// Watching the local file system is not supported on Android, Fuchsia, or
// iOS.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
// Deleting the watched file should notify `accumulator`.
DeleteFile(file_path);
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
// There is no way to know the correct handle type on Windows in this
// scenario.
//
// Window's content::FilePathWatcher uses base::GetFileInfo to figure out the
// file path type. Since `fileInDir` is deleted, there is nothing to call
// base::GetFileInfo on.
//
// FSEvents (Mac) relies on calls to `vnode_getattr`, which operates similarly
// to `GetFileInfo`.
ChangeInfo change_info(FilePathType::kUnknown, ChangeType::kDeleted,
file_url.path());
#else
ChangeInfo change_info =
ReportsChangeInfoForLocalObservations()
? ChangeInfo(FilePathType::kFile, ChangeType::kDeleted,
file_url.path())
: ChangeInfo();
#endif
std::list<Change> expected_changes = {{file_url, change_info}};
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(testing::ContainerEq(expected_changes))(
accumulator.changes());
}));
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
TEST_F(FileSystemAccessWatcherManagerTest,
WatchLocalFileWithMultipleObservations) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
// Create the file to be watched.
WriteFile(file_path, "watch me");
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future1, get_observation_future2, get_observation_future3;
watcher_manager().GetFileObservation(file_url,
get_observation_future1.GetCallback());
watcher_manager().GetFileObservation(file_url,
get_observation_future2.GetCallback());
watcher_manager().GetFileObservation(file_url,
get_observation_future3.GetCallback());
// Watching the local file system is not supported on Android, iOS, or
// Fuchsia.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future1.Get().has_value());
ASSERT_FALSE(get_observation_future2.Get().has_value());
ASSERT_FALSE(get_observation_future3.Get().has_value());
EXPECT_EQ(get_observation_future1.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
EXPECT_EQ(get_observation_future2.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
EXPECT_EQ(get_observation_future3.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
ASSERT_TRUE(get_observation_future1.Get().has_value());
ASSERT_TRUE(get_observation_future2.Get().has_value());
ASSERT_TRUE(get_observation_future3.Get().has_value());
ChangeAccumulator accumulator1(get_observation_future1.Take().value());
ChangeAccumulator accumulator2(get_observation_future2.Take().value());
ChangeAccumulator accumulator3(get_observation_future3.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator1.observation()));
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator2.observation()));
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator3.observation()));
// Deleting the watched file should notify each `accumulator`.
DeleteFile(file_path);
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
// There is no way to know the correct handle type on Windows in this
// scenario.
//
// Window's content::FilePathWatcher uses base::GetFileInfo to figure out the
// file path type. Since `fileInDir` is deleted, there is nothing to call
// base::GetFileInfo on.
ChangeInfo change_info(FilePathType::kUnknown, ChangeType::kDeleted,
file_url.path());
#else
ChangeInfo change_info =
ReportsChangeInfoForLocalObservations()
? ChangeInfo(FilePathType::kFile, ChangeType::kDeleted,
file_url.path())
: ChangeInfo();
#endif
std::list<Change> expected_changes = {{file_url, change_info}};
const auto expected_changes_matcher = testing::ContainerEq(expected_changes);
EXPECT_TRUE(base::test::RunUntil([&]() {
return testing::Matches(expected_changes_matcher)(accumulator1.changes()) &&
testing::Matches(expected_changes_matcher)(accumulator2.changes()) &&
testing::Matches(expected_changes_matcher)(accumulator3.changes());
}));
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
TEST_F(FileSystemAccessWatcherManagerTest, OutOfScope) {
base::FilePath file_path = dir_.GetPath().AppendASCII("foo");
auto file_url = manager_->CreateFileSystemURLFromPath(PathInfo(file_path));
#if BUILDFLAG(IS_MAC)
// Flush setup events before observation begins.
SpinEventLoopForABit();
#endif
base::test::TestFuture<base::expected<std::unique_ptr<Observation>,
blink::mojom::FileSystemAccessErrorPtr>>
get_observation_future;
watcher_manager().GetFileObservation(file_url,
get_observation_future.GetCallback());
// Watching the local file system is not supported on Android, Fuchsia, or
// iOS.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
ASSERT_FALSE(get_observation_future.Get().has_value());
EXPECT_EQ(get_observation_future.Get().error()->status,
blink::mojom::FileSystemAccessStatus::kNotSupportedError);
#else
ASSERT_TRUE(get_observation_future.Get().has_value());
ChangeAccumulator accumulator(get_observation_future.Take().value());
EXPECT_TRUE(
watcher_manager().HasObservationForTesting(accumulator.observation()));
// Making a change to a sibling of the watched file should _not_ report a
// change to the accumulator.
base::FilePath sibling_path = file_path.DirName().AppendASCII("sibling");
WriteFile(sibling_path, "do not watch me");
// Give unexpected events a chance to arrive.
SpinEventLoopForABit();
EXPECT_THAT(accumulator.changes(), testing::IsEmpty());
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
} // namespace content