| OLD | NEW |
| 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/pref_service.h" | 8 #include "chrome/browser/pref_service.h" |
| 9 #include "chrome/browser/profile.h" | 9 #include "chrome/browser/profile.h" |
| 10 #include "chrome/browser/sync/glue/sync_backend_host.h" | 10 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool StateChangeTimeoutEvent::Abort() { | 79 bool StateChangeTimeoutEvent::Abort() { |
| 80 aborted_ = true; | 80 aborted_ = true; |
| 81 caller_ = NULL; | 81 caller_ = NULL; |
| 82 return !did_timeout_; | 82 return !did_timeout_; |
| 83 } | 83 } |
| 84 | 84 |
| 85 ProfileSyncServiceTestHarness::ProfileSyncServiceTestHarness( | 85 ProfileSyncServiceTestHarness::ProfileSyncServiceTestHarness(Profile* p, |
| 86 Profile* p, const std::string& username, const std::string& password) | 86 const std::string& username, const std::string& password, int id) |
| 87 : wait_state_(WAITING_FOR_ON_AUTH_ERROR), profile_(p), service_(NULL), | 87 : wait_state_(WAITING_FOR_ON_AUTH_ERROR), profile_(p), service_(NULL), |
| 88 last_status_(kInvalidStatus), | 88 last_status_(kInvalidStatus), |
| 89 last_timestamp_(0), | 89 last_timestamp_(0), |
| 90 min_timestamp_needed_(kMinTimestampNeededNone), | 90 min_timestamp_needed_(kMinTimestampNeededNone), |
| 91 username_(username), password_(password) { | 91 username_(username), password_(password), id_(id) { |
| 92 // Ensure the profile has enough prefs registered for use by sync. | 92 // Ensure the profile has enough prefs registered for use by sync. |
| 93 if (!p->GetPrefs()->FindPreference(prefs::kAcceptLanguages)) | 93 if (!p->GetPrefs()->FindPreference(prefs::kAcceptLanguages)) |
| 94 TabContents::RegisterUserPrefs(p->GetPrefs()); | 94 TabContents::RegisterUserPrefs(p->GetPrefs()); |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool ProfileSyncServiceTestHarness::SetupSync() { | 97 bool ProfileSyncServiceTestHarness::SetupSync() { |
| 98 service_ = profile_->GetProfileSyncService(); | 98 service_ = profile_->GetProfileSyncService(); |
| 99 service_->StartUp(); | 99 service_->StartUp(); |
| 100 service_->AddObserver(this); | 100 service_->AddObserver(this); |
| 101 return WaitForServiceInit(false); | 101 return WaitForServiceInit(false); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 | 115 |
| 116 void ProfileSyncServiceTestHarness::SignalStateComplete() { | 116 void ProfileSyncServiceTestHarness::SignalStateComplete() { |
| 117 MessageLoopForUI::current()->Quit(); | 117 MessageLoopForUI::current()->Quit(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool ProfileSyncServiceTestHarness::RunStateChangeMachine() { | 120 bool ProfileSyncServiceTestHarness::RunStateChangeMachine() { |
| 121 WaitState state = wait_state_; | 121 WaitState state = wait_state_; |
| 122 ProfileSyncService::Status status(service_->QueryDetailedSyncStatus()); | 122 ProfileSyncService::Status status(service_->QueryDetailedSyncStatus()); |
| 123 switch (wait_state_) { | 123 switch (wait_state_) { |
| 124 case WAITING_FOR_ON_AUTH_ERROR: { | 124 case WAITING_FOR_ON_AUTH_ERROR: { |
| 125 LogClientInfo("WAITING_FOR_ON_AUTH_ERROR"); |
| 125 SignalStateCompleteWithNextState(WAITING_FOR_ON_BACKEND_INITIALIZED); | 126 SignalStateCompleteWithNextState(WAITING_FOR_ON_BACKEND_INITIALIZED); |
| 126 break; | 127 break; |
| 127 } | 128 } |
| 128 case WAITING_FOR_ON_BACKEND_INITIALIZED: { | 129 case WAITING_FOR_ON_BACKEND_INITIALIZED: { |
| 130 LogClientInfo("WAITING_FOR_ON_BACKEND_INITIALIZED"); |
| 129 if (service_->GetAuthError().state() != GoogleServiceAuthError::NONE) { | 131 if (service_->GetAuthError().state() != GoogleServiceAuthError::NONE) { |
| 130 SignalStateCompleteWithNextState(AUTH_ERROR); | 132 SignalStateCompleteWithNextState(AUTH_ERROR); |
| 131 } | 133 } |
| 132 if (service_->sync_initialized()) { | 134 if (service_->sync_initialized()) { |
| 133 SignalStateCompleteWithNextState(WAITING_FOR_NOTIFICATIONS_ENABLED); | 135 SignalStateCompleteWithNextState(WAITING_FOR_NOTIFICATIONS_ENABLED); |
| 134 } | 136 } |
| 135 break; | 137 break; |
| 136 } | 138 } |
| 137 case WAITING_FOR_NOTIFICATIONS_ENABLED: { | 139 case WAITING_FOR_NOTIFICATIONS_ENABLED: { |
| 140 LogClientInfo("WAITING_FOR_NOTIFICATIONS_ENABLED"); |
| 138 if (status.notifications_enabled) { | 141 if (status.notifications_enabled) { |
| 139 SignalStateCompleteWithNextState(FULLY_SYNCED); | 142 SignalStateCompleteWithNextState(FULLY_SYNCED); |
| 140 } | 143 } |
| 141 break; | 144 break; |
| 142 } | 145 } |
| 143 case WAITING_FOR_SYNC_TO_FINISH: { | 146 case WAITING_FOR_SYNC_TO_FINISH: { |
| 147 LogClientInfo("WAITING_FOR_SYNC_TO_FINISH"); |
| 144 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 148 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
| 145 DCHECK(snap) << "Should have been at least one sync session by now"; | 149 DCHECK(snap) << "Should have been at least one sync session by now"; |
| 146 // TODO(rsimha): In an ideal world, snap->has_more_to_sync == false should | 150 // TODO(rsimha): In an ideal world, snap->has_more_to_sync == false should |
| 147 // be a sufficient condition for sync to have completed. However, the | 151 // be a sufficient condition for sync to have completed. However, the |
| 148 // additional check of snap->unsynced_count is required due to | 152 // additional check of snap->unsynced_count is required due to |
| 149 // http://crbug.com/48989. | 153 // http://crbug.com/48989. |
| 150 if (snap->has_more_to_sync || snap->unsynced_count != 0) { | 154 if (snap->has_more_to_sync || snap->unsynced_count != 0) { |
| 151 break; | 155 break; |
| 152 } | 156 } |
| 153 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); | 157 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); |
| 154 last_timestamp_ = snap->max_local_timestamp; | 158 last_timestamp_ = snap->max_local_timestamp; |
| 155 SignalStateCompleteWithNextState(FULLY_SYNCED); | 159 SignalStateCompleteWithNextState(FULLY_SYNCED); |
| 156 break; | 160 break; |
| 157 } | 161 } |
| 158 case WAITING_FOR_UPDATES: { | 162 case WAITING_FOR_UPDATES: { |
| 163 LogClientInfo("WAITING_FOR_UPDATES"); |
| 159 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 164 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
| 160 DCHECK(snap) << "Should have been at least one sync session by now"; | 165 DCHECK(snap) << "Should have been at least one sync session by now"; |
| 161 if (snap->max_local_timestamp < min_timestamp_needed_) { | 166 if (snap->max_local_timestamp < min_timestamp_needed_) { |
| 162 break; | 167 break; |
| 163 } | 168 } |
| 164 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); | 169 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); |
| 165 last_timestamp_ = snap->max_local_timestamp; | 170 last_timestamp_ = snap->max_local_timestamp; |
| 166 SignalStateCompleteWithNextState(FULLY_SYNCED); | 171 SignalStateCompleteWithNextState(FULLY_SYNCED); |
| 167 break; | 172 break; |
| 168 } | 173 } |
| 169 case FULLY_SYNCED: | 174 case FULLY_SYNCED: { |
| 175 LogClientInfo("FULLY_SYNCED"); |
| 176 break; |
| 177 } |
| 178 case AUTH_ERROR: { |
| 179 LogClientInfo("AUTH_ERROR"); |
| 180 break; |
| 181 } |
| 170 default: | 182 default: |
| 171 // Invalid state during observer callback which may be triggered by other | 183 // Invalid state during observer callback which may be triggered by other |
| 172 // classes using the the UI message loop. Defer to their handling. | 184 // classes using the the UI message loop. Defer to their handling. |
| 173 break; | 185 break; |
| 174 } | 186 } |
| 175 last_status_ = status; | 187 last_status_ = status; |
| 176 return state != wait_state_; | 188 return state != wait_state_; |
| 177 } | 189 } |
| 178 | 190 |
| 179 void ProfileSyncServiceTestHarness::OnStateChanged() { | 191 void ProfileSyncServiceTestHarness::OnStateChanged() { |
| 180 RunStateChangeMachine(); | 192 RunStateChangeMachine(); |
| 181 } | 193 } |
| 182 | 194 |
| 183 bool ProfileSyncServiceTestHarness::AwaitSyncCycleCompletion( | 195 bool ProfileSyncServiceTestHarness::AwaitSyncCycleCompletion( |
| 184 const std::string& reason) { | 196 const std::string& reason) { |
| 197 LogClientInfo("AwaitSyncCycleCompletion"); |
| 185 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 198 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
| 186 DCHECK(snap) << "Should have been at least one sync session by now"; | 199 DCHECK(snap) << "Should have been at least one sync session by now"; |
| 187 // TODO(rsimha): Remove additional checks of snap->has_more_to_sync and | 200 // TODO(rsimha): Remove additional checks of snap->has_more_to_sync and |
| 188 // snap->unsynced_count once http://crbug.com/48989 is fixed. | 201 // snap->unsynced_count once http://crbug.com/48989 is fixed. |
| 189 if (service()->backend()->HasUnsyncedItems() || | 202 if (service()->backend()->HasUnsyncedItems() || |
| 190 snap->has_more_to_sync || | 203 snap->has_more_to_sync || |
| 191 snap->unsynced_count != 0) { | 204 snap->unsynced_count != 0) { |
| 192 wait_state_ = WAITING_FOR_SYNC_TO_FINISH; | 205 wait_state_ = WAITING_FOR_SYNC_TO_FINISH; |
| 193 return AwaitStatusChangeWithTimeout(60, reason); | 206 return AwaitStatusChangeWithTimeout(60, reason); |
| 194 } else { | 207 } else { |
| 195 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); | 208 EXPECT_LE(last_timestamp_, snap->max_local_timestamp); |
| 196 last_timestamp_ = snap->max_local_timestamp; | 209 last_timestamp_ = snap->max_local_timestamp; |
| 197 return true; | 210 return true; |
| 198 } | 211 } |
| 199 } | 212 } |
| 200 | 213 |
| 201 bool ProfileSyncServiceTestHarness::AwaitMutualSyncCycleCompletion( | 214 bool ProfileSyncServiceTestHarness::AwaitMutualSyncCycleCompletion( |
| 202 ProfileSyncServiceTestHarness* partner) { | 215 ProfileSyncServiceTestHarness* partner) { |
| 216 LogClientInfo("AwaitMutualSyncCycleCompletion"); |
| 203 bool success = AwaitSyncCycleCompletion( | 217 bool success = AwaitSyncCycleCompletion( |
| 204 "Sync cycle completion on active client."); | 218 "Sync cycle completion on active client."); |
| 205 if (!success) | 219 if (!success) |
| 206 return false; | 220 return false; |
| 207 return partner->WaitUntilTimestampIsAtLeast(last_timestamp_, | 221 return partner->WaitUntilTimestampIsAtLeast(last_timestamp_, |
| 208 "Sync cycle completion on passive client."); | 222 "Sync cycle completion on passive client."); |
| 209 } | 223 } |
| 210 | 224 |
| 211 bool ProfileSyncServiceTestHarness::AwaitGroupSyncCycleCompletion( | 225 bool ProfileSyncServiceTestHarness::AwaitGroupSyncCycleCompletion( |
| 212 std::vector<ProfileSyncServiceTestHarness*>& partners) { | 226 std::vector<ProfileSyncServiceTestHarness*>& partners) { |
| 227 LogClientInfo("AwaitGroupSyncCycleCompletion"); |
| 213 bool success = AwaitSyncCycleCompletion( | 228 bool success = AwaitSyncCycleCompletion( |
| 214 "Sync cycle completion on active client."); | 229 "Sync cycle completion on active client."); |
| 215 if (!success) | 230 if (!success) |
| 216 return false; | 231 return false; |
| 217 bool return_value = true; | 232 bool return_value = true; |
| 218 for (std::vector<ProfileSyncServiceTestHarness*>::iterator it = | 233 for (std::vector<ProfileSyncServiceTestHarness*>::iterator it = |
| 219 partners.begin(); it != partners.end(); ++it) { | 234 partners.begin(); it != partners.end(); ++it) { |
| 220 if (this != *it) { | 235 if (this != *it) { |
| 221 return_value = return_value && | 236 return_value = return_value && |
| 222 (*it)->WaitUntilTimestampIsAtLeast(last_timestamp_, | 237 (*it)->WaitUntilTimestampIsAtLeast(last_timestamp_, |
| 223 "Sync cycle completion on partner client."); | 238 "Sync cycle completion on partner client."); |
| 224 } | 239 } |
| 225 } | 240 } |
| 226 return return_value; | 241 return return_value; |
| 227 } | 242 } |
| 228 | 243 |
| 229 // static | 244 // static |
| 230 bool ProfileSyncServiceTestHarness::AwaitQuiescence( | 245 bool ProfileSyncServiceTestHarness::AwaitQuiescence( |
| 231 std::vector<ProfileSyncServiceTestHarness*>& clients) { | 246 std::vector<ProfileSyncServiceTestHarness*>& clients) { |
| 247 LOG(INFO) << "AwaitQuiescence."; |
| 232 bool return_value = true; | 248 bool return_value = true; |
| 233 for (std::vector<ProfileSyncServiceTestHarness*>::iterator it = | 249 for (std::vector<ProfileSyncServiceTestHarness*>::iterator it = |
| 234 clients.begin(); it != clients.end(); ++it) { | 250 clients.begin(); it != clients.end(); ++it) { |
| 235 return_value = return_value && | 251 return_value = return_value && |
| 236 (*it)->AwaitGroupSyncCycleCompletion(clients); | 252 (*it)->AwaitGroupSyncCycleCompletion(clients); |
| 237 } | 253 } |
| 238 return return_value; | 254 return return_value; |
| 239 } | 255 } |
| 240 | 256 |
| 241 bool ProfileSyncServiceTestHarness::WaitUntilTimestampIsAtLeast( | 257 bool ProfileSyncServiceTestHarness::WaitUntilTimestampIsAtLeast( |
| 242 int64 timestamp, const std::string& reason) { | 258 int64 timestamp, const std::string& reason) { |
| 259 LogClientInfo("WaitUntilTimestampIsAtLeast"); |
| 243 min_timestamp_needed_ = timestamp; | 260 min_timestamp_needed_ = timestamp; |
| 244 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 261 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
| 245 DCHECK(snap) << "Should have been at least one sync session by now"; | 262 DCHECK(snap) << "Should have been at least one sync session by now"; |
| 246 if (snap->max_local_timestamp < min_timestamp_needed_) { | 263 if (snap->max_local_timestamp < min_timestamp_needed_) { |
| 247 wait_state_ = WAITING_FOR_UPDATES; | 264 wait_state_ = WAITING_FOR_UPDATES; |
| 248 return AwaitStatusChangeWithTimeout(60, reason); | 265 return AwaitStatusChangeWithTimeout(60, reason); |
| 249 } else { | 266 } else { |
| 250 return true; | 267 return true; |
| 251 } | 268 } |
| 252 } | 269 } |
| 253 | 270 |
| 254 bool ProfileSyncServiceTestHarness::AwaitStatusChangeWithTimeout( | 271 bool ProfileSyncServiceTestHarness::AwaitStatusChangeWithTimeout( |
| 255 int timeout_seconds, | 272 int timeout_seconds, |
| 256 const std::string& reason) { | 273 const std::string& reason) { |
| 274 LogClientInfo("AwaitStatusChangeWithTimeout"); |
| 257 scoped_refptr<StateChangeTimeoutEvent> timeout_signal( | 275 scoped_refptr<StateChangeTimeoutEvent> timeout_signal( |
| 258 new StateChangeTimeoutEvent(this, reason)); | 276 new StateChangeTimeoutEvent(this, reason)); |
| 259 MessageLoopForUI* loop = MessageLoopForUI::current(); | 277 MessageLoopForUI* loop = MessageLoopForUI::current(); |
| 260 loop->PostDelayedTask( | 278 loop->PostDelayedTask( |
| 261 FROM_HERE, | 279 FROM_HERE, |
| 262 NewRunnableMethod(timeout_signal.get(), | 280 NewRunnableMethod(timeout_signal.get(), |
| 263 &StateChangeTimeoutEvent::Callback), | 281 &StateChangeTimeoutEvent::Callback), |
| 264 1000 * timeout_seconds); | 282 1000 * timeout_seconds); |
| 283 LogClientInfo("Before RunMessageLoop"); |
| 265 ui_test_utils::RunMessageLoop(); | 284 ui_test_utils::RunMessageLoop(); |
| 285 LogClientInfo("After RunMessageLoop"); |
| 266 return timeout_signal->Abort(); | 286 return timeout_signal->Abort(); |
| 267 } | 287 } |
| 268 | 288 |
| 269 bool ProfileSyncServiceTestHarness::WaitForServiceInit(bool is_auth_retry) { | 289 bool ProfileSyncServiceTestHarness::WaitForServiceInit(bool is_auth_retry) { |
| 290 LogClientInfo("WaitForServiceInit"); |
| 270 if (!is_auth_retry) { | 291 if (!is_auth_retry) { |
| 271 // Wait for the OnAuthError() callback. | 292 // Wait for the OnAuthError() callback. |
| 272 EXPECT_EQ(wait_state_, WAITING_FOR_ON_AUTH_ERROR); | 293 EXPECT_EQ(wait_state_, WAITING_FOR_ON_AUTH_ERROR); |
| 273 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30, | 294 EXPECT_TRUE(AwaitStatusChangeWithTimeout(30, |
| 274 "Waiting for the OnAuthError() callback.")) << | 295 "Waiting for the OnAuthError() callback.")) << |
| 275 "OnAuthError() not seen after 30 seconds."; | 296 "OnAuthError() not seen after 30 seconds."; |
| 276 } | 297 } |
| 277 | 298 |
| 278 // Enter GAIA credentials and wait for the OnBackendInitialized() callback. | 299 // Enter GAIA credentials and wait for the OnBackendInitialized() callback. |
| 279 service_->backend()->Authenticate(username_, password_, std::string()); | 300 service_->backend()->Authenticate(username_, password_, std::string()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 303 "notifications_enabled not set to true after 30 seconds."; | 324 "notifications_enabled not set to true after 30 seconds."; |
| 304 | 325 |
| 305 return true; | 326 return true; |
| 306 } | 327 } |
| 307 | 328 |
| 308 const SyncSessionSnapshot* | 329 const SyncSessionSnapshot* |
| 309 ProfileSyncServiceTestHarness::GetLastSessionSnapshot() const { | 330 ProfileSyncServiceTestHarness::GetLastSessionSnapshot() const { |
| 310 EXPECT_FALSE(service_ == NULL) << "Sync service has not yet been set up."; | 331 EXPECT_FALSE(service_ == NULL) << "Sync service has not yet been set up."; |
| 311 return service_->backend()->GetLastSessionSnapshot(); | 332 return service_->backend()->GetLastSessionSnapshot(); |
| 312 } | 333 } |
| 334 |
| 335 void ProfileSyncServiceTestHarness::LogClientInfo(std::string message) { |
| 336 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
| 337 if (snap) { |
| 338 LOG(INFO) << "Client " << id_ << ": " << message << ": " |
| 339 << "has_more_to_sync: " << snap->has_more_to_sync |
| 340 << ", max_local_timestamp: " << snap->max_local_timestamp |
| 341 << ", unsynced_count: " << snap->unsynced_count |
| 342 << ", has_unsynced_items: " << service()->backend()->HasUnsyncedItems() |
| 343 << "."; |
| 344 } else { |
| 345 LOG(INFO) << "Client " << id_ << ": " << message << ": " |
| 346 << "Snap not available."; |
| 347 } |
| 348 } |
| OLD | NEW |