1 // Copyright 2014 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 "chrome/browser/signin/easy_unlock_screenlock_state_handler.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/histogram_tester.h"
14 #include "chrome/browser/signin/easy_unlock_metrics.h"
15 #include "chrome/browser/signin/easy_unlock_service.h"
16 #include "chrome/browser/signin/screenlock_bridge.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/base/l10n/l10n_util.h"
23 // Icons used by EasyUnlockScreenlockStateHandler. The icon id values are the
24 // same as the ones set by ScreenlockBridge.
25 const char kLockedIconId
[] = "locked";
26 const char kLockedToBeActivatedIconId
[] = "locked-to-be-activated";
27 const char kUnlockedIconId
[] = "unlocked";
28 const char kSpinnerIconId
[] = "spinner";
29 const char kHardlockedIconId
[] = "hardlocked";
31 // Checks if |input| string has any unreplaced placeholders.
32 bool StringHasPlaceholders(const base::string16
& input
) {
33 std::vector
<size_t> offsets
;
34 std::vector
<base::string16
> subst
;
35 subst
.push_back(base::string16());
37 base::string16 replaced
= ReplaceStringPlaceholders(input
, subst
, &offsets
);
38 return !offsets
.empty();
41 // Fake lock handler to be used in these tests.
42 class TestLockHandler
: public ScreenlockBridge::LockHandler
{
44 explicit TestLockHandler(const std::string
& user_email
)
45 : user_email_(user_email
),
47 auth_type_(OFFLINE_PASSWORD
) {
49 ~TestLockHandler() override
{}
51 // ScreenlockBridge::LockHandler implementation:
52 void ShowBannerMessage(const base::string16
& message
) override
{
53 ASSERT_FALSE(true) << "Should not be reached.";
56 void ShowUserPodCustomIcon(
57 const std::string
& user_email
,
58 const ScreenlockBridge::UserPodCustomIconOptions
& icon
) override
{
59 ASSERT_EQ(user_email_
, user_email
);
61 last_custom_icon_
= icon
.ToDictionaryValue().Pass();
65 void HideUserPodCustomIcon(const std::string
& user_email
) override
{
66 ASSERT_EQ(user_email_
, user_email
);
67 last_custom_icon_
.reset();
70 void EnableInput() override
{
71 ASSERT_FALSE(true) << "Should not be reached.";
74 void SetAuthType(const std::string
& user_email
,
76 const base::string16
& auth_value
) override
{
77 ASSERT_EQ(user_email_
, user_email
);
78 // Generally, this is allowed, but EasyUnlockScreenlockStateHandler should
79 // avoid resetting the same auth type.
80 EXPECT_NE(auth_type_
, auth_type
);
82 auth_type_
= auth_type
;
83 auth_value_
= auth_value
;
86 AuthType
GetAuthType(const std::string
& user_email
) const override
{
87 EXPECT_EQ(user_email_
, user_email
);
91 ScreenType
GetScreenType() const override
{
95 void Unlock(const std::string
& user_email
) override
{
96 ASSERT_FALSE(true) << "Should not be reached.";
99 void AttemptEasySignin(const std::string
& user_email
,
100 const std::string
& secret
,
101 const std::string
& key_label
) override
{
102 ASSERT_FALSE(true) << "Should not be reached.";
105 // Utility methods used by tests:
107 // Gets last set auth value.
108 base::string16
GetAuthValue() const {
112 // Sets the auth value.
113 void SetAuthValue(const base::string16
& value
) {
117 // Returns the number of times an icon was shown since the last call to this
119 size_t GetAndResetShowIconCount() {
120 size_t result
= show_icon_count_
;
121 show_icon_count_
= 0u;
125 // Whether the custom icon is set.
126 bool HasCustomIcon() const {
127 return last_custom_icon_
;
130 // If custom icon is set, returns the icon's id.
131 // If there is no icon, or if it doesn't have an id set, returns an empty
133 std::string
GetCustomIconId() const {
135 if (last_custom_icon_
)
136 last_custom_icon_
->GetString("id", &result
);
140 // Whether the custom icon is set and it has a tooltip.
141 bool CustomIconHasTooltip() const {
142 return last_custom_icon_
&& last_custom_icon_
->HasKey("tooltip");
145 // Gets the custom icon's tooltip text, if one is set.
146 base::string16
GetCustomIconTooltip() const {
147 base::string16 result
;
148 if (last_custom_icon_
)
149 last_custom_icon_
->GetString("tooltip.text", &result
);
153 // Whether the custom icon's tooltip should be autoshown. If the icon is not
154 // set, or it doesn't have a tooltip, returns false.
155 bool IsCustomIconTooltipAutoshown() const {
157 if (last_custom_icon_
)
158 last_custom_icon_
->GetBoolean("tooltip.autoshow", &result
);
162 // Whether the custom icon is set and if has hardlock capability enabed.
163 bool CustomIconHardlocksOnClick() const {
165 if (last_custom_icon_
)
166 last_custom_icon_
->GetBoolean("hardlockOnClick", &result
);
171 // Does some sanity checks on the last icon set by |ShowUserPodCustomIcon|.
172 // It will cause a test failure if the icon is not valid.
173 void ValidateCustomIcon() {
174 ASSERT_TRUE(last_custom_icon_
.get());
176 EXPECT_TRUE(last_custom_icon_
->HasKey("id"));
178 if (last_custom_icon_
->HasKey("tooltip")) {
179 base::string16 tooltip
;
180 EXPECT_TRUE(last_custom_icon_
->GetString("tooltip.text", &tooltip
));
181 EXPECT_FALSE(tooltip
.empty());
182 EXPECT_FALSE(StringHasPlaceholders(tooltip
));
186 // The fake user email used in test. All methods called on |this| should be
187 // associated with this user.
188 const std::string user_email_
;
190 // The last icon set using |SetUserPodCustomIcon|. Call to
191 // |HideUserPodcustomIcon| resets it.
192 scoped_ptr
<base::DictionaryValue
> last_custom_icon_
;
193 size_t show_icon_count_
;
195 // Auth type and value set using |SetAuthType|.
197 base::string16 auth_value_
;
199 DISALLOW_COPY_AND_ASSIGN(TestLockHandler
);
202 class EasyUnlockScreenlockStateHandlerTest
: public testing::Test
{
204 EasyUnlockScreenlockStateHandlerTest() : user_email_("test_user@gmail.com") {}
205 ~EasyUnlockScreenlockStateHandlerTest() override
{}
207 void SetUp() override
{
208 // Create and inject fake lock handler to the screenlock bridge.
209 lock_handler_
.reset(new TestLockHandler(user_email_
));
210 ScreenlockBridge
* screenlock_bridge
= ScreenlockBridge::Get();
211 screenlock_bridge
->SetLockHandler(lock_handler_
.get());
213 // Create the screenlock state handler object that will be tested.
214 state_handler_
.reset(new EasyUnlockScreenlockStateHandler(
216 EasyUnlockScreenlockStateHandler::NO_HARDLOCK
,
220 void TearDown() override
{
221 ScreenlockBridge::Get()->SetLockHandler(NULL
);
222 lock_handler_
.reset();
223 state_handler_
.reset();
227 // The state handler that is being tested.
228 scoped_ptr
<EasyUnlockScreenlockStateHandler
> state_handler_
;
230 // The user associated with |state_handler_|.
231 const std::string user_email_
;
233 // Faked lock handler given to ScreenlockBridge during the test. Abstracts
234 // the screen lock UI.
235 scoped_ptr
<TestLockHandler
> lock_handler_
;
238 TEST_F(EasyUnlockScreenlockStateHandlerTest
, AuthenticatedTrialRun
) {
239 state_handler_
->SetTrialRun();
240 state_handler_
->ChangeState(
241 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
243 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
244 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
245 lock_handler_
->GetAuthType(user_email_
));
247 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
248 EXPECT_EQ(kUnlockedIconId
, lock_handler_
->GetCustomIconId());
249 EXPECT_TRUE(lock_handler_
->CustomIconHasTooltip());
250 EXPECT_TRUE(lock_handler_
->IsCustomIconTooltipAutoshown());
251 EXPECT_FALSE(lock_handler_
->CustomIconHardlocksOnClick());
253 state_handler_
->ChangeState(
254 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
255 // Duplicated state change should be ignored.
256 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
259 TEST_F(EasyUnlockScreenlockStateHandlerTest
, AuthenticatedNotInitialRun
) {
260 state_handler_
->ChangeState(
261 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
263 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
264 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
265 lock_handler_
->GetAuthType(user_email_
));
267 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
268 EXPECT_EQ(kUnlockedIconId
, lock_handler_
->GetCustomIconId());
269 EXPECT_TRUE(lock_handler_
->CustomIconHasTooltip());
270 EXPECT_FALSE(lock_handler_
->IsCustomIconTooltipAutoshown());
271 EXPECT_TRUE(lock_handler_
->CustomIconHardlocksOnClick());
274 TEST_F(EasyUnlockScreenlockStateHandlerTest
, IsActive
) {
275 EXPECT_FALSE(state_handler_
->IsActive());
276 state_handler_
->ChangeState(
277 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
278 EXPECT_TRUE(state_handler_
->IsActive());
279 state_handler_
->ChangeState(
280 EasyUnlockScreenlockStateHandler::STATE_INACTIVE
);
281 EXPECT_FALSE(state_handler_
->IsActive());
284 TEST_F(EasyUnlockScreenlockStateHandlerTest
, BluetoothConnecting
) {
285 state_handler_
->ChangeState(
286 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING
);
287 EXPECT_TRUE(state_handler_
->IsActive());
289 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
290 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
291 lock_handler_
->GetAuthType(user_email_
));
293 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
294 EXPECT_EQ(kSpinnerIconId
, lock_handler_
->GetCustomIconId());
295 EXPECT_FALSE(lock_handler_
->CustomIconHasTooltip());
296 EXPECT_TRUE(lock_handler_
->CustomIconHardlocksOnClick());
298 state_handler_
->ChangeState(
299 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING
);
300 // Duplicated state change should be ignored.
301 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
304 TEST_F(EasyUnlockScreenlockStateHandlerTest
, HardlockedState
) {
305 state_handler_
->ChangeState(
306 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
308 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
309 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
310 lock_handler_
->GetAuthType(user_email_
));
312 state_handler_
->SetHardlockState(
313 EasyUnlockScreenlockStateHandler::USER_HARDLOCK
);
315 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
316 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
317 lock_handler_
->GetAuthType(user_email_
));
319 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
320 EXPECT_EQ(kHardlockedIconId
, lock_handler_
->GetCustomIconId());
321 EXPECT_TRUE(lock_handler_
->CustomIconHasTooltip());
322 EXPECT_TRUE(lock_handler_
->IsCustomIconTooltipAutoshown());
323 EXPECT_FALSE(lock_handler_
->CustomIconHardlocksOnClick());
325 state_handler_
->SetHardlockState(
326 EasyUnlockScreenlockStateHandler::USER_HARDLOCK
);
328 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
329 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
332 TEST_F(EasyUnlockScreenlockStateHandlerTest
, HardlockedStateNoPairing
) {
333 state_handler_
->ChangeState(
334 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
336 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
337 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
338 lock_handler_
->GetAuthType(user_email_
));
340 state_handler_
->SetHardlockState(
341 EasyUnlockScreenlockStateHandler::NO_PAIRING
);
343 EXPECT_FALSE(lock_handler_
->HasCustomIcon());
344 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
345 lock_handler_
->GetAuthType(user_email_
));
348 TEST_F(EasyUnlockScreenlockStateHandlerTest
, StatesWithLockedIcon
) {
349 std::vector
<EasyUnlockScreenlockStateHandler::State
> states
;
350 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH
);
351 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
352 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED
);
353 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE
);
355 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED
);
356 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED
);
358 for (size_t i
= 0; i
< states
.size(); ++i
) {
359 state_handler_
->ChangeState(states
[i
]);
360 EXPECT_TRUE(state_handler_
->IsActive());
362 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount())
363 << "State: " << states
[i
];
364 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
365 lock_handler_
->GetAuthType(user_email_
))
366 << "State: " << states
[i
];
368 ASSERT_TRUE(lock_handler_
->HasCustomIcon())
369 << "State: " << states
[i
];
370 EXPECT_EQ(kLockedIconId
, lock_handler_
->GetCustomIconId())
371 << "State: " << states
[i
];
372 EXPECT_TRUE(lock_handler_
->CustomIconHasTooltip())
373 << "State: " << states
[i
];
374 EXPECT_TRUE(lock_handler_
->IsCustomIconTooltipAutoshown())
375 << "State: " << states
[i
];
376 EXPECT_TRUE(lock_handler_
->CustomIconHardlocksOnClick())
377 << "State: " << states
[i
];
379 state_handler_
->ChangeState(states
[i
]);
380 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount())
381 << "State: " << states
[i
];
385 TEST_F(EasyUnlockScreenlockStateHandlerTest
, SettingTrialRunUpdatesUI
) {
386 state_handler_
->ChangeState(
387 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
389 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
390 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
391 lock_handler_
->GetAuthType(user_email_
));
393 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
394 ASSERT_FALSE(lock_handler_
->IsCustomIconTooltipAutoshown());
396 state_handler_
->SetTrialRun();
398 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
399 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
400 lock_handler_
->GetAuthType(user_email_
));
402 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
403 ASSERT_TRUE(lock_handler_
->IsCustomIconTooltipAutoshown());
406 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
407 LockScreenClearedOnStateHandlerDestruction
) {
408 state_handler_
->ChangeState(
409 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
411 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
412 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
413 lock_handler_
->GetAuthType(user_email_
));
415 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
417 state_handler_
.reset();
419 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
420 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
421 lock_handler_
->GetAuthType(user_email_
));
423 ASSERT_FALSE(lock_handler_
->HasCustomIcon());
426 TEST_F(EasyUnlockScreenlockStateHandlerTest
, StatePreservedWhenScreenUnlocks
) {
427 state_handler_
->ChangeState(
428 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
430 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
431 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
432 lock_handler_
->GetAuthType(user_email_
));
433 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
435 ScreenlockBridge::Get()->SetLockHandler(NULL
);
436 lock_handler_
.reset(new TestLockHandler(user_email_
));
437 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
438 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
440 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
441 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
442 lock_handler_
->GetAuthType(user_email_
));
443 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
446 TEST_F(EasyUnlockScreenlockStateHandlerTest
, StateChangeWhileScreenUnlocked
) {
447 state_handler_
->ChangeState(
448 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
450 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
451 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
452 lock_handler_
->GetAuthType(user_email_
));
453 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
455 ScreenlockBridge::Get()->SetLockHandler(NULL
);
456 lock_handler_
.reset(new TestLockHandler(user_email_
));
457 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
459 state_handler_
->ChangeState(
460 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING
);
462 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
464 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
465 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
466 lock_handler_
->GetAuthType(user_email_
));
467 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
468 EXPECT_EQ(kSpinnerIconId
, lock_handler_
->GetCustomIconId());
471 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
472 HardlockEnabledAfterInitialUnlock
) {
473 state_handler_
->SetTrialRun();
475 std::vector
<EasyUnlockScreenlockStateHandler::State
> states
;
477 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING
);
479 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED
);
480 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH
);
481 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
482 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED
);
483 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE
);
484 // This one should go last as changing state to AUTHENTICATED enables hard
486 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
488 for (size_t i
= 0; i
< states
.size(); ++i
) {
489 state_handler_
->ChangeState(states
[i
]);
490 ASSERT_TRUE(lock_handler_
->HasCustomIcon()) << "State: " << states
[i
];
491 EXPECT_FALSE(lock_handler_
->CustomIconHardlocksOnClick())
492 << "State: " << states
[i
];
495 ScreenlockBridge::Get()->SetLockHandler(NULL
);
496 lock_handler_
.reset(new TestLockHandler(user_email_
));
497 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
499 for (size_t i
= 0; i
< states
.size(); ++i
) {
500 state_handler_
->ChangeState(states
[i
]);
501 ASSERT_TRUE(lock_handler_
->HasCustomIcon()) << "State: " << states
[i
];
502 EXPECT_TRUE(lock_handler_
->CustomIconHardlocksOnClick())
503 << "State: " << states
[i
];
507 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
508 NoPairingHardlockClearsIcon
) {
509 state_handler_
->ChangeState(
510 EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED
);
512 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
513 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
514 EXPECT_EQ(kLockedIconId
, lock_handler_
->GetCustomIconId());
516 state_handler_
->SetHardlockState(
517 EasyUnlockScreenlockStateHandler::NO_PAIRING
);
519 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
520 ASSERT_FALSE(lock_handler_
->HasCustomIcon());
523 TEST_F(EasyUnlockScreenlockStateHandlerTest
, PairingChangedHardlock
) {
524 state_handler_
->ChangeState(
525 EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED
);
527 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
528 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
529 EXPECT_EQ(kLockedIconId
, lock_handler_
->GetCustomIconId());
531 state_handler_
->SetHardlockState(
532 EasyUnlockScreenlockStateHandler::PAIRING_CHANGED
);
534 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
535 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
536 EXPECT_EQ(kLockedToBeActivatedIconId
, lock_handler_
->GetCustomIconId());
538 state_handler_
->ChangeState(
539 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
541 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
542 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
543 EXPECT_EQ(kLockedToBeActivatedIconId
, lock_handler_
->GetCustomIconId());
546 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
547 PairingChangedHardlockIneffectiveOnInitialRun
) {
548 state_handler_
->SetTrialRun();
550 state_handler_
->ChangeState(
551 EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED
);
553 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
554 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
555 EXPECT_EQ(kLockedIconId
, lock_handler_
->GetCustomIconId());
557 state_handler_
->SetHardlockState(
558 EasyUnlockScreenlockStateHandler::PAIRING_CHANGED
);
560 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
561 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
562 EXPECT_EQ(kLockedIconId
, lock_handler_
->GetCustomIconId());
565 TEST_F(EasyUnlockScreenlockStateHandlerTest
, InactiveStateHidesIcon
) {
566 state_handler_
->ChangeState(
567 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
569 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
571 state_handler_
->ChangeState(
572 EasyUnlockScreenlockStateHandler::STATE_INACTIVE
);
574 ASSERT_FALSE(lock_handler_
->HasCustomIcon());
577 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
578 AuthenticatedStateClearsPreviousAuthValue
) {
579 state_handler_
->ChangeState(
580 EasyUnlockScreenlockStateHandler::STATE_INACTIVE
);
582 lock_handler_
->SetAuthValue(base::ASCIIToUTF16("xxx"));
584 state_handler_
->ChangeState(
585 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
587 EXPECT_EQ(l10n_util::GetStringUTF16(
588 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE
),
589 lock_handler_
->GetAuthValue());
591 state_handler_
->ChangeState(
592 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
594 EXPECT_EQ(base::string16(), lock_handler_
->GetAuthValue());
597 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
598 ChangingStateDoesNotAffectAuthValueIfAuthTypeDoesNotChange
) {
599 lock_handler_
->SetAuthValue(base::ASCIIToUTF16("xxx"));
601 state_handler_
->ChangeState(
602 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
603 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_
->GetAuthValue());
605 state_handler_
->ChangeState(
606 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED
);
607 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_
->GetAuthValue());
609 state_handler_
->ChangeState(
610 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING
);
611 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_
->GetAuthValue());
612 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
613 EXPECT_EQ(kSpinnerIconId
, lock_handler_
->GetCustomIconId());
616 TEST_F(EasyUnlockScreenlockStateHandlerTest
, StateChangesIgnoredIfHardlocked
) {
617 state_handler_
->ChangeState(
618 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
620 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
621 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
622 lock_handler_
->GetAuthType(user_email_
));
624 state_handler_
->SetHardlockState(
625 EasyUnlockScreenlockStateHandler::USER_HARDLOCK
);
627 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
628 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
629 lock_handler_
->GetAuthType(user_email_
));
630 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
631 EXPECT_EQ(kHardlockedIconId
, lock_handler_
->GetCustomIconId());
633 state_handler_
->ChangeState(
634 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
635 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
636 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
638 state_handler_
->ChangeState(
639 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
640 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
641 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
642 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
643 lock_handler_
->GetAuthType(user_email_
));
646 TEST_F(EasyUnlockScreenlockStateHandlerTest
,
647 LockScreenChangeableOnLockAfterHardlockReset
) {
648 state_handler_
->ChangeState(
649 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
651 state_handler_
->SetHardlockState(
652 EasyUnlockScreenlockStateHandler::USER_HARDLOCK
);
653 EXPECT_EQ(2u, lock_handler_
->GetAndResetShowIconCount());
655 state_handler_
->SetHardlockState(
656 EasyUnlockScreenlockStateHandler::NO_HARDLOCK
);
658 ScreenlockBridge::Get()->SetLockHandler(NULL
);
659 lock_handler_
.reset(new TestLockHandler(user_email_
));
660 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
661 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
663 state_handler_
->ChangeState(
664 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
666 EXPECT_EQ(2u, lock_handler_
->GetAndResetShowIconCount());
667 EXPECT_TRUE(lock_handler_
->HasCustomIcon());
669 ScreenlockBridge::Get()->SetLockHandler(NULL
);
670 lock_handler_
.reset(new TestLockHandler(user_email_
));
671 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
672 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
674 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
675 EXPECT_TRUE(lock_handler_
->HasCustomIcon());
676 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
677 lock_handler_
->GetAuthType(user_email_
));
678 EXPECT_EQ(kLockedIconId
, lock_handler_
->GetCustomIconId());
680 state_handler_
->ChangeState(
681 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
682 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
683 EXPECT_TRUE(lock_handler_
->HasCustomIcon());
684 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK
,
685 lock_handler_
->GetAuthType(user_email_
));
686 EXPECT_TRUE(lock_handler_
->CustomIconHardlocksOnClick());
689 TEST_F(EasyUnlockScreenlockStateHandlerTest
, HardlockStatePersistsOverUnlocks
) {
690 state_handler_
->ChangeState(
691 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
692 state_handler_
->SetHardlockState(
693 EasyUnlockScreenlockStateHandler::USER_HARDLOCK
);
694 EXPECT_EQ(2u, lock_handler_
->GetAndResetShowIconCount());
696 ScreenlockBridge::Get()->SetLockHandler(NULL
);
697 lock_handler_
.reset(new TestLockHandler(user_email_
));
698 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
699 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
701 EXPECT_EQ(1u, lock_handler_
->GetAndResetShowIconCount());
702 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
703 lock_handler_
->GetAuthType(user_email_
));
704 ASSERT_TRUE(lock_handler_
->HasCustomIcon());
705 EXPECT_EQ(kHardlockedIconId
, lock_handler_
->GetCustomIconId());
707 state_handler_
->ChangeState(
708 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
709 EXPECT_EQ(0u, lock_handler_
->GetAndResetShowIconCount());
710 EXPECT_TRUE(lock_handler_
->HasCustomIcon());
711 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
,
712 lock_handler_
->GetAuthType(user_email_
));
715 TEST_F(EasyUnlockScreenlockStateHandlerTest
, NoOverrideOnlineSignin
) {
716 lock_handler_
->SetAuthType(user_email_
,
717 ScreenlockBridge::LockHandler::ONLINE_SIGN_IN
,
720 std::vector
<EasyUnlockScreenlockStateHandler::State
> states
;
721 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH
);
722 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE
);
723 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED
);
724 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE
);
726 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED
);
727 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED
);
728 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE
);
729 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED
);
730 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_RSSI_TOO_LOW
);
731 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_TX_POWER_TOO_HIGH
);
732 states
.push_back(EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED
);
734 for (size_t i
= 0; i
< states
.size(); ++i
) {
735 state_handler_
->ChangeState(states
[i
]);
736 EXPECT_EQ(ScreenlockBridge::LockHandler::ONLINE_SIGN_IN
,
737 lock_handler_
->GetAuthType(user_email_
));
738 EXPECT_FALSE(lock_handler_
->HasCustomIcon());
741 std::vector
<EasyUnlockScreenlockStateHandler::HardlockState
> hardlock_states
;
742 hardlock_states
.push_back(EasyUnlockScreenlockStateHandler::NO_HARDLOCK
);
743 hardlock_states
.push_back(EasyUnlockScreenlockStateHandler::USER_HARDLOCK
);
744 hardlock_states
.push_back(EasyUnlockScreenlockStateHandler::PAIRING_CHANGED
);
745 hardlock_states
.push_back(EasyUnlockScreenlockStateHandler::PAIRING_ADDED
);
746 hardlock_states
.push_back(EasyUnlockScreenlockStateHandler::NO_PAIRING
);
747 hardlock_states
.push_back(EasyUnlockScreenlockStateHandler::LOGIN_FAILED
);
749 for (size_t i
= 0; i
< hardlock_states
.size(); ++i
) {
750 state_handler_
->SetHardlockState(hardlock_states
[i
]);
751 EXPECT_EQ(ScreenlockBridge::LockHandler::ONLINE_SIGN_IN
,
752 lock_handler_
->GetAuthType(user_email_
));
753 EXPECT_FALSE(lock_handler_
->HasCustomIcon());
757 TEST_F(EasyUnlockScreenlockStateHandlerTest
, TrialRunMetrics
) {
758 base::HistogramTester histogram_tester
;
760 // Simulate the user clicking on the lock icon twice outside of a trial run.
761 // No trial run metrics should be recorded.
762 state_handler_
->RecordClickOnLockIcon();
763 state_handler_
->RecordClickOnLockIcon();
764 histogram_tester
.ExpectTotalCount("EasyUnlock.TrialRun.Events", 0);
766 // Simulate the user clicking on the lock icon three times during a trial run.
767 state_handler_
->SetTrialRun();
768 state_handler_
->RecordClickOnLockIcon();
769 state_handler_
->RecordClickOnLockIcon();
770 state_handler_
->RecordClickOnLockIcon();
771 histogram_tester
.ExpectTotalCount("EasyUnlock.TrialRun.Events", 4);
772 histogram_tester
.ExpectBucketCount("EasyUnlock.TrialRun.Events",
773 EASY_UNLOCK_TRIAL_RUN_EVENT_LAUNCHED
, 1);
774 histogram_tester
.ExpectBucketCount(
775 "EasyUnlock.TrialRun.Events",
776 EASY_UNLOCK_TRIAL_RUN_EVENT_CLICKED_LOCK_ICON
, 3);