| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 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 #include <string.h> | 5 #include <string.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h" | 14 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h" |
| 15 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" | 15 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" |
| 16 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h
" | 16 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h
" |
| 17 #include "components/arc/arc_bridge_service.h" | 17 #include "components/arc/arc_bridge_service.h" |
| 18 #include "components/arc/arc_service_manager.h" | 18 #include "components/arc/arc_service_manager.h" |
| 19 #include "components/arc/common/file_system.mojom.h" |
| 19 #include "components/arc/test/fake_file_system_instance.h" | 20 #include "components/arc/test/fake_file_system_instance.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "storage/common/fileapi/directory_entry.h" | 22 #include "storage/common/fileapi/directory_entry.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 24 | 25 |
| 26 using ChangeType = arc::ArcDocumentsProviderRoot::ChangeType; |
| 25 using storage::DirectoryEntry; | 27 using storage::DirectoryEntry; |
| 26 using Document = arc::FakeFileSystemInstance::Document; | 28 using Document = arc::FakeFileSystemInstance::Document; |
| 27 using EntryList = storage::AsyncFileUtil::EntryList; | 29 using EntryList = storage::AsyncFileUtil::EntryList; |
| 28 | 30 |
| 29 namespace arc { | 31 namespace arc { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 // Simliar as FakeFileSystemInstance::Document, but all fields are primitives | 35 // Simliar as FakeFileSystemInstance::Document, but all fields are primitives |
| 34 // so that values can be constexpr. | 36 // so that values can be constexpr. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 fake_file_system_.AddDocument(ToDocument(spec)); | 115 fake_file_system_.AddDocument(ToDocument(spec)); |
| 114 } | 116 } |
| 115 | 117 |
| 116 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); | 118 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); |
| 117 arc_service_manager_->AddService( | 119 arc_service_manager_->AddService( |
| 118 ArcFileSystemOperationRunner::CreateForTesting( | 120 ArcFileSystemOperationRunner::CreateForTesting( |
| 119 arc_service_manager_->arc_bridge_service())); | 121 arc_service_manager_->arc_bridge_service())); |
| 120 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( | 122 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| 121 &fake_file_system_); | 123 &fake_file_system_); |
| 122 | 124 |
| 125 // Run the message loop until FileSystemInstance::Init() is called. |
| 126 base::RunLoop().RunUntilIdle(); |
| 127 ASSERT_TRUE(fake_file_system_.InitCalled()); |
| 128 |
| 123 root_ = base::MakeUnique<ArcDocumentsProviderRoot>(kAuthority, | 129 root_ = base::MakeUnique<ArcDocumentsProviderRoot>(kAuthority, |
| 124 kRootSpec.document_id); | 130 kRootSpec.document_id); |
| 125 } | 131 } |
| 126 | 132 |
| 127 protected: | 133 protected: |
| 128 content::TestBrowserThreadBundle thread_bundle_; | 134 content::TestBrowserThreadBundle thread_bundle_; |
| 129 FakeFileSystemInstance fake_file_system_; | 135 FakeFileSystemInstance fake_file_system_; |
| 130 std::unique_ptr<ArcServiceManager> arc_service_manager_; | 136 std::unique_ptr<ArcServiceManager> arc_service_manager_; |
| 131 std::unique_ptr<ArcDocumentsProviderRoot> root_; | 137 std::unique_ptr<ArcDocumentsProviderRoot> root_; |
| 132 | 138 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 EXPECT_FALSE(file_list[2].is_directory); | 286 EXPECT_FALSE(file_list[2].is_directory); |
| 281 EXPECT_EQ(FILE_PATH_LITERAL("dup.mp4"), file_list[3].name); | 287 EXPECT_EQ(FILE_PATH_LITERAL("dup.mp4"), file_list[3].name); |
| 282 EXPECT_FALSE(file_list[3].is_directory); | 288 EXPECT_FALSE(file_list[3].is_directory); |
| 283 EXPECT_FALSE(has_more); | 289 EXPECT_FALSE(has_more); |
| 284 run_loop->Quit(); | 290 run_loop->Quit(); |
| 285 }, | 291 }, |
| 286 &run_loop)); | 292 &run_loop)); |
| 287 run_loop.Run(); | 293 run_loop.Run(); |
| 288 } | 294 } |
| 289 | 295 |
| 296 TEST_F(ArcDocumentsProviderRootTest, WatchChanged) { |
| 297 int num_called = 0; |
| 298 auto watcher_callback = base::Bind( |
| 299 [](int* num_called, ChangeType type) { |
| 300 EXPECT_EQ(ChangeType::CHANGED, type); |
| 301 ++(*num_called); |
| 302 }, |
| 303 &num_called); |
| 304 |
| 305 { |
| 306 base::RunLoop run_loop; |
| 307 root_->AddWatcher(base::FilePath(FILE_PATH_LITERAL("dir")), |
| 308 watcher_callback, |
| 309 base::Bind( |
| 310 [](base::RunLoop* run_loop, base::File::Error error) { |
| 311 EXPECT_EQ(base::File::FILE_OK, error); |
| 312 run_loop->Quit(); |
| 313 }, |
| 314 &run_loop)); |
| 315 run_loop.Run(); |
| 316 } |
| 317 |
| 318 EXPECT_EQ(0, num_called); |
| 319 fake_file_system_.TriggerWatchers(kAuthority, kDirSpec.document_id, |
| 320 mojom::ChangeType::CHANGED); |
| 321 base::RunLoop().RunUntilIdle(); |
| 322 EXPECT_EQ(1, num_called); |
| 323 |
| 324 { |
| 325 base::RunLoop run_loop; |
| 326 root_->RemoveWatcher( |
| 327 base::FilePath(FILE_PATH_LITERAL("dir")), |
| 328 base::Bind( |
| 329 [](base::RunLoop* run_loop, base::File::Error error) { |
| 330 EXPECT_EQ(base::File::FILE_OK, error); |
| 331 run_loop->Quit(); |
| 332 }, |
| 333 &run_loop)); |
| 334 run_loop.Run(); |
| 335 } |
| 336 } |
| 337 |
| 338 TEST_F(ArcDocumentsProviderRootTest, WatchDeleted) { |
| 339 int num_called = 0; |
| 340 auto watcher_callback = base::Bind( |
| 341 [](int* num_called, ChangeType type) { |
| 342 EXPECT_EQ(ChangeType::DELETED, type); |
| 343 ++(*num_called); |
| 344 }, |
| 345 &num_called); |
| 346 |
| 347 { |
| 348 base::RunLoop run_loop; |
| 349 root_->AddWatcher(base::FilePath(FILE_PATH_LITERAL("dir")), |
| 350 watcher_callback, |
| 351 base::Bind( |
| 352 [](base::RunLoop* run_loop, base::File::Error error) { |
| 353 EXPECT_EQ(base::File::FILE_OK, error); |
| 354 run_loop->Quit(); |
| 355 }, |
| 356 &run_loop)); |
| 357 run_loop.Run(); |
| 358 } |
| 359 |
| 360 EXPECT_EQ(0, num_called); |
| 361 fake_file_system_.TriggerWatchers(kAuthority, kDirSpec.document_id, |
| 362 mojom::ChangeType::DELETED); |
| 363 base::RunLoop().RunUntilIdle(); |
| 364 EXPECT_EQ(1, num_called); |
| 365 |
| 366 // Even if the watched file was deleted, the watcher is still alive and we |
| 367 // should clean it up. |
| 368 { |
| 369 base::RunLoop run_loop; |
| 370 root_->RemoveWatcher( |
| 371 base::FilePath(FILE_PATH_LITERAL("dir")), |
| 372 base::Bind( |
| 373 [](base::RunLoop* run_loop, base::File::Error error) { |
| 374 EXPECT_EQ(base::File::FILE_OK, error); |
| 375 run_loop->Quit(); |
| 376 }, |
| 377 &run_loop)); |
| 378 run_loop.Run(); |
| 379 } |
| 380 } |
| 381 |
| 290 TEST_F(ArcDocumentsProviderRootTest, ResolveToContentUrl) { | 382 TEST_F(ArcDocumentsProviderRootTest, ResolveToContentUrl) { |
| 291 base::RunLoop run_loop; | 383 base::RunLoop run_loop; |
| 292 root_->ResolveToContentUrl( | 384 root_->ResolveToContentUrl( |
| 293 base::FilePath(FILE_PATH_LITERAL("dir/photo.jpg")), | 385 base::FilePath(FILE_PATH_LITERAL("dir/photo.jpg")), |
| 294 base::Bind( | 386 base::Bind( |
| 295 [](base::RunLoop* run_loop, const GURL& url) { | 387 [](base::RunLoop* run_loop, const GURL& url) { |
| 296 EXPECT_EQ(GURL("content://org.chromium.test/document/photo-id"), | 388 EXPECT_EQ(GURL("content://org.chromium.test/document/photo-id"), |
| 297 url); | 389 url); |
| 298 run_loop->Quit(); | 390 run_loop->Quit(); |
| 299 }, | 391 }, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 [](base::RunLoop* run_loop, const GURL& url) { | 429 [](base::RunLoop* run_loop, const GURL& url) { |
| 338 EXPECT_EQ(GURL("content://org.chromium.test/document/dup3-id"), | 430 EXPECT_EQ(GURL("content://org.chromium.test/document/dup3-id"), |
| 339 url); | 431 url); |
| 340 run_loop->Quit(); | 432 run_loop->Quit(); |
| 341 }, | 433 }, |
| 342 &run_loop)); | 434 &run_loop)); |
| 343 run_loop.Run(); | 435 run_loop.Run(); |
| 344 } | 436 } |
| 345 | 437 |
| 346 } // namespace arc | 438 } // namespace arc |
| OLD | NEW |