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/gdata/gdata_sync_client.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_sync_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 // Action used to set mock expectations for GetFileByResourceId(). | 31 // Action used to set mock expectations for GetFileByResourceId(). |
32 ACTION_P4(MockGetFileByResourceId, error, local_path, mime_type, file_type) { | 32 ACTION_P4(MockGetFileByResourceId, error, local_path, mime_type, file_type) { |
33 arg1.Run(error, local_path, mime_type, file_type); | 33 arg1.Run(error, local_path, mime_type, file_type); |
34 } | 34 } |
35 | 35 |
36 class GDataSyncClientTest : public testing::Test { | 36 class GDataSyncClientTest : public testing::Test { |
37 public: | 37 public: |
38 GDataSyncClientTest() | 38 GDataSyncClientTest() |
39 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 39 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 40 io_thread_(content::BrowserThread::IO), |
| 41 sequence_token_( |
| 42 content::BrowserThread::GetBlockingPool()->GetSequenceToken()), |
40 profile_(new TestingProfile), | 43 profile_(new TestingProfile), |
41 mock_file_system_(new MockGDataFileSystem), | 44 mock_file_system_(new MockGDataFileSystem), |
42 sync_client_(new GDataSyncClient(profile_.get(), | |
43 mock_file_system_.get())), | |
44 mock_network_library_(NULL) { | 45 mock_network_library_(NULL) { |
45 } | 46 } |
46 | 47 |
47 virtual void SetUp() OVERRIDE { | 48 virtual void SetUp() OVERRIDE { |
48 chromeos::CrosLibrary::Initialize(true /* use_stub */); | 49 chromeos::CrosLibrary::Initialize(true /* use_stub */); |
49 | 50 |
50 // CrosLibrary takes ownership of MockNetworkLibrary. | 51 // CrosLibrary takes ownership of MockNetworkLibrary. |
51 mock_network_library_ = new chromeos::MockNetworkLibrary; | 52 mock_network_library_ = new chromeos::MockNetworkLibrary; |
52 chromeos::CrosLibrary::Get()->GetTestApi()->SetNetworkLibrary( | 53 chromeos::CrosLibrary::Get()->GetTestApi()->SetNetworkLibrary( |
53 mock_network_library_, true); | 54 mock_network_library_, true); |
| 55 |
| 56 // Create a temporary directory. |
| 57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 58 |
| 59 // Initialize the sync client. |
| 60 cache_.reset(GDataCache::CreateGDataCache( |
| 61 temp_dir_.path(), |
| 62 content::BrowserThread::GetBlockingPool(), |
| 63 sequence_token_).release()); |
| 64 sync_client_.reset(new GDataSyncClient(profile_.get(), |
| 65 mock_file_system_.get(), |
| 66 cache_.get())); |
| 67 |
54 EXPECT_CALL(*mock_network_library_, AddNetworkManagerObserver( | 68 EXPECT_CALL(*mock_network_library_, AddNetworkManagerObserver( |
55 sync_client_.get())).Times(1); | 69 sync_client_.get())).Times(1); |
56 EXPECT_CALL(*mock_network_library_, RemoveNetworkManagerObserver( | 70 EXPECT_CALL(*mock_network_library_, RemoveNetworkManagerObserver( |
57 sync_client_.get())).Times(1); | 71 sync_client_.get())).Times(1); |
58 | |
59 // Create a temporary directory. | |
60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
61 | |
62 EXPECT_CALL(*mock_file_system_, AddObserver(sync_client_.get())) | 72 EXPECT_CALL(*mock_file_system_, AddObserver(sync_client_.get())) |
63 .Times(1); | 73 .Times(1); |
64 EXPECT_CALL(*mock_file_system_, RemoveObserver(sync_client_.get())) | 74 EXPECT_CALL(*mock_file_system_, RemoveObserver(sync_client_.get())) |
65 .Times(1); | 75 .Times(1); |
66 | 76 |
67 sync_client_->Initialize(); | 77 sync_client_->Initialize(); |
68 } | 78 } |
69 | 79 |
70 virtual void TearDown() OVERRIDE { | 80 virtual void TearDown() OVERRIDE { |
71 // The sync client should be deleted before NetworkLibrary, as the sync | 81 // The sync client should be deleted before NetworkLibrary, as the sync |
72 // client registers itself as observer of NetworkLibrary. | 82 // client registers itself as observer of NetworkLibrary. |
73 sync_client_.reset(); | 83 sync_client_.reset(); |
74 chromeos::CrosLibrary::Shutdown(); | 84 chromeos::CrosLibrary::Shutdown(); |
| 85 content::BrowserThread::GetBlockingPool() |
| 86 ->GetSequencedTaskRunner(sequence_token_)->PostTask( |
| 87 FROM_HERE, |
| 88 base::Bind(&base::DeletePointer<GDataCache>, cache_.release())); |
| 89 RunAllPendingForIO(); |
| 90 } |
| 91 |
| 92 // Used to wait for the result from an operation that involves file IO, |
| 93 // that runs on the blocking pool thread. |
| 94 void RunAllPendingForIO() { |
| 95 // We should first flush tasks on UI thread, as it can require some |
| 96 // tasks to be run before IO tasks start. |
| 97 message_loop_.RunAllPending(); |
| 98 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 99 // Once IO tasks are done, flush UI thread again so the results from IO |
| 100 // tasks are processed. |
| 101 message_loop_.RunAllPending(); |
75 } | 102 } |
76 | 103 |
77 // Sets up MockNetworkLibrary as if it's connected to wifi network. | 104 // Sets up MockNetworkLibrary as if it's connected to wifi network. |
78 void ConnectToWifi() { | 105 void ConnectToWifi() { |
79 active_network_.reset( | 106 active_network_.reset( |
80 chromeos::Network::CreateForTesting(chromeos::TYPE_WIFI)); | 107 chromeos::Network::CreateForTesting(chromeos::TYPE_WIFI)); |
81 EXPECT_CALL(*mock_network_library_, active_network()) | 108 EXPECT_CALL(*mock_network_library_, active_network()) |
82 .Times(AnyNumber()) | 109 .Times(AnyNumber()) |
83 .WillRepeatedly((Return(active_network_.get()))); | 110 .WillRepeatedly((Return(active_network_.get()))); |
84 chromeos::Network::TestApi(active_network_.get()).SetConnected(true); | 111 chromeos::Network::TestApi(active_network_.get()).SetConnected(true); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 EXPECT_CALL(*mock_network_library_, active_network()) | 144 EXPECT_CALL(*mock_network_library_, active_network()) |
118 .Times(AnyNumber()) | 145 .Times(AnyNumber()) |
119 .WillRepeatedly((Return(active_network_.get()))); | 146 .WillRepeatedly((Return(active_network_.get()))); |
120 // Here false is passed to make it disconnected. | 147 // Here false is passed to make it disconnected. |
121 chromeos::Network::TestApi(active_network_.get()).SetConnected(false); | 148 chromeos::Network::TestApi(active_network_.get()).SetConnected(false); |
122 sync_client_->OnNetworkManagerChanged(mock_network_library_); | 149 sync_client_->OnNetworkManagerChanged(mock_network_library_); |
123 } | 150 } |
124 | 151 |
125 // Sets up test files in the temporary directory. | 152 // Sets up test files in the temporary directory. |
126 void SetUpTestFiles() { | 153 void SetUpTestFiles() { |
| 154 // Create a directory in the temporary directory for pinned files. |
| 155 const FilePath pinned_dir = |
| 156 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_PINNED); |
| 157 ASSERT_TRUE(file_util::CreateDirectory(pinned_dir)); |
| 158 |
127 // Create a symlink in the temporary directory to /dev/null. | 159 // Create a symlink in the temporary directory to /dev/null. |
128 // We'll collect this resource ID as a file to be fetched. | 160 // We'll collect this resource ID as a file to be fetched. |
129 ASSERT_TRUE( | 161 ASSERT_TRUE( |
130 file_util::CreateSymbolicLink( | 162 file_util::CreateSymbolicLink( |
131 FilePath::FromUTF8Unsafe("/dev/null"), | 163 FilePath::FromUTF8Unsafe("/dev/null"), |
132 temp_dir_.path().Append("resource_id_not_fetched_foo"))); | 164 pinned_dir.Append("resource_id_not_fetched_foo"))); |
133 // Create some more. | 165 // Create some more. |
134 ASSERT_TRUE( | 166 ASSERT_TRUE( |
135 file_util::CreateSymbolicLink( | 167 file_util::CreateSymbolicLink( |
136 FilePath::FromUTF8Unsafe("/dev/null"), | 168 FilePath::FromUTF8Unsafe("/dev/null"), |
137 temp_dir_.path().Append("resource_id_not_fetched_bar"))); | 169 pinned_dir.Append("resource_id_not_fetched_bar"))); |
138 ASSERT_TRUE( | 170 ASSERT_TRUE( |
139 file_util::CreateSymbolicLink( | 171 file_util::CreateSymbolicLink( |
140 FilePath::FromUTF8Unsafe("/dev/null"), | 172 FilePath::FromUTF8Unsafe("/dev/null"), |
141 temp_dir_.path().Append("resource_id_not_fetched_baz"))); | 173 pinned_dir.Append("resource_id_not_fetched_baz"))); |
142 | 174 |
143 // Create a symlink in the temporary directory to a test file | 175 // Create a symlink in the temporary directory to a test file |
144 // We won't collect this resource ID, as it already exists. | 176 // We won't collect this resource ID, as it already exists. |
145 FilePath test_data_dir; | 177 FilePath test_data_dir; |
146 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); | 178 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); |
147 FilePath test_file_path = test_data_dir.AppendASCII("chromeos") | 179 FilePath test_file_path = test_data_dir.AppendASCII("chromeos") |
148 .AppendASCII("gdata").AppendASCII("testfile.txt"); | 180 .AppendASCII("gdata").AppendASCII("testfile.txt"); |
149 ASSERT_TRUE( | 181 ASSERT_TRUE( |
150 file_util::CreateSymbolicLink( | 182 file_util::CreateSymbolicLink( |
151 test_file_path, | 183 test_file_path, |
152 temp_dir_.path().Append("resource_id_fetched"))); | 184 pinned_dir.Append("resource_id_fetched"))); |
153 } | 185 } |
154 | 186 |
155 // Called when StartInitialScan() is complete. | 187 // Called when StartInitialScan() is complete. |
156 void OnInitialScanComplete() { | 188 void OnInitialScanComplete() { |
157 message_loop_.Quit(); | 189 message_loop_.Quit(); |
158 } | 190 } |
159 | 191 |
160 // Sets the expectation for MockGDataFileSystem::GetFileByResourceId(), | 192 // Sets the expectation for MockGDataFileSystem::GetFileByResourceId(), |
161 // that simulates successful retrieval of a file for the given resource ID. | 193 // that simulates successful retrieval of a file for the given resource ID. |
162 void SetExpectationForGetFileByResourceId(const std::string& resource_id) { | 194 void SetExpectationForGetFileByResourceId(const std::string& resource_id) { |
163 EXPECT_CALL(*mock_file_system_, | 195 EXPECT_CALL(*mock_file_system_, |
164 GetFileByResourceId(resource_id, _, _)) | 196 GetFileByResourceId(resource_id, _, _)) |
165 .WillOnce(MockGetFileByResourceId( | 197 .WillOnce(MockGetFileByResourceId( |
166 base::PLATFORM_FILE_OK, | 198 base::PLATFORM_FILE_OK, |
167 FilePath::FromUTF8Unsafe("local_path_does_not_matter"), | 199 FilePath::FromUTF8Unsafe("local_path_does_not_matter"), |
168 std::string("mime_type_does_not_matter"), | 200 std::string("mime_type_does_not_matter"), |
169 REGULAR_FILE)); | 201 REGULAR_FILE)); |
170 } | 202 } |
171 | 203 |
172 protected: | 204 protected: |
173 MessageLoopForUI message_loop_; | 205 MessageLoopForUI message_loop_; |
174 content::TestBrowserThread ui_thread_; | 206 content::TestBrowserThread ui_thread_; |
| 207 content::TestBrowserThread io_thread_; |
| 208 const base::SequencedWorkerPool::SequenceToken sequence_token_; |
175 ScopedTempDir temp_dir_; | 209 ScopedTempDir temp_dir_; |
176 scoped_ptr<TestingProfile> profile_; | 210 scoped_ptr<TestingProfile> profile_; |
177 scoped_ptr<MockGDataFileSystem> mock_file_system_; | 211 scoped_ptr<MockGDataFileSystem> mock_file_system_; |
| 212 scoped_ptr<GDataCache> cache_; |
178 scoped_ptr<GDataSyncClient> sync_client_; | 213 scoped_ptr<GDataSyncClient> sync_client_; |
179 chromeos::MockNetworkLibrary* mock_network_library_; | 214 chromeos::MockNetworkLibrary* mock_network_library_; |
180 scoped_ptr<chromeos::Network> active_network_; | 215 scoped_ptr<chromeos::Network> active_network_; |
181 }; | 216 }; |
182 | 217 |
183 TEST_F(GDataSyncClientTest, StartInitialScan) { | 218 TEST_F(GDataSyncClientTest, StartInitialScan) { |
184 SetUpTestFiles(); | 219 SetUpTestFiles(); |
185 | 220 |
186 EXPECT_CALL(*mock_file_system_, GetCacheDirectoryPath( | |
187 GDataCache::CACHE_TYPE_PINNED)) | |
188 .WillOnce(Return(temp_dir_.path())); | |
189 | |
190 sync_client_->StartInitialScan( | 221 sync_client_->StartInitialScan( |
191 base::Bind(&GDataSyncClientTest::OnInitialScanComplete, | 222 base::Bind(&GDataSyncClientTest::OnInitialScanComplete, |
192 base::Unretained(this))); | 223 base::Unretained(this))); |
193 // Wait until the initial scan is complete. | 224 // Wait until the initial scan is complete. |
194 message_loop_.Run(); | 225 message_loop_.Run(); |
195 | 226 |
196 // Check the contents of the queue. | 227 // Check the contents of the queue. |
197 const std::deque<std::string>& resource_ids = | 228 const std::deque<std::string>& resource_ids = |
198 sync_client_->GetResourceIdsForTesting(); | 229 sync_client_->GetResourceIdsForTesting(); |
199 ASSERT_EQ(3U, resource_ids.size()); | 230 ASSERT_EQ(3U, resource_ids.size()); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 ASSERT_EQ(1U, resource_ids.size()); | 420 ASSERT_EQ(1U, resource_ids.size()); |
390 EXPECT_EQ("resource_id_not_fetched_baz", resource_ids[1]); | 421 EXPECT_EQ("resource_id_not_fetched_baz", resource_ids[1]); |
391 | 422 |
392 sync_client_->OnFileUnpinned("resource_id_not_fetched_baz", "md5"); | 423 sync_client_->OnFileUnpinned("resource_id_not_fetched_baz", "md5"); |
393 // "baz" should be gone. | 424 // "baz" should be gone. |
394 resource_ids = sync_client_->GetResourceIdsForTesting(); | 425 resource_ids = sync_client_->GetResourceIdsForTesting(); |
395 ASSERT_TRUE(resource_ids.empty()); | 426 ASSERT_TRUE(resource_ids.empty()); |
396 } | 427 } |
397 | 428 |
398 } // namespace gdata | 429 } // namespace gdata |
OLD | NEW |