Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / chrome / browser / sync / sync_ui_util_unittest.cc
blob84323e2d3b0b83e3ea8f9f9401a518a4bcd490b7
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.
5 #include <set>
6 #include "base/basictypes.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/signin/fake_signin_manager.h"
10 #include "chrome/browser/signin/signin_error_controller_factory.h"
11 #include "chrome/browser/sync/profile_sync_service_mock.h"
12 #include "chrome/browser/sync/sync_ui_util.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/signin/core/browser/fake_auth_status_provider.h"
15 #include "components/signin/core/browser/signin_manager.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gmock/include/gmock/gmock-actions.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/base/l10n/l10n_util.h"
23 using ::testing::AtMost;
24 using ::testing::NiceMock;
25 using ::testing::Return;
26 using ::testing::ReturnRef;
27 using ::testing::SetArgPointee;
28 using ::testing::_;
29 using content::BrowserThread;
31 // A number of distinct states of the ProfileSyncService can be generated for
32 // tests.
33 enum DistinctState {
34 STATUS_CASE_SETUP_IN_PROGRESS,
35 STATUS_CASE_SETUP_ERROR,
36 STATUS_CASE_AUTHENTICATING,
37 STATUS_CASE_AUTH_ERROR,
38 STATUS_CASE_PROTOCOL_ERROR,
39 STATUS_CASE_PASSPHRASE_ERROR,
40 STATUS_CASE_SYNCED,
41 STATUS_CASE_SYNC_DISABLED_BY_POLICY,
42 NUMBER_OF_STATUS_CASES
45 namespace {
47 const char kTestGaiaId[] = "gaia-id-test_user@test.com";
48 const char kTestUser[] = "test_user@test.com";
50 #if !defined(OS_CHROMEOS)
51 // Utility function to test that GetStatusLabelsForSyncGlobalError returns
52 // the correct results for the given states.
53 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service,
54 GoogleServiceAuthError::State error_state,
55 bool is_signed_in,
56 bool is_error) {
57 EXPECT_CALL(*service, HasSyncSetupCompleted())
58 .WillRepeatedly(Return(is_signed_in));
60 GoogleServiceAuthError auth_error(error_state);
61 EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error));
63 base::string16 label1, label2, label3;
64 sync_ui_util::GetStatusLabelsForSyncGlobalError(
65 service, &label1, &label2, &label3);
66 EXPECT_EQ(label1.empty(), !is_error);
67 EXPECT_EQ(label2.empty(), !is_error);
68 EXPECT_EQ(label3.empty(), !is_error);
70 #endif
72 } // namespace
75 class SyncUIUtilTest : public testing::Test {
76 private:
77 content::TestBrowserThreadBundle thread_bundle_;
80 #if !defined(OS_CHROMEOS)
81 // Test that GetStatusLabelsForSyncGlobalError returns an error if a
82 // passphrase is required.
83 TEST_F(SyncUIUtilTest, PassphraseGlobalError) {
84 scoped_ptr<Profile> profile(
85 ProfileSyncServiceMock::MakeSignedInTestingProfile());
86 NiceMock<ProfileSyncServiceMock> service(profile.get());
87 browser_sync::SyncBackendHost::Status status;
88 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
89 .WillRepeatedly(Return(false));
90 EXPECT_CALL(service, IsPassphraseRequired())
91 .WillRepeatedly(Return(true));
92 EXPECT_CALL(service, IsPassphraseRequiredForDecryption())
93 .WillRepeatedly(Return(true));
95 VerifySyncGlobalErrorResult(&service,
96 GoogleServiceAuthError::NONE,
97 true /* signed in */,
98 true /* error */);
101 // Test that GetStatusLabelsForSyncGlobalError returns an error if a
102 // passphrase is required and not for auth errors.
103 TEST_F(SyncUIUtilTest, AuthAndPassphraseGlobalError) {
104 scoped_ptr<Profile> profile(
105 ProfileSyncServiceMock::MakeSignedInTestingProfile());
106 NiceMock<ProfileSyncServiceMock> service(profile.get());
107 browser_sync::SyncBackendHost::Status status;
108 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
109 .WillRepeatedly(Return(false));
111 EXPECT_CALL(service, IsPassphraseRequired())
112 .WillRepeatedly(Return(true));
113 EXPECT_CALL(service, IsPassphraseRequiredForDecryption())
114 .WillRepeatedly(Return(true));
115 EXPECT_CALL(service, HasSyncSetupCompleted())
116 .WillRepeatedly(Return(true));
118 GoogleServiceAuthError auth_error(
119 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
120 EXPECT_CALL(service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error));
121 base::string16 menu_label, label2, label3;
122 sync_ui_util::GetStatusLabelsForSyncGlobalError(
123 &service, &menu_label, &label2, &label3);
124 // Make sure we are still displaying the passphrase error badge (don't show
125 // auth errors through SyncUIUtil).
126 EXPECT_EQ(menu_label, l10n_util::GetStringUTF16(
127 IDS_SYNC_PASSPHRASE_ERROR_WRENCH_MENU_ITEM));
130 // Test that GetStatusLabelsForSyncGlobalError does not indicate errors for
131 // auth errors (these are reported through SigninGlobalError).
132 TEST_F(SyncUIUtilTest, AuthStateGlobalError) {
133 scoped_ptr<Profile> profile(
134 ProfileSyncServiceMock::MakeSignedInTestingProfile());
135 NiceMock<ProfileSyncServiceMock> service(profile.get());
137 browser_sync::SyncBackendHost::Status status;
138 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
139 .WillRepeatedly(Return(false));
141 GoogleServiceAuthError::State table[] = {
142 GoogleServiceAuthError::NONE,
143 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
144 GoogleServiceAuthError::USER_NOT_SIGNED_UP,
145 GoogleServiceAuthError::CONNECTION_FAILED,
146 GoogleServiceAuthError::CAPTCHA_REQUIRED,
147 GoogleServiceAuthError::ACCOUNT_DELETED,
148 GoogleServiceAuthError::ACCOUNT_DISABLED,
149 GoogleServiceAuthError::SERVICE_UNAVAILABLE,
150 GoogleServiceAuthError::TWO_FACTOR,
151 GoogleServiceAuthError::REQUEST_CANCELED,
152 GoogleServiceAuthError::HOSTED_NOT_ALLOWED
155 FakeSigninManagerBase signin(profile.get());
156 for (size_t i = 0; i < arraysize(table); ++i) {
157 VerifySyncGlobalErrorResult(&service,
158 table[i],
159 true /* signed in */,
160 false /* no error */);
161 VerifySyncGlobalErrorResult(&service,
162 table[i],
163 false /* not signed in */,
164 false /* no error */);
167 #endif
169 // TODO(tim): This shouldn't be required. r194857 removed the
170 // AuthInProgress override from FakeSigninManager, which meant this test started
171 // using the "real" SigninManager AuthInProgress logic. Without that override,
172 // it's no longer possible to test both chrome os + desktop flows as part of the
173 // same test, because AuthInProgress is always false on chrome os. Most of the
174 // tests are unaffected, but STATUS_CASE_AUTHENTICATING can't exist in both
175 // versions, so it we will require two separate tests, one using SigninManager
176 // and one using SigninManagerBase (which require different setup procedures.
177 class FakeSigninManagerForSyncUIUtilTest : public FakeSigninManagerBase {
178 public:
179 explicit FakeSigninManagerForSyncUIUtilTest(Profile* profile)
180 : FakeSigninManagerBase(profile), auth_in_progress_(false) {
181 Initialize(NULL);
184 ~FakeSigninManagerForSyncUIUtilTest() override {}
186 bool AuthInProgress() const override { return auth_in_progress_; }
188 void set_auth_in_progress() {
189 auth_in_progress_ = true;
192 private:
193 bool auth_in_progress_;
196 // Loads a ProfileSyncServiceMock to emulate one of a number of distinct cases
197 // in order to perform tests on the generated messages.
198 void GetDistinctCase(ProfileSyncServiceMock& service,
199 FakeSigninManagerForSyncUIUtilTest* signin,
200 FakeAuthStatusProvider* provider,
201 int caseNumber) {
202 // Auth Error object is returned by reference in mock and needs to stay in
203 // scope throughout test, so it is owned by calling method. However it is
204 // immutable so can only be allocated in this method.
205 switch (caseNumber) {
206 case STATUS_CASE_SETUP_IN_PROGRESS: {
207 EXPECT_CALL(service, HasSyncSetupCompleted())
208 .WillRepeatedly(Return(false));
209 EXPECT_CALL(service, FirstSetupInProgress())
210 .WillRepeatedly(Return(true));
211 browser_sync::SyncBackendHost::Status status;
212 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
213 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
214 Return(false)));
215 return;
217 case STATUS_CASE_SETUP_ERROR: {
218 EXPECT_CALL(service, HasSyncSetupCompleted())
219 .WillRepeatedly(Return(false));
220 EXPECT_CALL(service, FirstSetupInProgress())
221 .WillRepeatedly(Return(false));
222 EXPECT_CALL(service, HasUnrecoverableError())
223 .WillRepeatedly(Return(true));
224 browser_sync::SyncBackendHost::Status status;
225 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
226 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
227 Return(false)));
228 return;
230 case STATUS_CASE_AUTHENTICATING: {
231 EXPECT_CALL(service, HasSyncSetupCompleted())
232 .WillRepeatedly(Return(true));
233 EXPECT_CALL(service, IsSyncActive()).WillRepeatedly(Return(true));
234 EXPECT_CALL(service, IsPassphraseRequired())
235 .WillRepeatedly(Return(false));
236 browser_sync::SyncBackendHost::Status status;
237 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
238 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
239 Return(false)));
240 EXPECT_CALL(service, HasUnrecoverableError())
241 .WillRepeatedly(Return(false));
242 signin->set_auth_in_progress();
243 return;
245 case STATUS_CASE_AUTH_ERROR: {
246 EXPECT_CALL(service, HasSyncSetupCompleted())
247 .WillRepeatedly(Return(true));
248 EXPECT_CALL(service, IsSyncActive()).WillRepeatedly(Return(true));
249 EXPECT_CALL(service, IsPassphraseRequired())
250 .WillRepeatedly(Return(false));
251 browser_sync::SyncBackendHost::Status status;
252 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
253 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
254 Return(false)));
255 provider->SetAuthError(
256 signin->GetAuthenticatedAccountId(),
257 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
258 EXPECT_CALL(service, HasUnrecoverableError())
259 .WillRepeatedly(Return(false));
260 return;
262 case STATUS_CASE_PROTOCOL_ERROR: {
263 EXPECT_CALL(service, HasSyncSetupCompleted())
264 .WillRepeatedly(Return(true));
265 EXPECT_CALL(service, IsSyncActive()).WillRepeatedly(Return(true));
266 EXPECT_CALL(service, IsPassphraseRequired())
267 .WillRepeatedly(Return(false));
268 syncer::SyncProtocolError protocolError;
269 protocolError.action = syncer::STOP_AND_RESTART_SYNC;
270 browser_sync::SyncBackendHost::Status status;
271 status.sync_protocol_error = protocolError;
272 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
273 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
274 Return(false)));
275 EXPECT_CALL(service, HasUnrecoverableError())
276 .WillRepeatedly(Return(false));
277 return;
279 case STATUS_CASE_PASSPHRASE_ERROR: {
280 EXPECT_CALL(service, HasSyncSetupCompleted())
281 .WillRepeatedly(Return(true));
282 EXPECT_CALL(service, IsSyncActive()).WillRepeatedly(Return(true));
283 browser_sync::SyncBackendHost::Status status;
284 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
285 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
286 Return(false)));
287 EXPECT_CALL(service, HasUnrecoverableError())
288 .WillRepeatedly(Return(false));
289 EXPECT_CALL(service, IsPassphraseRequired())
290 .WillRepeatedly(Return(true));
291 EXPECT_CALL(service, IsPassphraseRequiredForDecryption())
292 .WillRepeatedly(Return(true));
293 return;
295 case STATUS_CASE_SYNCED: {
296 EXPECT_CALL(service, HasSyncSetupCompleted())
297 .WillRepeatedly(Return(true));
298 EXPECT_CALL(service, IsSyncActive()).WillRepeatedly(Return(true));
299 EXPECT_CALL(service, IsPassphraseRequired())
300 .WillRepeatedly(Return(false));
301 browser_sync::SyncBackendHost::Status status;
302 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
303 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
304 Return(false)));
305 EXPECT_CALL(service, HasUnrecoverableError())
306 .WillRepeatedly(Return(false));
307 EXPECT_CALL(service, IsPassphraseRequired())
308 .WillRepeatedly(Return(false));
309 return;
311 case STATUS_CASE_SYNC_DISABLED_BY_POLICY: {
312 EXPECT_CALL(service, IsManaged()).WillRepeatedly(Return(true));
313 EXPECT_CALL(service, HasSyncSetupCompleted())
314 .WillRepeatedly(Return(false));
315 EXPECT_CALL(service, IsSyncActive()).WillRepeatedly(Return(false));
316 EXPECT_CALL(service, IsPassphraseRequired())
317 .WillRepeatedly(Return(false));
318 browser_sync::SyncBackendHost::Status status;
319 EXPECT_CALL(service, QueryDetailedSyncStatus(_))
320 .WillRepeatedly(DoAll(SetArgPointee<0>(status),
321 Return(false)));
322 EXPECT_CALL(service, HasUnrecoverableError())
323 .WillRepeatedly(Return(false));
324 return;
326 default:
327 NOTREACHED();
331 // This test ensures that a each distinctive ProfileSyncService statuses
332 // will return a unique combination of status and link messages from
333 // GetStatusLabels().
334 TEST_F(SyncUIUtilTest, DistinctCasesReportUniqueMessageSets) {
335 std::set<base::string16> messages;
336 for (int idx = 0; idx != NUMBER_OF_STATUS_CASES; idx++) {
337 scoped_ptr<Profile> profile(new TestingProfile());
338 ProfileSyncServiceMock service(profile.get());
339 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone();
340 EXPECT_CALL(service, GetAuthError()).WillRepeatedly(ReturnRef(error));
341 FakeSigninManagerForSyncUIUtilTest signin(profile.get());
342 signin.SetAuthenticatedAccountInfo(kTestGaiaId, kTestUser);
343 scoped_ptr<FakeAuthStatusProvider> provider(new FakeAuthStatusProvider(
344 SigninErrorControllerFactory::GetForProfile(profile.get())));
345 GetDistinctCase(service, &signin, provider.get(), idx);
346 base::string16 status_label;
347 base::string16 link_label;
348 sync_ui_util::GetStatusLabels(&service,
349 signin,
350 sync_ui_util::WITH_HTML,
351 &status_label,
352 &link_label);
353 // If the status and link message combination is already present in the set
354 // of messages already seen, this is a duplicate rather than a unique
355 // message, and the test has failed.
356 EXPECT_FALSE(status_label.empty()) <<
357 "Empty status label returned for case #" << idx;
358 base::string16 combined_label =
359 status_label + base::ASCIIToUTF16("#") + link_label;
360 EXPECT_TRUE(messages.find(combined_label) == messages.end()) <<
361 "Duplicate message for case #" << idx << ": " << combined_label;
362 messages.insert(combined_label);
363 testing::Mock::VerifyAndClearExpectations(&service);
364 testing::Mock::VerifyAndClearExpectations(&signin);
365 EXPECT_CALL(service, GetAuthError()).WillRepeatedly(ReturnRef(error));
366 provider.reset();
367 signin.Shutdown();
371 // This test ensures that the html_links parameter on GetStatusLabels() is
372 // honored.
373 TEST_F(SyncUIUtilTest, HtmlNotIncludedInStatusIfNotRequested) {
374 for (int idx = 0; idx != NUMBER_OF_STATUS_CASES; idx++) {
375 scoped_ptr<Profile> profile(
376 ProfileSyncServiceMock::MakeSignedInTestingProfile());
377 ProfileSyncServiceMock service(profile.get());
378 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone();
379 EXPECT_CALL(service, GetAuthError()).WillRepeatedly(ReturnRef(error));
380 FakeSigninManagerForSyncUIUtilTest signin(profile.get());
381 signin.SetAuthenticatedAccountInfo(kTestGaiaId, kTestUser);
382 scoped_ptr<FakeAuthStatusProvider> provider(new FakeAuthStatusProvider(
383 SigninErrorControllerFactory::GetForProfile(profile.get())));
384 GetDistinctCase(service, &signin, provider.get(), idx);
385 base::string16 status_label;
386 base::string16 link_label;
387 sync_ui_util::GetStatusLabels(&service,
388 signin,
389 sync_ui_util::PLAIN_TEXT,
390 &status_label,
391 &link_label);
393 // Ensures a search for string 'href' (found in links, not a string to be
394 // found in an English language message) fails when links are excluded from
395 // the status label.
396 EXPECT_FALSE(status_label.empty());
397 EXPECT_EQ(status_label.find(base::ASCIIToUTF16("href")),
398 base::string16::npos);
399 testing::Mock::VerifyAndClearExpectations(&service);
400 testing::Mock::VerifyAndClearExpectations(&signin);
401 EXPECT_CALL(service, GetAuthError()).WillRepeatedly(ReturnRef(error));
402 provider.reset();
403 signin.Shutdown();