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.
8 #include "base/strings/string16.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/signin/easy_unlock_screenlock_state_handler.h"
12 #include "chrome/browser/signin/easy_unlock_service.h"
13 #include "chrome/browser/signin/screenlock_bridge.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/l10n/l10n_util.h"
20 // Icons used by EasyUnlockScreenlockStateHandler. The icon id values are the
21 // same as the ones set by ScreenlockBridge.
22 const char kLockedIconId[] = "locked";
23 const char kLockedToBeActivatedIconId[] = "locked-to-be-activated";
24 const char kUnlockedIconId[] = "unlocked";
25 const char kSpinnerIconId[] = "spinner";
26 const char kHardlockedIconId[] = "hardlocked";
28 // Checks if |input| string has any unreplaced placeholders.
29 bool StringHasPlaceholders(const base::string16& input) {
30 std::vector<size_t> offsets;
31 std::vector<base::string16> subst;
32 subst.push_back(base::string16());
34 base::string16 replaced = ReplaceStringPlaceholders(input, subst, &offsets);
35 return !offsets.empty();
38 // Fake lock handler to be used in these tests.
39 class TestLockHandler : public ScreenlockBridge::LockHandler {
41 explicit TestLockHandler(const std::string& user_email)
42 : user_email_(user_email),
44 auth_type_(OFFLINE_PASSWORD) {
46 ~TestLockHandler() override {}
48 // ScreenlockBridge::LockHandler implementation:
49 void ShowBannerMessage(const base::string16& message) override {
50 ASSERT_FALSE(true) << "Should not be reached.";
53 void ShowUserPodCustomIcon(
54 const std::string& user_email,
55 const ScreenlockBridge::UserPodCustomIconOptions& icon) override {
56 ASSERT_EQ(user_email_, user_email);
58 last_custom_icon_ = icon.ToDictionaryValue().Pass();
62 void HideUserPodCustomIcon(const std::string& user_email) override {
63 ASSERT_EQ(user_email_, user_email);
64 last_custom_icon_.reset();
67 void EnableInput() override {
68 ASSERT_FALSE(true) << "Should not be reached.";
71 void SetAuthType(const std::string& user_email,
73 const base::string16& auth_value) override {
74 ASSERT_EQ(user_email_, user_email);
75 // Generally, this is allowed, but EasyUnlockScreenlockStateHandler should
76 // avoid resetting the same auth type.
77 EXPECT_NE(auth_type_, auth_type);
79 auth_type_ = auth_type;
80 auth_value_ = auth_value;
83 AuthType GetAuthType(const std::string& user_email) const override {
84 EXPECT_EQ(user_email_, user_email);
88 void Unlock(const std::string& user_email) override {
89 ASSERT_FALSE(true) << "Should not be reached.";
92 void AttemptEasySignin(const std::string& user_email,
93 const std::string& secret,
94 const std::string& key_label) override {
95 ASSERT_FALSE(true) << "Should not be reached.";
98 // Utility methods used by tests:
100 // Gets last set auth value.
101 base::string16 GetAuthValue() const {
105 // Sets the auth value.
106 void SetAuthValue(const base::string16& value) {
110 // Returns the number of times an icon was shown since the last call to this
112 size_t GetAndResetShowIconCount() {
113 size_t result = show_icon_count_;
114 show_icon_count_ = 0u;
118 // Whether the custom icon is set.
119 bool HasCustomIcon() const {
120 return last_custom_icon_;
123 // If custom icon is set, returns the icon's id.
124 // If there is no icon, or if it doesn't have an id set, returns an empty
126 std::string GetCustomIconId() const {
128 if (last_custom_icon_)
129 last_custom_icon_->GetString("id", &result);
133 // Whether the custom icon is set and it has a tooltip.
134 bool CustomIconHasTooltip() const {
135 return last_custom_icon_ && last_custom_icon_->HasKey("tooltip");
138 // Gets the custom icon's tooltip text, if one is set.
139 base::string16 GetCustomIconTooltip() const {
140 base::string16 result;
141 if (last_custom_icon_)
142 last_custom_icon_->GetString("tooltip.text", &result);
146 // Whether the custom icon's tooltip should be autoshown. If the icon is not
147 // set, or it doesn't have a tooltip, returns false.
148 bool IsCustomIconTooltipAutoshown() const {
150 if (last_custom_icon_)
151 last_custom_icon_->GetBoolean("tooltip.autoshow", &result);
155 // Whether the custom icon is set and if has hardlock capability enabed.
156 bool CustomIconHardlocksOnClick() const {
158 if (last_custom_icon_)
159 last_custom_icon_->GetBoolean("hardlockOnClick", &result);
164 // Does some sanity checks on the last icon set by |ShowUserPodCustomIcon|.
165 // It will cause a test failure if the icon is not valid.
166 void ValidateCustomIcon() {
167 ASSERT_TRUE(last_custom_icon_.get());
169 EXPECT_TRUE(last_custom_icon_->HasKey("id"));
171 if (last_custom_icon_->HasKey("tooltip")) {
172 base::string16 tooltip;
173 EXPECT_TRUE(last_custom_icon_->GetString("tooltip.text", &tooltip));
174 EXPECT_FALSE(tooltip.empty());
175 EXPECT_FALSE(StringHasPlaceholders(tooltip));
179 // The fake user email used in test. All methods called on |this| should be
180 // associated with this user.
181 const std::string user_email_;
183 // The last icon set using |SetUserPodCustomIcon|. Call to
184 // |HideUserPodcustomIcon| resets it.
185 scoped_ptr<base::DictionaryValue> last_custom_icon_;
186 size_t show_icon_count_;
188 // Auth type and value set using |SetAuthType|.
190 base::string16 auth_value_;
192 DISALLOW_COPY_AND_ASSIGN(TestLockHandler);
195 class EasyUnlockScreenlockStateHandlerTest : public testing::Test {
197 EasyUnlockScreenlockStateHandlerTest() : user_email_("test_user@gmail.com") {}
198 ~EasyUnlockScreenlockStateHandlerTest() override {}
200 void SetUp() override {
201 // Create and inject fake lock handler to the screenlock bridge.
202 lock_handler_.reset(new TestLockHandler(user_email_));
203 ScreenlockBridge* screenlock_bridge = ScreenlockBridge::Get();
204 screenlock_bridge->SetLockHandler(lock_handler_.get());
206 // Create the screenlock state handler object that will be tested.
207 state_handler_.reset(new EasyUnlockScreenlockStateHandler(
209 EasyUnlockScreenlockStateHandler::NO_HARDLOCK,
213 void TearDown() override {
214 ScreenlockBridge::Get()->SetLockHandler(NULL);
215 lock_handler_.reset();
216 state_handler_.reset();
220 // The state handler that is being tested.
221 scoped_ptr<EasyUnlockScreenlockStateHandler> state_handler_;
223 // The user associated with |state_handler_|.
224 const std::string user_email_;
226 // Faked lock handler given to ScreenlockBridge during the test. Abstracts
227 // the screen lock UI.
228 scoped_ptr<TestLockHandler> lock_handler_;
231 TEST_F(EasyUnlockScreenlockStateHandlerTest, AuthenticatedTrialRun) {
232 state_handler_->SetTrialRun();
233 state_handler_->ChangeState(
234 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
236 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
237 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
238 lock_handler_->GetAuthType(user_email_));
240 ASSERT_TRUE(lock_handler_->HasCustomIcon());
241 EXPECT_EQ(kUnlockedIconId, lock_handler_->GetCustomIconId());
242 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
243 EXPECT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
244 EXPECT_FALSE(lock_handler_->CustomIconHardlocksOnClick());
246 state_handler_->ChangeState(
247 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
248 // Duplicated state change should be ignored.
249 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
252 TEST_F(EasyUnlockScreenlockStateHandlerTest, AuthenticatedNotInitialRun) {
253 state_handler_->ChangeState(
254 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
256 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
257 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
258 lock_handler_->GetAuthType(user_email_));
260 ASSERT_TRUE(lock_handler_->HasCustomIcon());
261 EXPECT_EQ(kUnlockedIconId, lock_handler_->GetCustomIconId());
262 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
263 EXPECT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown());
264 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
267 TEST_F(EasyUnlockScreenlockStateHandlerTest, IsActive) {
268 EXPECT_FALSE(state_handler_->IsActive());
269 state_handler_->ChangeState(
270 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
271 EXPECT_TRUE(state_handler_->IsActive());
272 state_handler_->ChangeState(
273 EasyUnlockScreenlockStateHandler::STATE_INACTIVE);
274 EXPECT_FALSE(state_handler_->IsActive());
277 TEST_F(EasyUnlockScreenlockStateHandlerTest, BluetoothConnecting) {
278 state_handler_->ChangeState(
279 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
280 EXPECT_TRUE(state_handler_->IsActive());
282 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
283 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
284 lock_handler_->GetAuthType(user_email_));
286 ASSERT_TRUE(lock_handler_->HasCustomIcon());
287 EXPECT_EQ(kSpinnerIconId, lock_handler_->GetCustomIconId());
288 EXPECT_FALSE(lock_handler_->CustomIconHasTooltip());
289 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
291 state_handler_->ChangeState(
292 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
293 // Duplicated state change should be ignored.
294 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
297 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockedState) {
298 state_handler_->ChangeState(
299 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
301 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
302 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
303 lock_handler_->GetAuthType(user_email_));
305 state_handler_->SetHardlockState(
306 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
308 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
309 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
310 lock_handler_->GetAuthType(user_email_));
312 ASSERT_TRUE(lock_handler_->HasCustomIcon());
313 EXPECT_EQ(kHardlockedIconId, lock_handler_->GetCustomIconId());
314 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
315 EXPECT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
316 EXPECT_FALSE(lock_handler_->CustomIconHardlocksOnClick());
318 state_handler_->SetHardlockState(
319 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
321 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
322 ASSERT_TRUE(lock_handler_->HasCustomIcon());
325 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockedStateNoPairing) {
326 state_handler_->ChangeState(
327 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
329 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
330 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
331 lock_handler_->GetAuthType(user_email_));
333 state_handler_->SetHardlockState(
334 EasyUnlockScreenlockStateHandler::NO_PAIRING);
336 EXPECT_FALSE(lock_handler_->HasCustomIcon());
337 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
338 lock_handler_->GetAuthType(user_email_));
341 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatesWithLockedIcon) {
342 std::vector<EasyUnlockScreenlockStateHandler::State> states;
343 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH);
344 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
345 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED);
346 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE);
348 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
349 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
351 for (size_t i = 0; i < states.size(); ++i) {
352 state_handler_->ChangeState(states[i]);
353 EXPECT_TRUE(state_handler_->IsActive());
355 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount())
356 << "State: " << states[i];
357 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
358 lock_handler_->GetAuthType(user_email_))
359 << "State: " << states[i];
361 ASSERT_TRUE(lock_handler_->HasCustomIcon())
362 << "State: " << states[i];
363 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId())
364 << "State: " << states[i];
365 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip())
366 << "State: " << states[i];
367 EXPECT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown())
368 << "State: " << states[i];
369 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick())
370 << "State: " << states[i];
372 state_handler_->ChangeState(states[i]);
373 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount())
374 << "State: " << states[i];
378 // Verifies tooltips are autoshown on initial run.
379 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatesWithLockedIcon_TrialRun) {
380 state_handler_->SetTrialRun();
382 std::vector<EasyUnlockScreenlockStateHandler::State> states;
383 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH);
384 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
385 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED);
386 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE);
388 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
389 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
391 for (size_t i = 0; i < states.size(); ++i) {
392 state_handler_->ChangeState(states[i]);
393 ASSERT_TRUE(lock_handler_->HasCustomIcon())
394 << "State: " << states[i];
395 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip())
396 << "State: " << states[i];
397 EXPECT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown())
398 << "State: " << states[i];
401 ScreenlockBridge::Get()->SetLockHandler(NULL);
402 lock_handler_.reset(new TestLockHandler(user_email_));
403 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
404 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
406 // After the screen unlocks the tooltips should not be shown anymore.
407 for (size_t i = 0; i < states.size(); ++i) {
408 state_handler_->ChangeState(states[i]);
409 ASSERT_TRUE(lock_handler_->HasCustomIcon())
410 << "State: " << states[i];
411 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip())
412 << "State: " << states[i];
413 EXPECT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown())
414 << "State: " << states[i];
418 TEST_F(EasyUnlockScreenlockStateHandlerTest, SettingTrialRunUpdatesUI) {
419 state_handler_->ChangeState(
420 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
422 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
423 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
424 lock_handler_->GetAuthType(user_email_));
426 ASSERT_TRUE(lock_handler_->HasCustomIcon());
427 ASSERT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown());
429 state_handler_->SetTrialRun();
431 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
432 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
433 lock_handler_->GetAuthType(user_email_));
435 ASSERT_TRUE(lock_handler_->HasCustomIcon());
436 ASSERT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
439 TEST_F(EasyUnlockScreenlockStateHandlerTest,
440 LockScreenClearedOnStateHandlerDestruction) {
441 state_handler_->ChangeState(
442 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
444 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
445 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
446 lock_handler_->GetAuthType(user_email_));
448 ASSERT_TRUE(lock_handler_->HasCustomIcon());
450 state_handler_.reset();
452 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
453 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
454 lock_handler_->GetAuthType(user_email_));
456 ASSERT_FALSE(lock_handler_->HasCustomIcon());
459 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatePreservedWhenScreenUnlocks) {
460 state_handler_->ChangeState(
461 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
463 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
464 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
465 lock_handler_->GetAuthType(user_email_));
466 ASSERT_TRUE(lock_handler_->HasCustomIcon());
468 ScreenlockBridge::Get()->SetLockHandler(NULL);
469 lock_handler_.reset(new TestLockHandler(user_email_));
470 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
471 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
473 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
474 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
475 lock_handler_->GetAuthType(user_email_));
476 ASSERT_TRUE(lock_handler_->HasCustomIcon());
479 TEST_F(EasyUnlockScreenlockStateHandlerTest, StateChangeWhileScreenUnlocked) {
480 state_handler_->ChangeState(
481 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
483 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
484 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
485 lock_handler_->GetAuthType(user_email_));
486 ASSERT_TRUE(lock_handler_->HasCustomIcon());
488 ScreenlockBridge::Get()->SetLockHandler(NULL);
489 lock_handler_.reset(new TestLockHandler(user_email_));
490 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
492 state_handler_->ChangeState(
493 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
495 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
497 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
498 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
499 lock_handler_->GetAuthType(user_email_));
500 ASSERT_TRUE(lock_handler_->HasCustomIcon());
501 EXPECT_EQ(kSpinnerIconId, lock_handler_->GetCustomIconId());
504 TEST_F(EasyUnlockScreenlockStateHandlerTest,
505 HardlockEnabledAfterInitialUnlock) {
506 state_handler_->SetTrialRun();
508 std::vector<EasyUnlockScreenlockStateHandler::State> states;
510 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
512 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
513 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH);
514 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
515 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED);
516 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE);
517 // This one should go last as changing state to AUTHENTICATED enables hard
519 states.push_back(EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
521 for (size_t i = 0; i < states.size(); ++i) {
522 state_handler_->ChangeState(states[i]);
523 ASSERT_TRUE(lock_handler_->HasCustomIcon()) << "State: " << states[i];
524 EXPECT_FALSE(lock_handler_->CustomIconHardlocksOnClick())
525 << "State: " << states[i];
528 ScreenlockBridge::Get()->SetLockHandler(NULL);
529 lock_handler_.reset(new TestLockHandler(user_email_));
530 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
532 for (size_t i = 0; i < states.size(); ++i) {
533 state_handler_->ChangeState(states[i]);
534 ASSERT_TRUE(lock_handler_->HasCustomIcon()) << "State: " << states[i];
535 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick())
536 << "State: " << states[i];
540 TEST_F(EasyUnlockScreenlockStateHandlerTest,
541 NoPairingHardlockClearsIcon) {
542 state_handler_->ChangeState(
543 EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
545 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
546 ASSERT_TRUE(lock_handler_->HasCustomIcon());
547 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
549 state_handler_->SetHardlockState(
550 EasyUnlockScreenlockStateHandler::NO_PAIRING);
552 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
553 ASSERT_FALSE(lock_handler_->HasCustomIcon());
556 TEST_F(EasyUnlockScreenlockStateHandlerTest, PairingChangedHardlock) {
557 state_handler_->ChangeState(
558 EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
560 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
561 ASSERT_TRUE(lock_handler_->HasCustomIcon());
562 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
564 state_handler_->SetHardlockState(
565 EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
567 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
568 ASSERT_TRUE(lock_handler_->HasCustomIcon());
569 EXPECT_EQ(kLockedToBeActivatedIconId, lock_handler_->GetCustomIconId());
571 state_handler_->ChangeState(
572 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
574 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
575 ASSERT_TRUE(lock_handler_->HasCustomIcon());
576 EXPECT_EQ(kLockedToBeActivatedIconId, lock_handler_->GetCustomIconId());
579 TEST_F(EasyUnlockScreenlockStateHandlerTest,
580 PairingChangedHardlockIneffectiveOnInitialRun) {
581 state_handler_->SetTrialRun();
583 state_handler_->ChangeState(
584 EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
586 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
587 ASSERT_TRUE(lock_handler_->HasCustomIcon());
588 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
590 state_handler_->SetHardlockState(
591 EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
593 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
594 ASSERT_TRUE(lock_handler_->HasCustomIcon());
595 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
598 TEST_F(EasyUnlockScreenlockStateHandlerTest, InactiveStateHidesIcon) {
599 state_handler_->ChangeState(
600 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
602 ASSERT_TRUE(lock_handler_->HasCustomIcon());
604 state_handler_->ChangeState(
605 EasyUnlockScreenlockStateHandler::STATE_INACTIVE);
607 ASSERT_FALSE(lock_handler_->HasCustomIcon());
610 TEST_F(EasyUnlockScreenlockStateHandlerTest,
611 AuthenticatedStateClearsPreviousAuthValue) {
612 state_handler_->ChangeState(
613 EasyUnlockScreenlockStateHandler::STATE_INACTIVE);
615 lock_handler_->SetAuthValue(base::ASCIIToUTF16("xxx"));
617 state_handler_->ChangeState(
618 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
620 EXPECT_EQ(l10n_util::GetStringUTF16(
621 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE),
622 lock_handler_->GetAuthValue());
624 state_handler_->ChangeState(
625 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
627 EXPECT_EQ(base::string16(), lock_handler_->GetAuthValue());
630 TEST_F(EasyUnlockScreenlockStateHandlerTest,
631 ChangingStateDoesNotAffectAuthValueIfAuthTypeDoesNotChange) {
632 lock_handler_->SetAuthValue(base::ASCIIToUTF16("xxx"));
634 state_handler_->ChangeState(
635 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
636 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
638 state_handler_->ChangeState(
639 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
640 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
642 state_handler_->ChangeState(
643 EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
644 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
645 ASSERT_TRUE(lock_handler_->HasCustomIcon());
646 EXPECT_EQ(kSpinnerIconId, lock_handler_->GetCustomIconId());
649 TEST_F(EasyUnlockScreenlockStateHandlerTest, StateChangesIgnoredIfHardlocked) {
650 state_handler_->ChangeState(
651 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
653 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
654 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
655 lock_handler_->GetAuthType(user_email_));
657 state_handler_->SetHardlockState(
658 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
660 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
661 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
662 lock_handler_->GetAuthType(user_email_));
663 ASSERT_TRUE(lock_handler_->HasCustomIcon());
664 EXPECT_EQ(kHardlockedIconId, lock_handler_->GetCustomIconId());
666 state_handler_->ChangeState(
667 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
668 ASSERT_TRUE(lock_handler_->HasCustomIcon());
669 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
671 state_handler_->ChangeState(
672 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
673 ASSERT_TRUE(lock_handler_->HasCustomIcon());
674 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
675 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
676 lock_handler_->GetAuthType(user_email_));
679 TEST_F(EasyUnlockScreenlockStateHandlerTest,
680 LockScreenChangeableOnLockAfterHardlockReset) {
681 state_handler_->ChangeState(
682 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
684 state_handler_->SetHardlockState(
685 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
686 EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
688 state_handler_->SetHardlockState(
689 EasyUnlockScreenlockStateHandler::NO_HARDLOCK);
691 ScreenlockBridge::Get()->SetLockHandler(NULL);
692 lock_handler_.reset(new TestLockHandler(user_email_));
693 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
694 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
696 state_handler_->ChangeState(
697 EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
699 EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
700 EXPECT_TRUE(lock_handler_->HasCustomIcon());
702 ScreenlockBridge::Get()->SetLockHandler(NULL);
703 lock_handler_.reset(new TestLockHandler(user_email_));
704 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
705 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
707 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
708 EXPECT_TRUE(lock_handler_->HasCustomIcon());
709 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
710 lock_handler_->GetAuthType(user_email_));
711 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
713 state_handler_->ChangeState(
714 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
715 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
716 EXPECT_TRUE(lock_handler_->HasCustomIcon());
717 EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
718 lock_handler_->GetAuthType(user_email_));
719 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
722 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockStatePersistsOverUnlocks) {
723 state_handler_->ChangeState(
724 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
725 state_handler_->SetHardlockState(
726 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
727 EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
729 ScreenlockBridge::Get()->SetLockHandler(NULL);
730 lock_handler_.reset(new TestLockHandler(user_email_));
731 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
732 ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
734 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
735 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
736 lock_handler_->GetAuthType(user_email_));
737 ASSERT_TRUE(lock_handler_->HasCustomIcon());
738 EXPECT_EQ(kHardlockedIconId, lock_handler_->GetCustomIconId());
740 state_handler_->ChangeState(
741 EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
742 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
743 EXPECT_TRUE(lock_handler_->HasCustomIcon());
744 EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
745 lock_handler_->GetAuthType(user_email_));
748 TEST_F(EasyUnlockScreenlockStateHandlerTest, NoOverrideOnlineSignin) {
749 lock_handler_->SetAuthType(user_email_,
750 ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
753 std::vector<EasyUnlockScreenlockStateHandler::State> states;
754 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_BLUETOOTH);
755 states.push_back(EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
756 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED);
757 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE);
759 EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
760 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
761 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNLOCKABLE);
762 states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_UNSUPPORTED);
763 states.push_back(EasyUnlockScreenlockStateHandler::STATE_RSSI_TOO_LOW);
764 states.push_back(EasyUnlockScreenlockStateHandler::STATE_TX_POWER_TOO_HIGH);
765 states.push_back(EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
767 for (size_t i = 0; i < states.size(); ++i) {
768 state_handler_->ChangeState(states[i]);
769 EXPECT_EQ(ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
770 lock_handler_->GetAuthType(user_email_));
771 EXPECT_FALSE(lock_handler_->HasCustomIcon());
774 std::vector<EasyUnlockScreenlockStateHandler::HardlockState> hardlock_states;
775 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::NO_HARDLOCK);
776 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
777 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
778 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::PAIRING_ADDED);
779 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::NO_PAIRING);
780 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::LOGIN_FAILED);
782 for (size_t i = 0; i < hardlock_states.size(); ++i) {
783 state_handler_->SetHardlockState(hardlock_states[i]);
784 EXPECT_EQ(ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
785 lock_handler_->GetAuthType(user_email_));
786 EXPECT_FALSE(lock_handler_->HasCustomIcon());