1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <set>
#include "base/basictypes.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_fake.h"
#include "chrome/browser/sync/profile_sync_service_mock.h"
#include "chrome/browser/sync/sync_ui_util.h"
#include "content/public/test/test_browser_thread.h"
#include "grit/generated_resources.h"
#include "testing/gmock/include/gmock/gmock-actions.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
using ::testing::AtMost;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::SetArgPointee;
using ::testing::_;
using content::BrowserThread;
// A number of distinct states of the ProfileSyncService can be generated for
// tests.
enum DistinctState {
STATUS_CASE_SETUP_IN_PROGRESS,
STATUS_CASE_SETUP_ERROR,
STATUS_CASE_AUTHENTICATING,
STATUS_CASE_AUTH_ERROR,
STATUS_CASE_PROTOCOL_ERROR,
STATUS_CASE_PASSPHRASE_ERROR,
STATUS_CASE_SYNCED,
NUMBER_OF_STATUS_CASES
};
namespace {
// Mock that allows us to mock a SigninManager that is authenticating.
class SigninManagerMock : public SigninManager {
public:
MOCK_CONST_METHOD0(AuthInProgress, bool());
};
// Utility function to test that GetStatusLabelsForSyncGlobalError returns
// the correct results for the given states.
void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service,
const SigninManager& signin,
GoogleServiceAuthError::State error_state,
bool is_signed_in,
bool is_error) {
EXPECT_CALL(*service, HasSyncSetupCompleted())
.WillRepeatedly(Return(is_signed_in));
GoogleServiceAuthError auth_error(error_state);
EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error));
string16 label1, label2, label3;
sync_ui_util::GetStatusLabelsForSyncGlobalError(
service, signin, &label1, &label2, &label3);
EXPECT_EQ(label1.empty(), !is_error);
EXPECT_EQ(label2.empty(), !is_error);
EXPECT_EQ(label3.empty(), !is_error);
}
} // namespace
// Test that GetStatusLabelsForSyncGlobalError returns an error if a
// passphrase is required.
TEST(SyncUIUtilTest, PassphraseGlobalError) {
MessageLoopForUI message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
scoped_ptr<Profile> profile(
ProfileSyncServiceMock::MakeSignedInTestingProfile());
NiceMock<ProfileSyncServiceMock> service(profile.get());
FakeSigninManager signin(profile.get());
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillRepeatedly(Return(false));
EXPECT_CALL(service, IsPassphraseRequired())
.WillRepeatedly(Return(true));
EXPECT_CALL(service, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(true));
VerifySyncGlobalErrorResult(
&service, signin, GoogleServiceAuthError::NONE, true, true);
}
// Test that GetStatusLabelsForSyncGlobalError returns an error if a
// passphrase is required.
TEST(SyncUIUtilTest, AuthAndPassphraseGlobalError) {
MessageLoopForUI message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
scoped_ptr<Profile> profile(
ProfileSyncServiceMock::MakeSignedInTestingProfile());
NiceMock<ProfileSyncServiceMock> service(profile.get());
FakeSigninManager signin(profile.get());
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillRepeatedly(Return(false));
EXPECT_CALL(service, IsPassphraseRequired())
.WillRepeatedly(Return(true));
EXPECT_CALL(service, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(true));
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillRepeatedly(Return(true));
GoogleServiceAuthError auth_error(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
EXPECT_CALL(service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error));
string16 menu_label, label2, label3;
sync_ui_util::GetStatusLabelsForSyncGlobalError(
&service, signin, &menu_label, &label2, &label3);
// Make sure we aren't displaying the passphrase error badge.
EXPECT_NE(menu_label, l10n_util::GetStringUTF16(
IDS_SYNC_PASSPHRASE_ERROR_WRENCH_MENU_ITEM));
}
// Test that GetStatusLabelsForSyncGlobalError indicates errors for conditions
// that can be resolved by the user and suppresses errors for conditions that
// cannot be resolved by the user.
TEST(SyncUIUtilTest, AuthStateGlobalError) {
MessageLoopForUI message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
scoped_ptr<Profile> profile(
ProfileSyncServiceMock::MakeSignedInTestingProfile());
NiceMock<ProfileSyncServiceMock> service(profile.get());
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillRepeatedly(Return(false));
struct {
GoogleServiceAuthError::State error_state;
bool is_error;
} table[] = {
{ GoogleServiceAuthError::NONE, false },
{ GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, true },
{ GoogleServiceAuthError::USER_NOT_SIGNED_UP, true },
{ GoogleServiceAuthError::CONNECTION_FAILED, false },
{ GoogleServiceAuthError::CAPTCHA_REQUIRED, true },
{ GoogleServiceAuthError::ACCOUNT_DELETED, true },
{ GoogleServiceAuthError::ACCOUNT_DISABLED, true },
{ GoogleServiceAuthError::SERVICE_UNAVAILABLE, true },
{ GoogleServiceAuthError::TWO_FACTOR, true },
{ GoogleServiceAuthError::REQUEST_CANCELED, true },
{ GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true },
};
FakeSigninManager signin(profile.get());
for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) {
VerifySyncGlobalErrorResult(
&service, signin, table[i].error_state, true, table[i].is_error);
VerifySyncGlobalErrorResult(
&service, signin, table[i].error_state, false, false);
}
}
// Loads a ProfileSyncServiceMock to emulate one of a number of distinct cases
// in order to perform tests on the generated messages.
void GetDistinctCase(ProfileSyncServiceMock& service,
SigninManagerMock& signin,
GoogleServiceAuthError** auth_error,
int caseNumber) {
// Auth Error object is returned by reference in mock and needs to stay in
// scope throughout test, so it is owned by calling method. However it is
// immutable so can only be allocated in this method.
switch (caseNumber) {
case STATUS_CASE_SETUP_IN_PROGRESS: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(false));
EXPECT_CALL(service, FirstSetupInProgress())
.WillOnce(Return(true));
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
*auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE);
EXPECT_CALL(service, GetAuthError())
.WillOnce(ReturnRef(**auth_error));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(false));
return;
}
case STATUS_CASE_SETUP_ERROR: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(false));
EXPECT_CALL(service, FirstSetupInProgress())
.WillOnce(Return(false));
EXPECT_CALL(service, HasUnrecoverableError())
.WillOnce(Return(true));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(false));
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
return;
}
case STATUS_CASE_AUTHENTICATING: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(true));
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
EXPECT_CALL(service, HasUnrecoverableError())
.WillOnce(Return(false));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(true));
*auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE);
EXPECT_CALL(service, GetAuthError())
.WillOnce(ReturnRef(**auth_error));
return;
}
case STATUS_CASE_AUTH_ERROR: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(true));
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
*auth_error = new GoogleServiceAuthError(
GoogleServiceAuthError::SERVICE_UNAVAILABLE);
EXPECT_CALL(service, HasUnrecoverableError())
.WillOnce(Return(false));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(false));
EXPECT_CALL(service, GetAuthError())
.WillOnce(ReturnRef(**auth_error));
return;
}
case STATUS_CASE_PROTOCOL_ERROR: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(true));
syncer::SyncProtocolError protocolError;
protocolError.action = syncer::STOP_AND_RESTART_SYNC;
browser_sync::SyncBackendHost::Status status;
status.sync_protocol_error = protocolError;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
*auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE);
EXPECT_CALL(service, GetAuthError())
.WillOnce(ReturnRef(**auth_error));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(false));
EXPECT_CALL(service, HasUnrecoverableError())
.WillOnce(Return(false));
return;
}
case STATUS_CASE_PASSPHRASE_ERROR: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(true));
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
*auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE);
EXPECT_CALL(service, GetAuthError())
.WillOnce(ReturnRef(**auth_error));
EXPECT_CALL(service, HasUnrecoverableError())
.WillOnce(Return(false));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(false));
EXPECT_CALL(service, IsPassphraseRequired())
.WillOnce(Return(true));
EXPECT_CALL(service, IsPassphraseRequiredForDecryption())
.WillOnce(Return(true));
return;
}
case STATUS_CASE_SYNCED: {
EXPECT_CALL(service, HasSyncSetupCompleted())
.WillOnce(Return(true));
browser_sync::SyncBackendHost::Status status;
EXPECT_CALL(service, QueryDetailedSyncStatus(_))
.WillOnce(DoAll(SetArgPointee<0>(status),
Return(false)));
*auth_error = new GoogleServiceAuthError(GoogleServiceAuthError::NONE);
EXPECT_CALL(service, GetAuthError())
.WillOnce(ReturnRef(**auth_error));
EXPECT_CALL(signin, AuthInProgress()).WillRepeatedly(Return(false));
EXPECT_CALL(service, HasUnrecoverableError())
.WillOnce(Return(false));
EXPECT_CALL(service, IsPassphraseRequired())
.WillOnce(Return(false));
return;
}
default:
NOTREACHED();
}
}
// This test ensures that a each distinctive ProfileSyncService statuses
// will return a unique combination of status and link messages from
// GetStatusLabels().
TEST(SyncUIUtilTest, DistinctCasesReportUniqueMessageSets) {
std::set<string16> messages;
for (int idx = 0; idx != NUMBER_OF_STATUS_CASES; idx++) {
scoped_ptr<Profile> profile(
ProfileSyncServiceMock::MakeSignedInTestingProfile());
ProfileSyncServiceMock service(profile.get());
NiceMock<SigninManagerMock> signin;
GoogleServiceAuthError* auth_error = NULL;
GetDistinctCase(service, signin, &auth_error, idx);
string16 status_label;
string16 link_label;
sync_ui_util::GetStatusLabels(&service,
signin,
sync_ui_util::WITH_HTML,
&status_label,
&link_label);
// If the status and link message combination is already present in the set
// of messages already seen, this is a duplicate rather than a unique
// message, and the test has failed.
string16 combined_label =
status_label + string16(ASCIIToUTF16("#")) + link_label;
EXPECT_TRUE(messages.find(combined_label) == messages.end());
messages.insert(combined_label);
if (auth_error)
delete auth_error;
}
}
// This test ensures that the html_links parameter on GetStatusLabels() is
// honored.
TEST(SyncUIUtilTest, HtmlNotIncludedInStatusIfNotRequested) {
for (int idx = 0; idx != NUMBER_OF_STATUS_CASES; idx++) {
scoped_ptr<Profile> profile(
ProfileSyncServiceMock::MakeSignedInTestingProfile());
ProfileSyncServiceMock service(profile.get());
NiceMock<SigninManagerMock> signin;
GoogleServiceAuthError* auth_error = NULL;
GetDistinctCase(service, signin, &auth_error, idx);
string16 status_label;
string16 link_label;
sync_ui_util::GetStatusLabels(&service,
signin,
sync_ui_util::PLAIN_TEXT,
&status_label,
&link_label);
// Ensures a search for string 'href' (found in links, not a string to be
// found in an English language message) fails when links are excluded from
// the status label.
EXPECT_EQ(status_label.find(string16(ASCIIToUTF16("href"))),
string16::npos);
if (auth_error) {
delete auth_error;
}
}
}
|