[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 5 | #include "chrome/browser/chromeos/drive/sync_client.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 6 | |
[email protected] | 7a66996 | 2012-08-21 06:25:38 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/message_loop.h" |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 14 | #include "base/test/test_timeouts.h" |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 15 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/drive/drive.pb.h" |
[email protected] | 70e1665c | 2013-04-19 18:05:02 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/drive/drive_cache.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 19 | #include "chrome/browser/chromeos/drive/drive_test_util.h" |
| 20 | #include "chrome/browser/chromeos/drive/mock_drive_file_system.h" |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 21 | #include "chrome/browser/google_apis/test_util.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 22 | #include "content/public/test/test_browser_thread.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 23 | #include "testing/gmock/include/gmock/gmock.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 26 | using ::testing::StrictMock; |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 27 | using ::testing::_; |
| 28 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 29 | namespace drive { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 30 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 31 | namespace { |
| 32 | |
[email protected] | aaa70a4 | 2012-06-05 22:22:51 | [diff] [blame] | 33 | // Action used to set mock expectations for GetFileByResourceId(). |
| 34 | ACTION_P4(MockGetFileByResourceId, error, local_path, mime_type, file_type) { |
[email protected] | 192c3cc | 2013-02-11 22:15:49 | [diff] [blame] | 35 | arg2.Run(error, local_path, mime_type, file_type); |
[email protected] | aaa70a4 | 2012-06-05 22:22:51 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 38 | // Action used to set mock expectations for UpdateFileByResourceId(). |
| 39 | ACTION_P(MockUpdateFileByResourceId, error) { |
[email protected] | 192c3cc | 2013-02-11 22:15:49 | [diff] [blame] | 40 | arg2.Run(error); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 43 | // Action used to set mock expectations for GetFileInfoByResourceId(). |
| 44 | ACTION_P2(MockUpdateFileByResourceId, error, md5) { |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 45 | scoped_ptr<DriveEntryProto> entry_proto(new DriveEntryProto); |
[email protected] | 8a4c1d1a | 2012-07-21 05:12:17 | [diff] [blame] | 46 | entry_proto->mutable_file_specific_info()->set_file_md5(md5); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 47 | arg1.Run(error, base::FilePath(), entry_proto.Pass()); |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 50 | } // namespace |
| 51 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 52 | class SyncClientTest : public testing::Test { |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 53 | public: |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 54 | SyncClientTest() |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 55 | : ui_thread_(content::BrowserThread::UI, &message_loop_), |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 56 | mock_file_system_(new StrictMock<MockDriveFileSystem>) { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | virtual void SetUp() OVERRIDE { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 60 | // Create a temporary directory. |
| 61 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 62 | |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 63 | // Initialize the cache. |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 64 | scoped_refptr<base::SequencedWorkerPool> pool = |
| 65 | content::BrowserThread::GetBlockingPool(); |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 66 | cache_.reset(new DriveCache( |
[email protected] | e0529fc | 2012-06-12 11:07:58 | [diff] [blame] | 67 | temp_dir_.path(), |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 68 | pool->GetSequencedTaskRunner(pool->GetSequenceToken()), |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 69 | NULL /* free_disk_space_getter */)); |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 70 | bool success = false; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 71 | cache_->RequestInitialize( |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 72 | google_apis::test_util::CreateCopyResultCallback(&success)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 73 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 74 | ASSERT_TRUE(success); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 75 | SetUpCache(); |
| 76 | |
| 77 | // Initialize the sync client. |
[email protected] | 70e1665c | 2013-04-19 18:05:02 | [diff] [blame] | 78 | EXPECT_CALL(*mock_file_system_, AddObserver(_)).Times(1); |
| 79 | EXPECT_CALL(*mock_file_system_, RemoveObserver(_)).Times(1); |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 80 | sync_client_.reset(new SyncClient(mock_file_system_.get(), cache_.get())); |
[email protected] | e0529fc | 2012-06-12 11:07:58 | [diff] [blame] | 81 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 82 | // Disable delaying so that DoSyncLoop() starts immediately. |
| 83 | sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0)); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 86 | virtual void TearDown() OVERRIDE { |
[email protected] | 2416a2e | 2012-05-07 20:58:49 | [diff] [blame] | 87 | sync_client_.reset(); |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 88 | cache_.reset(); |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 91 | // Sets up cache for tests. |
| 92 | void SetUpCache() { |
| 93 | // Prepare a temp file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 94 | base::FilePath temp_file; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 95 | EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 96 | &temp_file)); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 97 | const std::string content = "hello"; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 98 | EXPECT_EQ(static_cast<int>(content.size()), |
| 99 | file_util::WriteFile(temp_file, content.data(), content.size())); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 100 | |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 101 | // Prepare 3 pinned-but-not-present files. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 102 | FileError error = FILE_ERROR_OK; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 103 | cache_->Pin("resource_id_not_fetched_foo", "", |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 104 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 105 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 106 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 107 | |
| 108 | cache_->Pin("resource_id_not_fetched_bar", "", |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 109 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 110 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 111 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 112 | |
| 113 | cache_->Pin("resource_id_not_fetched_baz", "", |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 114 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 115 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 116 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 117 | |
| 118 | // Prepare a pinned-and-fetched file. |
| 119 | const std::string resource_id_fetched = "resource_id_fetched"; |
| 120 | const std::string md5_fetched = "md5"; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 121 | base::FilePath cache_file_path; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 122 | cache_->Store(resource_id_fetched, md5_fetched, temp_file, |
| 123 | DriveCache::FILE_OPERATION_COPY, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 124 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 125 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 126 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 127 | cache_->Pin(resource_id_fetched, md5_fetched, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 128 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 129 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 130 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 131 | |
| 132 | // Prepare a pinned-and-fetched-and-dirty file. |
| 133 | const std::string resource_id_dirty = "resource_id_dirty"; |
| 134 | const std::string md5_dirty = ""; // Don't care. |
| 135 | cache_->Store(resource_id_dirty, md5_dirty, temp_file, |
| 136 | DriveCache::FILE_OPERATION_COPY, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 137 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 138 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 139 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 140 | cache_->Pin(resource_id_dirty, md5_dirty, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 141 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 142 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 143 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 144 | cache_->MarkDirty( |
| 145 | resource_id_dirty, md5_dirty, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 146 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 147 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 148 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 149 | cache_->CommitDirty( |
| 150 | resource_id_dirty, md5_dirty, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 151 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 152 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 153 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 156 | // Sets the expectation for MockDriveFileSystem::GetFileByResourceId(), |
[email protected] | aaa70a4 | 2012-06-05 22:22:51 | [diff] [blame] | 157 | // that simulates successful retrieval of a file for the given resource ID. |
| 158 | void SetExpectationForGetFileByResourceId(const std::string& resource_id) { |
| 159 | EXPECT_CALL(*mock_file_system_, |
[email protected] | 192c3cc | 2013-02-11 22:15:49 | [diff] [blame] | 160 | GetFileByResourceId(resource_id, _, _, _)) |
[email protected] | 1e4ff46 | 2013-02-26 05:36:36 | [diff] [blame] | 161 | .WillOnce( |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 162 | MockGetFileByResourceId( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 163 | FILE_ERROR_OK, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 164 | base::FilePath::FromUTF8Unsafe("local_path_does_not_matter"), |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 165 | std::string("mime_type_does_not_matter"), |
[email protected] | 1e4ff46 | 2013-02-26 05:36:36 | [diff] [blame] | 166 | REGULAR_FILE)); |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 169 | // Sets the expectation for MockDriveFileSystem::UpdateFileByResourceId(), |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 170 | // that simulates successful uploading of a file for the given resource ID. |
| 171 | void SetExpectationForUpdateFileByResourceId( |
| 172 | const std::string& resource_id) { |
| 173 | EXPECT_CALL(*mock_file_system_, |
[email protected] | 192c3cc | 2013-02-11 22:15:49 | [diff] [blame] | 174 | UpdateFileByResourceId(resource_id, _, _)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 175 | .WillOnce(MockUpdateFileByResourceId(FILE_ERROR_OK)); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 178 | // Sets the expectation for MockDriveFileSystem::GetFileInfoByResourceId(), |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 179 | // that simulates successful retrieval of file info for the given resource |
| 180 | // ID. |
| 181 | // |
| 182 | // This is used for testing StartCheckingExistingPinnedFiles(), hence we |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 183 | // are only interested in the MD5 value in DriveEntryProto. |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 184 | void SetExpectationForGetFileInfoByResourceId( |
| 185 | const std::string& resource_id, |
| 186 | const std::string& new_md5) { |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 187 | EXPECT_CALL(*mock_file_system_, |
| 188 | GetEntryInfoByResourceId(resource_id, _)) |
| 189 | .WillOnce(MockUpdateFileByResourceId( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 190 | FILE_ERROR_OK, |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 191 | new_md5)); |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 194 | // Returns the resource IDs in the queue to be fetched. |
| 195 | std::vector<std::string> GetResourceIdsToBeFetched() { |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 196 | return sync_client_->GetResourceIdsForTesting(SyncClient::FETCH); |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 199 | // Returns the resource IDs in the queue to be uploaded. |
| 200 | std::vector<std::string> GetResourceIdsToBeUploaded() { |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 201 | return sync_client_->GetResourceIdsForTesting(SyncClient::UPLOAD); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 204 | // Adds a resource ID of a file to fetch. |
| 205 | void AddResourceIdToFetch(const std::string& resource_id) { |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 206 | sync_client_->AddResourceIdForTesting(SyncClient::FETCH, resource_id); |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 209 | // Adds a resource ID of a file to upload. |
| 210 | void AddResourceIdToUpload(const std::string& resource_id) { |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 211 | sync_client_->AddResourceIdForTesting(SyncClient::UPLOAD, resource_id); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 214 | protected: |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 215 | MessageLoopForUI message_loop_; |
| 216 | content::TestBrowserThread ui_thread_; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 217 | base::ScopedTempDir temp_dir_; |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 218 | scoped_ptr<StrictMock<MockDriveFileSystem> > mock_file_system_; |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 219 | scoped_ptr<DriveCache, test_util::DestroyHelperForTests> cache_; |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 220 | scoped_ptr<SyncClient> sync_client_; |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 221 | }; |
| 222 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 223 | TEST_F(SyncClientTest, StartInitialScan) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 224 | // Start processing the files in the backlog. This will collect the |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 225 | // resource IDs of these files. |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 226 | sync_client_->StartProcessingBacklog(); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 227 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 228 | // Check the contents of the queue for fetching. |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 229 | SetExpectationForGetFileByResourceId("resource_id_not_fetched_bar"); |
| 230 | SetExpectationForGetFileByResourceId("resource_id_not_fetched_baz"); |
| 231 | SetExpectationForGetFileByResourceId("resource_id_not_fetched_foo"); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 232 | |
| 233 | // Check the contents of the queue for uploading. |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 234 | SetExpectationForUpdateFileByResourceId("resource_id_dirty"); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 235 | |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 236 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 239 | TEST_F(SyncClientTest, OnCachePinned) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 240 | // This file will be fetched by GetFileByResourceId() as OnCachePinned() |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 241 | // will kick off the sync loop. |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 242 | SetExpectationForGetFileByResourceId("resource_id_not_fetched_foo"); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 243 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 244 | sync_client_->OnCachePinned("resource_id_not_fetched_foo", "md5"); |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 245 | |
| 246 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 249 | TEST_F(SyncClientTest, OnCacheUnpinned) { |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 250 | AddResourceIdToFetch("resource_id_not_fetched_foo"); |
| 251 | AddResourceIdToFetch("resource_id_not_fetched_bar"); |
| 252 | AddResourceIdToFetch("resource_id_not_fetched_baz"); |
| 253 | ASSERT_EQ(3U, GetResourceIdsToBeFetched().size()); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 254 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 255 | sync_client_->OnCacheUnpinned("resource_id_not_fetched_foo", "md5"); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 256 | sync_client_->OnCacheUnpinned("resource_id_not_fetched_baz", "md5"); |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 257 | |
| 258 | // Only resource_id_not_fetched_foo should be fetched. |
| 259 | SetExpectationForGetFileByResourceId("resource_id_not_fetched_bar"); |
| 260 | |
| 261 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 264 | TEST_F(SyncClientTest, Deduplication) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 265 | AddResourceIdToFetch("resource_id_not_fetched_foo"); |
| 266 | |
| 267 | // Set the delay so that DoSyncLoop() is delayed. |
| 268 | sync_client_->set_delay_for_testing(TestTimeouts::action_max_timeout()); |
| 269 | // Raise OnCachePinned() event. This shouldn't result in adding the second |
| 270 | // task, as tasks are de-duplicated. |
| 271 | sync_client_->OnCachePinned("resource_id_not_fetched_foo", "md5"); |
| 272 | |
| 273 | ASSERT_EQ(1U, GetResourceIdsToBeFetched().size()); |
| 274 | } |
| 275 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame^] | 276 | TEST_F(SyncClientTest, ExistingPinnedFiles) { |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 277 | // Set the expectation so that the MockDriveFileSystem returns "new_md5" |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 278 | // for "resource_id_fetched". This simulates that the file is updated on |
| 279 | // the server side, and the new MD5 is obtained from the server (i.e. the |
[email protected] | fba5954 | 2012-12-18 06:05:38 | [diff] [blame] | 280 | // local cache file is stale). |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 281 | SetExpectationForGetFileInfoByResourceId("resource_id_fetched", |
| 282 | "new_md5"); |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 283 | // Set the expectation so that the MockDriveFileSystem returns "some_md5" |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 284 | // for "resource_id_dirty". The MD5 on the server is always different from |
| 285 | // the MD5 of a dirty file, which is set to "local". We should not collect |
| 286 | // this by StartCheckingExistingPinnedFiles(). |
| 287 | SetExpectationForGetFileInfoByResourceId("resource_id_dirty", |
| 288 | "some_md5"); |
| 289 | |
| 290 | // Start checking the existing pinned files. This will collect the resource |
| 291 | // IDs of pinned files, with stale local cache files. |
| 292 | sync_client_->StartCheckingExistingPinnedFiles(); |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 293 | |
| 294 | SetExpectationForGetFileByResourceId("resource_id_fetched"); |
| 295 | |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 296 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 299 | } // namespace drive |