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

Side by Side Diff: chrome/test/live_sync/profile_sync_service_test_harness.cc

Issue 3305003: New authorization framework for sync. ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 3 months 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/message_loop.h" 5 #include "base/message_loop.h"
6 #include "chrome/browser/browser.h" 6 #include "chrome/browser/browser.h"
7 #include "chrome/browser/defaults.h" 7 #include "chrome/browser/defaults.h"
8 #include "chrome/browser/prefs/pref_service.h" 8 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/profile.h" 9 #include "chrome/browser/profile.h"
10 #include "chrome/browser/net/gaia/token_service.h"
10 #include "chrome/browser/sync/glue/sync_backend_host.h" 11 #include "chrome/browser/sync/glue/sync_backend_host.h"
11 #include "chrome/browser/sync/sessions/session_state.h" 12 #include "chrome/browser/sync/sessions/session_state.h"
12 #include "chrome/browser/tab_contents/tab_contents.h" 13 #include "chrome/browser/tab_contents/tab_contents.h"
14 #include "chrome/common/net/gaia/gaia_constants.h"
13 #include "chrome/common/net/gaia/google_service_auth_error.h" 15 #include "chrome/common/net/gaia/google_service_auth_error.h"
14 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
15 #include "chrome/test/live_sync/profile_sync_service_test_harness.h" 17 #include "chrome/test/live_sync/profile_sync_service_test_harness.h"
16 #include "chrome/test/ui_test_utils.h" 18 #include "chrome/test/ui_test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 // The default value for min_timestamp_needed_ when we're not in the 21 // The default value for min_timestamp_needed_ when we're not in the
20 // WAITING_FOR_UPDATES state. 22 // WAITING_FOR_UPDATES state.
21 static const int kMinTimestampNeededNone = -1; 23 static const int kMinTimestampNeededNone = -1;
22 24
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 79 }
78 80
79 bool StateChangeTimeoutEvent::Abort() { 81 bool StateChangeTimeoutEvent::Abort() {
80 aborted_ = true; 82 aborted_ = true;
81 caller_ = NULL; 83 caller_ = NULL;
82 return !did_timeout_; 84 return !did_timeout_;
83 } 85 }
84 86
85 ProfileSyncServiceTestHarness::ProfileSyncServiceTestHarness(Profile* p, 87 ProfileSyncServiceTestHarness::ProfileSyncServiceTestHarness(Profile* p,
86 const std::string& username, const std::string& password, int id) 88 const std::string& username, const std::string& password, int id)
87 : wait_state_(WAITING_FOR_ON_AUTH_ERROR), profile_(p), service_(NULL), 89 : wait_state_(WAITING_FOR_ON_BACKEND_INITIALIZED),
90 profile_(p), service_(NULL),
88 last_status_(kInvalidStatus), 91 last_status_(kInvalidStatus),
89 last_timestamp_(0), 92 last_timestamp_(0),
90 min_timestamp_needed_(kMinTimestampNeededNone), 93 min_timestamp_needed_(kMinTimestampNeededNone),
91 username_(username), password_(password), id_(id) { 94 username_(username), password_(password), id_(id) {
92 // Ensure the profile has enough prefs registered for use by sync. 95 // Ensure the profile has enough prefs registered for use by sync.
93 if (!p->GetPrefs()->FindPreference(prefs::kAcceptLanguages)) 96 if (!p->GetPrefs()->FindPreference(prefs::kAcceptLanguages))
94 TabContents::RegisterUserPrefs(p->GetPrefs()); 97 TabContents::RegisterUserPrefs(p->GetPrefs());
95 } 98 }
96 99
97 bool ProfileSyncServiceTestHarness::SetupSync() { 100 bool ProfileSyncServiceTestHarness::SetupSync() {
98 service_ = profile_->GetProfileSyncService(); 101 service_ = profile_->GetProfileSyncService("");
99 service_->StartUp();
100 service_->AddObserver(this); 102 service_->AddObserver(this);
101 return WaitForServiceInit(false); 103 service_->signin_.StartSignIn(username_, password_, "", "");
102 }
103 104
104 bool ProfileSyncServiceTestHarness::RetryAuthentication() { 105 return WaitForServiceInit();
105 wait_state_ = WAITING_FOR_ON_BACKEND_INITIALIZED;
106 return WaitForServiceInit(true);
107 } 106 }
108 107
109 void ProfileSyncServiceTestHarness::SignalStateCompleteWithNextState( 108 void ProfileSyncServiceTestHarness::SignalStateCompleteWithNextState(
110 WaitState next_state) { 109 WaitState next_state) {
111 110
112 wait_state_ = next_state; 111 wait_state_ = next_state;
113 SignalStateComplete(); 112 SignalStateComplete();
114 } 113 }
115 114
116 void ProfileSyncServiceTestHarness::SignalStateComplete() { 115 void ProfileSyncServiceTestHarness::SignalStateComplete() {
117 MessageLoopForUI::current()->Quit(); 116 MessageLoopForUI::current()->Quit();
118 } 117 }
119 118
120 bool ProfileSyncServiceTestHarness::RunStateChangeMachine() { 119 bool ProfileSyncServiceTestHarness::RunStateChangeMachine() {
121 WaitState state = wait_state_; 120 WaitState state = wait_state_;
122 ProfileSyncService::Status status(service_->QueryDetailedSyncStatus()); 121 ProfileSyncService::Status status(service_->QueryDetailedSyncStatus());
123 switch (wait_state_) { 122 switch (wait_state_) {
124 case WAITING_FOR_ON_AUTH_ERROR: {
125 LogClientInfo("WAITING_FOR_ON_AUTH_ERROR");
126 SignalStateCompleteWithNextState(WAITING_FOR_ON_BACKEND_INITIALIZED);
127 break;
128 }
129 case WAITING_FOR_ON_BACKEND_INITIALIZED: { 123 case WAITING_FOR_ON_BACKEND_INITIALIZED: {
130 LogClientInfo("WAITING_FOR_ON_BACKEND_INITIALIZED"); 124 LogClientInfo("WAITING_FOR_ON_BACKEND_INITIALIZED");
131 if (service_->GetAuthError().state() != GoogleServiceAuthError::NONE) { 125 if (service_->GetAuthError().state() != GoogleServiceAuthError::NONE) {
132 SignalStateCompleteWithNextState(AUTH_ERROR); 126 SignalStateCompleteWithNextState(AUTH_ERROR);
133 } 127 }
134 if (service_->sync_initialized()) { 128 if (service_->sync_initialized()) {
135 SignalStateCompleteWithNextState(WAITING_FOR_NOTIFICATIONS_ENABLED); 129 SignalStateCompleteWithNextState(WAITING_FOR_NOTIFICATIONS_ENABLED);
136 } 130 }
137 break; 131 break;
138 } 132 }
139 case WAITING_FOR_NOTIFICATIONS_ENABLED: { 133 case WAITING_FOR_NOTIFICATIONS_ENABLED: {
140 LogClientInfo("WAITING_FOR_NOTIFICATIONS_ENABLED"); 134 LogClientInfo("WAITING_FOR_NOTIFICATIONS_ENABLED");
141 if (status.notifications_enabled) { 135 if (status.notifications_enabled) {
142 SignalStateCompleteWithNextState(FULLY_SYNCED); 136 SignalStateCompleteWithNextState(FULLY_SYNCED);
143 } 137 }
144 break; 138 break;
145 } 139 }
140 case WAITING_FOR_SERVER_REACHABLE: {
141 LogClientInfo("WAITING_FOR_SERVER_REACHABLE");
142 const SyncSessionSnapshot* snap = GetLastSessionSnapshot();
143 if (!status.server_reachable) {
144 break;
145 }
146 if (service()->backend()->HasUnsyncedItems() ||
147 snap->has_more_to_sync || snap->unsynced_count != 0) {
148 SignalStateCompleteWithNextState(WAITING_FOR_SYNC_TO_FINISH);
149 break;
150 }
151 last_timestamp_ = snap->max_local_timestamp;
152 SignalStateCompleteWithNextState(FULLY_SYNCED);
153 break;
154 }
146 case WAITING_FOR_SYNC_TO_FINISH: { 155 case WAITING_FOR_SYNC_TO_FINISH: {
147 LogClientInfo("WAITING_FOR_SYNC_TO_FINISH"); 156 LogClientInfo("WAITING_FOR_SYNC_TO_FINISH");
148 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); 157 const SyncSessionSnapshot* snap = GetLastSessionSnapshot();
149 DCHECK(snap) << "Should have been at least one sync session by now"; 158 DCHECK(snap) << "Should have been at least one sync session by now";
150 // TODO(rsimha): In an ideal world, snap->has_more_to_sync == false should 159 // TODO(rsimha): In an ideal world, snap->has_more_to_sync == false should
151 // be a sufficient condition for sync to have completed. However, the 160 // be a sufficient condition for sync to have completed. However, the
152 // additional check of snap->unsynced_count is required due to 161 // additional check of snap->unsynced_count is required due to
153 // http://crbug.com/48989. 162 // http://crbug.com/48989.
154 if (snap->has_more_to_sync || snap->unsynced_count != 0) { 163 if (service()->backend()->HasUnsyncedItems() ||
164 snap->has_more_to_sync || snap->unsynced_count != 0) {
165 if (!status.server_reachable)
166 SignalStateCompleteWithNextState(WAITING_FOR_SERVER_REACHABLE);
155 break; 167 break;
156 } 168 }
157 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); 169 EXPECT_LE(last_timestamp_, snap->max_local_timestamp);
158 last_timestamp_ = snap->max_local_timestamp; 170 last_timestamp_ = snap->max_local_timestamp;
159 SignalStateCompleteWithNextState(FULLY_SYNCED); 171 SignalStateCompleteWithNextState(FULLY_SYNCED);
160 break; 172 break;
161 } 173 }
162 case WAITING_FOR_UPDATES: { 174 case WAITING_FOR_UPDATES: {
163 LogClientInfo("WAITING_FOR_UPDATES"); 175 LogClientInfo("WAITING_FOR_UPDATES");
164 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); 176 const SyncSessionSnapshot* snap = GetLastSessionSnapshot();
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 FROM_HERE, 291 FROM_HERE,
280 NewRunnableMethod(timeout_signal.get(), 292 NewRunnableMethod(timeout_signal.get(),
281 &StateChangeTimeoutEvent::Callback), 293 &StateChangeTimeoutEvent::Callback),
282 1000 * timeout_seconds); 294 1000 * timeout_seconds);
283 LogClientInfo("Before RunMessageLoop"); 295 LogClientInfo("Before RunMessageLoop");
284 ui_test_utils::RunMessageLoop(); 296 ui_test_utils::RunMessageLoop();
285 LogClientInfo("After RunMessageLoop"); 297 LogClientInfo("After RunMessageLoop");
286 return timeout_signal->Abort(); 298 return timeout_signal->Abort();
287 } 299 }
288 300
289 bool ProfileSyncServiceTestHarness::WaitForServiceInit(bool is_auth_retry) { 301 bool ProfileSyncServiceTestHarness::WaitForServiceInit() {
290 LogClientInfo("WaitForServiceInit"); 302 LogClientInfo("WaitForServiceInit");
291 if (!is_auth_retry) {
292 // Wait for the OnAuthError() callback.
293 EXPECT_EQ(wait_state_, WAITING_FOR_ON_AUTH_ERROR);
294 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30,
295 "Waiting for the OnAuthError() callback.")) <<
296 "OnAuthError() not seen after 30 seconds.";
297 }
298 303
299 // Enter GAIA credentials and wait for the OnBackendInitialized() callback. 304 // Wait for the OnBackendInitialized() callback.
300 service_->backend()->Authenticate(username_, password_, std::string());
301 EXPECT_EQ(wait_state_, WAITING_FOR_ON_BACKEND_INITIALIZED); 305 EXPECT_EQ(wait_state_, WAITING_FOR_ON_BACKEND_INITIALIZED);
302 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30, 306 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30,
303 "Waiting for OnBackendInitialized().")) << 307 "Waiting for OnBackendInitialized().")) <<
304 "OnBackendInitialized() not seen after 30 seconds."; 308 "OnBackendInitialized() not seen after 30 seconds.";
305 309
306 if (wait_state_ == AUTH_ERROR) { 310 if (wait_state_ == AUTH_ERROR) {
307 return false; 311 return false;
308 } 312 }
309 313
310 // Choose datatypes to be synced. Note: This is unnecessary on Chrome OS. 314 // Choose datatypes to be synced.
311 if (!browser_defaults::kBootstrapSyncAuthentication) { 315 syncable::ModelTypeSet set;
312 syncable::ModelTypeSet set; 316 for (int i = syncable::FIRST_REAL_MODEL_TYPE;
313 for (int i = syncable::FIRST_REAL_MODEL_TYPE; 317 i < syncable::MODEL_TYPE_COUNT; ++i) {
314 i < syncable::MODEL_TYPE_COUNT; ++i) { 318 set.insert(syncable::ModelTypeFromInt(i));
315 set.insert(syncable::ModelTypeFromInt(i));
316 }
317 service_->OnUserChoseDatatypes(true, set);
318 } 319 }
320 service_->OnUserChoseDatatypes(true, set);
319 321
320 // Wait for notifications_enabled to be set to true. 322 // Wait for notifications_enabled to be set to true.
321 EXPECT_EQ(wait_state_, WAITING_FOR_NOTIFICATIONS_ENABLED); 323 EXPECT_EQ(wait_state_, WAITING_FOR_NOTIFICATIONS_ENABLED);
322 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30, 324 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30,
323 "Waiting for notifications_enabled to be set to true.")) << 325 "Waiting for notifications_enabled to be set to true.")) <<
324 "notifications_enabled not set to true after 30 seconds."; 326 "notifications_enabled not set to true after 30 seconds.";
325 327
326 return true; 328 return true;
327 } 329 }
328 330
329 const SyncSessionSnapshot* 331 const SyncSessionSnapshot*
330 ProfileSyncServiceTestHarness::GetLastSessionSnapshot() const { 332 ProfileSyncServiceTestHarness::GetLastSessionSnapshot() const {
331 EXPECT_FALSE(service_ == NULL) << "Sync service has not yet been set up."; 333 EXPECT_FALSE(service_ == NULL) << "Sync service has not yet been set up.";
332 return service_->backend()->GetLastSessionSnapshot(); 334 if (service_->backend()) {
335 return service_->backend()->GetLastSessionSnapshot();
336 }
337 return NULL;
333 } 338 }
334 339
335 void ProfileSyncServiceTestHarness::LogClientInfo(std::string message) { 340 void ProfileSyncServiceTestHarness::LogClientInfo(std::string message) {
336 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); 341 const SyncSessionSnapshot* snap = GetLastSessionSnapshot();
337 if (snap) { 342 if (snap) {
338 LOG(INFO) << "Client " << id_ << ": " << message << ": " 343 LOG(INFO) << "Client " << id_ << ": " << message << ": "
339 << "has_more_to_sync: " << snap->has_more_to_sync 344 << "has_more_to_sync: " << snap->has_more_to_sync
340 << ", max_local_timestamp: " << snap->max_local_timestamp 345 << ", max_local_timestamp: " << snap->max_local_timestamp
341 << ", unsynced_count: " << snap->unsynced_count 346 << ", unsynced_count: " << snap->unsynced_count
342 << ", has_unsynced_items: " << service()->backend()->HasUnsyncedItems() 347 << ", has_unsynced_items: " << service()->backend()->HasUnsyncedItems()
343 << "."; 348 << ".";
344 } else { 349 } else {
345 LOG(INFO) << "Client " << id_ << ": " << message << ": " 350 LOG(INFO) << "Client " << id_ << ": " << message << ": "
346 << "Snap not available."; 351 << "Snap not available.";
347 } 352 }
348 } 353 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698