Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / easy_unlock_screenlock_state_handler_unittest.cc
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.
4
5 #include <string>
6 #include <vector>
7
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"
17
18 namespace {
19
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";
27
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());
33
34   base::string16 replaced = ReplaceStringPlaceholders(input, subst, &offsets);
35   return !offsets.empty();
36 }
37
38 // Fake lock handler to be used in these tests.
39 class TestLockHandler : public ScreenlockBridge::LockHandler {
40  public:
41   explicit TestLockHandler(const std::string& user_email)
42       : user_email_(user_email),
43         show_icon_count_(0u),
44         auth_type_(OFFLINE_PASSWORD) {
45   }
46   ~TestLockHandler() override {}
47
48   // ScreenlockBridge::LockHandler implementation:
49   void ShowBannerMessage(const base::string16& message) override {
50     ASSERT_FALSE(true) << "Should not be reached.";
51   }
52
53   void ShowUserPodCustomIcon(
54       const std::string& user_email,
55       const ScreenlockBridge::UserPodCustomIconOptions& icon) override {
56     ASSERT_EQ(user_email_, user_email);
57     ++show_icon_count_;
58     last_custom_icon_ = icon.ToDictionaryValue().Pass();
59     ValidateCustomIcon();
60   }
61
62   void HideUserPodCustomIcon(const std::string& user_email) override {
63     ASSERT_EQ(user_email_, user_email);
64     last_custom_icon_.reset();
65   }
66
67   void EnableInput() override {
68     ASSERT_FALSE(true) << "Should not be reached.";
69   }
70
71   void SetAuthType(const std::string& user_email,
72                    AuthType auth_type,
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);
78
79     auth_type_ = auth_type;
80     auth_value_ = auth_value;
81   }
82
83   AuthType GetAuthType(const std::string& user_email) const override {
84     EXPECT_EQ(user_email_, user_email);
85     return auth_type_;
86   }
87
88   void Unlock(const std::string& user_email) override {
89     ASSERT_FALSE(true) << "Should not be reached.";
90   }
91
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.";
96   }
97
98   // Utility methods used by tests:
99
100   // Gets last set auth value.
101   base::string16 GetAuthValue() const {
102     return auth_value_;
103   }
104
105   // Sets the auth value.
106   void SetAuthValue(const base::string16& value) {
107     auth_value_ = value;
108   }
109
110   // Returns the number of times an icon was shown since the last call to this
111   // method.
112   size_t GetAndResetShowIconCount() {
113     size_t result = show_icon_count_;
114     show_icon_count_ = 0u;
115     return result;
116   }
117
118   // Whether the custom icon is set.
119   bool HasCustomIcon() const {
120     return last_custom_icon_;
121   }
122
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
125   // string.
126   std::string GetCustomIconId() const {
127     std::string result;
128     if (last_custom_icon_)
129       last_custom_icon_->GetString("id", &result);
130     return result;
131   }
132
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");
136   }
137
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);
143     return result;
144   }
145
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 {
149     bool result = false;
150     if (last_custom_icon_)
151       last_custom_icon_->GetBoolean("tooltip.autoshow", &result);
152     return result;
153   }
154
155   // Whether the custom icon is set and if has hardlock capability enabed.
156   bool CustomIconHardlocksOnClick() const {
157     bool result = false;
158     if (last_custom_icon_)
159       last_custom_icon_->GetBoolean("hardlockOnClick", &result);
160     return result;
161   }
162
163  private:
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());
168
169     EXPECT_TRUE(last_custom_icon_->HasKey("id"));
170
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));
176     }
177   }
178
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_;
182
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_;
187
188   // Auth type and value set using |SetAuthType|.
189   AuthType auth_type_;
190   base::string16 auth_value_;
191
192   DISALLOW_COPY_AND_ASSIGN(TestLockHandler);
193 };
194
195 class EasyUnlockScreenlockStateHandlerTest : public testing::Test {
196  public:
197   EasyUnlockScreenlockStateHandlerTest() : user_email_("test_user@gmail.com") {}
198   ~EasyUnlockScreenlockStateHandlerTest() override {}
199
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());
205
206     // Create the screenlock state handler object that will be tested.
207     state_handler_.reset(new EasyUnlockScreenlockStateHandler(
208         user_email_,
209         EasyUnlockScreenlockStateHandler::NO_HARDLOCK,
210         screenlock_bridge));
211   }
212
213   void TearDown() override {
214     ScreenlockBridge::Get()->SetLockHandler(NULL);
215     lock_handler_.reset();
216     state_handler_.reset();
217   }
218
219  protected:
220   // The state handler that is being tested.
221   scoped_ptr<EasyUnlockScreenlockStateHandler> state_handler_;
222
223   // The user associated with |state_handler_|.
224   const std::string user_email_;
225
226   // Faked lock handler given to ScreenlockBridge during the test. Abstracts
227   // the screen lock UI.
228   scoped_ptr<TestLockHandler> lock_handler_;
229 };
230
231 TEST_F(EasyUnlockScreenlockStateHandlerTest, AuthenticatedTrialRun) {
232   state_handler_->SetTrialRun();
233   state_handler_->ChangeState(
234       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
235
236   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
237   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
238             lock_handler_->GetAuthType(user_email_));
239
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());
245
246   state_handler_->ChangeState(
247       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
248   // Duplicated state change should be ignored.
249   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
250 }
251
252 TEST_F(EasyUnlockScreenlockStateHandlerTest, AuthenticatedNotInitialRun) {
253   state_handler_->ChangeState(
254       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
255
256   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
257   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
258             lock_handler_->GetAuthType(user_email_));
259
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());
265 }
266
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());
275 }
276
277 TEST_F(EasyUnlockScreenlockStateHandlerTest, BluetoothConnecting) {
278   state_handler_->ChangeState(
279       EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
280   EXPECT_TRUE(state_handler_->IsActive());
281
282   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
283   EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
284             lock_handler_->GetAuthType(user_email_));
285
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());
290
291   state_handler_->ChangeState(
292       EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
293   // Duplicated state change should be ignored.
294   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
295 }
296
297 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockedState) {
298   state_handler_->ChangeState(
299       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
300
301   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
302   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
303             lock_handler_->GetAuthType(user_email_));
304
305   state_handler_->SetHardlockState(
306       EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
307
308   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
309   EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
310             lock_handler_->GetAuthType(user_email_));
311
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());
317
318   state_handler_->SetHardlockState(
319       EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
320
321   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
322   ASSERT_TRUE(lock_handler_->HasCustomIcon());
323 }
324
325 TEST_F(EasyUnlockScreenlockStateHandlerTest, HardlockedStateNoPairing) {
326   state_handler_->ChangeState(
327       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
328
329   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
330   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
331             lock_handler_->GetAuthType(user_email_));
332
333   state_handler_->SetHardlockState(
334       EasyUnlockScreenlockStateHandler::NO_PAIRING);
335
336   EXPECT_FALSE(lock_handler_->HasCustomIcon());
337   EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
338             lock_handler_->GetAuthType(user_email_));
339 }
340
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);
347   states.push_back(
348       EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
349   states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
350
351   for (size_t i = 0; i < states.size(); ++i) {
352     state_handler_->ChangeState(states[i]);
353     EXPECT_TRUE(state_handler_->IsActive());
354
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];
360
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];
371
372     state_handler_->ChangeState(states[i]);
373     EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount())
374         << "State: " << states[i];
375   }
376 }
377
378 // Verifies tooltips are autoshown on initial run.
379 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatesWithLockedIcon_TrialRun) {
380   state_handler_->SetTrialRun();
381
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);
387   states.push_back(
388       EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
389   states.push_back(EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
390
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];
399   }
400
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());
405
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];
415   }
416 }
417
418 TEST_F(EasyUnlockScreenlockStateHandlerTest, SettingTrialRunUpdatesUI) {
419   state_handler_->ChangeState(
420       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
421
422   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
423   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
424             lock_handler_->GetAuthType(user_email_));
425
426   ASSERT_TRUE(lock_handler_->HasCustomIcon());
427   ASSERT_FALSE(lock_handler_->IsCustomIconTooltipAutoshown());
428
429   state_handler_->SetTrialRun();
430
431   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
432   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
433             lock_handler_->GetAuthType(user_email_));
434
435   ASSERT_TRUE(lock_handler_->HasCustomIcon());
436   ASSERT_TRUE(lock_handler_->IsCustomIconTooltipAutoshown());
437 }
438
439 TEST_F(EasyUnlockScreenlockStateHandlerTest,
440        LockScreenClearedOnStateHandlerDestruction) {
441   state_handler_->ChangeState(
442       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
443
444   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
445   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
446             lock_handler_->GetAuthType(user_email_));
447
448   ASSERT_TRUE(lock_handler_->HasCustomIcon());
449
450   state_handler_.reset();
451
452   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
453   EXPECT_EQ(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
454             lock_handler_->GetAuthType(user_email_));
455
456   ASSERT_FALSE(lock_handler_->HasCustomIcon());
457 }
458
459 TEST_F(EasyUnlockScreenlockStateHandlerTest, StatePreservedWhenScreenUnlocks) {
460   state_handler_->ChangeState(
461       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
462
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());
467
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());
472
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());
477 }
478
479 TEST_F(EasyUnlockScreenlockStateHandlerTest, StateChangeWhileScreenUnlocked) {
480   state_handler_->ChangeState(
481       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
482
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());
487
488   ScreenlockBridge::Get()->SetLockHandler(NULL);
489   lock_handler_.reset(new TestLockHandler(user_email_));
490   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
491
492   state_handler_->ChangeState(
493       EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
494
495   ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
496
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());
502 }
503
504 TEST_F(EasyUnlockScreenlockStateHandlerTest,
505        HardlockEnabledAfterInitialUnlock) {
506   state_handler_->SetTrialRun();
507
508   std::vector<EasyUnlockScreenlockStateHandler::State> states;
509   states.push_back(
510       EasyUnlockScreenlockStateHandler::STATE_BLUETOOTH_CONNECTING);
511   states.push_back(
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
518   // locking.
519   states.push_back(EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
520
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];
526   }
527
528   ScreenlockBridge::Get()->SetLockHandler(NULL);
529   lock_handler_.reset(new TestLockHandler(user_email_));
530   ScreenlockBridge::Get()->SetLockHandler(lock_handler_.get());
531
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];
537   }
538 }
539
540 TEST_F(EasyUnlockScreenlockStateHandlerTest,
541        NoPairingHardlockClearsIcon) {
542   state_handler_->ChangeState(
543       EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
544
545   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
546   ASSERT_TRUE(lock_handler_->HasCustomIcon());
547   EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
548
549   state_handler_->SetHardlockState(
550       EasyUnlockScreenlockStateHandler::NO_PAIRING);
551
552   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
553   ASSERT_FALSE(lock_handler_->HasCustomIcon());
554 }
555
556 TEST_F(EasyUnlockScreenlockStateHandlerTest, PairingChangedHardlock) {
557   state_handler_->ChangeState(
558       EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
559
560   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
561   ASSERT_TRUE(lock_handler_->HasCustomIcon());
562   EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
563
564   state_handler_->SetHardlockState(
565       EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
566
567   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
568   ASSERT_TRUE(lock_handler_->HasCustomIcon());
569   EXPECT_EQ(kLockedToBeActivatedIconId, lock_handler_->GetCustomIconId());
570
571   state_handler_->ChangeState(
572       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
573
574   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
575   ASSERT_TRUE(lock_handler_->HasCustomIcon());
576   EXPECT_EQ(kLockedToBeActivatedIconId, lock_handler_->GetCustomIconId());
577 }
578
579 TEST_F(EasyUnlockScreenlockStateHandlerTest,
580        PairingChangedHardlockIneffectiveOnInitialRun) {
581   state_handler_->SetTrialRun();
582
583   state_handler_->ChangeState(
584       EasyUnlockScreenlockStateHandler::STATE_PHONE_LOCKED);
585
586   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
587   ASSERT_TRUE(lock_handler_->HasCustomIcon());
588   EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
589
590   state_handler_->SetHardlockState(
591       EasyUnlockScreenlockStateHandler::PAIRING_CHANGED);
592
593   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
594   ASSERT_TRUE(lock_handler_->HasCustomIcon());
595   EXPECT_EQ(kLockedIconId, lock_handler_->GetCustomIconId());
596 }
597
598 TEST_F(EasyUnlockScreenlockStateHandlerTest, InactiveStateHidesIcon) {
599   state_handler_->ChangeState(
600       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
601
602   ASSERT_TRUE(lock_handler_->HasCustomIcon());
603
604   state_handler_->ChangeState(
605       EasyUnlockScreenlockStateHandler::STATE_INACTIVE);
606
607   ASSERT_FALSE(lock_handler_->HasCustomIcon());
608 }
609
610 TEST_F(EasyUnlockScreenlockStateHandlerTest,
611        AuthenticatedStateClearsPreviousAuthValue) {
612   state_handler_->ChangeState(
613       EasyUnlockScreenlockStateHandler::STATE_INACTIVE);
614
615   lock_handler_->SetAuthValue(base::ASCIIToUTF16("xxx"));
616
617   state_handler_->ChangeState(
618       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
619
620   EXPECT_EQ(l10n_util::GetStringUTF16(
621                 IDS_EASY_UNLOCK_SCREENLOCK_USER_POD_AUTH_VALUE),
622             lock_handler_->GetAuthValue());
623
624   state_handler_->ChangeState(
625       EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
626
627   EXPECT_EQ(base::string16(), lock_handler_->GetAuthValue());
628 }
629
630 TEST_F(EasyUnlockScreenlockStateHandlerTest,
631        ChangingStateDoesNotAffectAuthValueIfAuthTypeDoesNotChange) {
632   lock_handler_->SetAuthValue(base::ASCIIToUTF16("xxx"));
633
634   state_handler_->ChangeState(
635       EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
636   EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
637
638   state_handler_->ChangeState(
639       EasyUnlockScreenlockStateHandler::STATE_PHONE_NOT_AUTHENTICATED);
640   EXPECT_EQ(base::ASCIIToUTF16("xxx"), lock_handler_->GetAuthValue());
641
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());
647 }
648
649 TEST_F(EasyUnlockScreenlockStateHandlerTest, StateChangesIgnoredIfHardlocked) {
650   state_handler_->ChangeState(
651       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
652
653   EXPECT_EQ(1u, lock_handler_->GetAndResetShowIconCount());
654   EXPECT_EQ(ScreenlockBridge::LockHandler::USER_CLICK,
655             lock_handler_->GetAuthType(user_email_));
656
657   state_handler_->SetHardlockState(
658       EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
659
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());
665
666   state_handler_->ChangeState(
667       EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
668   ASSERT_TRUE(lock_handler_->HasCustomIcon());
669   EXPECT_EQ(0u, lock_handler_->GetAndResetShowIconCount());
670
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_));
677 }
678
679 TEST_F(EasyUnlockScreenlockStateHandlerTest,
680        LockScreenChangeableOnLockAfterHardlockReset) {
681   state_handler_->ChangeState(
682       EasyUnlockScreenlockStateHandler::STATE_AUTHENTICATED);
683
684   state_handler_->SetHardlockState(
685       EasyUnlockScreenlockStateHandler::USER_HARDLOCK);
686   EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
687
688   state_handler_->SetHardlockState(
689       EasyUnlockScreenlockStateHandler::NO_HARDLOCK);
690
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());
695
696   state_handler_->ChangeState(
697       EasyUnlockScreenlockStateHandler::STATE_NO_PHONE);
698
699   EXPECT_EQ(2u, lock_handler_->GetAndResetShowIconCount());
700   EXPECT_TRUE(lock_handler_->HasCustomIcon());
701
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());
706
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());
712
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());
720 }
721
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());
728
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());
733
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());
739
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_));
746 }
747
748 TEST_F(EasyUnlockScreenlockStateHandlerTest, NoOverrideOnlineSignin) {
749   lock_handler_->SetAuthType(user_email_,
750                              ScreenlockBridge::LockHandler::ONLINE_SIGN_IN,
751                              base::string16());
752
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);
758   states.push_back(
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);
766
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());
772   }
773
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);
781
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());
787   }
788 }
789
790 }  // namespace