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/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "chrome/browser/chromeos/drive/change_list_loader_observer.h" | 12 #include "chrome/browser/chromeos/drive/change_list_loader_observer.h" |
13 #include "chrome/browser/chromeos/drive/file_cache.h" | 13 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 14 #include "chrome/browser/chromeos/drive/file_change.h" |
14 #include "chrome/browser/chromeos/drive/file_system_util.h" | 15 #include "chrome/browser/chromeos/drive/file_system_util.h" |
15 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 16 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
16 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 17 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
17 #include "chrome/browser/chromeos/drive/test_util.h" | 18 #include "chrome/browser/chromeos/drive/test_util.h" |
18 #include "chrome/browser/drive/event_logger.h" | 19 #include "chrome/browser/drive/event_logger.h" |
19 #include "chrome/browser/drive/fake_drive_service.h" | 20 #include "chrome/browser/drive/fake_drive_service.h" |
20 #include "chrome/browser/drive/test_util.h" | 21 #include "chrome/browser/drive/test_util.h" |
21 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
22 #include "google_apis/drive/drive_api_parser.h" | 23 #include "google_apis/drive/drive_api_parser.h" |
23 #include "google_apis/drive/test_util.h" | 24 #include "google_apis/drive/test_util.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 namespace drive { | 27 namespace drive { |
27 namespace internal { | 28 namespace internal { |
28 | 29 |
29 class TestChangeListLoaderObserver : public ChangeListLoaderObserver { | 30 class TestChangeListLoaderObserver : public ChangeListLoaderObserver { |
30 public: | 31 public: |
31 explicit TestChangeListLoaderObserver(ChangeListLoader* loader) | 32 explicit TestChangeListLoaderObserver(ChangeListLoader* loader) |
32 : loader_(loader), | 33 : loader_(loader), |
33 load_from_server_complete_count_(0), | 34 load_from_server_complete_count_(0), |
34 initial_load_complete_count_(0) { | 35 initial_load_complete_count_(0) { |
35 loader_->AddObserver(this); | 36 loader_->AddObserver(this); |
36 } | 37 } |
37 | 38 |
38 virtual ~TestChangeListLoaderObserver() { | 39 virtual ~TestChangeListLoaderObserver() { |
39 loader_->RemoveObserver(this); | 40 loader_->RemoveObserver(this); |
40 } | 41 } |
41 | 42 |
42 const std::set<base::FilePath>& changed_directories() const { | 43 const FileChange& changed_files() const { return changed_files_; } |
43 return changed_directories_; | 44 void clear_changed_files() { changed_files_.ClearForTest(); } |
44 } | |
45 void clear_changed_directories() { changed_directories_.clear(); } | |
46 | 45 |
47 int load_from_server_complete_count() const { | 46 int load_from_server_complete_count() const { |
48 return load_from_server_complete_count_; | 47 return load_from_server_complete_count_; |
49 } | 48 } |
50 int initial_load_complete_count() const { | 49 int initial_load_complete_count() const { |
51 return initial_load_complete_count_; | 50 return initial_load_complete_count_; |
52 } | 51 } |
53 | 52 |
54 // ChageListObserver overrides: | 53 // ChageListObserver overrides: |
55 virtual void OnDirectoryChanged( | 54 virtual void OnFileChanged(const FileChange& changed_files) OVERRIDE { |
56 const base::FilePath& directory_path) OVERRIDE { | 55 changed_files_.Apply(changed_files); |
57 changed_directories_.insert(directory_path); | |
58 } | 56 } |
59 virtual void OnLoadFromServerComplete() OVERRIDE { | 57 virtual void OnLoadFromServerComplete() OVERRIDE { |
60 ++load_from_server_complete_count_; | 58 ++load_from_server_complete_count_; |
61 } | 59 } |
62 virtual void OnInitialLoadComplete() OVERRIDE { | 60 virtual void OnInitialLoadComplete() OVERRIDE { |
63 ++initial_load_complete_count_; | 61 ++initial_load_complete_count_; |
64 } | 62 } |
65 | 63 |
66 private: | 64 private: |
67 ChangeListLoader* loader_; | 65 ChangeListLoader* loader_; |
68 std::set<base::FilePath> changed_directories_; | 66 FileChange changed_files_; |
69 int load_from_server_complete_count_; | 67 int load_from_server_complete_count_; |
70 int initial_load_complete_count_; | 68 int initial_load_complete_count_; |
71 | 69 |
72 DISALLOW_COPY_AND_ASSIGN(TestChangeListLoaderObserver); | 70 DISALLOW_COPY_AND_ASSIGN(TestChangeListLoaderObserver); |
73 }; | 71 }; |
74 | 72 |
75 class ChangeListLoaderTest : public testing::Test { | 73 class ChangeListLoaderTest : public testing::Test { |
76 protected: | 74 protected: |
77 virtual void SetUp() OVERRIDE { | 75 virtual void SetUp() OVERRIDE { |
78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 EXPECT_EQ(FILE_ERROR_OK, error); | 159 EXPECT_EQ(FILE_ERROR_OK, error); |
162 | 160 |
163 EXPECT_FALSE(change_list_loader_->IsRefreshing()); | 161 EXPECT_FALSE(change_list_loader_->IsRefreshing()); |
164 int64 changestamp = 0; | 162 int64 changestamp = 0; |
165 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp)); | 163 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp)); |
166 EXPECT_LT(0, changestamp); | 164 EXPECT_LT(0, changestamp); |
167 EXPECT_EQ(1, drive_service_->file_list_load_count()); | 165 EXPECT_EQ(1, drive_service_->file_list_load_count()); |
168 EXPECT_EQ(1, drive_service_->about_resource_load_count()); | 166 EXPECT_EQ(1, drive_service_->about_resource_load_count()); |
169 EXPECT_EQ(1, observer.initial_load_complete_count()); | 167 EXPECT_EQ(1, observer.initial_load_complete_count()); |
170 EXPECT_EQ(1, observer.load_from_server_complete_count()); | 168 EXPECT_EQ(1, observer.load_from_server_complete_count()); |
171 EXPECT_TRUE(observer.changed_directories().empty()); | 169 EXPECT_TRUE(observer.changed_files().empty()); |
172 | 170 |
173 base::FilePath file_path = | 171 base::FilePath file_path = |
174 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 172 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
175 ResourceEntry entry; | 173 ResourceEntry entry; |
176 EXPECT_EQ(FILE_ERROR_OK, | 174 EXPECT_EQ(FILE_ERROR_OK, |
177 metadata_->GetResourceEntryByPath(file_path, &entry)); | 175 metadata_->GetResourceEntryByPath(file_path, &entry)); |
178 } | 176 } |
179 | 177 |
180 TEST_F(ChangeListLoaderTest, Load_LocalMetadataAvailable) { | 178 TEST_F(ChangeListLoaderTest, Load_LocalMetadataAvailable) { |
181 // Prepare metadata. | 179 // Prepare metadata. |
(...skipping 30 matching lines...) Expand all Loading... |
212 EXPECT_EQ(previous_file_list_load_count, | 210 EXPECT_EQ(previous_file_list_load_count, |
213 drive_service_->file_list_load_count()); | 211 drive_service_->file_list_load_count()); |
214 EXPECT_EQ(1, observer.initial_load_complete_count()); | 212 EXPECT_EQ(1, observer.initial_load_complete_count()); |
215 | 213 |
216 // Update should be checked by Load(). | 214 // Update should be checked by Load(). |
217 int64 changestamp = 0; | 215 int64 changestamp = 0; |
218 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp)); | 216 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp)); |
219 EXPECT_EQ(drive_service_->about_resource().largest_change_id(), changestamp); | 217 EXPECT_EQ(drive_service_->about_resource().largest_change_id(), changestamp); |
220 EXPECT_EQ(1, drive_service_->change_list_load_count()); | 218 EXPECT_EQ(1, drive_service_->change_list_load_count()); |
221 EXPECT_EQ(1, observer.load_from_server_complete_count()); | 219 EXPECT_EQ(1, observer.load_from_server_complete_count()); |
222 EXPECT_EQ(1U, observer.changed_directories().count( | 220 EXPECT_TRUE( |
223 util::GetDriveMyDriveRootPath())); | 221 observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath())); |
224 | 222 |
225 base::FilePath file_path = | 223 base::FilePath file_path = |
226 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); | 224 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); |
227 ResourceEntry entry; | 225 ResourceEntry entry; |
228 EXPECT_EQ(FILE_ERROR_OK, | 226 EXPECT_EQ(FILE_ERROR_OK, |
229 metadata_->GetResourceEntryByPath(file_path, &entry)); | 227 metadata_->GetResourceEntryByPath(file_path, &entry)); |
230 } | 228 } |
231 | 229 |
232 TEST_F(ChangeListLoaderTest, CheckForUpdates) { | 230 TEST_F(ChangeListLoaderTest, CheckForUpdates) { |
233 // CheckForUpdates() results in no-op before load. | 231 // CheckForUpdates() results in no-op before load. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 TestChangeListLoaderObserver observer(change_list_loader_.get()); | 283 TestChangeListLoaderObserver observer(change_list_loader_.get()); |
286 change_list_loader_->CheckForUpdates( | 284 change_list_loader_->CheckForUpdates( |
287 google_apis::test_util::CreateCopyResultCallback( | 285 google_apis::test_util::CreateCopyResultCallback( |
288 &check_for_updates_error)); | 286 &check_for_updates_error)); |
289 EXPECT_TRUE(change_list_loader_->IsRefreshing()); | 287 EXPECT_TRUE(change_list_loader_->IsRefreshing()); |
290 base::RunLoop().RunUntilIdle(); | 288 base::RunLoop().RunUntilIdle(); |
291 EXPECT_FALSE(change_list_loader_->IsRefreshing()); | 289 EXPECT_FALSE(change_list_loader_->IsRefreshing()); |
292 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp)); | 290 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp)); |
293 EXPECT_LT(previous_changestamp, changestamp); | 291 EXPECT_LT(previous_changestamp, changestamp); |
294 EXPECT_EQ(1, observer.load_from_server_complete_count()); | 292 EXPECT_EQ(1, observer.load_from_server_complete_count()); |
295 EXPECT_EQ(1U, observer.changed_directories().count( | 293 EXPECT_TRUE( |
296 util::GetDriveMyDriveRootPath())); | 294 observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath())); |
297 | 295 |
298 // The new file is found in the local metadata. | 296 // The new file is found in the local metadata. |
299 base::FilePath new_file_path = | 297 base::FilePath new_file_path = |
300 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); | 298 util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title()); |
301 ResourceEntry entry; | 299 ResourceEntry entry; |
302 EXPECT_EQ(FILE_ERROR_OK, | 300 EXPECT_EQ(FILE_ERROR_OK, |
303 metadata_->GetResourceEntryByPath(new_file_path, &entry)); | 301 metadata_->GetResourceEntryByPath(new_file_path, &entry)); |
304 } | 302 } |
305 | 303 |
306 TEST_F(ChangeListLoaderTest, Lock) { | 304 TEST_F(ChangeListLoaderTest, Lock) { |
(...skipping 12 matching lines...) Expand all Loading... |
319 | 317 |
320 // Start update. | 318 // Start update. |
321 TestChangeListLoaderObserver observer(change_list_loader_.get()); | 319 TestChangeListLoaderObserver observer(change_list_loader_.get()); |
322 FileError check_for_updates_error = FILE_ERROR_FAILED; | 320 FileError check_for_updates_error = FILE_ERROR_FAILED; |
323 change_list_loader_->CheckForUpdates( | 321 change_list_loader_->CheckForUpdates( |
324 google_apis::test_util::CreateCopyResultCallback( | 322 google_apis::test_util::CreateCopyResultCallback( |
325 &check_for_updates_error)); | 323 &check_for_updates_error)); |
326 base::RunLoop().RunUntilIdle(); | 324 base::RunLoop().RunUntilIdle(); |
327 | 325 |
328 // Update is pending due to the lock. | 326 // Update is pending due to the lock. |
329 EXPECT_TRUE(observer.changed_directories().empty()); | 327 EXPECT_TRUE(observer.changed_files().empty()); |
330 | 328 |
331 // Unlock the loader, this should resume the pending udpate. | 329 // Unlock the loader, this should resume the pending udpate. |
332 lock.reset(); | 330 lock.reset(); |
333 base::RunLoop().RunUntilIdle(); | 331 base::RunLoop().RunUntilIdle(); |
334 EXPECT_EQ(1U, observer.changed_directories().count( | 332 EXPECT_TRUE( |
335 util::GetDriveMyDriveRootPath())); | 333 observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath())); |
336 } | 334 } |
337 | 335 |
338 } // namespace internal | 336 } // namespace internal |
339 } // namespace drive | 337 } // namespace drive |
OLD | NEW |