OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/change_list_loader.h" | 5 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/prefs/testing_pref_service.h" | 9 #include "base/prefs/testing_pref_service.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 | 59 |
60 private: | 60 private: |
61 ChangeListLoader* loader_; | 61 ChangeListLoader* loader_; |
62 std::set<base::FilePath> changed_directories_; | 62 std::set<base::FilePath> changed_directories_; |
63 int load_from_server_complete_count_; | 63 int load_from_server_complete_count_; |
64 int initial_load_complete_count_; | 64 int initial_load_complete_count_; |
65 | 65 |
66 DISALLOW_COPY_AND_ASSIGN(TestChangeListLoaderObserver); | 66 DISALLOW_COPY_AND_ASSIGN(TestChangeListLoaderObserver); |
67 }; | 67 }; |
68 | 68 |
69 class TestDriveService : public FakeDriveService { | |
70 public: | |
71 TestDriveService() : never_return_all_resource_list_(false), | |
72 blocked_call_count_(0) {} | |
73 | |
74 void set_never_return_all_resource_list(bool value) { | |
75 never_return_all_resource_list_ = value; | |
76 } | |
77 | |
78 int blocked_call_count() const { return blocked_call_count_; } | |
79 | |
80 // FakeDriveService override. | |
81 virtual google_apis::CancelCallback GetAllResourceList( | |
82 const google_apis::GetResourceListCallback& callback) OVERRIDE { | |
83 if (never_return_all_resource_list_) { | |
84 ++blocked_call_count_; | |
85 return google_apis::CancelCallback(); | |
86 } | |
87 return FakeDriveService::GetAllResourceList(callback); | |
88 } | |
89 | |
90 private: | |
91 // GetAllResourceList never returns result when this is set to true. | |
92 // Used to emulate the real server's slowness. | |
93 bool never_return_all_resource_list_; | |
94 | |
95 int blocked_call_count_; // Number of blocked method calls. | |
96 }; | |
97 | |
69 class ChangeListLoaderTest : public testing::Test { | 98 class ChangeListLoaderTest : public testing::Test { |
70 protected: | 99 protected: |
71 virtual void SetUp() OVERRIDE { | 100 virtual void SetUp() OVERRIDE { |
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
73 pref_service_.reset(new TestingPrefServiceSimple); | 102 pref_service_.reset(new TestingPrefServiceSimple); |
74 test_util::RegisterDrivePrefs(pref_service_->registry()); | 103 test_util::RegisterDrivePrefs(pref_service_->registry()); |
75 | 104 |
76 drive_service_.reset(new FakeDriveService); | 105 drive_service_.reset(new TestDriveService); |
77 ASSERT_TRUE(drive_service_->LoadResourceListForWapi( | 106 ASSERT_TRUE(drive_service_->LoadResourceListForWapi( |
78 "gdata/root_feed.json")); | 107 "gdata/root_feed.json")); |
79 ASSERT_TRUE(drive_service_->LoadAccountMetadataForWapi( | 108 ASSERT_TRUE(drive_service_->LoadAccountMetadataForWapi( |
80 "gdata/account_metadata.json")); | 109 "gdata/account_metadata.json")); |
81 | 110 |
82 scheduler_.reset(new JobScheduler(pref_service_.get(), | 111 scheduler_.reset(new JobScheduler(pref_service_.get(), |
83 drive_service_.get(), | 112 drive_service_.get(), |
84 base::MessageLoopProxy::current().get())); | 113 base::MessageLoopProxy::current().get())); |
85 metadata_storage_.reset(new ResourceMetadataStorage( | 114 metadata_storage_.reset(new ResourceMetadataStorage( |
86 temp_dir_.path(), base::MessageLoopProxy::current().get())); | 115 temp_dir_.path(), base::MessageLoopProxy::current().get())); |
(...skipping 27 matching lines...) Expand all Loading... | |
114 false, // shared_with_me | 143 false, // shared_with_me |
115 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 144 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
116 base::RunLoop().RunUntilIdle(); | 145 base::RunLoop().RunUntilIdle(); |
117 EXPECT_EQ(google_apis::HTTP_CREATED, error); | 146 EXPECT_EQ(google_apis::HTTP_CREATED, error); |
118 return entry.Pass(); | 147 return entry.Pass(); |
119 } | 148 } |
120 | 149 |
121 content::TestBrowserThreadBundle thread_bundle_; | 150 content::TestBrowserThreadBundle thread_bundle_; |
122 base::ScopedTempDir temp_dir_; | 151 base::ScopedTempDir temp_dir_; |
123 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 152 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
124 scoped_ptr<FakeDriveService> drive_service_; | 153 scoped_ptr<TestDriveService> drive_service_; |
125 scoped_ptr<JobScheduler> scheduler_; | 154 scoped_ptr<JobScheduler> scheduler_; |
126 scoped_ptr<ResourceMetadataStorage, | 155 scoped_ptr<ResourceMetadataStorage, |
127 test_util::DestroyHelperForTests> metadata_storage_; | 156 test_util::DestroyHelperForTests> metadata_storage_; |
128 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_; | 157 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_; |
129 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; | 158 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
130 scoped_ptr<ChangeListLoader> change_list_loader_; | 159 scoped_ptr<ChangeListLoader> change_list_loader_; |
131 }; | 160 }; |
132 | 161 |
133 TEST_F(ChangeListLoaderTest, LoadIfNeeded) { | 162 TEST_F(ChangeListLoaderTest, LoadIfNeeded) { |
134 EXPECT_FALSE(change_list_loader_->IsRefreshing()); | 163 EXPECT_FALSE(change_list_loader_->IsRefreshing()); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 EXPECT_EQ(1U, observer.changed_directories().count( | 246 EXPECT_EQ(1U, observer.changed_directories().count( |
218 util::GetDriveMyDriveRootPath())); | 247 util::GetDriveMyDriveRootPath())); |
219 | 248 |
220 base::FilePath file_path = | 249 base::FilePath file_path = |
221 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); | 250 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); |
222 ResourceEntry entry; | 251 ResourceEntry entry; |
223 EXPECT_EQ(FILE_ERROR_OK, | 252 EXPECT_EQ(FILE_ERROR_OK, |
224 metadata_->GetResourceEntryByPath(file_path, &entry)); | 253 metadata_->GetResourceEntryByPath(file_path, &entry)); |
225 } | 254 } |
226 | 255 |
256 TEST_F(ChangeListLoaderTest, LoadIfNeeded_MyDrive) { | |
257 // Emulate the slowness of GetAllResourceList(). | |
258 drive_service_->set_never_return_all_resource_list(true); | |
259 | |
260 // Load grand root. | |
261 FileError error = FILE_ERROR_FAILED; | |
262 change_list_loader_->LoadIfNeeded( | |
263 DirectoryFetchInfo(util::kDriveGrandRootSpecialResourceId, 0), | |
264 google_apis::test_util::CreateCopyResultCallback(&error)); | |
265 base::RunLoop().RunUntilIdle(); | |
266 EXPECT_EQ(FILE_ERROR_OK, error); | |
267 | |
268 // GetAllResourceList() was called. | |
269 EXPECT_EQ(1, drive_service_->blocked_call_count()); | |
270 | |
271 // My Drive is present in the local metadata, but its child is not. | |
272 ResourceEntry entry; | |
273 EXPECT_EQ(FILE_ERROR_OK, | |
274 metadata_->GetResourceEntryByPath(util::GetDriveMyDriveRootPath(), | |
275 &entry)); | |
276 const int mydrive_changestamp = entry.directory_specific_info().changestamp(); | |
kinaba
2013/07/25 08:48:39
changestamp should be int64
hashimoto
2013/07/25 09:32:10
Good catch.
Done.
| |
277 | |
278 base::FilePath file_path = | |
279 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | |
280 EXPECT_EQ(FILE_ERROR_NOT_FOUND, | |
281 metadata_->GetResourceEntryByPath(file_path, &entry)); | |
282 | |
283 // Load My Drive. | |
284 change_list_loader_->LoadIfNeeded( | |
285 DirectoryFetchInfo(drive_service_->GetRootResourceId(), | |
286 mydrive_changestamp), | |
287 google_apis::test_util::CreateCopyResultCallback(&error)); | |
288 base::RunLoop().RunUntilIdle(); | |
289 EXPECT_EQ(FILE_ERROR_OK, error); | |
290 | |
291 // Now the file is present. | |
292 EXPECT_EQ(FILE_ERROR_OK, | |
293 metadata_->GetResourceEntryByPath(file_path, &entry)); | |
294 } | |
295 | |
296 TEST_F(ChangeListLoaderTest, LoadIfNeeded_NewDirectories) { | |
297 // Make local metadata up to date. | |
298 FileError error = FILE_ERROR_FAILED; | |
299 change_list_loader_->LoadIfNeeded( | |
300 DirectoryFetchInfo(), | |
301 google_apis::test_util::CreateCopyResultCallback(&error)); | |
302 base::RunLoop().RunUntilIdle(); | |
303 EXPECT_EQ(FILE_ERROR_OK, error); | |
304 | |
305 // Add a new file. | |
306 scoped_ptr<google_apis::ResourceEntry> file = AddNewFile("New File"); | |
307 ASSERT_TRUE(file); | |
308 | |
309 // Emulate the slowness of GetAllResourceList(). | |
310 drive_service_->set_never_return_all_resource_list(true); | |
311 | |
312 // Enter refreshing state. | |
313 FileError check_for_updates_error = FILE_ERROR_FAILED; | |
314 change_list_loader_->CheckForUpdates( | |
315 google_apis::test_util::CreateCopyResultCallback( | |
316 &check_for_updates_error)); | |
317 EXPECT_TRUE(change_list_loader_->IsRefreshing()); | |
318 | |
319 // Load My Drive. | |
320 change_list_loader_->LoadIfNeeded( | |
321 DirectoryFetchInfo(drive_service_->GetRootResourceId(), | |
322 metadata_->GetLargestChangestamp()), | |
323 google_apis::test_util::CreateCopyResultCallback(&error)); | |
324 base::RunLoop().RunUntilIdle(); | |
325 EXPECT_EQ(FILE_ERROR_OK, error); | |
326 | |
327 // The new file is present in the local metadata. | |
328 base::FilePath file_path = | |
329 util::GetDriveMyDriveRootPath().AppendASCII(file->title()); | |
330 ResourceEntry entry; | |
331 EXPECT_EQ(FILE_ERROR_OK, | |
332 metadata_->GetResourceEntryByPath(file_path, &entry)); | |
333 } | |
334 | |
227 TEST_F(ChangeListLoaderTest, CheckForUpdates) { | 335 TEST_F(ChangeListLoaderTest, CheckForUpdates) { |
228 // CheckForUpdates() results in no-op before load. | 336 // CheckForUpdates() results in no-op before load. |
229 FileError check_for_updates_error = FILE_ERROR_FAILED; | 337 FileError check_for_updates_error = FILE_ERROR_FAILED; |
230 change_list_loader_->CheckForUpdates( | 338 change_list_loader_->CheckForUpdates( |
231 google_apis::test_util::CreateCopyResultCallback( | 339 google_apis::test_util::CreateCopyResultCallback( |
232 &check_for_updates_error)); | 340 &check_for_updates_error)); |
233 EXPECT_FALSE(change_list_loader_->IsRefreshing()); | 341 EXPECT_FALSE(change_list_loader_->IsRefreshing()); |
234 base::RunLoop().RunUntilIdle(); | 342 base::RunLoop().RunUntilIdle(); |
235 EXPECT_EQ(FILE_ERROR_FAILED, | 343 EXPECT_EQ(FILE_ERROR_FAILED, |
236 check_for_updates_error); // Callback was not run. | 344 check_for_updates_error); // Callback was not run. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 // The new file is found in the local metadata. | 394 // The new file is found in the local metadata. |
287 base::FilePath new_file_path = | 395 base::FilePath new_file_path = |
288 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); | 396 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); |
289 ResourceEntry entry; | 397 ResourceEntry entry; |
290 EXPECT_EQ(FILE_ERROR_OK, | 398 EXPECT_EQ(FILE_ERROR_OK, |
291 metadata_->GetResourceEntryByPath(new_file_path, &entry)); | 399 metadata_->GetResourceEntryByPath(new_file_path, &entry)); |
292 } | 400 } |
293 | 401 |
294 } // namespace internal | 402 } // namespace internal |
295 } // namespace drive | 403 } // namespace drive |
OLD | NEW |