OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/drive/sync_client.h" | 5 #include "chrome/browser/chromeos/drive/sync_client.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
16 #include "chrome/browser/chromeos/drive/drive.pb.h" | 16 #include "chrome/browser/chromeos/drive/drive.pb.h" |
17 #include "chrome/browser/chromeos/drive/file_cache.h" | 17 #include "chrome/browser/chromeos/drive/file_cache.h" |
18 #include "chrome/browser/chromeos/drive/file_system_util.h" | 18 #include "chrome/browser/chromeos/drive/file_system_util.h" |
19 #include "chrome/browser/chromeos/drive/mock_drive_file_system.h" | 19 #include "chrome/browser/chromeos/drive/mock_file_system.h" |
20 #include "chrome/browser/chromeos/drive/test_util.h" | 20 #include "chrome/browser/chromeos/drive/test_util.h" |
21 #include "chrome/browser/google_apis/test_util.h" | 21 #include "chrome/browser/google_apis/test_util.h" |
22 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
25 | 25 |
26 using ::testing::StrictMock; | 26 using ::testing::StrictMock; |
27 using ::testing::_; | 27 using ::testing::_; |
28 | 28 |
29 namespace drive { | 29 namespace drive { |
(...skipping 16 matching lines...) Expand all Loading... |
46 entry_proto->mutable_file_specific_info()->set_file_md5(md5); | 46 entry_proto->mutable_file_specific_info()->set_file_md5(md5); |
47 arg1.Run(error, base::FilePath(), entry_proto.Pass()); | 47 arg1.Run(error, base::FilePath(), entry_proto.Pass()); |
48 } | 48 } |
49 | 49 |
50 } // namespace | 50 } // namespace |
51 | 51 |
52 class SyncClientTest : public testing::Test { | 52 class SyncClientTest : public testing::Test { |
53 public: | 53 public: |
54 SyncClientTest() | 54 SyncClientTest() |
55 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 55 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
56 mock_file_system_(new StrictMock<MockDriveFileSystem>) { | 56 mock_file_system_(new StrictMock<MockFileSystem>) { |
57 } | 57 } |
58 | 58 |
59 virtual void SetUp() OVERRIDE { | 59 virtual void SetUp() OVERRIDE { |
60 // Create a temporary directory. | 60 // Create a temporary directory. |
61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
62 | 62 |
63 // Initialize the cache. | 63 // Initialize the cache. |
64 scoped_refptr<base::SequencedWorkerPool> pool = | 64 scoped_refptr<base::SequencedWorkerPool> pool = |
65 content::BrowserThread::GetBlockingPool(); | 65 content::BrowserThread::GetBlockingPool(); |
66 cache_.reset(new FileCache( | 66 cache_.reset(new FileCache( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 google_apis::test_util::CreateCopyResultCallback(&error)); | 145 google_apis::test_util::CreateCopyResultCallback(&error)); |
146 google_apis::test_util::RunBlockingPoolTask(); | 146 google_apis::test_util::RunBlockingPoolTask(); |
147 EXPECT_EQ(FILE_ERROR_OK, error); | 147 EXPECT_EQ(FILE_ERROR_OK, error); |
148 cache_->CommitDirty( | 148 cache_->CommitDirty( |
149 resource_id_dirty, md5_dirty, | 149 resource_id_dirty, md5_dirty, |
150 google_apis::test_util::CreateCopyResultCallback(&error)); | 150 google_apis::test_util::CreateCopyResultCallback(&error)); |
151 google_apis::test_util::RunBlockingPoolTask(); | 151 google_apis::test_util::RunBlockingPoolTask(); |
152 EXPECT_EQ(FILE_ERROR_OK, error); | 152 EXPECT_EQ(FILE_ERROR_OK, error); |
153 } | 153 } |
154 | 154 |
155 // Sets the expectation for MockDriveFileSystem::GetFileByResourceId(), | 155 // Sets the expectation for MockFileSystem::GetFileByResourceId(), |
156 // that simulates successful retrieval of a file for the given resource ID. | 156 // that simulates successful retrieval of a file for the given resource ID. |
157 void SetExpectationForGetFileByResourceId(const std::string& resource_id) { | 157 void SetExpectationForGetFileByResourceId(const std::string& resource_id) { |
158 EXPECT_CALL(*mock_file_system_, | 158 EXPECT_CALL(*mock_file_system_, |
159 GetFileByResourceId(resource_id, _, _, _)) | 159 GetFileByResourceId(resource_id, _, _, _)) |
160 .WillOnce( | 160 .WillOnce( |
161 MockGetFileByResourceId( | 161 MockGetFileByResourceId( |
162 FILE_ERROR_OK, | 162 FILE_ERROR_OK, |
163 base::FilePath::FromUTF8Unsafe("local_path_does_not_matter"), | 163 base::FilePath::FromUTF8Unsafe("local_path_does_not_matter"), |
164 std::string("mime_type_does_not_matter"), | 164 std::string("mime_type_does_not_matter"), |
165 REGULAR_FILE)); | 165 REGULAR_FILE)); |
166 } | 166 } |
167 | 167 |
168 // Sets the expectation for MockDriveFileSystem::UpdateFileByResourceId(), | 168 // Sets the expectation for MockFileSystem::UpdateFileByResourceId(), |
169 // that simulates successful uploading of a file for the given resource ID. | 169 // that simulates successful uploading of a file for the given resource ID. |
170 void SetExpectationForUpdateFileByResourceId( | 170 void SetExpectationForUpdateFileByResourceId( |
171 const std::string& resource_id) { | 171 const std::string& resource_id) { |
172 EXPECT_CALL(*mock_file_system_, | 172 EXPECT_CALL(*mock_file_system_, |
173 UpdateFileByResourceId(resource_id, _, _)) | 173 UpdateFileByResourceId(resource_id, _, _)) |
174 .WillOnce(MockUpdateFileByResourceId(FILE_ERROR_OK)); | 174 .WillOnce(MockUpdateFileByResourceId(FILE_ERROR_OK)); |
175 } | 175 } |
176 | 176 |
177 // Sets the expectation for MockDriveFileSystem::GetFileInfoByResourceId(), | 177 // Sets the expectation for MockFileSystem::GetFileInfoByResourceId(), |
178 // that simulates successful retrieval of file info for the given resource | 178 // that simulates successful retrieval of file info for the given resource |
179 // ID. | 179 // ID. |
180 // | 180 // |
181 // This is used for testing StartCheckingExistingPinnedFiles(), hence we | 181 // This is used for testing StartCheckingExistingPinnedFiles(), hence we |
182 // are only interested in the MD5 value in DriveEntryProto. | 182 // are only interested in the MD5 value in DriveEntryProto. |
183 void SetExpectationForGetFileInfoByResourceId( | 183 void SetExpectationForGetFileInfoByResourceId( |
184 const std::string& resource_id, | 184 const std::string& resource_id, |
185 const std::string& new_md5) { | 185 const std::string& new_md5) { |
186 EXPECT_CALL(*mock_file_system_, | 186 EXPECT_CALL(*mock_file_system_, |
187 GetEntryInfoByResourceId(resource_id, _)) | 187 GetEntryInfoByResourceId(resource_id, _)) |
(...skipping 19 matching lines...) Expand all Loading... |
207 | 207 |
208 // Adds a resource ID of a file to upload. | 208 // Adds a resource ID of a file to upload. |
209 void AddResourceIdToUpload(const std::string& resource_id) { | 209 void AddResourceIdToUpload(const std::string& resource_id) { |
210 sync_client_->AddResourceIdForTesting(SyncClient::UPLOAD, resource_id); | 210 sync_client_->AddResourceIdForTesting(SyncClient::UPLOAD, resource_id); |
211 } | 211 } |
212 | 212 |
213 protected: | 213 protected: |
214 MessageLoopForUI message_loop_; | 214 MessageLoopForUI message_loop_; |
215 content::TestBrowserThread ui_thread_; | 215 content::TestBrowserThread ui_thread_; |
216 base::ScopedTempDir temp_dir_; | 216 base::ScopedTempDir temp_dir_; |
217 scoped_ptr<StrictMock<MockDriveFileSystem> > mock_file_system_; | 217 scoped_ptr<StrictMock<MockFileSystem> > mock_file_system_; |
218 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; | 218 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
219 scoped_ptr<SyncClient> sync_client_; | 219 scoped_ptr<SyncClient> sync_client_; |
220 }; | 220 }; |
221 | 221 |
222 TEST_F(SyncClientTest, StartInitialScan) { | 222 TEST_F(SyncClientTest, StartInitialScan) { |
223 // Start processing the files in the backlog. This will collect the | 223 // Start processing the files in the backlog. This will collect the |
224 // resource IDs of these files. | 224 // resource IDs of these files. |
225 sync_client_->StartProcessingBacklog(); | 225 sync_client_->StartProcessingBacklog(); |
226 | 226 |
227 // Check the contents of the queue for fetching. | 227 // Check the contents of the queue for fetching. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // Set the delay so that DoSyncLoop() is delayed. | 266 // Set the delay so that DoSyncLoop() is delayed. |
267 sync_client_->set_delay_for_testing(TestTimeouts::action_max_timeout()); | 267 sync_client_->set_delay_for_testing(TestTimeouts::action_max_timeout()); |
268 // Raise OnCachePinned() event. This shouldn't result in adding the second | 268 // Raise OnCachePinned() event. This shouldn't result in adding the second |
269 // task, as tasks are de-duplicated. | 269 // task, as tasks are de-duplicated. |
270 sync_client_->OnCachePinned("resource_id_not_fetched_foo", "md5"); | 270 sync_client_->OnCachePinned("resource_id_not_fetched_foo", "md5"); |
271 | 271 |
272 ASSERT_EQ(1U, GetResourceIdsToBeFetched().size()); | 272 ASSERT_EQ(1U, GetResourceIdsToBeFetched().size()); |
273 } | 273 } |
274 | 274 |
275 TEST_F(SyncClientTest, ExistingPinnedFiles) { | 275 TEST_F(SyncClientTest, ExistingPinnedFiles) { |
276 // Set the expectation so that the MockDriveFileSystem returns "new_md5" | 276 // Set the expectation so that the MockFileSystem returns "new_md5" |
277 // for "resource_id_fetched". This simulates that the file is updated on | 277 // for "resource_id_fetched". This simulates that the file is updated on |
278 // the server side, and the new MD5 is obtained from the server (i.e. the | 278 // the server side, and the new MD5 is obtained from the server (i.e. the |
279 // local cache file is stale). | 279 // local cache file is stale). |
280 SetExpectationForGetFileInfoByResourceId("resource_id_fetched", | 280 SetExpectationForGetFileInfoByResourceId("resource_id_fetched", |
281 "new_md5"); | 281 "new_md5"); |
282 // Set the expectation so that the MockDriveFileSystem returns "some_md5" | 282 // Set the expectation so that the MockFileSystem returns "some_md5" |
283 // for "resource_id_dirty". The MD5 on the server is always different from | 283 // for "resource_id_dirty". The MD5 on the server is always different from |
284 // the MD5 of a dirty file, which is set to "local". We should not collect | 284 // the MD5 of a dirty file, which is set to "local". We should not collect |
285 // this by StartCheckingExistingPinnedFiles(). | 285 // this by StartCheckingExistingPinnedFiles(). |
286 SetExpectationForGetFileInfoByResourceId("resource_id_dirty", | 286 SetExpectationForGetFileInfoByResourceId("resource_id_dirty", |
287 "some_md5"); | 287 "some_md5"); |
288 | 288 |
289 // Start checking the existing pinned files. This will collect the resource | 289 // Start checking the existing pinned files. This will collect the resource |
290 // IDs of pinned files, with stale local cache files. | 290 // IDs of pinned files, with stale local cache files. |
291 sync_client_->StartCheckingExistingPinnedFiles(); | 291 sync_client_->StartCheckingExistingPinnedFiles(); |
292 | 292 |
293 SetExpectationForGetFileByResourceId("resource_id_fetched"); | 293 SetExpectationForGetFileByResourceId("resource_id_fetched"); |
294 | 294 |
295 google_apis::test_util::RunBlockingPoolTask(); | 295 google_apis::test_util::RunBlockingPoolTask(); |
296 } | 296 } |
297 | 297 |
298 } // namespace drive | 298 } // namespace drive |
OLD | NEW |