OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/files/file.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/values.h" |
| 17 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
| 18 #include "chrome/browser/chromeos/file_system_provider/notification_manager.h" |
| 19 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.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" |
| 22 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
| 23 #include "chrome/common/extensions/api/file_system_provider.h" |
| 24 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 25 #include "chrome/test/base/testing_profile.h" |
| 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 27 #include "extensions/browser/event_router.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 |
| 30 namespace chromeos { |
| 31 namespace file_system_provider { |
| 32 namespace { |
| 33 |
| 34 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 35 const char kFileSystemId[] = "camera-pictures"; |
| 36 const char kDisplayName[] = "Camera Pictures"; |
| 37 const base::FilePath::CharType kDirectoryPath[] = "/hello/world"; |
| 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. |
| 42 class FakeEventRouter : public extensions::EventRouter { |
| 43 public: |
| 44 FakeEventRouter(Profile* profile, ProvidedFileSystemInterface* file_system) |
| 45 : EventRouter(profile, NULL), |
| 46 file_system_(file_system), |
| 47 reply_result_(base::File::FILE_OK) {} |
| 48 virtual ~FakeEventRouter() {} |
| 49 |
| 50 // Handles an event which would normally be routed to an extension. Instead |
| 51 // replies with a hard coded response. |
| 52 virtual void DispatchEventToExtension( |
| 53 const std::string& extension_id, |
| 54 scoped_ptr<extensions::Event> event) override { |
| 55 ASSERT_TRUE(file_system_); |
| 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 } |
| 87 } |
| 88 |
| 89 void set_reply_result(base::File::Error result) { reply_result_ = result; } |
| 90 |
| 91 private: |
| 92 ProvidedFileSystemInterface* const file_system_; // Not owned. |
| 93 base::File::Error reply_result_; |
| 94 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); |
| 95 }; |
| 96 |
| 97 // Observes the tested file system. |
| 98 class Observer : public ProvidedFileSystemObserver { |
| 99 public: |
| 100 class ChangeEvent { |
| 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() {} |
| 106 |
| 107 ProvidedFileSystemObserver::ChangeType change_type() const { |
| 108 return change_type_; |
| 109 } |
| 110 const ProvidedFileSystemObserver::ChildChanges& child_changes() const { |
| 111 return child_changes_; |
| 112 } |
| 113 |
| 114 private: |
| 115 const ProvidedFileSystemObserver::ChangeType change_type_; |
| 116 const ProvidedFileSystemObserver::ChildChanges child_changes_; |
| 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); |
| 133 } |
| 134 |
| 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_; } |
| 153 |
| 154 private: |
| 155 ScopedVector<ChangeEvent> change_events_; |
| 156 int list_changed_counter_; |
| 157 int tag_updated_counter_; |
| 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 160 }; |
| 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 |
| 185 } // namespace |
| 186 |
| 187 class FileSystemProviderProvidedFileSystemTest : public testing::Test { |
| 188 protected: |
| 189 FileSystemProviderProvidedFileSystemTest() {} |
| 190 virtual ~FileSystemProviderProvidedFileSystemTest() {} |
| 191 |
| 192 virtual void SetUp() override { |
| 193 profile_.reset(new TestingProfile); |
| 194 const base::FilePath mount_path = |
| 195 util::GetMountPath(profile_.get(), kExtensionId, kFileSystemId); |
| 196 file_system_info_.reset( |
| 197 new ProvidedFileSystemInfo(kExtensionId, |
| 198 kFileSystemId, |
| 199 kDisplayName, |
| 200 false /* writable */, |
| 201 true /* supports_notify_tag */, |
| 202 mount_path)); |
| 203 provided_file_system_.reset( |
| 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)); |
| 218 } |
| 219 |
| 220 content::TestBrowserThreadBundle thread_bundle_; |
| 221 scoped_ptr<TestingProfile> profile_; |
| 222 scoped_ptr<FakeEventRouter> event_router_; |
| 223 scoped_ptr<ProvidedFileSystemInfo> file_system_info_; |
| 224 scoped_ptr<ProvidedFileSystem> provided_file_system_; |
| 225 }; |
| 226 |
| 227 TEST_F(FileSystemProviderProvidedFileSystemTest, AutoUpdater) { |
| 228 Log log; |
| 229 base::Closure firstCallback; |
| 230 base::Closure secondCallback; |
| 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); |
| 246 base::RunLoop().RunUntilIdle(); |
| 247 EXPECT_EQ(0u, log.size()); |
| 248 |
| 249 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, secondCallback); |
| 250 base::RunLoop().RunUntilIdle(); |
| 251 EXPECT_EQ(1u, log.size()); |
| 252 } |
| 253 |
| 254 TEST_F(FileSystemProviderProvidedFileSystemTest, AutoUpdater_NoCallbacks) { |
| 255 Log log; |
| 256 { |
| 257 scoped_refptr<AutoUpdater> auto_updater(new AutoUpdater( |
| 258 base::Bind(&LogStatus, base::Unretained(&log), base::File::FILE_OK))); |
| 259 } |
| 260 EXPECT_EQ(1u, log.size()); |
| 261 } |
| 262 |
| 263 TEST_F(FileSystemProviderProvidedFileSystemTest, AutoUpdater_CallbackIgnored) { |
| 264 Log log; |
| 265 { |
| 266 scoped_refptr<AutoUpdater> auto_updater(new AutoUpdater( |
| 267 base::Bind(&LogStatus, base::Unretained(&log), base::File::FILE_OK))); |
| 268 base::Closure callback = auto_updater->CreateCallback(); |
| 269 // The callback gets out of scope, so the ref counted auto updater instance |
| 270 // gets deleted. Still, updating shouldn't be invoked, since the callback |
| 271 // wasn't executed. |
| 272 } |
| 273 base::RunLoop().RunUntilIdle(); |
| 274 EXPECT_EQ(0u, log.size()); |
| 275 } |
| 276 |
| 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); |
| 608 } |
| 609 |
| 610 } // namespace file_system_provider |
| 611 } // namespace chromeos |
OLD | NEW |