| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/js_sync_manager_observer.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/tracked.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/sync/engine/syncapi.h" | |
| 14 #include "chrome/browser/sync/js_arg_list.h" | |
| 15 #include "chrome/browser/sync/js_event_details.h" | |
| 16 #include "chrome/browser/sync/js_test_util.h" | |
| 17 #include "chrome/browser/sync/sessions/session_state.h" | |
| 18 #include "chrome/browser/sync/syncable/model_type.h" | |
| 19 #include "chrome/browser/sync/weak_handle.h" | |
| 20 #include "chrome/test/sync/engine/test_user_share.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace browser_sync { | |
| 24 namespace { | |
| 25 | |
| 26 using ::testing::InSequence; | |
| 27 using ::testing::StrictMock; | |
| 28 | |
| 29 class JsSyncManagerObserverTest : public testing::Test { | |
| 30 protected: | |
| 31 JsSyncManagerObserverTest() { | |
| 32 js_sync_manager_observer_.SetJsEventHandler( | |
| 33 mock_js_event_handler_.AsWeakHandle()); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 // This must be destroyed after the member variables below in order | |
| 38 // for WeakHandles to be destroyed properly. | |
| 39 MessageLoop message_loop_; | |
| 40 | |
| 41 protected: | |
| 42 StrictMock<MockJsEventHandler> mock_js_event_handler_; | |
| 43 JsSyncManagerObserver js_sync_manager_observer_; | |
| 44 | |
| 45 void PumpLoop() { | |
| 46 message_loop_.RunAllPending(); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 TEST_F(JsSyncManagerObserverTest, NoArgNotifiations) { | |
| 51 InSequence dummy; | |
| 52 | |
| 53 EXPECT_CALL(mock_js_event_handler_, | |
| 54 HandleJsEvent("onInitializationComplete", | |
| 55 HasDetails(JsEventDetails()))); | |
| 56 EXPECT_CALL(mock_js_event_handler_, | |
| 57 HandleJsEvent("onStopSyncingPermanently", | |
| 58 HasDetails(JsEventDetails()))); | |
| 59 EXPECT_CALL(mock_js_event_handler_, | |
| 60 HandleJsEvent("onClearServerDataSucceeded", | |
| 61 HasDetails(JsEventDetails()))); | |
| 62 EXPECT_CALL(mock_js_event_handler_, | |
| 63 HandleJsEvent("onClearServerDataFailed", | |
| 64 HasDetails(JsEventDetails()))); | |
| 65 | |
| 66 js_sync_manager_observer_.OnInitializationComplete(WeakHandle<JsBackend>()); | |
| 67 js_sync_manager_observer_.OnStopSyncingPermanently(); | |
| 68 js_sync_manager_observer_.OnClearServerDataSucceeded(); | |
| 69 js_sync_manager_observer_.OnClearServerDataFailed(); | |
| 70 PumpLoop(); | |
| 71 } | |
| 72 | |
| 73 TEST_F(JsSyncManagerObserverTest, OnChangesComplete) { | |
| 74 InSequence dummy; | |
| 75 | |
| 76 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 77 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 78 DictionaryValue expected_details; | |
| 79 expected_details.SetString( | |
| 80 "modelType", | |
| 81 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i))); | |
| 82 EXPECT_CALL(mock_js_event_handler_, | |
| 83 HandleJsEvent("onChangesComplete", | |
| 84 HasDetailsAsDictionary(expected_details))); | |
| 85 } | |
| 86 | |
| 87 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 88 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 89 js_sync_manager_observer_.OnChangesComplete(syncable::ModelTypeFromInt(i)); | |
| 90 } | |
| 91 PumpLoop(); | |
| 92 } | |
| 93 | |
| 94 TEST_F(JsSyncManagerObserverTest, OnSyncCycleCompleted) { | |
| 95 std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; | |
| 96 sessions::SyncSessionSnapshot snapshot(sessions::SyncerStatus(), | |
| 97 sessions::ErrorCounters(), | |
| 98 100, | |
| 99 false, | |
| 100 syncable::ModelTypeBitSet(), | |
| 101 download_progress_markers, | |
| 102 false, | |
| 103 true, | |
| 104 100, | |
| 105 8, | |
| 106 5, | |
| 107 false, | |
| 108 sessions::SyncSourceInfo(), | |
| 109 0); | |
| 110 DictionaryValue expected_details; | |
| 111 expected_details.Set("snapshot", snapshot.ToValue()); | |
| 112 | |
| 113 EXPECT_CALL(mock_js_event_handler_, | |
| 114 HandleJsEvent("onSyncCycleCompleted", | |
| 115 HasDetailsAsDictionary(expected_details))); | |
| 116 | |
| 117 js_sync_manager_observer_.OnSyncCycleCompleted(&snapshot); | |
| 118 PumpLoop(); | |
| 119 } | |
| 120 | |
| 121 TEST_F(JsSyncManagerObserverTest, OnAuthError) { | |
| 122 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | |
| 123 DictionaryValue expected_details; | |
| 124 expected_details.Set("authError", error.ToValue()); | |
| 125 | |
| 126 EXPECT_CALL(mock_js_event_handler_, | |
| 127 HandleJsEvent("onAuthError", | |
| 128 HasDetailsAsDictionary(expected_details))); | |
| 129 | |
| 130 js_sync_manager_observer_.OnAuthError(error); | |
| 131 PumpLoop(); | |
| 132 } | |
| 133 | |
| 134 TEST_F(JsSyncManagerObserverTest, OnPassphraseRequired) { | |
| 135 InSequence dummy; | |
| 136 | |
| 137 DictionaryValue reason_passphrase_not_required_details; | |
| 138 DictionaryValue reason_encryption_details; | |
| 139 DictionaryValue reason_decryption_details; | |
| 140 DictionaryValue reason_set_passphrase_failed_details; | |
| 141 | |
| 142 reason_passphrase_not_required_details.SetString( | |
| 143 "reason", | |
| 144 sync_api::PassphraseRequiredReasonToString( | |
| 145 sync_api::REASON_PASSPHRASE_NOT_REQUIRED)); | |
| 146 reason_encryption_details.SetString( | |
| 147 "reason", | |
| 148 sync_api::PassphraseRequiredReasonToString(sync_api::REASON_ENCRYPTION)); | |
| 149 reason_decryption_details.SetString( | |
| 150 "reason", | |
| 151 sync_api::PassphraseRequiredReasonToString(sync_api::REASON_DECRYPTION)); | |
| 152 reason_set_passphrase_failed_details.SetString( | |
| 153 "reason", | |
| 154 sync_api::PassphraseRequiredReasonToString( | |
| 155 sync_api::REASON_SET_PASSPHRASE_FAILED)); | |
| 156 | |
| 157 EXPECT_CALL(mock_js_event_handler_, | |
| 158 HandleJsEvent("onPassphraseRequired", | |
| 159 HasDetailsAsDictionary( | |
| 160 reason_passphrase_not_required_details))); | |
| 161 EXPECT_CALL(mock_js_event_handler_, | |
| 162 HandleJsEvent("onPassphraseRequired", | |
| 163 HasDetailsAsDictionary(reason_encryption_details))); | |
| 164 EXPECT_CALL(mock_js_event_handler_, | |
| 165 HandleJsEvent("onPassphraseRequired", | |
| 166 HasDetailsAsDictionary(reason_decryption_details))); | |
| 167 EXPECT_CALL(mock_js_event_handler_, | |
| 168 HandleJsEvent("onPassphraseRequired", | |
| 169 HasDetailsAsDictionary( | |
| 170 reason_set_passphrase_failed_details))); | |
| 171 | |
| 172 js_sync_manager_observer_.OnPassphraseRequired( | |
| 173 sync_api::REASON_PASSPHRASE_NOT_REQUIRED); | |
| 174 js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_ENCRYPTION); | |
| 175 js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_DECRYPTION); | |
| 176 js_sync_manager_observer_.OnPassphraseRequired( | |
| 177 sync_api::REASON_SET_PASSPHRASE_FAILED); | |
| 178 PumpLoop(); | |
| 179 } | |
| 180 | |
| 181 TEST_F(JsSyncManagerObserverTest, SensitiveNotifiations) { | |
| 182 DictionaryValue redacted_token_details; | |
| 183 redacted_token_details.SetString("token", "<redacted>"); | |
| 184 DictionaryValue redacted_bootstrap_token_details; | |
| 185 redacted_bootstrap_token_details.SetString("bootstrapToken", "<redacted>"); | |
| 186 | |
| 187 EXPECT_CALL(mock_js_event_handler_, | |
| 188 HandleJsEvent("onUpdatedToken", | |
| 189 HasDetailsAsDictionary(redacted_token_details))); | |
| 190 EXPECT_CALL(mock_js_event_handler_, | |
| 191 HandleJsEvent( | |
| 192 "onPassphraseAccepted", | |
| 193 HasDetailsAsDictionary(redacted_bootstrap_token_details))); | |
| 194 | |
| 195 js_sync_manager_observer_.OnUpdatedToken("sensitive_token"); | |
| 196 js_sync_manager_observer_.OnPassphraseAccepted("sensitive_token"); | |
| 197 PumpLoop(); | |
| 198 } | |
| 199 | |
| 200 TEST_F(JsSyncManagerObserverTest, OnEncryptionComplete) { | |
| 201 DictionaryValue expected_details; | |
| 202 ListValue* encrypted_type_values = new ListValue(); | |
| 203 expected_details.Set("encryptedTypes", encrypted_type_values); | |
| 204 syncable::ModelTypeSet encrypted_types; | |
| 205 | |
| 206 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 207 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 208 syncable::ModelType type = syncable::ModelTypeFromInt(i); | |
| 209 encrypted_types.insert(type); | |
| 210 encrypted_type_values->Append(Value::CreateStringValue( | |
| 211 syncable::ModelTypeToString(type))); | |
| 212 } | |
| 213 | |
| 214 EXPECT_CALL(mock_js_event_handler_, | |
| 215 HandleJsEvent("onEncryptionComplete", | |
| 216 HasDetailsAsDictionary(expected_details))); | |
| 217 | |
| 218 js_sync_manager_observer_.OnEncryptionComplete(encrypted_types); | |
| 219 PumpLoop(); | |
| 220 } | |
| 221 | |
| 222 TEST_F(JsSyncManagerObserverTest, OnMigrationNeededForTypes) { | |
| 223 DictionaryValue expected_details; | |
| 224 ListValue* type_values = new ListValue(); | |
| 225 expected_details.Set("types", type_values); | |
| 226 syncable::ModelTypeSet types; | |
| 227 | |
| 228 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 229 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 230 syncable::ModelType type = syncable::ModelTypeFromInt(i); | |
| 231 types.insert(type); | |
| 232 type_values->Append(Value::CreateStringValue( | |
| 233 syncable::ModelTypeToString(type))); | |
| 234 } | |
| 235 | |
| 236 EXPECT_CALL(mock_js_event_handler_, | |
| 237 HandleJsEvent("onMigrationNeededForTypes", | |
| 238 HasDetailsAsDictionary(expected_details))); | |
| 239 | |
| 240 js_sync_manager_observer_.OnMigrationNeededForTypes(types); | |
| 241 PumpLoop(); | |
| 242 } | |
| 243 | |
| 244 namespace { | |
| 245 | |
| 246 // Makes a node of the given model type. Returns the id of the | |
| 247 // newly-created node. | |
| 248 int64 MakeNode(sync_api::UserShare* share, syncable::ModelType model_type) { | |
| 249 sync_api::WriteTransaction trans(FROM_HERE, share); | |
| 250 sync_api::ReadNode root_node(&trans); | |
| 251 root_node.InitByRootLookup(); | |
| 252 sync_api::WriteNode node(&trans); | |
| 253 EXPECT_TRUE(node.InitUniqueByCreation( | |
| 254 model_type, root_node, | |
| 255 syncable::ModelTypeToString(model_type))); | |
| 256 node.SetIsFolder(false); | |
| 257 return node.GetId(); | |
| 258 } | |
| 259 | |
| 260 } // namespace | |
| 261 | |
| 262 TEST_F(JsSyncManagerObserverTest, OnChangesApplied) { | |
| 263 InSequence dummy; | |
| 264 | |
| 265 TestUserShare test_user_share; | |
| 266 test_user_share.SetUp(); | |
| 267 | |
| 268 // We don't test with passwords as that requires additional setup. | |
| 269 | |
| 270 // Build a list of example ChangeRecords. | |
| 271 sync_api::SyncManager::ChangeRecord changes[syncable::MODEL_TYPE_COUNT]; | |
| 272 for (int i = syncable::AUTOFILL_PROFILE; | |
| 273 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 274 changes[i].id = | |
| 275 MakeNode(test_user_share.user_share(), syncable::ModelTypeFromInt(i)); | |
| 276 switch (i % 3) { | |
| 277 case 0: | |
| 278 changes[i].action = | |
| 279 sync_api::SyncManager::ChangeRecord::ACTION_ADD; | |
| 280 break; | |
| 281 case 1: | |
| 282 changes[i].action = | |
| 283 sync_api::SyncManager::ChangeRecord::ACTION_UPDATE; | |
| 284 break; | |
| 285 default: | |
| 286 changes[i].action = | |
| 287 sync_api::SyncManager::ChangeRecord::ACTION_DELETE; | |
| 288 break; | |
| 289 } | |
| 290 { | |
| 291 sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); | |
| 292 sync_api::ReadNode node(&trans); | |
| 293 EXPECT_TRUE(node.InitByIdLookup(changes[i].id)); | |
| 294 changes[i].specifics = node.GetEntry()->Get(syncable::SPECIFICS); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 // For each i, we call OnChangesApplied() with the first arg equal | |
| 299 // to i cast to ModelType and the second argument with the changes | |
| 300 // starting from changes[i]. | |
| 301 | |
| 302 // Set expectations for each data type. | |
| 303 for (int i = syncable::AUTOFILL_PROFILE; | |
| 304 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 305 const std::string& model_type_str = | |
| 306 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); | |
| 307 DictionaryValue expected_details; | |
| 308 expected_details.SetString("modelType", model_type_str); | |
| 309 ListValue* expected_changes = new ListValue(); | |
| 310 expected_details.Set("changes", expected_changes); | |
| 311 for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { | |
| 312 sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); | |
| 313 expected_changes->Append(changes[j].ToValue(&trans)); | |
| 314 } | |
| 315 EXPECT_CALL(mock_js_event_handler_, | |
| 316 HandleJsEvent("onChangesApplied", | |
| 317 HasDetailsAsDictionary(expected_details))); | |
| 318 } | |
| 319 | |
| 320 // Fire OnChangesApplied() for each data type. | |
| 321 for (int i = syncable::AUTOFILL_PROFILE; | |
| 322 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 323 sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); | |
| 324 js_sync_manager_observer_.OnChangesApplied(syncable::ModelTypeFromInt(i), | |
| 325 &trans, &changes[i], | |
| 326 syncable::MODEL_TYPE_COUNT - i); | |
| 327 } | |
| 328 | |
| 329 test_user_share.TearDown(); | |
| 330 PumpLoop(); | |
| 331 } | |
| 332 | |
| 333 } // namespace | |
| 334 } // namespace browser_sync | |
| OLD | NEW |