OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <set> |
5 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
6 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
7 #include "chrome/browser/sync/profile_sync_service_mock.h" | 8 #include "chrome/browser/sync/profile_sync_service_mock.h" |
8 #include "chrome/browser/sync/sync_ui_util.h" | 9 #include "chrome/browser/sync/sync_ui_util.h" |
9 #include "content/test/test_browser_thread.h" | 10 #include "content/test/test_browser_thread.h" |
10 #include "testing/gmock/include/gmock/gmock-actions.h" | 11 #include "testing/gmock/include/gmock/gmock-actions.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 using ::testing::Return; | 15 using ::testing::Return; |
| 16 using ::testing::ReturnRef; |
15 using ::testing::NiceMock; | 17 using ::testing::NiceMock; |
16 using content::BrowserThread; | 18 using content::BrowserThread; |
17 | 19 |
| 20 // A number of distinct states of the ProfileSyncService can be generated for |
| 21 // tests. |
| 22 enum DistinctState { |
| 23 STATUS_CASE_SETUP_IN_PROGRESS, |
| 24 STATUS_CASE_SETUP_ERROR, |
| 25 STATUS_CASE_AUTHENTICATING, |
| 26 STATUS_CASE_AUTH_ERROR, |
| 27 STATUS_CASE_PROTOCOL_ERROR, |
| 28 STATUS_CASE_PASSPHRASE_ERROR, |
| 29 STATUS_CASE_SYNCED, |
| 30 NUMBER_OF_STATUS_CASES |
| 31 }; |
| 32 |
18 namespace { | 33 namespace { |
19 | 34 |
20 // Utility function to test that GetStatusLabelsForSyncGlobalError returns | 35 // Utility function to test that GetStatusLabelsForSyncGlobalError returns |
21 // the correct results for the given states. | 36 // the correct results for the given states. |
22 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service, | 37 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service, |
23 GoogleServiceAuthError::State error_state, | 38 GoogleServiceAuthError::State error_state, |
24 bool is_signed_in, | 39 bool is_signed_in, |
25 bool is_error) { | 40 bool is_error) { |
26 GoogleServiceAuthError auth_error(error_state); | |
27 service->UpdateAuthErrorState(auth_error); | |
28 | |
29 EXPECT_CALL(*service, HasSyncSetupCompleted()) | 41 EXPECT_CALL(*service, HasSyncSetupCompleted()) |
30 .WillRepeatedly(Return(is_signed_in)); | 42 .WillRepeatedly(Return(is_signed_in)); |
| 43 |
31 if (error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { | 44 if (error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { |
32 EXPECT_CALL(*service, GetAuthenticatedUsername()) | 45 EXPECT_CALL(*service, GetAuthenticatedUsername()) |
33 .WillRepeatedly(Return(UTF8ToUTF16(""))); | 46 .WillRepeatedly(Return(UTF8ToUTF16(""))); |
34 } else { | 47 } else { |
35 EXPECT_CALL(*service, GetAuthenticatedUsername()) | 48 EXPECT_CALL(*service, GetAuthenticatedUsername()) |
36 .WillRepeatedly(Return(UTF8ToUTF16("foo"))); | 49 .WillRepeatedly(Return(UTF8ToUTF16("foo"))); |
37 } | 50 } |
38 | 51 |
| 52 GoogleServiceAuthError auth_error(error_state); |
| 53 EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error)); |
| 54 |
39 string16 label1, label2, label3; | 55 string16 label1, label2, label3; |
40 sync_ui_util::GetStatusLabelsForSyncGlobalError( | 56 sync_ui_util::GetStatusLabelsForSyncGlobalError( |
41 service, &label1, &label2, &label3); | 57 service, &label1, &label2, &label3); |
42 EXPECT_EQ(label1.empty(), !is_error); | 58 EXPECT_EQ(label1.empty(), !is_error); |
43 EXPECT_EQ(label2.empty(), !is_error); | 59 EXPECT_EQ(label2.empty(), !is_error); |
44 EXPECT_EQ(label3.empty(), !is_error); | 60 EXPECT_EQ(label3.empty(), !is_error); |
45 } | 61 } |
46 | 62 |
47 } // namespace | 63 } // namespace |
48 | 64 |
49 TEST(SyncUIUtilTest, ConstructAboutInformationWithUnrecoverableErrorTest) { | 65 TEST(SyncUIUtilTest, ConstructAboutInformationWithUnrecoverableErrorTest) { |
50 MessageLoopForUI message_loop; | 66 MessageLoopForUI message_loop; |
51 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 67 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
52 NiceMock<ProfileSyncServiceMock> service; | 68 NiceMock<ProfileSyncServiceMock> service; |
53 DictionaryValue strings; | 69 DictionaryValue strings; |
54 | 70 |
55 // Will be released when the dictionary is destroyed | 71 // Will be released when the dictionary is destroyed |
56 string16 str(ASCIIToUTF16("none")); | 72 string16 str(ASCIIToUTF16("none")); |
57 | 73 |
58 browser_sync::SyncBackendHost::Status status; | 74 browser_sync::SyncBackendHost::Status status; |
59 status.summary = browser_sync::SyncBackendHost::Status::OFFLINE_UNUSABLE; | 75 status.summary = browser_sync::SyncBackendHost::Status::OFFLINE_UNUSABLE; |
60 | 76 |
61 EXPECT_CALL(service, HasSyncSetupCompleted()) | 77 EXPECT_CALL(service, HasSyncSetupCompleted()) |
62 .WillOnce(Return(true)); | 78 .WillOnce(Return(true)); |
63 EXPECT_CALL(service, QueryDetailedSyncStatus()) | 79 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
64 .WillOnce(Return(status)); | 80 .WillOnce(Return(status)); |
65 | 81 |
| 82 GoogleServiceAuthError auth_error(GoogleServiceAuthError::NONE); |
| 83 EXPECT_CALL(service, GetAuthError()).WillOnce(ReturnRef(auth_error)); |
| 84 |
66 EXPECT_CALL(service, unrecoverable_error_detected()) | 85 EXPECT_CALL(service, unrecoverable_error_detected()) |
67 .WillOnce(Return(true)); | 86 .WillOnce(Return(true)); |
68 | 87 |
69 EXPECT_CALL(service, GetLastSyncedTimeString()) | 88 EXPECT_CALL(service, GetLastSyncedTimeString()) |
70 .WillOnce(Return(str)); | 89 .WillOnce(Return(str)); |
71 | 90 |
72 sync_ui_util::ConstructAboutInformation(&service, &strings); | 91 sync_ui_util::ConstructAboutInformation(&service, &strings); |
73 | 92 |
74 EXPECT_TRUE(strings.HasKey("unrecoverable_error_detected")); | 93 EXPECT_TRUE(strings.HasKey("unrecoverable_error_detected")); |
75 } | 94 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | 137 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
119 }; | 138 }; |
120 | 139 |
121 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { | 140 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { |
122 VerifySyncGlobalErrorResult( | 141 VerifySyncGlobalErrorResult( |
123 &service, table[i].error_state, true, table[i].is_error); | 142 &service, table[i].error_state, true, table[i].is_error); |
124 VerifySyncGlobalErrorResult( | 143 VerifySyncGlobalErrorResult( |
125 &service, table[i].error_state, false, false); | 144 &service, table[i].error_state, false, false); |
126 } | 145 } |
127 } | 146 } |
| 147 // Loads a ProfileSyncServiceMock to emulate one of a number of distinct cases |
| 148 // in order to perform tests on the generated messages. |
| 149 void GetDistinctCase(ProfileSyncServiceMock& service, |
| 150 GoogleServiceAuthError** auth_error, |
| 151 int caseNumber) { |
| 152 // Auth Error object is returned by reference in mock and needs to stay in |
| 153 // scope throughout test, so it is owned by calling method. However it is |
| 154 // immutable so can only be allocated in this method. |
| 155 switch (caseNumber) { |
| 156 case STATUS_CASE_SETUP_IN_PROGRESS: { |
| 157 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 158 .WillOnce(Return(false)); |
| 159 EXPECT_CALL(service, SetupInProgress()) |
| 160 .WillOnce(Return(true)); |
| 161 browser_sync::SyncBackendHost::Status status; |
| 162 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 163 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 164 .WillOnce(Return(status)); |
| 165 *auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
| 166 EXPECT_CALL(service, GetAuthError()) |
| 167 .WillOnce(ReturnRef(**auth_error)); |
| 168 EXPECT_CALL(service, UIShouldDepictAuthInProgress()) |
| 169 .WillOnce(Return(false)); |
| 170 return; |
| 171 } |
| 172 case STATUS_CASE_SETUP_ERROR: { |
| 173 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 174 .WillOnce(Return(false)); |
| 175 EXPECT_CALL(service, SetupInProgress()) |
| 176 .WillOnce(Return(false)); |
| 177 EXPECT_CALL(service, unrecoverable_error_detected()) |
| 178 .WillOnce(Return(true)); |
| 179 browser_sync::SyncBackendHost::Status status; |
| 180 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 181 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 182 .WillOnce(Return(status)); |
| 183 return; |
| 184 } |
| 185 case STATUS_CASE_AUTHENTICATING: { |
| 186 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 187 .WillOnce(Return(true)); |
| 188 browser_sync::SyncBackendHost::Status status; |
| 189 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 190 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 191 .WillOnce(Return(status)); |
| 192 EXPECT_CALL(service, UIShouldDepictAuthInProgress()) |
| 193 .WillOnce(Return(true)); |
| 194 *auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
| 195 EXPECT_CALL(service, GetAuthError()) |
| 196 .WillOnce(ReturnRef(**auth_error)); |
| 197 return; |
| 198 } |
| 199 case STATUS_CASE_AUTH_ERROR: { |
| 200 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 201 .WillOnce(Return(true)); |
| 202 browser_sync::SyncBackendHost::Status status; |
| 203 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 204 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 205 .WillOnce(Return(status)); |
| 206 *auth_error = new GoogleServiceAuthError( |
| 207 GoogleServiceAuthError::SERVICE_UNAVAILABLE); |
| 208 EXPECT_CALL(service, GetAuthenticatedUsername()) |
| 209 .WillOnce(Return(ASCIIToUTF16(""))); |
| 210 EXPECT_CALL(service, GetAuthError()) |
| 211 .WillOnce(ReturnRef(**auth_error)); |
| 212 EXPECT_CALL(service, UIShouldDepictAuthInProgress()) |
| 213 .WillOnce(Return(false)); |
| 214 return; |
| 215 } |
| 216 case STATUS_CASE_PROTOCOL_ERROR: { |
| 217 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 218 .WillOnce(Return(true)); |
| 219 browser_sync::SyncProtocolError protocolError; |
| 220 protocolError.action = browser_sync::STOP_AND_RESTART_SYNC; |
| 221 browser_sync::SyncBackendHost::Status status; |
| 222 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 223 status.sync_protocol_error = protocolError; |
| 224 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 225 .WillOnce(Return(status)); |
| 226 *auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
| 227 EXPECT_CALL(service, GetAuthError()) |
| 228 .WillOnce(ReturnRef(**auth_error)); |
| 229 EXPECT_CALL(service, UIShouldDepictAuthInProgress()) |
| 230 .WillOnce(Return(false)); |
| 231 return; |
| 232 } |
| 233 case STATUS_CASE_PASSPHRASE_ERROR: { |
| 234 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 235 .WillOnce(Return(true)); |
| 236 browser_sync::SyncBackendHost::Status status; |
| 237 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 238 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 239 .WillOnce(Return(status)); |
| 240 *auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
| 241 EXPECT_CALL(service, GetAuthError()) |
| 242 .WillOnce(ReturnRef(**auth_error)); |
| 243 EXPECT_CALL(service, GetAuthenticatedUsername()) |
| 244 .WillOnce(Return(ASCIIToUTF16("example@example.com"))); |
| 245 EXPECT_CALL(service, UIShouldDepictAuthInProgress()) |
| 246 .WillOnce(Return(false)); |
| 247 EXPECT_CALL(service, IsPassphraseRequired()) |
| 248 .WillOnce(Return(true)); |
| 249 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) |
| 250 .WillOnce(Return(true)); |
| 251 return; |
| 252 } |
| 253 case STATUS_CASE_SYNCED: { |
| 254 EXPECT_CALL(service, HasSyncSetupCompleted()) |
| 255 .WillOnce(Return(true)); |
| 256 browser_sync::SyncBackendHost::Status status; |
| 257 status.summary = browser_sync::SyncBackendHost::Status::READY; |
| 258 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 259 .WillOnce(Return(status)); |
| 260 *auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE); |
| 261 EXPECT_CALL(service, GetAuthError()) |
| 262 .WillOnce(ReturnRef(**auth_error)); |
| 263 EXPECT_CALL(service, GetAuthenticatedUsername()) |
| 264 .WillOnce(Return(ASCIIToUTF16("example@example.com"))); |
| 265 EXPECT_CALL(service, UIShouldDepictAuthInProgress()) |
| 266 .WillOnce(Return(false)); |
| 267 EXPECT_CALL(service, IsPassphraseRequired()) |
| 268 .WillOnce(Return(false)); |
| 269 return; |
| 270 } |
| 271 default: |
| 272 NOTREACHED(); |
| 273 } |
| 274 } |
| 275 |
| 276 // This test ensures that a each distinctive ProfileSyncService statuses |
| 277 // will return a unique combination of status and link messages from |
| 278 // GetStatusLabels(). |
| 279 TEST(SyncUIUtilTest, DistinctCasesReportUniqueMessageSets) { |
| 280 ProfileSyncServiceMock service; |
| 281 |
| 282 std::set<string16> messages; |
| 283 for (int idx = 0; idx != NUMBER_OF_STATUS_CASES; idx++) { |
| 284 ProfileSyncServiceMock service; |
| 285 GoogleServiceAuthError* auth_error = NULL; |
| 286 GetDistinctCase(service, &auth_error, idx); |
| 287 string16 status_label; |
| 288 string16 link_label; |
| 289 sync_ui_util::GetStatusLabels(&service, |
| 290 sync_ui_util::WITH_HTML, |
| 291 &status_label, |
| 292 &link_label); |
| 293 // If the status and link message combination is already present in the set |
| 294 // of messages already seen, this is a duplicate rather than a unique |
| 295 // message, and the test has failed. |
| 296 string16 combined_label = |
| 297 status_label + string16(ASCIIToUTF16("#")) + link_label; |
| 298 EXPECT_TRUE(messages.find(combined_label) == messages.end()); |
| 299 messages.insert(combined_label); |
| 300 if (auth_error) |
| 301 delete auth_error; |
| 302 } |
| 303 } |
| 304 |
| 305 // This test ensures that the html_links parameter on GetStatusLabels() is |
| 306 // honored. |
| 307 TEST(SyncUIUtilTest, HtmlNotIncludedInStatusIfNotRequested) { |
| 308 ProfileSyncServiceMock service; |
| 309 for (int idx = 0; idx != NUMBER_OF_STATUS_CASES; idx++) { |
| 310 ProfileSyncServiceMock service; |
| 311 GoogleServiceAuthError* auth_error = NULL; |
| 312 GetDistinctCase(service, &auth_error, idx); |
| 313 string16 status_label; |
| 314 string16 link_label; |
| 315 sync_ui_util::GetStatusLabels(&service, |
| 316 sync_ui_util::PLAIN_TEXT, |
| 317 &status_label, |
| 318 &link_label); |
| 319 // Ensures a search for string 'href' (found in links, not a string to be |
| 320 // found in an English language message) fails when links are excluded from |
| 321 // the status label. |
| 322 EXPECT_EQ(status_label.find(string16(ASCIIToUTF16("href"))), |
| 323 string16::npos); |
| 324 if (auth_error) { |
| 325 delete auth_error; |
| 326 } |
| 327 } |
| 328 } |
OLD | NEW |