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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "chrome/browser/sync_file_system/local_file_sync_service.h" | 12 #include "chrome/browser/sync_file_system/local_file_sync_service.h" |
13 #include "chrome/browser/sync_file_system/mock_remote_file_sync_service.h" | 13 #include "chrome/browser/sync_file_system/mock_remote_file_sync_service.h" |
14 #include "chrome/browser/sync_file_system/sync_event_observer.h" | 14 #include "chrome/browser/sync_file_system/sync_event_observer.h" |
15 #include "chrome/browser/sync_file_system/sync_file_system_service.h" | 15 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
16 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" | 16 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" |
17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "webkit/browser/fileapi/file_system_context.h" | 21 #include "webkit/browser/fileapi/file_system_context.h" |
20 #include "webkit/browser/fileapi/syncable/canned_syncable_file_system.h" | 22 #include "webkit/browser/fileapi/syncable/canned_syncable_file_system.h" |
21 #include "webkit/browser/fileapi/syncable/local_file_sync_context.h" | 23 #include "webkit/browser/fileapi/syncable/local_file_sync_context.h" |
22 #include "webkit/browser/fileapi/syncable/mock_sync_status_observer.h" | 24 #include "webkit/browser/fileapi/syncable/mock_sync_status_observer.h" |
23 #include "webkit/browser/fileapi/syncable/sync_callbacks.h" | 25 #include "webkit/browser/fileapi/syncable/sync_callbacks.h" |
24 #include "webkit/browser/fileapi/syncable/sync_file_metadata.h" | 26 #include "webkit/browser/fileapi/syncable/sync_file_metadata.h" |
25 #include "webkit/browser/fileapi/syncable/sync_status_code.h" | 27 #include "webkit/browser/fileapi/syncable/sync_status_code.h" |
26 #include "webkit/browser/fileapi/syncable/syncable_file_system_util.h" | 28 #include "webkit/browser/fileapi/syncable/syncable_file_system_util.h" |
27 | 29 |
| 30 using content::BrowserThread; |
28 using fileapi::FileSystemURL; | 31 using fileapi::FileSystemURL; |
29 using fileapi::FileSystemURLSet; | 32 using fileapi::FileSystemURLSet; |
30 using ::testing::AnyNumber; | 33 using ::testing::AnyNumber; |
31 using ::testing::AtLeast; | 34 using ::testing::AtLeast; |
32 using ::testing::InSequence; | 35 using ::testing::InSequence; |
33 using ::testing::InvokeWithoutArgs; | 36 using ::testing::InvokeWithoutArgs; |
34 using ::testing::Return; | 37 using ::testing::Return; |
35 using ::testing::StrictMock; | 38 using ::testing::StrictMock; |
36 using ::testing::_; | 39 using ::testing::_; |
37 | 40 |
(...skipping 18 matching lines...) Expand all Loading... |
56 typename AssignTrait<R>::ArgumentType value) { | 59 typename AssignTrait<R>::ArgumentType value) { |
57 DCHECK(status_out); | 60 DCHECK(status_out); |
58 DCHECK(value_out); | 61 DCHECK(value_out); |
59 DCHECK(run_loop); | 62 DCHECK(run_loop); |
60 *status_out = status; | 63 *status_out = status; |
61 *value_out = value; | 64 *value_out = value; |
62 run_loop->Quit(); | 65 run_loop->Quit(); |
63 } | 66 } |
64 | 67 |
65 // This is called on IO thread. | 68 // This is called on IO thread. |
66 void VerifyFileError(base::WaitableEvent* event, | 69 void VerifyFileError(base::RunLoop* run_loop, |
67 base::PlatformFileError error) { | 70 base::PlatformFileError error) { |
68 DCHECK(event); | 71 DCHECK(run_loop); |
69 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | 72 EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
70 event->Signal(); | 73 run_loop->Quit(); |
71 } | 74 } |
72 | 75 |
73 } // namespace | 76 } // namespace |
74 | 77 |
75 class MockSyncEventObserver : public SyncEventObserver { | 78 class MockSyncEventObserver : public SyncEventObserver { |
76 public: | 79 public: |
77 MockSyncEventObserver() {} | 80 MockSyncEventObserver() {} |
78 virtual ~MockSyncEventObserver() {} | 81 virtual ~MockSyncEventObserver() {} |
79 | 82 |
80 MOCK_METHOD3(OnSyncStateUpdated, | 83 MOCK_METHOD3(OnSyncStateUpdated, |
(...skipping 28 matching lines...) Expand all Loading... |
109 base::MessageLoopProxy::current()->PostTask( | 112 base::MessageLoopProxy::current()->PostTask( |
110 FROM_HERE, base::Bind(arg0, status, url)); | 113 FROM_HERE, base::Bind(arg0, status, url)); |
111 } | 114 } |
112 | 115 |
113 class SyncFileSystemServiceTest : public testing::Test { | 116 class SyncFileSystemServiceTest : public testing::Test { |
114 protected: | 117 protected: |
115 SyncFileSystemServiceTest() {} | 118 SyncFileSystemServiceTest() {} |
116 virtual ~SyncFileSystemServiceTest() {} | 119 virtual ~SyncFileSystemServiceTest() {} |
117 | 120 |
118 virtual void SetUp() OVERRIDE { | 121 virtual void SetUp() OVERRIDE { |
119 thread_helper_.SetUp(); | |
120 | |
121 file_system_.reset(new CannedSyncableFileSystem( | 122 file_system_.reset(new CannedSyncableFileSystem( |
122 GURL(kOrigin), | 123 GURL(kOrigin), |
123 thread_helper_.io_task_runner(), | 124 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
124 thread_helper_.file_task_runner())); | 125 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); |
125 | 126 |
126 local_service_ = new LocalFileSyncService(&profile_); | 127 local_service_ = new LocalFileSyncService(&profile_); |
127 remote_service_ = new StrictMock<MockRemoteFileSyncService>; | 128 remote_service_ = new StrictMock<MockRemoteFileSyncService>; |
128 sync_service_.reset(new SyncFileSystemService(&profile_)); | 129 sync_service_.reset(new SyncFileSystemService(&profile_)); |
129 | 130 |
130 EXPECT_CALL(*mock_remote_service(), | 131 EXPECT_CALL(*mock_remote_service(), |
131 AddServiceObserver(sync_service_.get())).Times(1); | 132 AddServiceObserver(sync_service_.get())).Times(1); |
132 EXPECT_CALL(*mock_remote_service(), | 133 EXPECT_CALL(*mock_remote_service(), |
133 AddFileStatusObserver(sync_service_.get())).Times(1); | 134 AddFileStatusObserver(sync_service_.get())).Times(1); |
134 EXPECT_CALL(*mock_remote_service(), | 135 EXPECT_CALL(*mock_remote_service(), |
(...skipping 11 matching lines...) Expand all Loading... |
146 sync_service_->SetSyncEnabledForTesting(false); | 147 sync_service_->SetSyncEnabledForTesting(false); |
147 | 148 |
148 file_system_->SetUp(); | 149 file_system_->SetUp(); |
149 } | 150 } |
150 | 151 |
151 virtual void TearDown() OVERRIDE { | 152 virtual void TearDown() OVERRIDE { |
152 sync_service_->Shutdown(); | 153 sync_service_->Shutdown(); |
153 | 154 |
154 file_system_->TearDown(); | 155 file_system_->TearDown(); |
155 RevokeSyncableFileSystem(); | 156 RevokeSyncableFileSystem(); |
156 thread_helper_.TearDown(); | |
157 } | 157 } |
158 | 158 |
159 void InitializeApp() { | 159 void InitializeApp() { |
160 base::RunLoop run_loop; | 160 base::RunLoop run_loop; |
161 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 161 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
162 | 162 |
163 EXPECT_CALL(*mock_remote_service(), | 163 EXPECT_CALL(*mock_remote_service(), |
164 RegisterOriginForTrackingChanges(GURL(kOrigin), _)).Times(1); | 164 RegisterOriginForTrackingChanges(GURL(kOrigin), _)).Times(1); |
165 | 165 |
166 sync_service_->InitializeForApp( | 166 sync_service_->InitializeForApp( |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 return &local_change_processor_; | 230 return &local_change_processor_; |
231 } | 231 } |
232 | 232 |
233 void EnableSync() { | 233 void EnableSync() { |
234 EXPECT_CALL(*mock_remote_service(), SetSyncEnabled(true)).Times(1); | 234 EXPECT_CALL(*mock_remote_service(), SetSyncEnabled(true)).Times(1); |
235 sync_service_->SetSyncEnabledForTesting(true); | 235 sync_service_->SetSyncEnabledForTesting(true); |
236 } | 236 } |
237 | 237 |
238 ScopedEnableSyncFSDirectoryOperation enable_directory_operation_; | 238 ScopedEnableSyncFSDirectoryOperation enable_directory_operation_; |
239 | 239 |
240 MultiThreadTestHelper thread_helper_; | 240 content::TestBrowserThreadBundle thread_bundle_; |
241 TestingProfile profile_; | 241 TestingProfile profile_; |
242 scoped_ptr<CannedSyncableFileSystem> file_system_; | 242 scoped_ptr<CannedSyncableFileSystem> file_system_; |
243 | 243 |
244 // Their ownerships are transferred to SyncFileSystemService. | 244 // Their ownerships are transferred to SyncFileSystemService. |
245 LocalFileSyncService* local_service_; | 245 LocalFileSyncService* local_service_; |
246 StrictMock<MockRemoteFileSyncService>* remote_service_; | 246 StrictMock<MockRemoteFileSyncService>* remote_service_; |
247 StrictMock<MockLocalChangeProcessor> local_change_processor_; | 247 StrictMock<MockLocalChangeProcessor> local_change_processor_; |
248 | 248 |
249 scoped_ptr<SyncFileSystemService> sync_service_; | 249 scoped_ptr<SyncFileSystemService> sync_service_; |
250 }; | 250 }; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 // We might also see an activity for local sync as we're going to make | 385 // We might also see an activity for local sync as we're going to make |
386 // a local write operation on kFile. | 386 // a local write operation on kFile. |
387 EXPECT_CALL(*mock_local_change_processor(), | 387 EXPECT_CALL(*mock_local_change_processor(), |
388 ApplyLocalChange(_, _, _, kFile, _)) | 388 ApplyLocalChange(_, _, _, kFile, _)) |
389 .Times(AnyNumber()); | 389 .Times(AnyNumber()); |
390 | 390 |
391 // This should trigger a remote sync. | 391 // This should trigger a remote sync. |
392 mock_remote_service()->NotifyRemoteChangeQueueUpdated(1); | 392 mock_remote_service()->NotifyRemoteChangeQueueUpdated(1); |
393 | 393 |
394 // Start a local operation on the same file (to make it BUSY). | 394 // Start a local operation on the same file (to make it BUSY). |
395 base::WaitableEvent event(false, false); | 395 base::RunLoop verify_file_error_run_loop; |
396 thread_helper_.io_task_runner()->PostTask( | 396 BrowserThread::PostTask( |
397 FROM_HERE, base::Bind(&CannedSyncableFileSystem::DoCreateFile, | 397 BrowserThread::IO, |
398 base::Unretained(file_system_.get()), | 398 FROM_HERE, |
399 kFile, base::Bind(&VerifyFileError, &event))); | 399 base::Bind(&CannedSyncableFileSystem::DoCreateFile, |
| 400 base::Unretained(file_system_.get()), |
| 401 kFile, base::Bind(&VerifyFileError, |
| 402 &verify_file_error_run_loop))); |
400 | 403 |
401 run_loop.Run(); | 404 run_loop.Run(); |
402 | 405 |
403 mock_remote_service()->NotifyRemoteChangeQueueUpdated(0); | 406 mock_remote_service()->NotifyRemoteChangeQueueUpdated(0); |
404 | 407 |
405 event.Wait(); | 408 verify_file_error_run_loop.Run(); |
406 } | 409 } |
407 | 410 |
408 TEST_F(SyncFileSystemServiceTest, GetFileSyncStatus) { | 411 TEST_F(SyncFileSystemServiceTest, GetFileSyncStatus) { |
409 InitializeApp(); | 412 InitializeApp(); |
410 | 413 |
411 const FileSystemURL kFile(file_system_->URL("foo")); | 414 const FileSystemURL kFile(file_system_->URL("foo")); |
412 | 415 |
413 SyncStatusCode status; | 416 SyncStatusCode status; |
414 SyncFileStatus sync_file_status; | 417 SyncFileStatus sync_file_status; |
415 | 418 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 base::Bind(&AssignValueAndQuit<SyncFileStatus>, | 488 base::Bind(&AssignValueAndQuit<SyncFileStatus>, |
486 &run_loop, &status, &sync_file_status)); | 489 &run_loop, &status, &sync_file_status)); |
487 run_loop.Run(); | 490 run_loop.Run(); |
488 | 491 |
489 EXPECT_EQ(SYNC_STATUS_OK, status); | 492 EXPECT_EQ(SYNC_STATUS_OK, status); |
490 EXPECT_EQ(SYNC_FILE_STATUS_CONFLICTING, sync_file_status); | 493 EXPECT_EQ(SYNC_FILE_STATUS_CONFLICTING, sync_file_status); |
491 } | 494 } |
492 } | 495 } |
493 | 496 |
494 } // namespace sync_file_system | 497 } // namespace sync_file_system |
OLD | NEW |