Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / signin / easy_unlock_screenlock_state_handler_unittest.cc
blobc89d357627c9fc2067e0712cfe9d37ef413fdbb0
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"
7 #include <string>
8 #include <vector>
10 #include "base/strings/string16.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/histogram_tester.h"
15 #include "chrome/browser/signin/easy_unlock_metrics.h"
16 #include "chrome/browser/signin/easy_unlock_service.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/proximity_auth/screenlock_bridge.h"
19 #include "components/proximity_auth/screenlock_state.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/base/l10n/l10n_util.h"
23 using proximity_auth::ScreenlockState;
25 namespace {
27 // Icons used by EasyUnlockScreenlockStateHandler. The icon id values are the
28 // same as the ones set by proximity_auth::ScreenlockBridge.
29 const char kLockedIconId[] = "locked";
30 const char kLockedToBeActivatedIconId[] = "locked-to-be-activated";
31 const char kUnlockedIconId[] = "unlocked";
32 const char kSpinnerIconId[] = "spinner";
33 const char kHardlockedIconId[] = "hardlocked";
35 // Checks if |input| string has any unreplaced placeholders.
36 bool StringHasPlaceholders(const base::string16& input) {
37 std::vector<size_t> offsets;
38 std::vector<base::string16> subst;
39 subst.push_back(base::string16());
41 base::string16 replaced =
42 base::ReplaceStringPlaceholders(input, subst, &offsets);
43 return !offsets.empty();
46 // Fake lock handler to be used in these tests.
47 class TestLockHandler : public proximity_auth::ScreenlockBridge::LockHandler {
48 public:
49 explicit TestLockHandler(const std::string& user_email)
50 : user_email_(user_email),
51 show_icon_count_(0u),
52 auth_type_(OFFLINE_PASSWORD) {
54 ~TestLockHandler() override {}
56 // proximity_auth::ScreenlockBridge::LockHandler implementation:
57 void ShowBannerMessage(const base::string16& message) override {
58 ASSERT_FALSE(true) << "Should not be reached.";
61 void ShowUserPodCustomIcon(
62 const std::string& user_email,
63 const proximity_auth::ScreenlockBridge::UserPodCustomIconOptions& icon)
64 override {
65 ASSERT_EQ(user_email_, user_email);
66 ++show_icon_count_;
67 last_custom_icon_ = icon.ToDictionaryValue().Pass();
68 ValidateCustomIcon();
71 void HideUserPodCustomIcon(const std::string& user_email) override {
72 ASSERT_EQ(user_email_, user_email);
73 last_custom_icon_.reset();
76 void EnableInput() override {
77 ASSERT_FALSE(true) << "Should not be reached.";
80 void SetAuthType(const std::string& user_email,
81 AuthType auth_type,
82 const base::string16& auth_value) override {
83 ASSERT_EQ(user_email_, user_email);
84 // Generally, this is allowed, but EasyUnlockScreenlockStateHandler should
85 // avoid resetting the same auth type.
86 EXPECT_NE(auth_type_, auth_type);
88 auth_type_ = auth_type;
89 auth_value_ = auth_value;
92 AuthType GetAuthType(const std::string& user_email) const override {
93 EXPECT_EQ(user_email_, user_email);
94 return auth_type_;
97 ScreenType GetScreenType() const override {
98 return LOCK_SCREEN;
101 void Unlock(const std::string& user_email) override {
102 ASSERT_FALSE(true) << "Should not be reached.";
105 void AttemptEasySignin(const std::string& user_email,
106 const std::string& secret,
107 const std::string& key_label) override {
108 ASSERT_FALSE(true) << "Should not be reached.";
111 // Utility methods used by tests:
113 // Gets last set auth value.
114 base::string16 GetAuthValue() const {
115 return auth_value_;
118 // Sets the auth value.
119 void SetAuthValue(const base::string16& value) {
120 auth_value_ = value;
123 // Returns the number of times an icon was shown since the last call to this
124 // method.
125 size_t GetAndResetShowIconCount() {
126 size_t result = show_icon_count_;
127 show_icon_count_ = 0u;
128 return result;
131 // Whether the custom icon is set.
132 bool HasCustomIcon() const {
133 return last_custom_icon_;
136 // If custom icon is set, returns the icon's id.
137 // If there is no icon, or if it doesn't have an id set, returns an empty
138 // string.
139 std::string GetCustomIconId() const {
140 std::string result;
141 if (last_custom_icon_)
142 last_custom_icon_->GetString("id", &result);
143 return result;
146 // Whether the custom icon is set and it has a tooltip.
147 bool CustomIconHasTooltip() const {
148 return last_custom_icon_ && last_custom_icon_->HasKey("tooltip");
151 // Gets the custom icon's tooltip text, if one is set.
152 base::string16 GetCustomIconTooltip() const {
153 base::string16 result;
154 if (last_custom_icon_)
155 last_custom_icon_->GetString("tooltip.text", &result);
156 return result;
159 // Whether the custom icon's tooltip should be autoshown. If the icon is not
160 // set, or it doesn't have a tooltip, returns false.
161 bool IsCustomIconTooltipAutoshown() const {
162 bool result = false;
163 if (last_custom_icon_)
164 last_custom_icon_->GetBoolean("tooltip.autoshow", &result);
165 return result;
168 // Whether the custom icon is set and if has hardlock capability enabed.
169 bool CustomIconHardlocksOnClick() const {
170 bool result = false;
171 if (last_custom_icon_)
172 last_custom_icon_->GetBoolean("hardlockOnClick", &result);
173 return result;
176 private:
177 // Does some sanity checks on the last icon set by |ShowUserPodCustomIcon|.
178 // It will cause a test failure if the icon is not valid.
179 void ValidateCustomIcon() {
180 ASSERT_TRUE(last_custom_icon_.get());
182 EXPECT_TRUE(last_custom_icon_->HasKey("id"));
184 if (last_custom_icon_->HasKey("tooltip")) {
185 base::string16 tooltip;
186 EXPECT_TRUE(last_custom_icon_->GetString("tooltip.text", &tooltip));
187 EXPECT_FALSE(tooltip.empty());
188 EXPECT_FALSE(StringHasPlaceholders(tooltip));
192 // The fake user email used in test. All methods called on |this| should be
193 // associated with this user.
194 const std::string user_email_;
196 // The last icon set using |SetUserPodCustomIcon|. Call to
197 // |HideUserPodcustomIcon| resets it.
198 scoped_ptr<base::DictionaryValue> last_custom_icon_;
199 size_t show_icon_count_;
201 // Auth type and value set using |SetAuthType|.
202 AuthType auth_type_;
203 base::string16 auth_value_;
205 DISALLOW_COPY_AND_ASSIGN(TestLockHandler);
208 class EasyUnlockScreenlockStateHandlerTest : public testing::Test {
209 public:
210 EasyUnlockScreenlockStateHandlerTest() : user_email_("test_user@gmail.com") {}
211 ~EasyUnlockScreenlockStateHandlerTest() override {}
213 void SetUp() override {
214 // Create and inject fake lock handler to the screenlock bridge.
215 lock_handler_.reset(new TestLockHandler(user_email_));
216 proximity_auth::ScreenlockBridge* screenlock_bridge =
217 proximity_auth::ScreenlockBridge::Get();
218 screenlock_bridge->SetLockHandler(lock_handler_.get());
220 // Create the screenlock state handler object that will be tested.
221 state_handler_.reset(new EasyUnlockScreenlockStateHandler(
222 user_email_,
223 EasyUnlockScreenlockStateHandler::NO_HARDLOCK,
224 screenlock_bridge));
227 void TearDown() override {
228 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
229 lock_handler_.reset();
230 state_handler_.reset();
233 protected:
234 // The state handler that is being tested.
235 scoped_ptr<EasyUnlockScreenlockStateHandler> state_handler_;
237 // The user associated with |state_handler_|.
238 const std::string user_email_;
240 // Faked lock handler given to proximity_auth::ScreenlockBridge during the
241 // test. Abstracts the screen lock UI.
242 scoped_ptr<TestLockHandler> lock_handler_;
245 TEST_F(EasyUnlockScreenlockStateHandlerTest, AuthenticatedTrialRun) {
246 state_handler_->SetTrialRun();
247 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
249 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
250 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
251 lock_handler_->GetAuthType(user_email_));
253 ASSERT_TRUE(lock_handler_->HasCustomIcon());
254 EXPECT_EQ(kUnlockedIconId, lock_handler_->GetCustomIconId());
255 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
256 EXPECT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
257 EXPECT_FALSE(lock_handler_->CustomIconHardlocksOnClick());
259 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
260 // Duplicated state change should be ignored.
261 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
264 TEST_F(EasyUnlockScreenlockStateHandlerTest, AuthenticatedNotInitialRun) {
265 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
267 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
268 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
269 lock_handler_->GetAuthType(user_email_));
271 ASSERT_TRUE(lock_handler_->HasCustomIcon());
272 EXPECT_EQ(kUnlockedIconId, lock_handler_->GetCustomIconId());
273 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
274 EXPECT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown());
275 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
278 TEST_F(EasyUnlockScreenlockStateHandlerTest, IsActive) {
279 EXPECT_FALSE(state_handler_->IsActive());
280 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
281 EXPECT_TRUE(state_handler_->IsActive());
282 state_handler_->ChangeState(ScreenlockState::INACTIVE);
283 EXPECT_FALSE(state_handler_->IsActive());
286 TEST_F(EasyUnlockScreenlockStateHandlerTest, BluetoothConnecting) {
287 state_handler_->ChangeState(ScreenlockState::BLUETOOTH_CONNECTING);
288 EXPECT_TRUE(state_handler_->IsActive());
290 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
291 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
292 lock_handler_->GetAuthType(user_email_));
294 ASSERT_TRUE(lock_handler_->HasCustomIcon());
295 EXPECT_EQ(kSpinnerIconId, lock_handler_->GetCustomIconId());
296 EXPECT_FALSE(lock_handler_->CustomIconHasTooltip());
297 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
299 state_handler_->ChangeState(ScreenlockState::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(ScreenlockState::AUTHENTICATED);
307 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
308 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
309 lock_handler_->GetAuthType(user_email_));
311 state_handler_->SetHardlockState(
312 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
314 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
315 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
316 lock_handler_->GetAuthType(user_email_));
318 ASSERT_TRUE(lock_handler_->HasCustomIcon());
319 EXPECT_EQ(kHardlockedIconId, lock_handler_->GetCustomIconId());
320 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
321 EXPECT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
322 EXPECT_FALSE(lock_handler_->CustomIconHardlocksOnClick());
324 state_handler_->SetHardlockState(
325 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
327 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
328 ASSERT_TRUE(lock_handler_->HasCustomIcon());
331 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockedStateNoPairing) {
332 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
334 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
335 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
336 lock_handler_->GetAuthType(user_email_));
338 state_handler_->SetHardlockState(
339 EasyUnlockScreenlockStateHandler::NO_PAIRING);
341 EXPECT_FALSE(lock_handler_->HasCustomIcon());
342 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
343 lock_handler_->GetAuthType(user_email_));
346 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatesWithLockedIcon) {
347 std::vector<ScreenlockState> states;
348 states.push_back(ScreenlockState::NO_BLUETOOTH);
349 states.push_back(ScreenlockState::NO_PHONE);
350 states.push_back(ScreenlockState::PHONE_UNSUPPORTED);
351 states.push_back(ScreenlockState::PHONE_NOT_LOCKABLE);
352 states.push_back(ScreenlockState::PHONE_NOT_AUTHENTICATED);
353 states.push_back(ScreenlockState::PHONE_LOCKED);
355 for (size_t i = 0; i < states.size(); ++i) {
356 SCOPED_TRACE(base::IntToString(i));
357 state_handler_->ChangeState(states[i]);
358 EXPECT_TRUE(state_handler_->IsActive());
360 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
361 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
362 lock_handler_->GetAuthType(user_email_));
364 ASSERT_TRUE(lock_handler_->HasCustomIcon());
365 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
366 EXPECT_TRUE(lock_handler_->CustomIconHasTooltip());
367 EXPECT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
368 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
370 state_handler_->ChangeState(states[i]);
371 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
375 TEST_F(EasyUnlockScreenlockStateHandlerTest, SettingTrialRunUpdatesUI) {
376 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
378 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
379 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
380 lock_handler_->GetAuthType(user_email_));
382 ASSERT_TRUE(lock_handler_->HasCustomIcon());
383 ASSERT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown());
385 state_handler_->SetTrialRun();
387 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
388 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
389 lock_handler_->GetAuthType(user_email_));
391 ASSERT_TRUE(lock_handler_->HasCustomIcon());
392 ASSERT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
395 TEST_F(EasyUnlockScreenlockStateHandlerTest,
396 LockScreenClearedOnStateHandlerDestruction) {
397 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
399 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
400 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
401 lock_handler_->GetAuthType(user_email_));
403 ASSERT_TRUE(lock_handler_->HasCustomIcon());
405 state_handler_.reset();
407 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
408 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
409 lock_handler_->GetAuthType(user_email_));
411 ASSERT_FALSE(lock_handler_->HasCustomIcon());
414 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatePreservedWhenScreenUnlocks) {
415 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
417 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
418 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
419 lock_handler_->GetAuthType(user_email_));
420 ASSERT_TRUE(lock_handler_->HasCustomIcon());
422 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
423 lock_handler_.reset(new TestLockHandler(user_email_));
424 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
425 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
427 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
428 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
429 lock_handler_->GetAuthType(user_email_));
430 ASSERT_TRUE(lock_handler_->HasCustomIcon());
433 TEST_F(EasyUnlockScreenlockStateHandlerTest, StateChangeWhileScreenUnlocked) {
434 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
436 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
437 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
438 lock_handler_->GetAuthType(user_email_));
439 ASSERT_TRUE(lock_handler_->HasCustomIcon());
441 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
442 lock_handler_.reset(new TestLockHandler(user_email_));
443 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
445 state_handler_->ChangeState(ScreenlockState::BLUETOOTH_CONNECTING);
447 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
449 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
450 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
451 lock_handler_->GetAuthType(user_email_));
452 ASSERT_TRUE(lock_handler_->HasCustomIcon());
453 EXPECT_EQ(kSpinnerIconId, lock_handler_->GetCustomIconId());
456 TEST_F(EasyUnlockScreenlockStateHandlerTest,
457 HardlockEnabledAfterInitialUnlock) {
458 state_handler_->SetTrialRun();
460 std::vector<ScreenlockState> states;
461 states.push_back(ScreenlockState::BLUETOOTH_CONNECTING);
462 states.push_back(ScreenlockState::PHONE_NOT_AUTHENTICATED);
463 states.push_back(ScreenlockState::NO_BLUETOOTH);
464 states.push_back(ScreenlockState::NO_PHONE);
465 states.push_back(ScreenlockState::PHONE_UNSUPPORTED);
466 states.push_back(ScreenlockState::PHONE_NOT_LOCKABLE);
467 // This one should go last as changing state to AUTHENTICATED enables hard
468 // locking.
469 states.push_back(ScreenlockState::AUTHENTICATED);
471 for (size_t i = 0; i < states.size(); ++i) {
472 SCOPED_TRACE(base::IntToString(i));
473 state_handler_->ChangeState(states[i]);
474 ASSERT_TRUE(lock_handler_->HasCustomIcon());
475 EXPECT_FALSE(lock_handler_->CustomIconHardlocksOnClick());
478 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
479 lock_handler_.reset(new TestLockHandler(user_email_));
480 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
482 for (size_t i = 0; i < states.size(); ++i) {
483 SCOPED_TRACE(base::IntToString(i));
484 state_handler_->ChangeState(states[i]);
485 ASSERT_TRUE(lock_handler_->HasCustomIcon());
486 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
490 TEST_F(EasyUnlockScreenlockStateHandlerTest,
491 NoPairingHardlockClearsIcon) {
492 state_handler_->ChangeState(ScreenlockState::PHONE_LOCKED);
494 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
495 ASSERT_TRUE(lock_handler_->HasCustomIcon());
496 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
498 state_handler_->SetHardlockState(
499 EasyUnlockScreenlockStateHandler::NO_PAIRING);
501 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
502 ASSERT_FALSE(lock_handler_->HasCustomIcon());
505 TEST_F(EasyUnlockScreenlockStateHandlerTest, PairingChangedHardlock) {
506 state_handler_->ChangeState(ScreenlockState::PHONE_LOCKED);
508 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
509 ASSERT_TRUE(lock_handler_->HasCustomIcon());
510 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
512 state_handler_->SetHardlockState(
513 EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
515 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
516 ASSERT_TRUE(lock_handler_->HasCustomIcon());
517 EXPECT_EQ(kLockedToBeActivatedIconId, lock_handler_->GetCustomIconId());
519 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
521 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
522 ASSERT_TRUE(lock_handler_->HasCustomIcon());
523 EXPECT_EQ(kLockedToBeActivatedIconId, lock_handler_->GetCustomIconId());
526 TEST_F(EasyUnlockScreenlockStateHandlerTest,
527 PairingChangedHardlockIneffectiveOnInitialRun) {
528 state_handler_->SetTrialRun();
530 state_handler_->ChangeState(ScreenlockState::PHONE_LOCKED);
532 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
533 ASSERT_TRUE(lock_handler_->HasCustomIcon());
534 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
536 state_handler_->SetHardlockState(
537 EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
539 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
540 ASSERT_TRUE(lock_handler_->HasCustomIcon());
541 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
544 TEST_F(EasyUnlockScreenlockStateHandlerTest, InactiveStateHidesIcon) {
545 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
547 ASSERT_TRUE(lock_handler_->HasCustomIcon());
549 state_handler_->ChangeState(ScreenlockState::INACTIVE);
551 ASSERT_FALSE(lock_handler_->HasCustomIcon());
554 TEST_F(EasyUnlockScreenlockStateHandlerTest,
555 AuthenticatedStateClearsPreviousAuthValue) {
556 state_handler_->ChangeState(ScreenlockState::INACTIVE);
558 lock_handler_->SetAuthValue(base::ASCIIToUTF16("xxx"));
560 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
562 EXPECT_EQ(l10n_util::GetStringUTF16(
563 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE),
564 lock_handler_->GetAuthValue());
566 state_handler_->ChangeState(ScreenlockState::NO_PHONE);
568 EXPECT_EQ(base::string16(), lock_handler_->GetAuthValue());
571 TEST_F(EasyUnlockScreenlockStateHandlerTest,
572 ChangingStateDoesNotAffectAuthValueIfAuthTypeDoesNotChange) {
573 lock_handler_->SetAuthValue(base::ASCIIToUTF16("xxx"));
575 state_handler_->ChangeState(ScreenlockState::NO_PHONE);
576 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
578 state_handler_->ChangeState(ScreenlockState::PHONE_NOT_AUTHENTICATED);
579 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
581 state_handler_->ChangeState(ScreenlockState::BLUETOOTH_CONNECTING);
582 EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
583 ASSERT_TRUE(lock_handler_->HasCustomIcon());
584 EXPECT_EQ(kSpinnerIconId, lock_handler_->GetCustomIconId());
587 TEST_F(EasyUnlockScreenlockStateHandlerTest, StateChangesIgnoredIfHardlocked) {
588 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
590 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
591 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
592 lock_handler_->GetAuthType(user_email_));
594 state_handler_->SetHardlockState(
595 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
597 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
598 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
599 lock_handler_->GetAuthType(user_email_));
600 ASSERT_TRUE(lock_handler_->HasCustomIcon());
601 EXPECT_EQ(kHardlockedIconId, lock_handler_->GetCustomIconId());
603 state_handler_->ChangeState(ScreenlockState::NO_PHONE);
604 ASSERT_TRUE(lock_handler_->HasCustomIcon());
605 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
607 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
608 ASSERT_TRUE(lock_handler_->HasCustomIcon());
609 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
610 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
611 lock_handler_->GetAuthType(user_email_));
614 TEST_F(EasyUnlockScreenlockStateHandlerTest,
615 LockScreenChangeableOnLockAfterHardlockReset) {
616 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
618 state_handler_->SetHardlockState(
619 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
620 EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
622 state_handler_->SetHardlockState(
623 EasyUnlockScreenlockStateHandler::NO_HARDLOCK);
625 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
626 lock_handler_.reset(new TestLockHandler(user_email_));
627 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
628 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
630 state_handler_->ChangeState(ScreenlockState::NO_PHONE);
632 EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
633 EXPECT_TRUE(lock_handler_->HasCustomIcon());
635 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
636 lock_handler_.reset(new TestLockHandler(user_email_));
637 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
638 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
640 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
641 EXPECT_TRUE(lock_handler_->HasCustomIcon());
642 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
643 lock_handler_->GetAuthType(user_email_));
644 EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
646 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
647 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
648 EXPECT_TRUE(lock_handler_->HasCustomIcon());
649 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK,
650 lock_handler_->GetAuthType(user_email_));
651 EXPECT_TRUE(lock_handler_->CustomIconHardlocksOnClick());
654 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockStatePersistsOverUnlocks) {
655 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
656 state_handler_->SetHardlockState(
657 EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
658 EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
660 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL);
661 lock_handler_.reset(new TestLockHandler(user_email_));
662 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
663 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
665 EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
666 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
667 lock_handler_->GetAuthType(user_email_));
668 ASSERT_TRUE(lock_handler_->HasCustomIcon());
669 EXPECT_EQ(kHardlockedIconId, lock_handler_->GetCustomIconId());
671 state_handler_->ChangeState(ScreenlockState::AUTHENTICATED);
672 EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
673 EXPECT_TRUE(lock_handler_->HasCustomIcon());
674 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
675 lock_handler_->GetAuthType(user_email_));
678 TEST_F(EasyUnlockScreenlockStateHandlerTest, NoOverrideOnlineSignin) {
679 lock_handler_->SetAuthType(
680 user_email_,
681 proximity_auth::ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
682 base::string16());
684 std::vector<ScreenlockState> states;
685 states.push_back(ScreenlockState::NO_BLUETOOTH);
686 states.push_back(ScreenlockState::NO_PHONE);
687 states.push_back(ScreenlockState::PHONE_UNSUPPORTED);
688 states.push_back(ScreenlockState::PHONE_NOT_LOCKABLE);
689 states.push_back(ScreenlockState::PHONE_NOT_AUTHENTICATED);
690 states.push_back(ScreenlockState::PHONE_LOCKED);
691 states.push_back(ScreenlockState::PHONE_NOT_LOCKABLE);
692 states.push_back(ScreenlockState::PHONE_UNSUPPORTED);
693 states.push_back(ScreenlockState::RSSI_TOO_LOW);
694 states.push_back(ScreenlockState::TX_POWER_TOO_HIGH);
695 states.push_back(ScreenlockState::AUTHENTICATED);
697 for (size_t i = 0; i < states.size(); ++i) {
698 state_handler_->ChangeState(states[i]);
699 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
700 lock_handler_->GetAuthType(user_email_));
701 EXPECT_FALSE(lock_handler_->HasCustomIcon());
704 std::vector<EasyUnlockScreenlockStateHandler::HardlockState> hardlock_states;
705 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::NO_HARDLOCK);
706 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
707 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
708 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::PAIRING_ADDED);
709 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::NO_PAIRING);
710 hardlock_states.push_back(EasyUnlockScreenlockStateHandler::LOGIN_FAILED);
712 for (size_t i = 0; i < hardlock_states.size(); ++i) {
713 state_handler_->SetHardlockState(hardlock_states[i]);
714 EXPECT_EQ(proximity_auth::ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
715 lock_handler_->GetAuthType(user_email_));
716 EXPECT_FALSE(lock_handler_->HasCustomIcon());
720 TEST_F(EasyUnlockScreenlockStateHandlerTest, TrialRunMetrics) {
721 base::HistogramTester histogram_tester;
723 // Simulate the user clicking on the lock icon twice outside of a trial run.
724 // No trial run metrics should be recorded.
725 state_handler_->RecordClickOnLockIcon();
726 state_handler_->RecordClickOnLockIcon();
727 histogram_tester.ExpectTotalCount("EasyUnlock.TrialRun.Events", 0);
729 // Simulate the user clicking on the lock icon three times during a trial run.
730 state_handler_->SetTrialRun();
731 state_handler_->RecordClickOnLockIcon();
732 state_handler_->RecordClickOnLockIcon();
733 state_handler_->RecordClickOnLockIcon();
734 histogram_tester.ExpectTotalCount("EasyUnlock.TrialRun.Events", 4);
735 histogram_tester.ExpectBucketCount("EasyUnlock.TrialRun.Events",
736 EASY_UNLOCK_TRIAL_RUN_EVENT_LAUNCHED, 1);
737 histogram_tester.ExpectBucketCount(
738 "EasyUnlock.TrialRun.Events",
739 EASY_UNLOCK_TRIAL_RUN_EVENT_CLICKED_LOCK_ICON, 3);
742 } // namespace