| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/js/js_sync_manager_observer.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/sync/js/js_event_details.h" | |
| 12 #include "chrome/browser/sync/js/js_test_util.h" | |
| 13 #include "chrome/browser/sync/protocol/sync_protocol_error.h" | |
| 14 #include "chrome/browser/sync/sessions/session_state.h" | |
| 15 #include "chrome/browser/sync/syncable/model_type.h" | |
| 16 #include "chrome/browser/sync/util/weak_handle.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace browser_sync { | |
| 20 namespace { | |
| 21 | |
| 22 using ::testing::InSequence; | |
| 23 using ::testing::StrictMock; | |
| 24 | |
| 25 class JsSyncManagerObserverTest : public testing::Test { | |
| 26 protected: | |
| 27 JsSyncManagerObserverTest() { | |
| 28 js_sync_manager_observer_.SetJsEventHandler( | |
| 29 mock_js_event_handler_.AsWeakHandle()); | |
| 30 } | |
| 31 | |
| 32 private: | |
| 33 // This must be destroyed after the member variables below in order | |
| 34 // for WeakHandles to be destroyed properly. | |
| 35 MessageLoop message_loop_; | |
| 36 | |
| 37 protected: | |
| 38 StrictMock<MockJsEventHandler> mock_js_event_handler_; | |
| 39 JsSyncManagerObserver js_sync_manager_observer_; | |
| 40 | |
| 41 void PumpLoop() { | |
| 42 message_loop_.RunAllPending(); | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 TEST_F(JsSyncManagerObserverTest, NoArgNotifiations) { | |
| 47 InSequence dummy; | |
| 48 | |
| 49 EXPECT_CALL(mock_js_event_handler_, | |
| 50 HandleJsEvent("onInitializationComplete", | |
| 51 HasDetails(JsEventDetails()))); | |
| 52 EXPECT_CALL(mock_js_event_handler_, | |
| 53 HandleJsEvent("onStopSyncingPermanently", | |
| 54 HasDetails(JsEventDetails()))); | |
| 55 EXPECT_CALL(mock_js_event_handler_, | |
| 56 HandleJsEvent("onClearServerDataSucceeded", | |
| 57 HasDetails(JsEventDetails()))); | |
| 58 EXPECT_CALL(mock_js_event_handler_, | |
| 59 HandleJsEvent("onClearServerDataFailed", | |
| 60 HasDetails(JsEventDetails()))); | |
| 61 EXPECT_CALL(mock_js_event_handler_, | |
| 62 HandleJsEvent("onEncryptionComplete", | |
| 63 HasDetails(JsEventDetails()))); | |
| 64 | |
| 65 js_sync_manager_observer_.OnInitializationComplete(WeakHandle<JsBackend>(), | |
| 66 true); | |
| 67 js_sync_manager_observer_.OnStopSyncingPermanently(); | |
| 68 js_sync_manager_observer_.OnClearServerDataSucceeded(); | |
| 69 js_sync_manager_observer_.OnClearServerDataFailed(); | |
| 70 js_sync_manager_observer_.OnEncryptionComplete(); | |
| 71 PumpLoop(); | |
| 72 } | |
| 73 | |
| 74 TEST_F(JsSyncManagerObserverTest, OnSyncCycleCompleted) { | |
| 75 std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; | |
| 76 sessions::SyncSessionSnapshot snapshot(sessions::SyncerStatus(), | |
| 77 sessions::ErrorCounters(), | |
| 78 100, | |
| 79 false, | |
| 80 syncable::ModelTypeSet(), | |
| 81 download_progress_markers, | |
| 82 false, | |
| 83 true, | |
| 84 100, | |
| 85 8, | |
| 86 5, | |
| 87 2, | |
| 88 7, | |
| 89 false, | |
| 90 sessions::SyncSourceInfo(), | |
| 91 0, | |
| 92 base::Time::Now(), | |
| 93 false); | |
| 94 DictionaryValue expected_details; | |
| 95 expected_details.Set("snapshot", snapshot.ToValue()); | |
| 96 | |
| 97 EXPECT_CALL(mock_js_event_handler_, | |
| 98 HandleJsEvent("onSyncCycleCompleted", | |
| 99 HasDetailsAsDictionary(expected_details))); | |
| 100 | |
| 101 js_sync_manager_observer_.OnSyncCycleCompleted(&snapshot); | |
| 102 PumpLoop(); | |
| 103 } | |
| 104 | |
| 105 TEST_F(JsSyncManagerObserverTest, OnActionableError) { | |
| 106 browser_sync::SyncProtocolError sync_error; | |
| 107 sync_error.action = browser_sync::CLEAR_USER_DATA_AND_RESYNC; | |
| 108 sync_error.error_type = browser_sync::TRANSIENT_ERROR; | |
| 109 DictionaryValue expected_details; | |
| 110 expected_details.Set("syncError", sync_error.ToValue()); | |
| 111 | |
| 112 EXPECT_CALL(mock_js_event_handler_, | |
| 113 HandleJsEvent("onActionableError", | |
| 114 HasDetailsAsDictionary(expected_details))); | |
| 115 | |
| 116 js_sync_manager_observer_.OnActionableError(sync_error); | |
| 117 PumpLoop(); | |
| 118 } | |
| 119 | |
| 120 | |
| 121 TEST_F(JsSyncManagerObserverTest, OnConnectionStatusChange) { | |
| 122 const sync_api::ConnectionStatus kStatus = | |
| 123 sync_api::CONNECTION_AUTH_ERROR; | |
| 124 DictionaryValue expected_details; | |
| 125 expected_details.SetString("status", | |
| 126 sync_api::ConnectionStatusToString(kStatus)); | |
| 127 | |
| 128 EXPECT_CALL(mock_js_event_handler_, | |
| 129 HandleJsEvent("onConnectionStatusChange", | |
| 130 HasDetailsAsDictionary(expected_details))); | |
| 131 | |
| 132 js_sync_manager_observer_.OnConnectionStatusChange(kStatus); | |
| 133 PumpLoop(); | |
| 134 } | |
| 135 | |
| 136 TEST_F(JsSyncManagerObserverTest, OnPassphraseRequired) { | |
| 137 InSequence dummy; | |
| 138 | |
| 139 DictionaryValue reason_passphrase_not_required_details; | |
| 140 DictionaryValue reason_encryption_details; | |
| 141 DictionaryValue reason_decryption_details; | |
| 142 DictionaryValue reason_set_passphrase_failed_details; | |
| 143 | |
| 144 reason_passphrase_not_required_details.SetString( | |
| 145 "reason", | |
| 146 sync_api::PassphraseRequiredReasonToString( | |
| 147 sync_api::REASON_PASSPHRASE_NOT_REQUIRED)); | |
| 148 reason_encryption_details.SetString( | |
| 149 "reason", | |
| 150 sync_api::PassphraseRequiredReasonToString(sync_api::REASON_ENCRYPTION)); | |
| 151 reason_decryption_details.SetString( | |
| 152 "reason", | |
| 153 sync_api::PassphraseRequiredReasonToString(sync_api::REASON_DECRYPTION)); | |
| 154 reason_set_passphrase_failed_details.SetString( | |
| 155 "reason", | |
| 156 sync_api::PassphraseRequiredReasonToString( | |
| 157 sync_api::REASON_SET_PASSPHRASE_FAILED)); | |
| 158 | |
| 159 EXPECT_CALL(mock_js_event_handler_, | |
| 160 HandleJsEvent("onPassphraseRequired", | |
| 161 HasDetailsAsDictionary( | |
| 162 reason_passphrase_not_required_details))); | |
| 163 EXPECT_CALL(mock_js_event_handler_, | |
| 164 HandleJsEvent("onPassphraseRequired", | |
| 165 HasDetailsAsDictionary(reason_encryption_details))); | |
| 166 EXPECT_CALL(mock_js_event_handler_, | |
| 167 HandleJsEvent("onPassphraseRequired", | |
| 168 HasDetailsAsDictionary(reason_decryption_details))); | |
| 169 EXPECT_CALL(mock_js_event_handler_, | |
| 170 HandleJsEvent("onPassphraseRequired", | |
| 171 HasDetailsAsDictionary( | |
| 172 reason_set_passphrase_failed_details))); | |
| 173 | |
| 174 js_sync_manager_observer_.OnPassphraseRequired( | |
| 175 sync_api::REASON_PASSPHRASE_NOT_REQUIRED, | |
| 176 sync_pb::EncryptedData()); | |
| 177 js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_ENCRYPTION, | |
| 178 sync_pb::EncryptedData()); | |
| 179 js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_DECRYPTION, | |
| 180 sync_pb::EncryptedData()); | |
| 181 js_sync_manager_observer_.OnPassphraseRequired( | |
| 182 sync_api::REASON_SET_PASSPHRASE_FAILED, | |
| 183 sync_pb::EncryptedData()); | |
| 184 PumpLoop(); | |
| 185 } | |
| 186 | |
| 187 TEST_F(JsSyncManagerObserverTest, SensitiveNotifiations) { | |
| 188 DictionaryValue redacted_token_details; | |
| 189 redacted_token_details.SetString("token", "<redacted>"); | |
| 190 DictionaryValue redacted_bootstrap_token_details; | |
| 191 redacted_bootstrap_token_details.SetString("bootstrapToken", "<redacted>"); | |
| 192 | |
| 193 EXPECT_CALL(mock_js_event_handler_, | |
| 194 HandleJsEvent("onUpdatedToken", | |
| 195 HasDetailsAsDictionary(redacted_token_details))); | |
| 196 EXPECT_CALL(mock_js_event_handler_, | |
| 197 HandleJsEvent( | |
| 198 "OnBootstrapTokenUpdated", | |
| 199 HasDetailsAsDictionary(redacted_bootstrap_token_details))); | |
| 200 | |
| 201 js_sync_manager_observer_.OnUpdatedToken("sensitive_token"); | |
| 202 js_sync_manager_observer_.OnBootstrapTokenUpdated("sensitive_token"); | |
| 203 PumpLoop(); | |
| 204 } | |
| 205 | |
| 206 TEST_F(JsSyncManagerObserverTest, OnEncryptedTypesChanged) { | |
| 207 DictionaryValue expected_details; | |
| 208 ListValue* encrypted_type_values = new ListValue(); | |
| 209 const bool encrypt_everything = false; | |
| 210 expected_details.Set("encryptedTypes", encrypted_type_values); | |
| 211 expected_details.SetBoolean("encryptEverything", encrypt_everything); | |
| 212 syncable::ModelTypeSet encrypted_types; | |
| 213 | |
| 214 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 215 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 216 syncable::ModelType type = syncable::ModelTypeFromInt(i); | |
| 217 encrypted_types.Put(type); | |
| 218 encrypted_type_values->Append(Value::CreateStringValue( | |
| 219 syncable::ModelTypeToString(type))); | |
| 220 } | |
| 221 | |
| 222 EXPECT_CALL(mock_js_event_handler_, | |
| 223 HandleJsEvent("onEncryptedTypesChanged", | |
| 224 HasDetailsAsDictionary(expected_details))); | |
| 225 | |
| 226 js_sync_manager_observer_.OnEncryptedTypesChanged( | |
| 227 encrypted_types, encrypt_everything); | |
| 228 PumpLoop(); | |
| 229 } | |
| 230 | |
| 231 } // namespace | |
| 232 } // namespace browser_sync | |
| OLD | NEW |