Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: chrome/browser/chromeos/file_system_provider/provided_file_system_unittest.cc

Issue 625463002: [fsp] Add support for observing entries and notifying about changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More implementation. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
mtomasz 2014/10/03 04:22:48 The original file on the left was removed by me ye
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // TODO(mtomasz): Move these test cases to operations/unmount_unittest.cc.
6
7 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" 5 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h"
8 6
9 #include <string> 7 #include <string>
10 #include <vector> 8 #include <vector>
11 9
12 #include "base/files/file.h" 10 #include "base/files/file.h"
11 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/thread_task_runner_handle.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" 17 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
18 #include "chrome/browser/chromeos/file_system_provider/notification_manager.h"
17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info .h" 19 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info .h"
18 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h" 20 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h"
21 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_obse rver.h"
19 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" 22 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
20 #include "chrome/common/extensions/api/file_system_provider.h" 23 #include "chrome/common/extensions/api/file_system_provider.h"
24 #include "chrome/common/extensions/api/file_system_provider_internal.h"
21 #include "chrome/test/base/testing_profile.h" 25 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 26 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "extensions/browser/event_router.h" 27 #include "extensions/browser/event_router.h"
24 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
25 29
26 namespace chromeos { 30 namespace chromeos {
27 namespace file_system_provider { 31 namespace file_system_provider {
28
29 namespace { 32 namespace {
30 33
31 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; 34 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
32 const int kExpectedRequestId = 1;
33 const char kFileSystemId[] = "camera-pictures"; 35 const char kFileSystemId[] = "camera-pictures";
34 const char kDisplayName[] = "Camera Pictures"; 36 const char kDisplayName[] = "Camera Pictures";
37 const char kDirectoryPath[] = "/hello/world";
hirono 2014/10/03 07:19:13 char -> base::FilePath::CharType ?
mtomasz 2014/10/03 08:35:34 Done.
mtomasz 2014/10/03 08:35:34 Done.
35 38
39 // Fake implementation of the event router, mocking out a real extension.
40 // Handles requests and replies with fake answers back to the file system via
41 // the request manager.
36 class FakeEventRouter : public extensions::EventRouter { 42 class FakeEventRouter : public extensions::EventRouter {
37 public: 43 public:
38 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {} 44 FakeEventRouter(Profile* profile, ProvidedFileSystemInterface* file_system)
45 : EventRouter(profile, NULL),
46 file_system_(file_system),
47 reply_result_(base::File::FILE_OK) {}
39 virtual ~FakeEventRouter() {} 48 virtual ~FakeEventRouter() {}
40 49
50 // Handles an event which would normally be routed to an extension. Instead
51 // replies with a hard coded response.
41 virtual void DispatchEventToExtension(const std::string& extension_id, 52 virtual void DispatchEventToExtension(const std::string& extension_id,
42 scoped_ptr<extensions::Event> event) 53 scoped_ptr<extensions::Event> event)
43 OVERRIDE { 54 OVERRIDE {
44 extension_id_ = extension_id; 55 ASSERT_TRUE(file_system_);
45 event_ = event.Pass(); 56 std::string file_system_id;
57 const base::DictionaryValue* dictionary_value = NULL;
58 ASSERT_TRUE(event->event_args->GetDictionary(0, &dictionary_value));
59 EXPECT_TRUE(dictionary_value->GetString("fileSystemId", &file_system_id));
60 EXPECT_EQ(kFileSystemId, file_system_id);
61 int request_id = -1;
62 EXPECT_TRUE(dictionary_value->GetInteger("requestId", &request_id));
63 EXPECT_TRUE(event->event_name ==
64 extensions::api::file_system_provider::
65 OnObserveDirectoryRequested::kEventName ||
66 event->event_name == extensions::api::file_system_provider::
67 OnUnobserveEntryRequested::kEventName);
68
69 if (reply_result_ == base::File::FILE_OK) {
70 base::ListValue value_as_list;
71 value_as_list.Set(0, new base::StringValue(kFileSystemId));
72 value_as_list.Set(1, new base::FundamentalValue(request_id));
73 value_as_list.Set(2, new base::FundamentalValue(0) /* execution_time */);
74
75 using extensions::api::file_system_provider_internal::
76 OperationRequestedSuccess::Params;
77 scoped_ptr<Params> params(Params::Create(value_as_list));
78 ASSERT_TRUE(params.get());
79 file_system_->GetRequestManager()->FulfillRequest(
80 request_id,
81 RequestValue::CreateForOperationSuccess(params.Pass()),
82 false /* has_more */);
83 } else {
84 file_system_->GetRequestManager()->RejectRequest(
85 request_id, make_scoped_ptr(new RequestValue()), reply_result_);
86 }
46 } 87 }
47 88
48 const std::string& extension_id() const { return extension_id_; } 89 void set_reply_result(base::File::Error result) { reply_result_ = result; }
49
50 const extensions::Event* event() const { return event_.get(); }
51 90
52 private: 91 private:
53 std::string extension_id_; 92 ProvidedFileSystemInterface* file_system_; // Not owned.
hirono 2014/10/03 07:19:13 const?
mtomasz 2014/10/03 08:35:34 Done.
54 scoped_ptr<extensions::Event> event_; 93 base::File::Error reply_result_;
55
56 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); 94 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter);
57 }; 95 };
58 96
59 class EventLogger { 97 // Observes the tested file system.
98 class Observer : public ProvidedFileSystemObserver {
60 public: 99 public:
61 EventLogger() {} 100 class ChangeEvent {
62 virtual ~EventLogger() {} 101 public:
102 ChangeEvent(ProvidedFileSystemObserver::ChangeType change_type,
103 const ProvidedFileSystemObserver::ChildChanges& child_changes)
104 : change_type_(change_type), child_changes_(child_changes) {}
105 virtual ~ChangeEvent() {}
63 106
64 void OnStatusCallback(base::File::Error error) { 107 ProvidedFileSystemObserver::ChangeType change_type() const {
65 error_.reset(new base::File::Error(error)); 108 return change_type_;
109 }
110 const ProvidedFileSystemObserver::ChildChanges& child_changes() const {
111 return child_changes_;
112 }
113
114 private:
115 ProvidedFileSystemObserver::ChangeType change_type_;
hirono 2014/10/03 07:19:13 const?
mtomasz 2014/10/03 08:35:34 Done.
116 ProvidedFileSystemObserver::ChildChanges child_changes_;
hirono 2014/10/03 07:19:13 const?
mtomasz 2014/10/03 08:35:33 Done.
117
118 DISALLOW_COPY_AND_ASSIGN(ChangeEvent);
119 };
120
121 Observer() : list_changed_counter_(0), tag_updated_counter_(0) {}
122
123 // ProvidedFileSystemInterfaceObserver overrides.
124 virtual void OnObservedEntryChanged(
125 const ProvidedFileSystemInfo& file_system_info,
126 const base::FilePath& observed_path,
127 ProvidedFileSystemObserver::ChangeType change_type,
128 const ProvidedFileSystemObserver::ChildChanges& child_changes,
129 const base::Closure& callback) OVERRIDE {
130 EXPECT_EQ(kFileSystemId, file_system_info.file_system_id());
131 change_events_.push_back(new ChangeEvent(change_type, child_changes));
132 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
66 } 133 }
67 134
68 base::File::Error* error() { return error_.get(); } 135 virtual void OnObservedEntryTagUpdated(
136 const ProvidedFileSystemInfo& file_system_info,
137 const base::FilePath& observed_path) OVERRIDE {
138 EXPECT_EQ(kFileSystemId, file_system_info.file_system_id());
139 ++tag_updated_counter_;
140 }
141
142 virtual void OnObservedEntryListChanged(
143 const ProvidedFileSystemInfo& file_system_info) OVERRIDE {
144 EXPECT_EQ(kFileSystemId, file_system_info.file_system_id());
145 ++list_changed_counter_;
146 }
147
148 int list_changed_counter() const { return list_changed_counter_; }
149 const ScopedVector<ChangeEvent>& change_events() const {
150 return change_events_;
151 }
152 int tag_updated_counter() const { return tag_updated_counter_; }
69 153
70 private: 154 private:
71 scoped_ptr<base::File::Error> error_; 155 ScopedVector<ChangeEvent> change_events_;
72 DISALLOW_COPY_AND_ASSIGN(EventLogger); 156 int list_changed_counter_;
157 int tag_updated_counter_;
158
159 DISALLOW_COPY_AND_ASSIGN(Observer);
73 }; 160 };
74 161
162 // Stub notification manager, which works in unit tests.
163 class StubNotificationManager : public NotificationManagerInterface {
164 public:
165 StubNotificationManager() {}
166 virtual ~StubNotificationManager() {}
167
168 // NotificationManagerInterface overrides.
169 virtual void ShowUnresponsiveNotification(
170 int id,
171 const NotificationCallback& callback) OVERRIDE {}
172 virtual void HideUnresponsiveNotification(int id) OVERRIDE {}
173
174 private:
175 DISALLOW_COPY_AND_ASSIGN(StubNotificationManager);
176 };
177
178 typedef std::vector<base::File::Error> Log;
179
180 // Writes a |result| to the |log| vector.
181 void LogStatus(Log* log, base::File::Error result) {
182 log->push_back(result);
183 }
184
75 } // namespace 185 } // namespace
76 186
77 class FileSystemProviderProvidedFileSystemTest : public testing::Test { 187 class FileSystemProviderProvidedFileSystemTest : public testing::Test {
78 protected: 188 protected:
79 FileSystemProviderProvidedFileSystemTest() {} 189 FileSystemProviderProvidedFileSystemTest() {}
80 virtual ~FileSystemProviderProvidedFileSystemTest() {} 190 virtual ~FileSystemProviderProvidedFileSystemTest() {}
81 191
82 virtual void SetUp() OVERRIDE { 192 virtual void SetUp() OVERRIDE {
83 profile_.reset(new TestingProfile); 193 profile_.reset(new TestingProfile);
84 event_router_.reset(new FakeEventRouter(profile_.get()));
85 event_router_->AddEventListener(
86 extensions::api::file_system_provider::OnUnmountRequested::kEventName,
87 NULL,
88 kExtensionId);
89
90 const base::FilePath mount_path = 194 const base::FilePath mount_path =
91 util::GetMountPath(profile_.get(), kExtensionId, kFileSystemId); 195 util::GetMountPath(profile_.get(), kExtensionId, kFileSystemId);
92 file_system_info_.reset(new ProvidedFileSystemInfo( 196 file_system_info_.reset(
93 kExtensionId, kFileSystemId, kDisplayName, mount_path)); 197 new ProvidedFileSystemInfo(kExtensionId,
198 kFileSystemId,
199 kDisplayName,
200 false /* writable */,
201 true /* supports_notify_tag */,
202 mount_path));
94 provided_file_system_.reset( 203 provided_file_system_.reset(
95 new ProvidedFileSystem(event_router_.get(), *file_system_info_.get())); 204 new ProvidedFileSystem(profile_.get(), *file_system_info_.get()));
205 event_router_.reset(
206 new FakeEventRouter(profile_.get(), provided_file_system_.get()));
207 event_router_->AddEventListener(extensions::api::file_system_provider::
208 OnObserveDirectoryRequested::kEventName,
209 NULL,
210 kExtensionId);
211 event_router_->AddEventListener(extensions::api::file_system_provider::
212 OnUnobserveEntryRequested::kEventName,
213 NULL,
214 kExtensionId);
215 provided_file_system_->SetEventRouterForTesting(event_router_.get());
216 provided_file_system_->SetNotificationManagerForTesting(
217 make_scoped_ptr(new StubNotificationManager));
96 } 218 }
97 219
98 content::TestBrowserThreadBundle thread_bundle_; 220 content::TestBrowserThreadBundle thread_bundle_;
99 scoped_ptr<TestingProfile> profile_; 221 scoped_ptr<TestingProfile> profile_;
100 scoped_ptr<FakeEventRouter> event_router_; 222 scoped_ptr<FakeEventRouter> event_router_;
101 scoped_ptr<ProvidedFileSystemInfo> file_system_info_; 223 scoped_ptr<ProvidedFileSystemInfo> file_system_info_;
102 scoped_ptr<ProvidedFileSystemInterface> provided_file_system_; 224 scoped_ptr<ProvidedFileSystem> provided_file_system_;
103 }; 225 };
104 226
105 TEST_F(FileSystemProviderProvidedFileSystemTest, RequestUnmount_Success) { 227 TEST_F(FileSystemProviderProvidedFileSystemTest, AutoUpdater) {
106 EventLogger logger; 228 Log log;
107 229 base::Closure firstCallback;
108 provided_file_system_->RequestUnmount( 230 base::Closure secondCallback;
109 base::Bind(&EventLogger::OnStatusCallback, base::Unretained(logger))); 231
232 {
233 // Auto updater is ref counted, and bound to all callbacks.
234 scoped_refptr<AutoUpdater> auto_updater(new AutoUpdater(
235 base::Bind(&LogStatus, base::Unretained(&log), base::File::FILE_OK)));
236
237 firstCallback = auto_updater->CreateCallback();
238 secondCallback = auto_updater->CreateCallback();
239 }
240
241 // Getting out of scope, should not invoke updating if there are pending
242 // callbacks.
243 EXPECT_EQ(0u, log.size());
244
245 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, firstCallback);
110 base::RunLoop().RunUntilIdle(); 246 base::RunLoop().RunUntilIdle();
111 247 EXPECT_EQ(0u, log.size());
112 // Verify that the event has been sent to the providing extension. 248
113 EXPECT_EQ(kExtensionId, event_router_->extension_id()); 249 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, secondCallback);
114 const extensions::Event* event = event_router_->event();
115 ASSERT_TRUE(event);
116 ASSERT_TRUE(event->event_args);
117 base::ListValue* event_args = event->event_args.get();
118 EXPECT_EQ(2u, event_args->GetSize());
119 std::string file_system_id;
120 EXPECT_TRUE(event_args->GetString(0, &file_system_id));
121 EXPECT_EQ(kFileSystemId, file_system_id);
122
123 // Remember the request id, and verify it is valid.
124 int request_id = 0;
125 EXPECT_TRUE(event_args->GetInteger(1, &request_id));
126 EXPECT_EQ(kExpectedRequestId, request_id);
127
128 // Callback should not be called, yet.
129 EXPECT_FALSE(logger.error());
130
131 // Simulate sending a success response from the providing extension.
132 RequestManager* request_manager = provided_file_system_->GetRequestManager();
133 ASSERT_TRUE(request_manager);
134 scoped_ptr<RequestValue> response;
135 bool reply_result = request_manager->FulfillRequest(
136 request_id, response.Pass(), false /* has_more */);
137 EXPECT_TRUE(reply_result);
138
139 // Callback should be called. Verify the error code.
140 ASSERT_TRUE(logger.error());
141 EXPECT_EQ(base::File::FILE_OK, *logger.error());
142 }
143
144 TEST_F(FileSystemProviderProvidedFileSystemTest, RequestUnmount_Error) {
145 EventLogger logger;
146
147 provided_file_system_->RequestUnmount(
148 base::Bind(&EventLogger::OnStatusCallback, base::Unretained(logger)));
149 base::RunLoop().RunUntilIdle(); 250 base::RunLoop().RunUntilIdle();
150 251 EXPECT_EQ(1u, log.size());
151 // Verify that the event has been sent to the providing extension. 252 }
152 EXPECT_EQ(kExtensionId, event_router_->extension_id()); 253
153 const extensions::Event* event = event_router_->event(); 254 TEST_F(FileSystemProviderProvidedFileSystemTest, AutoUpdater_NoCallbacks) {
154 ASSERT_TRUE(event); 255 Log log;
155 ASSERT_TRUE(event->event_args); 256 {
156 base::ListValue* event_args = event->event_args.get(); 257 scoped_refptr<AutoUpdater> auto_updater(new AutoUpdater(
157 EXPECT_EQ(2u, event_args->GetSize()); 258 base::Bind(&LogStatus, base::Unretained(&log), base::File::FILE_OK)));
158 std::string file_system_id; 259 }
159 EXPECT_TRUE(event_args->GetString(0, &file_system_id)); 260 EXPECT_EQ(1u, log.size());
160 EXPECT_EQ(kFileSystemId, file_system_id); 261 }
161 262
162 // Remember the request id, and verify it is valid. 263 TEST_F(FileSystemProviderProvidedFileSystemTest, AutoUpdater_CallbackIgnored) {
163 int request_id = 0; 264 Log log;
164 EXPECT_TRUE(event_args->GetInteger(1, &request_id)); 265 {
165 EXPECT_EQ(kExpectedRequestId, request_id); 266 scoped_refptr<AutoUpdater> auto_updater(new AutoUpdater(
166 267 base::Bind(&LogStatus, base::Unretained(&log), base::File::FILE_OK)));
167 // Simulate sending an error response from the providing extension. 268 base::Closure callback = auto_updater->CreateCallback();
168 RequestManager* request_manager = provided_file_system_->GetRequestManager(); 269 // The callback gets out of scope, so the ref counted auto updater instance
169 ASSERT_TRUE(request_manager); 270 // gets deleted. Still, updating shouldn't be invoked, since the callback
170 bool reply_result = request_manager->RejectRequest( 271 // wasn't executed.
171 request_id, base::File::FILE_ERROR_NOT_FOUND); 272 }
172 EXPECT_TRUE(reply_result); 273 base::RunLoop().RunUntilIdle();
173 274 EXPECT_EQ(0u, log.size());
174 // Callback should be called. Verify the error code. 275 }
175 ASSERT_TRUE(logger.error()); 276
176 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); 277 TEST_F(FileSystemProviderProvidedFileSystemTest, ObserveDirectory_NotFound) {
278 Log log;
279 Observer observer;
280
281 provided_file_system_->AddObserver(&observer);
282
283 // First, set the extension response to an error.
284 event_router_->set_reply_result(base::File::FILE_ERROR_NOT_FOUND);
285
286 provided_file_system_->ObserveDirectory(
287 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
288 false /* recursive */,
289 base::Bind(&LogStatus, base::Unretained(&log)));
290 base::RunLoop().RunUntilIdle();
291
292 // The directory should not become observed because of an error.
293 ASSERT_EQ(1u, log.size());
294 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, log[0]);
295
296 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
297 provided_file_system_->GetObservedEntries();
298 EXPECT_EQ(0u, observed_entries->size());
299
300 // The observer should not be called.
301 EXPECT_EQ(0, observer.list_changed_counter());
302 EXPECT_EQ(0, observer.tag_updated_counter());
303
304 provided_file_system_->RemoveObserver(&observer);
305 }
306
307 TEST_F(FileSystemProviderProvidedFileSystemTest, ObserveDirectory) {
308 Log log;
309 Observer observer;
310
311 provided_file_system_->AddObserver(&observer);
312
313 provided_file_system_->ObserveDirectory(
314 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
315 false /* recursive */,
316 base::Bind(&LogStatus, base::Unretained(&log)));
317 base::RunLoop().RunUntilIdle();
318
319 ASSERT_EQ(1u, log.size());
320 EXPECT_EQ(base::File::FILE_OK, log[0]);
321 EXPECT_EQ(1, observer.list_changed_counter());
322 EXPECT_EQ(0, observer.tag_updated_counter());
323
324 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
325 provided_file_system_->GetObservedEntries();
326 ASSERT_EQ(1u, observed_entries->size());
327 const ProvidedFileSystemInterface::ObservedEntry& observed_entry =
328 observed_entries->begin()->second;
329 EXPECT_EQ(kDirectoryPath, observed_entry.entry_path.value());
330 EXPECT_FALSE(observed_entry.recursive);
331 EXPECT_EQ("", observed_entry.last_tag);
332
333 provided_file_system_->RemoveObserver(&observer);
334 }
335
336 TEST_F(FileSystemProviderProvidedFileSystemTest, ObserveDirectory_Exists) {
337 Observer observer;
338 provided_file_system_->AddObserver(&observer);
339
340 {
341 // First observe a directory not recursively.
342 Log log;
343 provided_file_system_->ObserveDirectory(
344 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
345 false /* recursive */,
346 base::Bind(&LogStatus, base::Unretained(&log)));
347 base::RunLoop().RunUntilIdle();
348
349 ASSERT_EQ(1u, log.size());
350 EXPECT_EQ(base::File::FILE_OK, log[0]);
351 EXPECT_EQ(1, observer.list_changed_counter());
352 EXPECT_EQ(0, observer.tag_updated_counter());
353 }
354
355 {
356 // Create another non-recursive observer. That should fail.
357 Log log;
358 provided_file_system_->ObserveDirectory(
359 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
360 false /* recursive */,
361 base::Bind(&LogStatus, base::Unretained(&log)));
362 base::RunLoop().RunUntilIdle();
363
364 ASSERT_EQ(1u, log.size());
365 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, log[0]);
366 EXPECT_EQ(1, observer.list_changed_counter()); // No changes on the list.
367 EXPECT_EQ(0, observer.tag_updated_counter());
368 }
369
370 {
371 // Create another observer on the same path, but a recursive one. That
372 // should
373 // succeed.
374 Log log;
375 provided_file_system_->ObserveDirectory(
376 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
377 true /* recursive */,
378 base::Bind(&LogStatus, base::Unretained(&log)));
379 base::RunLoop().RunUntilIdle();
380
381 ASSERT_EQ(1u, log.size());
382 EXPECT_EQ(base::File::FILE_OK, log[0]);
383 EXPECT_EQ(2, observer.list_changed_counter());
384 EXPECT_EQ(0, observer.tag_updated_counter());
385
386 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
387 provided_file_system_->GetObservedEntries();
388 ASSERT_EQ(1u, observed_entries->size());
389 const ProvidedFileSystemInterface::ObservedEntry& observed_entry =
390 observed_entries->begin()->second;
391 EXPECT_EQ(kDirectoryPath, observed_entry.entry_path.value());
392 EXPECT_TRUE(observed_entry.recursive);
393 EXPECT_EQ("", observed_entry.last_tag);
394 }
395
396 {
397 // Lastly, create another recursive observer. That should fail, too.
398 Log log;
399 provided_file_system_->ObserveDirectory(
400 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
401 true /* recursive */,
402 base::Bind(&LogStatus, base::Unretained(&log)));
403 base::RunLoop().RunUntilIdle();
404
405 ASSERT_EQ(1u, log.size());
406 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, log[0]);
407 EXPECT_EQ(2, observer.list_changed_counter()); // No changes on the list.
408 EXPECT_EQ(0, observer.tag_updated_counter());
409 }
410
411 provided_file_system_->RemoveObserver(&observer);
412 }
413
414 TEST_F(FileSystemProviderProvidedFileSystemTest, UnobserveEntry) {
415 Observer observer;
416 provided_file_system_->AddObserver(&observer);
417
418 {
419 // First, confirm that unobserving an entry which is not observed, results
420 // in an error.
421 Log log;
422 provided_file_system_->UnobserveEntry(
423 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
424 base::Bind(&LogStatus, base::Unretained(&log)));
425 base::RunLoop().RunUntilIdle();
426
427 ASSERT_EQ(1u, log.size());
428 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, log[0]);
429 EXPECT_EQ(0, observer.list_changed_counter());
430 EXPECT_EQ(0, observer.tag_updated_counter());
431 }
432
433 {
434 // Observe a directory not recursively.
435 Log log;
436 provided_file_system_->ObserveDirectory(
437 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
438 false /* recursive */,
439 base::Bind(&LogStatus, base::Unretained(&log)));
440 base::RunLoop().RunUntilIdle();
441
442 ASSERT_EQ(1u, log.size());
443 EXPECT_EQ(base::File::FILE_OK, log[0]);
444 EXPECT_EQ(1, observer.list_changed_counter());
445 EXPECT_EQ(0, observer.tag_updated_counter());
446
447 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
448 provided_file_system_->GetObservedEntries();
449 EXPECT_EQ(1u, observed_entries->size());
450 }
451
452 {
453 // Unobserve it gracefully.
454 Log log;
455 provided_file_system_->UnobserveEntry(
456 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
457 base::Bind(&LogStatus, base::Unretained(&log)));
458 base::RunLoop().RunUntilIdle();
459
460 ASSERT_EQ(1u, log.size());
461 EXPECT_EQ(base::File::FILE_OK, log[0]);
462 EXPECT_EQ(2, observer.list_changed_counter());
463 EXPECT_EQ(0, observer.tag_updated_counter());
464
465 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
466 provided_file_system_->GetObservedEntries();
467 EXPECT_EQ(0u, observed_entries->size());
468 }
469
470 {
471 // Confirm that it's possible to observe it again.
472 Log log;
473 provided_file_system_->ObserveDirectory(
474 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
475 false /* recursive */,
476 base::Bind(&LogStatus, base::Unretained(&log)));
477 base::RunLoop().RunUntilIdle();
478
479 ASSERT_EQ(1u, log.size());
480 EXPECT_EQ(base::File::FILE_OK, log[0]);
481 EXPECT_EQ(3, observer.list_changed_counter());
482 EXPECT_EQ(0, observer.tag_updated_counter());
483
484 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
485 provided_file_system_->GetObservedEntries();
486 EXPECT_EQ(1u, observed_entries->size());
487 }
488
489 {
490 // Finally, unobserve it, but with an error from extension. That should
491 // result
492 // in a removed observer, anyway.
493 event_router_->set_reply_result(base::File::FILE_ERROR_FAILED);
494
495 Log log;
496 provided_file_system_->UnobserveEntry(
497 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
498 base::Bind(&LogStatus, base::Unretained(&log)));
499 base::RunLoop().RunUntilIdle();
500
501 ASSERT_EQ(1u, log.size());
502 EXPECT_EQ(base::File::FILE_ERROR_FAILED, log[0]);
503 EXPECT_EQ(4, observer.list_changed_counter());
504 EXPECT_EQ(0, observer.tag_updated_counter());
505
506 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
507 provided_file_system_->GetObservedEntries();
508 EXPECT_EQ(0u, observed_entries->size());
509 }
510
511 provided_file_system_->RemoveObserver(&observer);
512 }
513
514 TEST_F(FileSystemProviderProvidedFileSystemTest, Notify) {
515 Observer observer;
516 provided_file_system_->AddObserver(&observer);
517
518 {
519 // Observe a directory.
520 Log log;
521 provided_file_system_->ObserveDirectory(
522 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
523 false /* recursive */,
524 base::Bind(&LogStatus, base::Unretained(&log)));
525 base::RunLoop().RunUntilIdle();
526
527 ASSERT_EQ(1u, log.size());
528 EXPECT_EQ(base::File::FILE_OK, log[0]);
529 EXPECT_EQ(1, observer.list_changed_counter());
530 EXPECT_EQ(0, observer.tag_updated_counter());
531
532 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
533 provided_file_system_->GetObservedEntries();
534 EXPECT_EQ(1u, observed_entries->size());
535 provided_file_system_->GetObservedEntries();
536 EXPECT_EQ("", observed_entries->begin()->second.last_tag);
537 }
538
539 {
540 // Notify about a change.
541 const ProvidedFileSystemObserver::ChangeType change_type =
542 ProvidedFileSystemObserver::CHANGED;
543 const ProvidedFileSystemObserver::ChildChanges child_changes;
544 const std::string tag = "hello-world";
545 EXPECT_TRUE(provided_file_system_->Notify(
546 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
547 change_type,
548 child_changes,
549 tag));
550
551 // Verify the observer event.
552 ASSERT_EQ(1u, observer.change_events().size());
553 const Observer::ChangeEvent* const change_event =
554 observer.change_events()[0];
555 EXPECT_EQ(change_type, change_event->change_type());
556 EXPECT_EQ(0u, change_event->child_changes().size());
557
558 // The tag should not be updated in advance, before all observers handle
559 // the notification.
560 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
561 provided_file_system_->GetObservedEntries();
562 EXPECT_EQ(1u, observed_entries->size());
563 provided_file_system_->GetObservedEntries();
564 EXPECT_EQ("", observed_entries->begin()->second.last_tag);
565
566 // Wait until all observers finish handling the notification.
567 base::RunLoop().RunUntilIdle();
568
569 // Confirm, that the entry is still being observed, and that the tag is
570 // updated.
571 ASSERT_EQ(1u, observed_entries->size());
572 EXPECT_EQ(tag, observed_entries->begin()->second.last_tag);
573 EXPECT_EQ(1, observer.list_changed_counter());
574 EXPECT_EQ(1, observer.tag_updated_counter());
575 }
576
577 {
578 // Notify about deleting of the observed entry.
579 const ProvidedFileSystemObserver::ChangeType change_type =
580 ProvidedFileSystemObserver::DELETED;
581 const ProvidedFileSystemObserver::ChildChanges child_changes;
582 const std::string tag = "chocolate-disco";
583 EXPECT_TRUE(provided_file_system_->Notify(
584 base::FilePath::FromUTF8Unsafe(kDirectoryPath),
585 change_type,
586 child_changes,
587 tag));
588 base::RunLoop().RunUntilIdle();
589
590 // Verify the observer event.
591 ASSERT_EQ(2u, observer.change_events().size());
592 const Observer::ChangeEvent* const change_event =
593 observer.change_events()[1];
594 EXPECT_EQ(change_type, change_event->change_type());
595 EXPECT_EQ(0u, change_event->child_changes().size());
596 }
597
598 // Confirm, that the entry is not observed anymore.
599 {
600 ProvidedFileSystemInterface::ObservedEntries* const observed_entries =
601 provided_file_system_->GetObservedEntries();
602 EXPECT_EQ(0u, observed_entries->size());
603 EXPECT_EQ(2, observer.list_changed_counter());
604 EXPECT_EQ(2, observer.tag_updated_counter());
605 }
606
607 provided_file_system_->RemoveObserver(&observer);
177 } 608 }
178 609
179 } // namespace file_system_provider 610 } // namespace file_system_provider
180 } // namespace chromeos 611 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698