| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/optional.h" | 11 #include "base/optional.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/chromeos/arc/fileapi/arc_deferred_file_system_operation
_runner.h" | 13 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h
" |
| 14 #include "components/arc/arc_bridge_service.h" | 14 #include "components/arc/arc_bridge_service.h" |
| 15 #include "components/arc/arc_service_manager.h" | 15 #include "components/arc/arc_service_manager.h" |
| 16 #include "components/arc/common/file_system.mojom.h" | 16 #include "components/arc/common/file_system.mojom.h" |
| 17 #include "components/arc/file_system/arc_file_system_operation_runner.h" | |
| 18 #include "components/arc/test/fake_file_system_instance.h" | 17 #include "components/arc/test/fake_file_system_instance.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 22 | 21 |
| 23 namespace arc { | 22 namespace arc { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 constexpr char kAuthority[] = "authority"; | 26 constexpr char kAuthority[] = "authority"; |
| 28 constexpr char kDocumentId[] = "document_id"; | 27 constexpr char kDocumentId[] = "document_id"; |
| 29 constexpr char kUrl[] = "content://test"; | 28 constexpr char kUrl[] = "content://test"; |
| 30 | 29 |
| 31 } // namespace | 30 } // namespace |
| 32 | 31 |
| 33 class ArcDeferredFileSystemOperationRunnerTest : public testing::Test { | 32 class ArcFileSystemOperationRunnerTest : public testing::Test { |
| 34 public: | 33 public: |
| 35 ArcDeferredFileSystemOperationRunnerTest() = default; | 34 ArcFileSystemOperationRunnerTest() = default; |
| 36 ~ArcDeferredFileSystemOperationRunnerTest() override = default; | 35 ~ArcFileSystemOperationRunnerTest() override = default; |
| 37 | 36 |
| 38 void SetUp() override { | 37 void SetUp() override { |
| 39 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); | 38 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); |
| 40 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( | 39 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| 41 &file_system_instance_); | 40 &file_system_instance_); |
| 42 // We can not use base::MakeUnique() for friend constructors. | |
| 43 arc_service_manager_->AddService( | 41 arc_service_manager_->AddService( |
| 44 base::WrapUnique(new ArcDeferredFileSystemOperationRunner( | 42 ArcFileSystemOperationRunner::CreateForTesting( |
| 45 arc_service_manager_->arc_bridge_service(), | 43 arc_service_manager_->arc_bridge_service())); |
| 46 false /* observe_events */))); | 44 runner_ = arc_service_manager_->GetService<ArcFileSystemOperationRunner>(); |
| 47 deferred_runner_ = static_cast<ArcDeferredFileSystemOperationRunner*>( | |
| 48 arc_service_manager_->GetService<ArcFileSystemOperationRunner>()); | |
| 49 } | 45 } |
| 50 | 46 |
| 51 protected: | 47 protected: |
| 52 // Calls private ArcDeferredFileSystemOperationRunner::SetShouldDefer(). | 48 // Calls private ArcFileSystemOperationRunner::SetShouldDefer(). |
| 53 void CallSetShouldDefer(bool should_defer) { | 49 void CallSetShouldDefer(bool should_defer) { |
| 54 deferred_runner_->SetShouldDefer(should_defer); | 50 runner_->SetShouldDefer(should_defer); |
| 55 } | 51 } |
| 56 | 52 |
| 57 content::TestBrowserThreadBundle thread_bundle_; | 53 content::TestBrowserThreadBundle thread_bundle_; |
| 58 FakeFileSystemInstance file_system_instance_; | 54 FakeFileSystemInstance file_system_instance_; |
| 59 std::unique_ptr<ArcServiceManager> arc_service_manager_; | 55 std::unique_ptr<ArcServiceManager> arc_service_manager_; |
| 60 // Owned by |arc_service_manager_|. | 56 // Owned by |arc_service_manager_|. |
| 61 ArcDeferredFileSystemOperationRunner* deferred_runner_; | 57 ArcFileSystemOperationRunner* runner_; |
| 62 | 58 |
| 63 private: | 59 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(ArcDeferredFileSystemOperationRunnerTest); | 60 DISALLOW_COPY_AND_ASSIGN(ArcFileSystemOperationRunnerTest); |
| 65 }; | 61 }; |
| 66 | 62 |
| 67 TEST_F(ArcDeferredFileSystemOperationRunnerTest, RunImmediately) { | 63 TEST_F(ArcFileSystemOperationRunnerTest, RunImmediately) { |
| 68 int counter = 0; | 64 int counter = 0; |
| 69 CallSetShouldDefer(false); | 65 CallSetShouldDefer(false); |
| 70 deferred_runner_->GetChildDocuments( | 66 runner_->GetChildDocuments( |
| 71 kAuthority, kDocumentId, | 67 kAuthority, kDocumentId, |
| 72 base::Bind( | 68 base::Bind( |
| 73 [](int* counter, | 69 [](int* counter, |
| 74 base::Optional<std::vector<mojom::DocumentPtr>> documents) { | 70 base::Optional<std::vector<mojom::DocumentPtr>> documents) { |
| 75 ++*counter; | 71 ++*counter; |
| 76 }, | 72 }, |
| 77 &counter)); | 73 &counter)); |
| 78 deferred_runner_->GetDocument( | 74 runner_->GetDocument( |
| 79 kAuthority, kDocumentId, | 75 kAuthority, kDocumentId, |
| 80 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, | 76 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, |
| 81 &counter)); | 77 &counter)); |
| 82 deferred_runner_->GetFileSize( | 78 runner_->GetFileSize( |
| 83 GURL(kUrl), | 79 GURL(kUrl), |
| 84 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); | 80 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); |
| 85 deferred_runner_->OpenFileToRead( | 81 runner_->OpenFileToRead( |
| 86 GURL(kUrl), | 82 GURL(kUrl), |
| 87 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, | 83 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, |
| 88 &counter)); | 84 &counter)); |
| 89 base::RunLoop().RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
| 90 EXPECT_EQ(4, counter); | 86 EXPECT_EQ(4, counter); |
| 91 } | 87 } |
| 92 | 88 |
| 93 TEST_F(ArcDeferredFileSystemOperationRunnerTest, DeferAndRun) { | 89 TEST_F(ArcFileSystemOperationRunnerTest, DeferAndRun) { |
| 94 int counter = 0; | 90 int counter = 0; |
| 95 CallSetShouldDefer(true); | 91 CallSetShouldDefer(true); |
| 96 deferred_runner_->GetChildDocuments( | 92 runner_->GetChildDocuments( |
| 97 kAuthority, kDocumentId, | 93 kAuthority, kDocumentId, |
| 98 base::Bind( | 94 base::Bind( |
| 99 [](int* counter, | 95 [](int* counter, |
| 100 base::Optional<std::vector<mojom::DocumentPtr>> documents) { | 96 base::Optional<std::vector<mojom::DocumentPtr>> documents) { |
| 101 ++*counter; | 97 ++*counter; |
| 102 }, | 98 }, |
| 103 &counter)); | 99 &counter)); |
| 104 deferred_runner_->GetDocument( | 100 runner_->GetDocument( |
| 105 kAuthority, kDocumentId, | 101 kAuthority, kDocumentId, |
| 106 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, | 102 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, |
| 107 &counter)); | 103 &counter)); |
| 108 deferred_runner_->GetFileSize( | 104 runner_->GetFileSize( |
| 109 GURL(kUrl), | 105 GURL(kUrl), |
| 110 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); | 106 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); |
| 111 deferred_runner_->OpenFileToRead( | 107 runner_->OpenFileToRead( |
| 112 GURL(kUrl), | 108 GURL(kUrl), |
| 113 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, | 109 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, |
| 114 &counter)); | 110 &counter)); |
| 115 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
| 116 EXPECT_EQ(0, counter); | 112 EXPECT_EQ(0, counter); |
| 117 | 113 |
| 118 CallSetShouldDefer(false); | 114 CallSetShouldDefer(false); |
| 119 base::RunLoop().RunUntilIdle(); | 115 base::RunLoop().RunUntilIdle(); |
| 120 EXPECT_EQ(4, counter); | 116 EXPECT_EQ(4, counter); |
| 121 } | 117 } |
| 122 | 118 |
| 123 TEST_F(ArcDeferredFileSystemOperationRunnerTest, DeferAndDiscard) { | 119 TEST_F(ArcFileSystemOperationRunnerTest, DeferAndDiscard) { |
| 124 int counter = 0; | 120 int counter = 0; |
| 125 CallSetShouldDefer(true); | 121 CallSetShouldDefer(true); |
| 126 deferred_runner_->GetChildDocuments( | 122 runner_->GetChildDocuments( |
| 127 kAuthority, kDocumentId, | 123 kAuthority, kDocumentId, |
| 128 base::Bind( | 124 base::Bind( |
| 129 [](int* counter, | 125 [](int* counter, |
| 130 base::Optional<std::vector<mojom::DocumentPtr>> documents) { | 126 base::Optional<std::vector<mojom::DocumentPtr>> documents) { |
| 131 ++*counter; | 127 ++*counter; |
| 132 }, | 128 }, |
| 133 &counter)); | 129 &counter)); |
| 134 deferred_runner_->GetDocument( | 130 runner_->GetDocument( |
| 135 kAuthority, kDocumentId, | 131 kAuthority, kDocumentId, |
| 136 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, | 132 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, |
| 137 &counter)); | 133 &counter)); |
| 138 deferred_runner_->GetFileSize( | 134 runner_->GetFileSize( |
| 139 GURL(kUrl), | 135 GURL(kUrl), |
| 140 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); | 136 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); |
| 141 deferred_runner_->OpenFileToRead( | 137 runner_->OpenFileToRead( |
| 142 GURL(kUrl), | 138 GURL(kUrl), |
| 143 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, | 139 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, |
| 144 &counter)); | 140 &counter)); |
| 145 base::RunLoop().RunUntilIdle(); | 141 base::RunLoop().RunUntilIdle(); |
| 146 EXPECT_EQ(0, counter); | 142 EXPECT_EQ(0, counter); |
| 147 | 143 |
| 148 arc_service_manager_.reset(); | 144 arc_service_manager_.reset(); |
| 149 base::RunLoop().RunUntilIdle(); | 145 base::RunLoop().RunUntilIdle(); |
| 150 EXPECT_EQ(0, counter); | 146 EXPECT_EQ(0, counter); |
| 151 } | 147 } |
| 152 | 148 |
| 153 TEST_F(ArcDeferredFileSystemOperationRunnerTest, FileInstanceUnavailable) { | 149 TEST_F(ArcFileSystemOperationRunnerTest, FileInstanceUnavailable) { |
| 154 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( | 150 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| 155 nullptr); | 151 nullptr); |
| 156 | 152 |
| 157 int counter = 0; | 153 int counter = 0; |
| 158 CallSetShouldDefer(false); | 154 CallSetShouldDefer(false); |
| 159 deferred_runner_->GetChildDocuments( | 155 runner_->GetChildDocuments( |
| 160 kAuthority, kDocumentId, | 156 kAuthority, kDocumentId, |
| 161 base::Bind( | 157 base::Bind( |
| 162 [](int* counter, | 158 [](int* counter, |
| 163 base::Optional<std::vector<mojom::DocumentPtr>> documents) { | 159 base::Optional<std::vector<mojom::DocumentPtr>> documents) { |
| 164 ++*counter; | 160 ++*counter; |
| 165 }, | 161 }, |
| 166 &counter)); | 162 &counter)); |
| 167 deferred_runner_->GetDocument( | 163 runner_->GetDocument( |
| 168 kAuthority, kDocumentId, | 164 kAuthority, kDocumentId, |
| 169 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, | 165 base::Bind([](int* counter, mojom::DocumentPtr document) { ++*counter; }, |
| 170 &counter)); | 166 &counter)); |
| 171 deferred_runner_->GetFileSize( | 167 runner_->GetFileSize( |
| 172 GURL(kUrl), | 168 GURL(kUrl), |
| 173 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); | 169 base::Bind([](int* counter, int64_t size) { ++*counter; }, &counter)); |
| 174 deferred_runner_->OpenFileToRead( | 170 runner_->OpenFileToRead( |
| 175 GURL(kUrl), | 171 GURL(kUrl), |
| 176 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, | 172 base::Bind([](int* counter, mojo::ScopedHandle handle) { ++*counter; }, |
| 177 &counter)); | 173 &counter)); |
| 178 base::RunLoop().RunUntilIdle(); | 174 base::RunLoop().RunUntilIdle(); |
| 179 EXPECT_EQ(4, counter); | 175 EXPECT_EQ(4, counter); |
| 180 } | 176 } |
| 181 | 177 |
| 182 } // namespace arc | 178 } // namespace arc |
| OLD | NEW |