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> | |
6 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
7 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/sync/profile_sync_service_mock.h" | 7 #include "chrome/browser/sync/profile_sync_service_mock.h" |
9 #include "chrome/browser/sync/sync_ui_util.h" | 8 #include "chrome/browser/sync/sync_ui_util.h" |
10 #include "content/test/test_browser_thread.h" | 9 #include "content/test/test_browser_thread.h" |
11 #include "testing/gmock/include/gmock/gmock-actions.h" | 10 #include "testing/gmock/include/gmock/gmock-actions.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 | 13 |
15 using ::testing::Return; | 14 using ::testing::Return; |
16 using ::testing::ReturnRef; | |
17 using ::testing::NiceMock; | 15 using ::testing::NiceMock; |
18 using content::BrowserThread; | 16 using content::BrowserThread; |
19 | 17 |
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 | |
33 namespace { | 18 namespace { |
34 | 19 |
35 // Utility function to test that GetStatusLabelsForSyncGlobalError returns | 20 // Utility function to test that GetStatusLabelsForSyncGlobalError returns |
36 // the correct results for the given states. | 21 // the correct results for the given states. |
37 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service, | 22 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service, |
38 GoogleServiceAuthError::State error_state, | 23 GoogleServiceAuthError::State error_state, |
39 bool is_signed_in, | 24 bool is_signed_in, |
40 bool is_error) { | 25 bool is_error) { |
| 26 GoogleServiceAuthError auth_error(error_state); |
| 27 service->UpdateAuthErrorState(auth_error); |
| 28 |
41 EXPECT_CALL(*service, HasSyncSetupCompleted()) | 29 EXPECT_CALL(*service, HasSyncSetupCompleted()) |
42 .WillRepeatedly(Return(is_signed_in)); | 30 .WillRepeatedly(Return(is_signed_in)); |
43 | |
44 if (error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { | 31 if (error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { |
45 EXPECT_CALL(*service, GetAuthenticatedUsername()) | 32 EXPECT_CALL(*service, GetAuthenticatedUsername()) |
46 .WillRepeatedly(Return(UTF8ToUTF16(""))); | 33 .WillRepeatedly(Return(UTF8ToUTF16(""))); |
47 } else { | 34 } else { |
48 EXPECT_CALL(*service, GetAuthenticatedUsername()) | 35 EXPECT_CALL(*service, GetAuthenticatedUsername()) |
49 .WillRepeatedly(Return(UTF8ToUTF16("foo"))); | 36 .WillRepeatedly(Return(UTF8ToUTF16("foo"))); |
50 } | 37 } |
51 | 38 |
52 GoogleServiceAuthError auth_error(error_state); | |
53 EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error)); | |
54 | |
55 string16 label1, label2, label3; | 39 string16 label1, label2, label3; |
56 sync_ui_util::GetStatusLabelsForSyncGlobalError( | 40 sync_ui_util::GetStatusLabelsForSyncGlobalError( |
57 service, &label1, &label2, &label3); | 41 service, &label1, &label2, &label3); |
58 EXPECT_EQ(label1.empty(), !is_error); | 42 EXPECT_EQ(label1.empty(), !is_error); |
59 EXPECT_EQ(label2.empty(), !is_error); | 43 EXPECT_EQ(label2.empty(), !is_error); |
60 EXPECT_EQ(label3.empty(), !is_error); | 44 EXPECT_EQ(label3.empty(), !is_error); |
61 } | 45 } |
62 | 46 |
63 } // namespace | 47 } // namespace |
64 | 48 |
65 TEST(SyncUIUtilTest, ConstructAboutInformationWithUnrecoverableErrorTest) { | 49 TEST(SyncUIUtilTest, ConstructAboutInformationWithUnrecoverableErrorTest) { |
66 MessageLoopForUI message_loop; | 50 MessageLoopForUI message_loop; |
67 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 51 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
68 NiceMock<ProfileSyncServiceMock> service; | 52 NiceMock<ProfileSyncServiceMock> service; |
69 DictionaryValue strings; | 53 DictionaryValue strings; |
70 | 54 |
71 // Will be released when the dictionary is destroyed | 55 // Will be released when the dictionary is destroyed |
72 string16 str(ASCIIToUTF16("none")); | 56 string16 str(ASCIIToUTF16("none")); |
73 | 57 |
74 browser_sync::SyncBackendHost::Status status; | 58 browser_sync::SyncBackendHost::Status status; |
75 status.summary = browser_sync::SyncBackendHost::Status::OFFLINE_UNUSABLE; | 59 status.summary = browser_sync::SyncBackendHost::Status::OFFLINE_UNUSABLE; |
76 | 60 |
77 EXPECT_CALL(service, HasSyncSetupCompleted()) | 61 EXPECT_CALL(service, HasSyncSetupCompleted()) |
78 .WillOnce(Return(true)); | 62 .WillOnce(Return(true)); |
79 EXPECT_CALL(service, QueryDetailedSyncStatus()) | 63 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
80 .WillOnce(Return(status)); | 64 .WillOnce(Return(status)); |
81 | 65 |
82 GoogleServiceAuthError auth_error(GoogleServiceAuthError::NONE); | |
83 EXPECT_CALL(service, GetAuthError()).WillOnce(ReturnRef(auth_error)); | |
84 | |
85 EXPECT_CALL(service, unrecoverable_error_detected()) | 66 EXPECT_CALL(service, unrecoverable_error_detected()) |
86 .WillOnce(Return(true)); | 67 .WillOnce(Return(true)); |
87 | 68 |
88 EXPECT_CALL(service, GetLastSyncedTimeString()) | 69 EXPECT_CALL(service, GetLastSyncedTimeString()) |
89 .WillOnce(Return(str)); | 70 .WillOnce(Return(str)); |
90 | 71 |
91 sync_ui_util::ConstructAboutInformation(&service, &strings); | 72 sync_ui_util::ConstructAboutInformation(&service, &strings); |
92 | 73 |
93 EXPECT_TRUE(strings.HasKey("unrecoverable_error_detected")); | 74 EXPECT_TRUE(strings.HasKey("unrecoverable_error_detected")); |
94 } | 75 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | 118 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
138 }; | 119 }; |
139 | 120 |
140 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { | 121 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { |
141 VerifySyncGlobalErrorResult( | 122 VerifySyncGlobalErrorResult( |
142 &service, table[i].error_state, true, table[i].is_error); | 123 &service, table[i].error_state, true, table[i].is_error); |
143 VerifySyncGlobalErrorResult( | 124 VerifySyncGlobalErrorResult( |
144 &service, table[i].error_state, false, false); | 125 &service, table[i].error_state, false, false); |
145 } | 126 } |
146 } | 127 } |
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 |