Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(795)

Side by Side Diff: chrome/browser/sync_file_system/drive_file_sync_service_unittest.cc

Issue 11299008: Remote service state handling (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/sync_file_system/drive_file_sync_service.h" 5 #include "chrome/browser/sync_file_system/drive_file_sync_service.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/message_loop_proxy.h" 8 #include "base/message_loop_proxy.h"
9 #include "chrome/browser/google_apis/gdata_errorcode.h" 9 #include "chrome/browser/google_apis/gdata_errorcode.h"
10 #include "chrome/browser/google_apis/mock_drive_service.h" 10 #include "chrome/browser/google_apis/mock_drive_service.h"
11 #include "chrome/browser/google_apis/mock_drive_uploader.h" 11 #include "chrome/browser/google_apis/mock_drive_uploader.h"
12 #include "chrome/browser/google_apis/test_util.h" 12 #include "chrome/browser/google_apis/test_util.h"
13 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" 13 #include "chrome/browser/sync_file_system/drive_file_sync_client.h"
14 #include "chrome/browser/sync_file_system/drive_metadata_store.h" 14 #include "chrome/browser/sync_file_system/drive_metadata_store.h"
15 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
16 #include "net/base/escape.h" 16 #include "net/base/escape.h"
17 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webkit/fileapi/syncable/syncable_file_system_util.h" 19 #include "webkit/fileapi/syncable/syncable_file_system_util.h"
20 20
21 using ::testing::StrictMock; 21 using ::testing::StrictMock;
22 using ::testing::InSequence; 22 using ::testing::InSequence;
23 using ::testing::AtLeast;
tzik 2012/11/16 05:21:51 sort?
kinuko 2012/11/16 05:45:02 Done.
23 using ::testing::_; 24 using ::testing::_;
24 25
25 using google_apis::DriveServiceInterface; 26 using google_apis::DriveServiceInterface;
26 using google_apis::DriveUploaderInterface; 27 using google_apis::DriveUploaderInterface;
27 using google_apis::test_util::LoadJSONFile; 28 using google_apis::test_util::LoadJSONFile;
28 29
29 namespace sync_file_system { 30 namespace sync_file_system {
30 31
31 namespace { 32 namespace {
32 33
(...skipping 13 matching lines...) Expand all
46 void ExpectEqStatus(bool* done, 47 void ExpectEqStatus(bool* done,
47 fileapi::SyncStatusCode expected, 48 fileapi::SyncStatusCode expected,
48 fileapi::SyncStatusCode actual) { 49 fileapi::SyncStatusCode actual) {
49 EXPECT_FALSE(*done); 50 EXPECT_FALSE(*done);
50 *done = true; 51 *done = true;
51 EXPECT_EQ(expected, actual); 52 EXPECT_EQ(expected, actual);
52 } 53 }
53 54
54 } // namespace 55 } // namespace
55 56
57 class MockRemoteServiceObserver : public RemoteFileSyncService::Observer {
58 public:
59 MockRemoteServiceObserver() {}
60 virtual ~MockRemoteServiceObserver() {}
61
62 // LocalChangeProcessor override.
63 MOCK_METHOD1(OnRemoteChangeAvailable,
64 void(int64 pending_changes));
65 MOCK_METHOD2(OnRemoteServiceStateUpdated,
66 void(RemoteServiceState state,
67 const std::string& description));
68 };
69
56 class DriveFileSyncServiceTest : public testing::Test { 70 class DriveFileSyncServiceTest : public testing::Test {
57 public: 71 public:
58 DriveFileSyncServiceTest() 72 DriveFileSyncServiceTest()
59 : mock_drive_service_(NULL), 73 : mock_drive_service_(NULL),
60 mock_drive_uploader_(NULL) { 74 mock_drive_uploader_(NULL) {
61 } 75 }
62 76
63 virtual void SetUp() OVERRIDE { 77 virtual void SetUp() OVERRIDE {
64 ASSERT_TRUE(fileapi::RegisterSyncableFileSystem( 78 ASSERT_TRUE(fileapi::RegisterSyncableFileSystem(
65 DriveFileSyncService::kServiceName)); 79 DriveFileSyncService::kServiceName));
(...skipping 14 matching lines...) Expand all
80 94
81 bool done = false; 95 bool done = false;
82 metadata_store_->Initialize(base::Bind(&DidInitialize, &done)); 96 metadata_store_->Initialize(base::Bind(&DidInitialize, &done));
83 message_loop_.RunUntilIdle(); 97 message_loop_.RunUntilIdle();
84 EXPECT_TRUE(done); 98 EXPECT_TRUE(done);
85 } 99 }
86 100
87 void SetUpDriveSyncService() { 101 void SetUpDriveSyncService() {
88 sync_service_ = DriveFileSyncService::CreateForTesting( 102 sync_service_ = DriveFileSyncService::CreateForTesting(
89 sync_client_.Pass(), metadata_store_.Pass()).Pass(); 103 sync_client_.Pass(), metadata_store_.Pass()).Pass();
104 sync_service_->AddObserver(&mock_remote_observer_);
90 } 105 }
91 106
92 virtual void TearDown() OVERRIDE { 107 virtual void TearDown() OVERRIDE {
93 if (sync_service_) { 108 if (sync_service_) {
94 sync_service_.reset(); 109 sync_service_.reset();
95 } 110 }
96 111
97 metadata_store_.reset(); 112 metadata_store_.reset();
98 sync_client_.reset(); 113 sync_client_.reset();
99 mock_drive_uploader_ = NULL; 114 mock_drive_uploader_ = NULL;
(...skipping 18 matching lines...) Expand all
118 } 133 }
119 134
120 StrictMock<google_apis::MockDriveService>* mock_drive_service() { 135 StrictMock<google_apis::MockDriveService>* mock_drive_service() {
121 return mock_drive_service_; 136 return mock_drive_service_;
122 } 137 }
123 138
124 StrictMock<google_apis::MockDriveUploader>* mock_drive_uploader() { 139 StrictMock<google_apis::MockDriveUploader>* mock_drive_uploader() {
125 return mock_drive_uploader_; 140 return mock_drive_uploader_;
126 } 141 }
127 142
143 StrictMock<MockRemoteServiceObserver>* mock_remote_observer() {
144 return &mock_remote_observer_;
145 }
146
128 MessageLoop* message_loop() { return &message_loop_; } 147 MessageLoop* message_loop() { return &message_loop_; }
129 DriveFileSyncService* sync_service() { return sync_service_.get(); } 148 DriveFileSyncService* sync_service() { return sync_service_.get(); }
130 149
131 std::string FormatTitleQuery(const std::string& title) { 150 std::string FormatTitleQuery(const std::string& title) {
132 return DriveFileSyncClient::FormatTitleQuery(title); 151 return DriveFileSyncClient::FormatTitleQuery(title);
133 } 152 }
134 153
135 const DriveFileSyncService::ChangeQueue& pending_changes() const { 154 const DriveFileSyncService::ChangeQueue& pending_changes() const {
136 return sync_service_->pending_changes_; 155 return sync_service_->pending_changes_;
137 } 156 }
138 157
139 private: 158 private:
140 MessageLoop message_loop_; 159 MessageLoop message_loop_;
141 160
142 ScopedTempDir base_dir_; 161 ScopedTempDir base_dir_;
143 TestingProfile profile_; 162 TestingProfile profile_;
144 163
145 scoped_ptr<DriveFileSyncService> sync_service_; 164 scoped_ptr<DriveFileSyncService> sync_service_;
146 165
147 // Owned by |sync_client_|. 166 // Owned by |sync_client_|.
148 StrictMock<google_apis::MockDriveService>* mock_drive_service_; 167 StrictMock<google_apis::MockDriveService>* mock_drive_service_;
149 StrictMock<google_apis::MockDriveUploader>* mock_drive_uploader_; 168 StrictMock<google_apis::MockDriveUploader>* mock_drive_uploader_;
169 StrictMock<MockRemoteServiceObserver> mock_remote_observer_;
150 170
151 scoped_ptr<DriveFileSyncClient> sync_client_; 171 scoped_ptr<DriveFileSyncClient> sync_client_;
152 scoped_ptr<DriveMetadataStore> metadata_store_; 172 scoped_ptr<DriveMetadataStore> metadata_store_;
153 173
154 DISALLOW_COPY_AND_ASSIGN(DriveFileSyncServiceTest); 174 DISALLOW_COPY_AND_ASSIGN(DriveFileSyncServiceTest);
155 }; 175 };
156 176
157 // Invokes |arg0| as a GetDataCallback. 177 // Invokes |arg0| as a GetDataCallback.
158 ACTION_P2(InvokeGetDataCallback0, error, result) { 178 ACTION_P2(InvokeGetDataCallback0, error, result) {
159 scoped_ptr<base::Value> value(result.Pass()); 179 scoped_ptr<base::Value> value(result.Pass());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 TEST_F(DriveFileSyncServiceTest, GetSyncRoot) { 211 TEST_F(DriveFileSyncServiceTest, GetSyncRoot) {
192 scoped_ptr<Value> sync_root_found(LoadJSONFile( 212 scoped_ptr<Value> sync_root_found(LoadJSONFile(
193 "sync_file_system/sync_root_found.json")); 213 "sync_file_system/sync_root_found.json"));
194 std::string query = FormatTitleQuery(kSyncRootDirectoryName); 214 std::string query = FormatTitleQuery(kSyncRootDirectoryName);
195 EXPECT_CALL(*mock_drive_service(), 215 EXPECT_CALL(*mock_drive_service(),
196 GetDocuments(GURL(), 0, query, std::string(), _)) 216 GetDocuments(GURL(), 0, query, std::string(), _))
197 .WillOnce(InvokeGetDataCallback4( 217 .WillOnce(InvokeGetDataCallback4(
198 google_apis::HTTP_SUCCESS, 218 google_apis::HTTP_SUCCESS,
199 base::Passed(&sync_root_found))); 219 base::Passed(&sync_root_found)));
200 220
221 EXPECT_CALL(*mock_remote_observer(),
222 OnRemoteServiceStateUpdated(REMOTE_SERVICE_OK, _))
223 .Times(1);
224
201 SetUpDriveSyncService(); 225 SetUpDriveSyncService();
202 message_loop()->RunUntilIdle(); 226 message_loop()->RunUntilIdle();
203 227
204 EXPECT_EQ("folder:sync_root_resource_id", 228 EXPECT_EQ("folder:sync_root_resource_id",
205 metadata_store()->sync_root_directory()); 229 metadata_store()->sync_root_directory());
206 230
207 EXPECT_EQ(0u, metadata_store()->batch_sync_origins().size()); 231 EXPECT_EQ(0u, metadata_store()->batch_sync_origins().size());
208 EXPECT_EQ(0u, metadata_store()->incremental_sync_origins().size()); 232 EXPECT_EQ(0u, metadata_store()->incremental_sync_origins().size());
209 EXPECT_EQ(0u, pending_changes().size()); 233 EXPECT_EQ(0u, pending_changes().size());
210 } 234 }
(...skipping 18 matching lines...) Expand all
229 "gdata/account_metadata.json")); 253 "gdata/account_metadata.json"));
230 EXPECT_CALL(*mock_drive_service(), 254 EXPECT_CALL(*mock_drive_service(),
231 GetAccountMetadata(_)) 255 GetAccountMetadata(_))
232 .WillOnce(InvokeGetDataCallback0( 256 .WillOnce(InvokeGetDataCallback0(
233 google_apis::HTTP_SUCCESS, 257 google_apis::HTTP_SUCCESS,
234 base::Passed(&account_metadata))); 258 base::Passed(&account_metadata)));
235 259
236 scoped_ptr<Value> listing_files_in_directory(LoadJSONFile( 260 scoped_ptr<Value> listing_files_in_directory(LoadJSONFile(
237 "sync_file_system/listing_files_in_directory.json")); 261 "sync_file_system/listing_files_in_directory.json"));
238 EXPECT_CALL(*mock_drive_service(), 262 EXPECT_CALL(*mock_drive_service(),
239 GetDocuments(GURL(), 0, std::string(), kDirectoryResourceId1, _)) 263 GetDocuments(GURL(), 0, std::string(),
264 kDirectoryResourceId1, _))
240 .WillOnce(InvokeGetDataCallback4( 265 .WillOnce(InvokeGetDataCallback4(
241 google_apis::HTTP_SUCCESS, 266 google_apis::HTTP_SUCCESS,
242 base::Passed(&listing_files_in_directory))); 267 base::Passed(&listing_files_in_directory)));
243 268
269 EXPECT_CALL(*mock_remote_observer(),
270 OnRemoteServiceStateUpdated(REMOTE_SERVICE_OK, _))
271 .Times(1);
272
244 SetUpDriveSyncService(); 273 SetUpDriveSyncService();
245 message_loop()->RunUntilIdle(); 274 message_loop()->RunUntilIdle();
246 275
247 // kOrigin1 should be a batch sync origin and kOrigin2 should be an 276 // kOrigin1 should be a batch sync origin and kOrigin2 should be an
248 // incremental sync origin. 277 // incremental sync origin.
249 // 4 pending remote changes are from listing_files_in_directory as batch sync 278 // 4 pending remote changes are from listing_files_in_directory as batch sync
250 // changes. 279 // changes.
251 EXPECT_EQ(1u, metadata_store()->batch_sync_origins().size()); 280 EXPECT_EQ(1u, metadata_store()->batch_sync_origins().size());
252 EXPECT_EQ(1u, metadata_store()->incremental_sync_origins().size()); 281 EXPECT_EQ(1u, metadata_store()->incremental_sync_origins().size());
253 EXPECT_EQ(4u, pending_changes().size()); 282 EXPECT_EQ(4u, pending_changes().size());
254 } 283 }
255 284
256 TEST_F(DriveFileSyncServiceTest, RegisterNewOrigin) { 285 TEST_F(DriveFileSyncServiceTest, RegisterNewOrigin) {
257 const GURL kOrigin("http://example.com"); 286 const GURL kOrigin("http://example.com");
258 const std::string kDirectoryResourceId("folder:origin_directory_resource_id"); 287 const std::string kDirectoryResourceId("folder:origin_directory_resource_id");
259 const std::string kSyncRootResourceId("folder:sync_root_resource_id"); 288 const std::string kSyncRootResourceId("folder:sync_root_resource_id");
260 const GURL kSyncRootContentURL("https://sync_root_content_url/"); 289 const GURL kSyncRootContentURL("https://sync_root_content_url/");
261 290
262 metadata_store()->SetSyncRootDirectory(kSyncRootResourceId); 291 metadata_store()->SetSyncRootDirectory(kSyncRootResourceId);
263 292
293 EXPECT_CALL(*mock_remote_observer(),
294 OnRemoteServiceStateUpdated(REMOTE_SERVICE_OK, _))
295 .Times(AtLeast(1));
296
264 InSequence sequence; 297 InSequence sequence;
265 298
266 scoped_ptr<Value> origin_directory_not_found(LoadJSONFile( 299 scoped_ptr<Value> origin_directory_not_found(LoadJSONFile(
267 "sync_file_system/origin_directory_not_found.json")); 300 "sync_file_system/origin_directory_not_found.json"));
268 std::string query = FormatTitleQuery(kOrigin.spec()); 301 std::string query = FormatTitleQuery(kOrigin.spec());
269 302
270 EXPECT_CALL(*mock_drive_service(), 303 EXPECT_CALL(*mock_drive_service(),
271 GetDocuments(GURL(), 0, query, kSyncRootResourceId, _)) 304 GetDocuments(GURL(), 0, query, kSyncRootResourceId, _))
272 .WillOnce(InvokeGetDataCallback4( 305 .WillOnce(InvokeGetDataCallback4(
273 google_apis::HTTP_SUCCESS, 306 google_apis::HTTP_SUCCESS,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 EXPECT_TRUE(pending_changes().empty()); 360 EXPECT_TRUE(pending_changes().empty());
328 } 361 }
329 362
330 TEST_F(DriveFileSyncServiceTest, RegisterExistingOrigin) { 363 TEST_F(DriveFileSyncServiceTest, RegisterExistingOrigin) {
331 const GURL kOrigin("http://example.com"); 364 const GURL kOrigin("http://example.com");
332 const std::string kDirectoryResourceId("folder:origin_directory_resource_id"); 365 const std::string kDirectoryResourceId("folder:origin_directory_resource_id");
333 const std::string kSyncRootResourceId("folder:sync_root_resource_id"); 366 const std::string kSyncRootResourceId("folder:sync_root_resource_id");
334 367
335 metadata_store()->SetSyncRootDirectory(kSyncRootResourceId); 368 metadata_store()->SetSyncRootDirectory(kSyncRootResourceId);
336 369
370 EXPECT_CALL(*mock_remote_observer(),
371 OnRemoteServiceStateUpdated(REMOTE_SERVICE_OK, _))
372 .Times(AtLeast(1));
373
337 InSequence sequence; 374 InSequence sequence;
338 375
339 scoped_ptr<Value> origin_directory_found(LoadJSONFile( 376 scoped_ptr<Value> origin_directory_found(LoadJSONFile(
340 "sync_file_system/origin_directory_found.json")); 377 "sync_file_system/origin_directory_found.json"));
341 std::string query = FormatTitleQuery("http://example.com/"); 378 std::string query = FormatTitleQuery("http://example.com/");
342 EXPECT_CALL(*mock_drive_service(), 379 EXPECT_CALL(*mock_drive_service(),
343 GetDocuments(GURL(), 0, query, kSyncRootResourceId, _)) 380 GetDocuments(GURL(), 0, query, kSyncRootResourceId, _))
344 .WillOnce(InvokeGetDataCallback4( 381 .WillOnce(InvokeGetDataCallback4(
345 google_apis::HTTP_SUCCESS, 382 google_apis::HTTP_SUCCESS,
346 base::Passed(&origin_directory_found))) 383 base::Passed(&origin_directory_found)))
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 "folder:origin_directory_resource_id"); 422 "folder:origin_directory_resource_id");
386 const std::string kDirectoryResourceId2( 423 const std::string kDirectoryResourceId2(
387 "folder:origin_directory_resource_id2"); 424 "folder:origin_directory_resource_id2");
388 const std::string kSyncRootResourceId("folder:sync_root_resource_id"); 425 const std::string kSyncRootResourceId("folder:sync_root_resource_id");
389 426
390 metadata_store()->SetSyncRootDirectory(kSyncRootResourceId); 427 metadata_store()->SetSyncRootDirectory(kSyncRootResourceId);
391 metadata_store()->AddBatchSyncOrigin(kOrigin1, kDirectoryResourceId1); 428 metadata_store()->AddBatchSyncOrigin(kOrigin1, kDirectoryResourceId1);
392 metadata_store()->AddBatchSyncOrigin(kOrigin2, kDirectoryResourceId2); 429 metadata_store()->AddBatchSyncOrigin(kOrigin2, kDirectoryResourceId2);
393 metadata_store()->MoveBatchSyncOriginToIncremental(kOrigin2); 430 metadata_store()->MoveBatchSyncOriginToIncremental(kOrigin2);
394 431
432 EXPECT_CALL(*mock_remote_observer(),
433 OnRemoteServiceStateUpdated(REMOTE_SERVICE_OK, _))
434 .Times(AtLeast(1));
435
395 InSequence sequence; 436 InSequence sequence;
396 437
397 scoped_ptr<Value> account_metadata(LoadJSONFile( 438 scoped_ptr<Value> account_metadata(LoadJSONFile(
398 "gdata/account_metadata.json")); 439 "gdata/account_metadata.json"));
399 EXPECT_CALL(*mock_drive_service(), 440 EXPECT_CALL(*mock_drive_service(),
400 GetAccountMetadata(_)) 441 GetAccountMetadata(_))
401 .WillOnce(InvokeGetDataCallback0( 442 .WillOnce(InvokeGetDataCallback0(
402 google_apis::HTTP_SUCCESS, 443 google_apis::HTTP_SUCCESS,
403 base::Passed(&account_metadata))); 444 base::Passed(&account_metadata)));
404 445
(...skipping 19 matching lines...) Expand all
424 EXPECT_TRUE(done); 465 EXPECT_TRUE(done);
425 466
426 EXPECT_TRUE(metadata_store()->batch_sync_origins().empty()); 467 EXPECT_TRUE(metadata_store()->batch_sync_origins().empty());
427 EXPECT_EQ(1u, metadata_store()->incremental_sync_origins().size()); 468 EXPECT_EQ(1u, metadata_store()->incremental_sync_origins().size());
428 EXPECT_TRUE(pending_changes().empty()); 469 EXPECT_TRUE(pending_changes().empty());
429 } 470 }
430 471
431 #endif // !defined(OS_ANDROID) 472 #endif // !defined(OS_ANDROID)
432 473
433 } // namespace sync_file_system 474 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698