| 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/drive_sync_client.h" | 5 #include "chrome/browser/chromeos/drive/drive_sync_client.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <vector> | 7 #include <vector> |
| 9 | 8 |
| 10 #include "base/bind.h" | |
| 11 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 16 #include "base/path_service.h" | |
| 17 #include "base/prefs/pref_service.h" | |
| 18 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "chrome/browser/chromeos/drive/drive.pb.h" | 16 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 17 #include "chrome/browser/chromeos/drive/drive_cache.h" |
| 21 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 18 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 22 #include "chrome/browser/chromeos/drive/drive_test_util.h" | 19 #include "chrome/browser/chromeos/drive/drive_test_util.h" |
| 23 #include "chrome/browser/chromeos/drive/mock_drive_file_system.h" | 20 #include "chrome/browser/chromeos/drive/mock_drive_file_system.h" |
| 24 #include "chrome/browser/google_apis/test_util.h" | 21 #include "chrome/browser/google_apis/test_util.h" |
| 25 #include "chrome/common/chrome_paths.h" | |
| 26 #include "chrome/common/pref_names.h" | |
| 27 #include "chrome/test/base/testing_profile.h" | |
| 28 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 25 |
| 32 using ::testing::AnyNumber; | |
| 33 using ::testing::DoAll; | |
| 34 using ::testing::Return; | |
| 35 using ::testing::StrictMock; | 26 using ::testing::StrictMock; |
| 36 using ::testing::_; | 27 using ::testing::_; |
| 37 | 28 |
| 38 namespace drive { | 29 namespace drive { |
| 39 | 30 |
| 40 namespace { | 31 namespace { |
| 41 | 32 |
| 42 // Action used to set mock expectations for GetFileByResourceId(). | 33 // Action used to set mock expectations for GetFileByResourceId(). |
| 43 ACTION_P4(MockGetFileByResourceId, error, local_path, mime_type, file_type) { | 34 ACTION_P4(MockGetFileByResourceId, error, local_path, mime_type, file_type) { |
| 44 arg2.Run(error, local_path, mime_type, file_type); | 35 arg2.Run(error, local_path, mime_type, file_type); |
| 45 } | 36 } |
| 46 | 37 |
| 47 // Action used to set mock expectations for UpdateFileByResourceId(). | 38 // Action used to set mock expectations for UpdateFileByResourceId(). |
| 48 ACTION_P(MockUpdateFileByResourceId, error) { | 39 ACTION_P(MockUpdateFileByResourceId, error) { |
| 49 arg2.Run(error); | 40 arg2.Run(error); |
| 50 } | 41 } |
| 51 | 42 |
| 52 // Action used to set mock expectations for GetFileInfoByResourceId(). | 43 // Action used to set mock expectations for GetFileInfoByResourceId(). |
| 53 ACTION_P2(MockUpdateFileByResourceId, error, md5) { | 44 ACTION_P2(MockUpdateFileByResourceId, error, md5) { |
| 54 scoped_ptr<DriveEntryProto> entry_proto(new DriveEntryProto); | 45 scoped_ptr<DriveEntryProto> entry_proto(new DriveEntryProto); |
| 55 entry_proto->mutable_file_specific_info()->set_file_md5(md5); | 46 entry_proto->mutable_file_specific_info()->set_file_md5(md5); |
| 56 arg1.Run(error, base::FilePath(), entry_proto.Pass()); | 47 arg1.Run(error, base::FilePath(), entry_proto.Pass()); |
| 57 } | 48 } |
| 58 | 49 |
| 59 class MockNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
| 60 public: | |
| 61 MOCK_CONST_METHOD0(GetCurrentConnectionType, | |
| 62 net::NetworkChangeNotifier::ConnectionType()); | |
| 63 }; | |
| 64 | |
| 65 } // namespace | 50 } // namespace |
| 66 | 51 |
| 67 class DriveSyncClientTest : public testing::Test { | 52 class DriveSyncClientTest : public testing::Test { |
| 68 public: | 53 public: |
| 69 DriveSyncClientTest() | 54 DriveSyncClientTest() |
| 70 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 55 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 71 profile_(new TestingProfile), | |
| 72 mock_file_system_(new StrictMock<MockDriveFileSystem>) { | 56 mock_file_system_(new StrictMock<MockDriveFileSystem>) { |
| 73 } | 57 } |
| 74 | 58 |
| 75 virtual void SetUp() OVERRIDE { | 59 virtual void SetUp() OVERRIDE { |
| 76 mock_network_change_notifier_.reset(new MockNetworkChangeNotifier); | |
| 77 | |
| 78 // Create a temporary directory. | 60 // Create a temporary directory. |
| 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 80 | 62 |
| 81 // Initialize the cache. | 63 // Initialize the cache. |
| 82 scoped_refptr<base::SequencedWorkerPool> pool = | 64 scoped_refptr<base::SequencedWorkerPool> pool = |
| 83 content::BrowserThread::GetBlockingPool(); | 65 content::BrowserThread::GetBlockingPool(); |
| 84 cache_.reset(new DriveCache( | 66 cache_.reset(new DriveCache( |
| 85 temp_dir_.path(), | 67 temp_dir_.path(), |
| 86 pool->GetSequencedTaskRunner(pool->GetSequenceToken()), | 68 pool->GetSequencedTaskRunner(pool->GetSequenceToken()), |
| 87 NULL /* free_disk_space_getter */)); | 69 NULL /* free_disk_space_getter */)); |
| 88 bool success = false; | 70 bool success = false; |
| 89 cache_->RequestInitialize( | 71 cache_->RequestInitialize( |
| 90 google_apis::test_util::CreateCopyResultCallback(&success)); | 72 google_apis::test_util::CreateCopyResultCallback(&success)); |
| 91 google_apis::test_util::RunBlockingPoolTask(); | 73 google_apis::test_util::RunBlockingPoolTask(); |
| 92 ASSERT_TRUE(success); | 74 ASSERT_TRUE(success); |
| 93 SetUpCache(); | 75 SetUpCache(); |
| 94 | 76 |
| 95 // Initialize the sync client. | 77 // Initialize the sync client. |
| 96 sync_client_.reset(new DriveSyncClient(profile_.get(), | 78 sync_client_.reset(new DriveSyncClient(mock_file_system_.get(), |
| 97 mock_file_system_.get(), | |
| 98 cache_.get())); | 79 cache_.get())); |
| 99 | 80 |
| 100 EXPECT_CALL(*mock_file_system_, AddObserver(sync_client_.get())).Times(1); | 81 EXPECT_CALL(*mock_file_system_, AddObserver(sync_client_.get())).Times(1); |
| 101 EXPECT_CALL(*mock_file_system_, | 82 EXPECT_CALL(*mock_file_system_, |
| 102 RemoveObserver(sync_client_.get())).Times(1); | 83 RemoveObserver(sync_client_.get())).Times(1); |
| 103 | 84 |
| 104 // Disable delaying so that DoSyncLoop() starts immediately. | 85 // Disable delaying so that DoSyncLoop() starts immediately. |
| 105 sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0)); | 86 sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0)); |
| 106 sync_client_->Initialize(); | 87 sync_client_->Initialize(); |
| 107 } | 88 } |
| 108 | 89 |
| 109 virtual void TearDown() OVERRIDE { | 90 virtual void TearDown() OVERRIDE { |
| 110 // The sync client should be deleted before NetworkLibrary, as the sync | 91 // The sync client should be deleted before NetworkLibrary, as the sync |
| 111 // client registers itself as observer of NetworkLibrary. | 92 // client registers itself as observer of NetworkLibrary. |
| 112 sync_client_.reset(); | 93 sync_client_.reset(); |
| 113 cache_.reset(); | 94 cache_.reset(); |
| 114 mock_network_change_notifier_.reset(); | |
| 115 } | 95 } |
| 116 | 96 |
| 117 // Sets up cache for tests. | 97 // Sets up cache for tests. |
| 118 void SetUpCache() { | 98 void SetUpCache() { |
| 119 // Prepare a temp file. | 99 // Prepare a temp file. |
| 120 base::FilePath temp_file; | 100 base::FilePath temp_file; |
| 121 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 101 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 122 &temp_file)); | 102 &temp_file)); |
| 123 const std::string content = "hello"; | 103 const std::string content = "hello"; |
| 124 EXPECT_EQ(static_cast<int>(content.size()), | 104 EXPECT_EQ(static_cast<int>(content.size()), |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 // Adds a resource ID of a file to upload. | 217 // Adds a resource ID of a file to upload. |
| 238 void AddResourceIdToUpload(const std::string& resource_id) { | 218 void AddResourceIdToUpload(const std::string& resource_id) { |
| 239 sync_client_->AddResourceIdForTesting(DriveSyncClient::UPLOAD, | 219 sync_client_->AddResourceIdForTesting(DriveSyncClient::UPLOAD, |
| 240 resource_id); | 220 resource_id); |
| 241 } | 221 } |
| 242 | 222 |
| 243 protected: | 223 protected: |
| 244 MessageLoopForUI message_loop_; | 224 MessageLoopForUI message_loop_; |
| 245 content::TestBrowserThread ui_thread_; | 225 content::TestBrowserThread ui_thread_; |
| 246 base::ScopedTempDir temp_dir_; | 226 base::ScopedTempDir temp_dir_; |
| 247 scoped_ptr<TestingProfile> profile_; | |
| 248 scoped_ptr<StrictMock<MockDriveFileSystem> > mock_file_system_; | 227 scoped_ptr<StrictMock<MockDriveFileSystem> > mock_file_system_; |
| 249 scoped_ptr<DriveCache, test_util::DestroyHelperForTests> cache_; | 228 scoped_ptr<DriveCache, test_util::DestroyHelperForTests> cache_; |
| 250 scoped_ptr<DriveSyncClient> sync_client_; | 229 scoped_ptr<DriveSyncClient> sync_client_; |
| 251 scoped_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; | |
| 252 }; | 230 }; |
| 253 | 231 |
| 254 TEST_F(DriveSyncClientTest, StartInitialScan) { | 232 TEST_F(DriveSyncClientTest, StartInitialScan) { |
| 255 // Start processing the files in the backlog. This will collect the | 233 // Start processing the files in the backlog. This will collect the |
| 256 // resource IDs of these files. | 234 // resource IDs of these files. |
| 257 sync_client_->StartProcessingBacklog(); | 235 sync_client_->StartProcessingBacklog(); |
| 258 | 236 |
| 259 // Check the contents of the queue for fetching. | 237 // Check the contents of the queue for fetching. |
| 260 SetExpectationForGetFileByResourceId("resource_id_not_fetched_bar"); | 238 SetExpectationForGetFileByResourceId("resource_id_not_fetched_bar"); |
| 261 SetExpectationForGetFileByResourceId("resource_id_not_fetched_baz"); | 239 SetExpectationForGetFileByResourceId("resource_id_not_fetched_baz"); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 // Start checking the existing pinned files. This will collect the resource | 299 // Start checking the existing pinned files. This will collect the resource |
| 322 // IDs of pinned files, with stale local cache files. | 300 // IDs of pinned files, with stale local cache files. |
| 323 sync_client_->StartCheckingExistingPinnedFiles(); | 301 sync_client_->StartCheckingExistingPinnedFiles(); |
| 324 | 302 |
| 325 SetExpectationForGetFileByResourceId("resource_id_fetched"); | 303 SetExpectationForGetFileByResourceId("resource_id_fetched"); |
| 326 | 304 |
| 327 google_apis::test_util::RunBlockingPoolTask(); | 305 google_apis::test_util::RunBlockingPoolTask(); |
| 328 } | 306 } |
| 329 | 307 |
| 330 } // namespace drive | 308 } // namespace drive |
| OLD | NEW |