1// Copyright 2013 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 "ash/wm/lock_state_controller.h"
6
7#include "ash/session/session_state_delegate.h"
8#include "ash/shell.h"
9#include "ash/test/ash_test_base.h"
10#include "ash/test/test_lock_state_controller_delegate.h"
11#include "ash/test/test_screenshot_delegate.h"
12#include "ash/test/test_session_state_animator.h"
13#include "ash/test/test_shell_delegate.h"
14#include "ash/wm/maximize_mode/maximize_mode_controller.h"
15#include "ash/wm/power_button_controller.h"
16#include "ash/wm/session_state_animator.h"
17#include "base/memory/scoped_ptr.h"
18#include "base/memory/scoped_vector.h"
19#include "base/time/time.h"
20#include "ui/events/test/event_generator.h"
21#include "ui/gfx/size.h"
22
23#if defined(OS_CHROMEOS)
24#include "ui/display/chromeos/display_configurator.h"
25#include "ui/display/chromeos/test/test_display_snapshot.h"
26#include "ui/display/types/display_constants.h"
27#endif
28
29namespace ash {
30namespace test {
31namespace {
32
33bool cursor_visible() {
34  return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible();
35}
36
37void CheckCalledCallback(bool* flag) {
38  if (flag)
39    (*flag) = true;
40}
41
42}  // namespace
43
44class LockStateControllerTest : public AshTestBase {
45 public:
46  LockStateControllerTest() : power_button_controller_(NULL),
47                              lock_state_controller_(NULL),
48                              lock_state_controller_delegate_(NULL),
49                              test_animator_(NULL) {
50  }
51  virtual ~LockStateControllerTest() {}
52
53  virtual void SetUp() OVERRIDE {
54    AshTestBase::SetUp();
55
56    scoped_ptr<LockStateControllerDelegate> lock_state_controller_delegate(
57        lock_state_controller_delegate_ = new TestLockStateControllerDelegate);
58    test_animator_ = new TestSessionStateAnimator;
59
60    lock_state_controller_ = Shell::GetInstance()->lock_state_controller();
61    lock_state_controller_->SetDelegate(lock_state_controller_delegate.Pass());
62    lock_state_controller_->set_animator_for_test(test_animator_);
63
64    test_api_.reset(new LockStateController::TestApi(lock_state_controller_));
65
66    power_button_controller_ = Shell::GetInstance()->power_button_controller();
67    session_state_delegate_ = Shell::GetInstance()->session_state_delegate();
68
69    shell_delegate_ = reinterpret_cast<TestShellDelegate*>(
70        ash::Shell::GetInstance()->delegate());
71  }
72
73 protected:
74  void GenerateMouseMoveEvent() {
75    ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
76    generator.MoveMouseTo(10, 10);
77  }
78
79  int NumShutdownRequests() {
80    return lock_state_controller_delegate_->num_shutdown_requests() +
81        shell_delegate_->num_exit_requests();
82  }
83
84  void Advance(SessionStateAnimator::AnimationSpeed speed) {
85    test_animator_->Advance(test_animator_->GetDuration(speed));
86  }
87
88  void AdvancePartially(SessionStateAnimator::AnimationSpeed speed,
89                        float factor) {
90    base::TimeDelta duration = test_animator_->GetDuration(speed);
91    base::TimeDelta partial_duration =
92        base::TimeDelta::FromInternalValue(duration.ToInternalValue() * factor);
93    test_animator_->Advance(partial_duration);
94  }
95
96  void ExpectPreLockAnimationStarted() {
97    SCOPED_TRACE("Failure in ExpectPreLockAnimationStarted");
98    EXPECT_LT(0u, test_animator_->GetAnimationCount());
99    EXPECT_TRUE(
100        test_animator_->AreContainersAnimated(
101            SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
102            SessionStateAnimator::ANIMATION_LIFT));
103    EXPECT_TRUE(
104        test_animator_->AreContainersAnimated(
105            SessionStateAnimator::LAUNCHER,
106            SessionStateAnimator::ANIMATION_FADE_OUT));
107    EXPECT_TRUE(
108        test_animator_->AreContainersAnimated(
109            SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
110            SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
111    EXPECT_TRUE(test_api_->is_animating_lock());
112  }
113
114  void ExpectPreLockAnimationRunning() {
115    SCOPED_TRACE("Failure in ExpectPreLockAnimationRunning");
116    EXPECT_LT(0u, test_animator_->GetAnimationCount());
117    EXPECT_TRUE(
118        test_animator_->AreContainersAnimated(
119            SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
120            SessionStateAnimator::ANIMATION_LIFT));
121    EXPECT_TRUE(
122        test_animator_->AreContainersAnimated(
123            SessionStateAnimator::LAUNCHER,
124            SessionStateAnimator::ANIMATION_FADE_OUT));
125    EXPECT_TRUE(test_api_->is_animating_lock());
126  }
127
128  void ExpectPreLockAnimationCancel() {
129    SCOPED_TRACE("Failure in ExpectPreLockAnimationCancel");
130    EXPECT_LT(0u, test_animator_->GetAnimationCount());
131    EXPECT_TRUE(
132        test_animator_->AreContainersAnimated(
133            SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
134            SessionStateAnimator::ANIMATION_UNDO_LIFT));
135    EXPECT_TRUE(
136        test_animator_->AreContainersAnimated(
137            SessionStateAnimator::LAUNCHER,
138            SessionStateAnimator::ANIMATION_FADE_IN));
139  }
140
141  void ExpectPreLockAnimationFinished() {
142    SCOPED_TRACE("Failure in ExpectPreLockAnimationFinished");
143    EXPECT_FALSE(
144        test_animator_->AreContainersAnimated(
145            SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
146            SessionStateAnimator::ANIMATION_LIFT));
147    EXPECT_FALSE(
148        test_animator_->AreContainersAnimated(
149            SessionStateAnimator::LAUNCHER,
150            SessionStateAnimator::ANIMATION_FADE_OUT));
151    EXPECT_FALSE(
152        test_animator_->AreContainersAnimated(
153            SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
154            SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
155  }
156
157  void ExpectPostLockAnimationStarted() {
158    SCOPED_TRACE("Failure in ExpectPostLockAnimationStarted");
159    EXPECT_LT(0u, test_animator_->GetAnimationCount());
160    EXPECT_TRUE(
161        test_animator_->AreContainersAnimated(
162            SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
163            SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
164  }
165
166  void ExpectPostLockAnimationFinished() {
167    SCOPED_TRACE("Failure in ExpectPostLockAnimationFinished");
168    EXPECT_FALSE(
169        test_animator_->AreContainersAnimated(
170            SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
171            SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
172  }
173
174  void ExpectUnlockBeforeUIDestroyedAnimationStarted() {
175    SCOPED_TRACE("Failure in ExpectUnlockBeforeUIDestroyedAnimationStarted");
176    EXPECT_LT(0u, test_animator_->GetAnimationCount());
177    EXPECT_TRUE(
178        test_animator_->AreContainersAnimated(
179            SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
180            SessionStateAnimator::ANIMATION_LIFT));
181  }
182
183  void ExpectUnlockBeforeUIDestroyedAnimationFinished() {
184    SCOPED_TRACE("Failure in ExpectUnlockBeforeUIDestroyedAnimationFinished");
185    EXPECT_FALSE(
186        test_animator_->AreContainersAnimated(
187            SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
188            SessionStateAnimator::ANIMATION_LIFT));
189  }
190
191  void ExpectUnlockAfterUIDestroyedAnimationStarted() {
192    SCOPED_TRACE("Failure in ExpectUnlockAfterUIDestroyedAnimationStarted");
193    EXPECT_LT(0u, test_animator_->GetAnimationCount());
194    EXPECT_TRUE(
195        test_animator_->AreContainersAnimated(
196            SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
197            SessionStateAnimator::ANIMATION_DROP));
198    EXPECT_TRUE(
199        test_animator_->AreContainersAnimated(
200            SessionStateAnimator::LAUNCHER,
201            SessionStateAnimator::ANIMATION_FADE_IN));
202  }
203
204  void ExpectUnlockAfterUIDestroyedAnimationFinished() {
205    SCOPED_TRACE("Failure in ExpectUnlockAfterUIDestroyedAnimationFinished");
206    EXPECT_EQ(0u, test_animator_->GetAnimationCount());
207    EXPECT_FALSE(
208        test_animator_->AreContainersAnimated(
209            SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
210            SessionStateAnimator::ANIMATION_DROP));
211    EXPECT_FALSE(
212        test_animator_->AreContainersAnimated(
213            SessionStateAnimator::LAUNCHER,
214            SessionStateAnimator::ANIMATION_FADE_IN));
215  }
216
217  void ExpectShutdownAnimationStarted() {
218    SCOPED_TRACE("Failure in ExpectShutdownAnimationStarted");
219    EXPECT_LT(0u, test_animator_->GetAnimationCount());
220    EXPECT_TRUE(
221        test_animator_->AreContainersAnimated(
222            SessionStateAnimator::ROOT_CONTAINER,
223            SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
224  }
225
226  void ExpectShutdownAnimationFinished() {
227    SCOPED_TRACE("Failure in ExpectShutdownAnimationFinished");
228    EXPECT_EQ(0u, test_animator_->GetAnimationCount());
229    EXPECT_FALSE(
230        test_animator_->AreContainersAnimated(
231            SessionStateAnimator::ROOT_CONTAINER,
232            SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
233  }
234
235  void ExpectShutdownAnimationCancel() {
236    SCOPED_TRACE("Failure in ExpectShutdownAnimationCancel");
237    EXPECT_LT(0u, test_animator_->GetAnimationCount());
238    EXPECT_TRUE(
239        test_animator_->AreContainersAnimated(
240            SessionStateAnimator::ROOT_CONTAINER,
241            SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS));
242  }
243
244  void ExpectBackgroundIsShowing() {
245    SCOPED_TRACE("Failure in ExpectBackgroundIsShowing");
246    EXPECT_LT(0u, test_animator_->GetAnimationCount());
247    EXPECT_TRUE(
248        test_animator_->AreContainersAnimated(
249            SessionStateAnimator::DESKTOP_BACKGROUND,
250            SessionStateAnimator::ANIMATION_FADE_IN));
251  }
252
253  void ExpectBackgroundIsHiding() {
254    SCOPED_TRACE("Failure in ExpectBackgroundIsHiding");
255    EXPECT_LT(0u, test_animator_->GetAnimationCount());
256    EXPECT_TRUE(
257        test_animator_->AreContainersAnimated(
258            SessionStateAnimator::DESKTOP_BACKGROUND,
259            SessionStateAnimator::ANIMATION_FADE_OUT));
260  }
261
262  void ExpectRestoringBackgroundVisibility() {
263    SCOPED_TRACE("Failure in ExpectRestoringBackgroundVisibility");
264    EXPECT_LT(0u, test_animator_->GetAnimationCount());
265    EXPECT_TRUE(
266        test_animator_->AreContainersAnimated(
267            SessionStateAnimator::DESKTOP_BACKGROUND,
268            SessionStateAnimator::ANIMATION_FADE_IN));
269  }
270
271  void ExpectUnlockedState() {
272    SCOPED_TRACE("Failure in ExpectUnlockedState");
273    EXPECT_EQ(0u, test_animator_->GetAnimationCount());
274    EXPECT_FALSE(session_state_delegate_->IsScreenLocked());
275  }
276
277  void ExpectLockedState() {
278    SCOPED_TRACE("Failure in ExpectLockedState");
279    EXPECT_EQ(0u, test_animator_->GetAnimationCount());
280    EXPECT_TRUE(session_state_delegate_->IsScreenLocked());
281  }
282
283  void HideBackground() {
284    test_animator_->HideBackground();
285  }
286
287  void PressPowerButton() {
288    power_button_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now());
289  }
290
291  void ReleasePowerButton() {
292    power_button_controller_->OnPowerButtonEvent(false, base::TimeTicks::Now());
293  }
294
295  void PressLockButton() {
296    power_button_controller_->OnLockButtonEvent(true, base::TimeTicks::Now());
297  }
298
299  void ReleaseLockButton() {
300    power_button_controller_->OnLockButtonEvent(false, base::TimeTicks::Now());
301  }
302
303  void PressVolumeDown() {
304    GetEventGenerator().PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
305  }
306
307  void ReleaseVolumeDown() {
308    GetEventGenerator().ReleaseKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
309  }
310
311  void SystemLocks() {
312    lock_state_controller_->OnLockStateChanged(true);
313    session_state_delegate_->LockScreen();
314  }
315
316  void SuccessfulAuthentication(bool* call_flag) {
317    base::Closure closure = base::Bind(&CheckCalledCallback, call_flag);
318    lock_state_controller_->OnLockScreenHide(closure);
319  }
320
321  void SystemUnlocks() {
322    lock_state_controller_->OnLockStateChanged(false);
323    session_state_delegate_->UnlockScreen();
324  }
325
326  void EnableMaximizeMode(bool enable) {
327    Shell::GetInstance()->maximize_mode_controller()->
328        EnableMaximizeModeWindowManager(enable);
329  }
330
331  void Initialize(bool legacy_button, user::LoginStatus status) {
332    power_button_controller_->set_has_legacy_power_button_for_test(
333        legacy_button);
334    lock_state_controller_->OnLoginStateChanged(status);
335    SetUserLoggedIn(status != user::LOGGED_IN_NONE);
336    if (status == user::LOGGED_IN_GUEST)
337      SetCanLockScreen(false);
338    lock_state_controller_->OnLockStateChanged(false);
339  }
340
341  PowerButtonController* power_button_controller_;  // not owned
342  LockStateController* lock_state_controller_;  // not owned
343  TestLockStateControllerDelegate*
344      lock_state_controller_delegate_;  // not owned
345  TestSessionStateAnimator* test_animator_;  // not owned
346  SessionStateDelegate* session_state_delegate_;  // not owned
347  scoped_ptr<LockStateController::TestApi> test_api_;
348  TestShellDelegate* shell_delegate_;  // not owned
349
350 private:
351  DISALLOW_COPY_AND_ASSIGN(LockStateControllerTest);
352};
353
354// Test the lock-to-shutdown flow for non-Chrome-OS hardware that doesn't
355// correctly report power button releases.  We should lock immediately the first
356// time the button is pressed and shut down when it's pressed from the locked
357// state.
358TEST_F(LockStateControllerTest, LegacyLockAndShutDown) {
359  Initialize(true, user::LOGGED_IN_USER);
360
361  ExpectUnlockedState();
362
363  // We should request that the screen be locked immediately after seeing the
364  // power button get pressed.
365  PressPowerButton();
366
367  EXPECT_FALSE(test_api_->is_lock_cancellable());
368
369  ExpectPreLockAnimationStarted();
370  test_animator_->CompleteAllAnimations(true);
371  ExpectPreLockAnimationFinished();
372
373  EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
374
375  // Notify that we locked successfully.
376  lock_state_controller_->OnStartingLock();
377  EXPECT_EQ(0u, test_animator_->GetAnimationCount());
378
379  SystemLocks();
380
381  ExpectPostLockAnimationStarted();
382  test_animator_->CompleteAllAnimations(true);
383  ExpectPostLockAnimationFinished();
384
385  // We shouldn't progress towards the shutdown state, however.
386  EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
387  EXPECT_FALSE(test_api_->shutdown_timer_is_running());
388
389  ReleasePowerButton();
390
391  // Hold the button again and check that we start shutting down.
392  PressPowerButton();
393
394  ExpectShutdownAnimationStarted();
395
396  EXPECT_EQ(0, NumShutdownRequests());
397  // Make sure a mouse move event won't show the cursor.
398  GenerateMouseMoveEvent();
399  EXPECT_FALSE(cursor_visible());
400
401  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
402  test_api_->trigger_real_shutdown_timeout();
403  EXPECT_EQ(1, NumShutdownRequests());
404}
405
406// Test that we start shutting down immediately if the power button is pressed
407// while we're not logged in on an unofficial system.
408TEST_F(LockStateControllerTest, LegacyNotLoggedIn) {
409  Initialize(true, user::LOGGED_IN_NONE);
410
411  PressPowerButton();
412  ExpectShutdownAnimationStarted();
413
414  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
415}
416
417// Test that we start shutting down immediately if the power button is pressed
418// while we're logged in as a guest on an unofficial system.
419TEST_F(LockStateControllerTest, LegacyGuest) {
420  Initialize(true, user::LOGGED_IN_GUEST);
421
422  PressPowerButton();
423  ExpectShutdownAnimationStarted();
424
425  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
426}
427
428// When we hold the power button while the user isn't logged in, we should shut
429// down the machine directly.
430TEST_F(LockStateControllerTest, ShutdownWhenNotLoggedIn) {
431  Initialize(false, user::LOGGED_IN_NONE);
432
433  // Press the power button and check that we start the shutdown timer.
434  PressPowerButton();
435  EXPECT_FALSE(test_api_->is_animating_lock());
436  EXPECT_TRUE(test_api_->shutdown_timer_is_running());
437  ExpectShutdownAnimationStarted();
438
439  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
440
441  // Release the power button before the shutdown timer fires.
442  ReleasePowerButton();
443
444  EXPECT_FALSE(test_api_->shutdown_timer_is_running());
445  ExpectShutdownAnimationCancel();
446
447  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_REVERT, 0.5f);
448
449  // Press the button again and make the shutdown timeout fire this time.
450  // Check that we start the timer for actually requesting the shutdown.
451  PressPowerButton();
452
453  EXPECT_TRUE(test_api_->shutdown_timer_is_running());
454
455  Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
456  ExpectShutdownAnimationFinished();
457  test_api_->trigger_shutdown_timeout();
458
459  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
460  EXPECT_EQ(0, NumShutdownRequests());
461
462  // When the timout fires, we should request a shutdown.
463  test_api_->trigger_real_shutdown_timeout();
464
465  EXPECT_EQ(1, NumShutdownRequests());
466}
467
468// Test that we lock the screen and deal with unlocking correctly.
469TEST_F(LockStateControllerTest, LockAndUnlock) {
470  Initialize(false, user::LOGGED_IN_USER);
471
472  ExpectUnlockedState();
473
474  // Press the power button and check that the lock timer is started and that we
475  // start lifting the non-screen-locker containers.
476  PressPowerButton();
477
478  ExpectPreLockAnimationStarted();
479  EXPECT_TRUE(test_api_->is_lock_cancellable());
480  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
481
482  test_animator_->CompleteAllAnimations(true);
483  ExpectPreLockAnimationFinished();
484
485  EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
486
487  // Notify that we locked successfully.
488  lock_state_controller_->OnStartingLock();
489  // We had that animation already.
490  EXPECT_EQ(0u, test_animator_->GetAnimationCount());
491
492  SystemLocks();
493
494  ExpectPostLockAnimationStarted();
495  test_animator_->CompleteAllAnimations(true);
496  ExpectPostLockAnimationFinished();
497
498  // When we release the power button, the lock-to-shutdown timer should be
499  // stopped.
500  ExpectLockedState();
501  EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
502  ReleasePowerButton();
503  ExpectLockedState();
504  EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
505
506  // Notify that the screen has been unlocked.  We should show the
507  // non-screen-locker windows.
508  bool called = false;
509  SuccessfulAuthentication(&called);
510
511  ExpectUnlockBeforeUIDestroyedAnimationStarted();
512  EXPECT_FALSE(called);
513  test_animator_->CompleteAllAnimations(true);
514  ExpectUnlockBeforeUIDestroyedAnimationFinished();
515
516  EXPECT_TRUE(called);
517
518  SystemUnlocks();
519
520  ExpectUnlockAfterUIDestroyedAnimationStarted();
521  test_animator_->CompleteAllAnimations(true);
522  ExpectUnlockAfterUIDestroyedAnimationFinished();
523
524  ExpectUnlockedState();
525}
526
527// Test that we deal with cancelling lock correctly.
528TEST_F(LockStateControllerTest, LockAndCancel) {
529  Initialize(false, user::LOGGED_IN_USER);
530
531  ExpectUnlockedState();
532
533  // Press the power button and check that the lock timer is started and that we
534  // start lifting the non-screen-locker containers.
535  PressPowerButton();
536
537  ExpectPreLockAnimationStarted();
538  EXPECT_TRUE(test_api_->is_lock_cancellable());
539
540  // forward only half way through
541  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
542
543  // Release the button before the lock timer fires.
544  ReleasePowerButton();
545
546  ExpectPreLockAnimationCancel();
547
548  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
549  ExpectUnlockedState();
550  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
551}
552
553// Test that we deal with cancelling lock correctly.
554TEST_F(LockStateControllerTest, LockAndCancelAndLockAgain) {
555  Initialize(false, user::LOGGED_IN_USER);
556
557  ExpectUnlockedState();
558
559  // Press the power button and check that the lock timer is started and that we
560  // start lifting the non-screen-locker containers.
561  PressPowerButton();
562
563  ExpectPreLockAnimationStarted();
564  EXPECT_TRUE(test_api_->is_lock_cancellable());
565
566  // forward only half way through
567  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
568
569  // Release the button before the lock timer fires.
570  ReleasePowerButton();
571  ExpectPreLockAnimationCancel();
572
573  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDO_MOVE_WINDOWS,
574                   0.5f);
575
576  PressPowerButton();
577  ExpectPreLockAnimationStarted();
578  EXPECT_TRUE(test_api_->is_lock_cancellable());
579
580  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f);
581
582  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
583
584  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f);
585  ExpectPreLockAnimationFinished();
586  EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
587}
588
589// Hold the power button down from the unlocked state to eventual shutdown.
590TEST_F(LockStateControllerTest, LockToShutdown) {
591  Initialize(false, user::LOGGED_IN_USER);
592
593  // Hold the power button and lock the screen.
594  PressPowerButton();
595  EXPECT_TRUE(test_api_->is_animating_lock());
596
597  Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
598  SystemLocks();
599  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
600
601  // When the lock-to-shutdown timeout fires, we should start the shutdown
602  // timer.
603  EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
604
605  test_api_->trigger_lock_to_shutdown_timeout();
606
607  ExpectShutdownAnimationStarted();
608  EXPECT_TRUE(test_api_->shutdown_timer_is_running());
609
610  // Fire the shutdown timeout and check that we request shutdown.
611  Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
612  ExpectShutdownAnimationFinished();
613  test_api_->trigger_shutdown_timeout();
614
615  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
616  EXPECT_EQ(0, NumShutdownRequests());
617  test_api_->trigger_real_shutdown_timeout();
618  EXPECT_EQ(1, NumShutdownRequests());
619}
620
621// Hold the power button down from the unlocked state to eventual shutdown,
622// then release the button while system does locking.
623TEST_F(LockStateControllerTest, CancelLockToShutdown) {
624  Initialize(false, user::LOGGED_IN_USER);
625
626  PressPowerButton();
627
628  // Hold the power button and lock the screen.
629  EXPECT_TRUE(test_api_->is_animating_lock());
630
631  Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
632  SystemLocks();
633  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
634
635  // Power button is released while system attempts to lock.
636  ReleasePowerButton();
637
638  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
639
640  EXPECT_FALSE(lock_state_controller_->ShutdownRequested());
641  EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
642  EXPECT_FALSE(test_api_->shutdown_timer_is_running());
643}
644
645// TODO(bruthig): Investigate why this hangs on Windows 8 and whether it can be
646// safely enabled on OS_WIN.
647#ifndef OS_WIN
648// Test that we handle the case where lock requests are ignored.
649TEST_F(LockStateControllerTest, Lock) {
650  Initialize(false, user::LOGGED_IN_USER);
651
652  // Hold the power button and lock the screen.
653  PressPowerButton();
654  ExpectPreLockAnimationStarted();
655
656  Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
657
658  EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
659  EXPECT_TRUE(test_api_->lock_fail_timer_is_running());
660  // We shouldn't start the lock-to-shutdown timer until the screen has actually
661  // been locked and this was animated.
662  EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
663
664  // Act as if the request timed out.
665  EXPECT_DEATH(test_api_->trigger_lock_fail_timeout(), "");
666}
667#endif
668
669// Test the basic operation of the lock button (not logged in).
670TEST_F(LockStateControllerTest, LockButtonBasicNotLoggedIn) {
671  // The lock button shouldn't do anything if we aren't logged in.
672  Initialize(false, user::LOGGED_IN_NONE);
673
674  PressLockButton();
675  EXPECT_FALSE(test_api_->is_animating_lock());
676  ReleaseLockButton();
677  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
678}
679
680// Test the basic operation of the lock button (guest).
681TEST_F(LockStateControllerTest, LockButtonBasicGuest) {
682  // The lock button shouldn't do anything when we're logged in as a guest.
683  Initialize(false, user::LOGGED_IN_GUEST);
684
685  PressLockButton();
686  EXPECT_FALSE(test_api_->is_animating_lock());
687  ReleaseLockButton();
688  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
689}
690
691// Test the basic operation of the lock button.
692TEST_F(LockStateControllerTest, LockButtonBasic) {
693  // If we're logged in as a regular user, we should start the lock timer and
694  // the pre-lock animation.
695  Initialize(false, user::LOGGED_IN_USER);
696
697  PressLockButton();
698  ExpectPreLockAnimationStarted();
699  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
700
701  // If the button is released immediately, we shouldn't lock the screen.
702  ReleaseLockButton();
703  ExpectPreLockAnimationCancel();
704  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
705
706  ExpectUnlockedState();
707  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
708
709  // Press the button again and let the lock timeout fire.  We should request
710  // that the screen be locked.
711  PressLockButton();
712  ExpectPreLockAnimationStarted();
713  Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
714  EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
715
716  // Pressing the lock button while we have a pending lock request shouldn't do
717  // anything.
718  ReleaseLockButton();
719  PressLockButton();
720  ExpectPreLockAnimationFinished();
721  ReleaseLockButton();
722
723  // Pressing the button also shouldn't do anything after the screen is locked.
724  SystemLocks();
725  ExpectPostLockAnimationStarted();
726
727  PressLockButton();
728  ReleaseLockButton();
729  ExpectPostLockAnimationStarted();
730
731  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
732  ExpectPostLockAnimationFinished();
733
734  PressLockButton();
735  ReleaseLockButton();
736  ExpectPostLockAnimationFinished();
737}
738
739// Test that the power button takes priority over the lock button.
740TEST_F(LockStateControllerTest, PowerButtonPreemptsLockButton) {
741  Initialize(false, user::LOGGED_IN_USER);
742
743  // While the lock button is down, hold the power button.
744  PressLockButton();
745  ExpectPreLockAnimationStarted();
746
747  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
748  ExpectPreLockAnimationRunning();
749
750  PressPowerButton();
751  ExpectPreLockAnimationRunning();
752
753  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
754  ExpectPreLockAnimationRunning();
755
756  // The lock timer shouldn't be stopped when the lock button is released.
757  ReleaseLockButton();
758  ExpectPreLockAnimationRunning();
759
760  ReleasePowerButton();
761  ExpectPreLockAnimationCancel();
762
763  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
764  ExpectUnlockedState();
765
766  // Now press the power button first and then the lock button.
767  PressPowerButton();
768  ExpectPreLockAnimationStarted();
769
770  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
771
772  PressLockButton();
773  ExpectPreLockAnimationRunning();
774
775  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
776
777  // Releasing the power button should stop the lock timer.
778  ReleasePowerButton();
779  ExpectPreLockAnimationCancel();
780
781  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
782
783  ReleaseLockButton();
784  ExpectPreLockAnimationCancel();
785}
786
787// When the screen is locked without going through the usual power-button
788// slow-close path (e.g. via the wrench menu), test that we still show the
789// fast-close animation.
790TEST_F(LockStateControllerTest, LockWithoutButton) {
791  Initialize(false, user::LOGGED_IN_USER);
792  lock_state_controller_->OnStartingLock();
793
794  ExpectPreLockAnimationStarted();
795  EXPECT_FALSE(test_api_->is_lock_cancellable());
796  EXPECT_LT(0u, test_animator_->GetAnimationCount());
797
798  test_animator_->CompleteAllAnimations(true);
799  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
800}
801
802// When we hear that the process is exiting but we haven't had a chance to
803// display an animation, we should just blank the screen.
804TEST_F(LockStateControllerTest, ShutdownWithoutButton) {
805  Initialize(false, user::LOGGED_IN_USER);
806  lock_state_controller_->OnAppTerminating();
807
808  EXPECT_TRUE(
809      test_animator_->AreContainersAnimated(
810          SessionStateAnimator::kAllNonRootContainersMask,
811          SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
812  GenerateMouseMoveEvent();
813  EXPECT_FALSE(cursor_visible());
814}
815
816// Test that we display the fast-close animation and shut down when we get an
817// outside request to shut down (e.g. from the login or lock screen).
818TEST_F(LockStateControllerTest, RequestShutdownFromLoginScreen) {
819  Initialize(false, user::LOGGED_IN_NONE);
820
821  lock_state_controller_->RequestShutdown();
822
823  ExpectShutdownAnimationStarted();
824  Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
825
826  GenerateMouseMoveEvent();
827  EXPECT_FALSE(cursor_visible());
828
829  EXPECT_EQ(0, NumShutdownRequests());
830  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
831  test_api_->trigger_real_shutdown_timeout();
832  EXPECT_EQ(1, NumShutdownRequests());
833}
834
835TEST_F(LockStateControllerTest, RequestShutdownFromLockScreen) {
836  Initialize(false, user::LOGGED_IN_USER);
837
838  SystemLocks();
839
840  Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
841  ExpectPostLockAnimationFinished();
842
843  lock_state_controller_->RequestShutdown();
844
845  ExpectShutdownAnimationStarted();
846  Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
847
848  GenerateMouseMoveEvent();
849  EXPECT_FALSE(cursor_visible());
850
851  EXPECT_EQ(0, NumShutdownRequests());
852  EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
853  test_api_->trigger_real_shutdown_timeout();
854  EXPECT_EQ(1, NumShutdownRequests());
855}
856
857TEST_F(LockStateControllerTest, RequestAndCancelShutdownFromLockScreen) {
858  Initialize(false, user::LOGGED_IN_USER);
859
860  SystemLocks();
861  Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
862  ExpectLockedState();
863
864  // Press the power button and check that we start the shutdown timer.
865  PressPowerButton();
866  EXPECT_FALSE(test_api_->is_animating_lock());
867  EXPECT_TRUE(test_api_->shutdown_timer_is_running());
868
869  ExpectShutdownAnimationStarted();
870
871  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
872
873  // Release the power button before the shutdown timer fires.
874  ReleasePowerButton();
875
876  EXPECT_FALSE(test_api_->shutdown_timer_is_running());
877
878  ExpectShutdownAnimationCancel();
879
880  Advance(SessionStateAnimator::ANIMATION_SPEED_REVERT_SHUTDOWN);
881  ExpectLockedState();
882}
883
884// Test that we ignore power button presses when the screen is turned off.
885TEST_F(LockStateControllerTest, IgnorePowerButtonIfScreenIsOff) {
886  Initialize(false, user::LOGGED_IN_USER);
887
888  // When the screen brightness is at 0%, we shouldn't do anything in response
889  // to power button presses.
890  power_button_controller_->OnScreenBrightnessChanged(0.0);
891  PressPowerButton();
892  EXPECT_FALSE(test_api_->is_animating_lock());
893  ReleasePowerButton();
894
895  // After increasing the brightness to 10%, we should start the timer like
896  // usual.
897  power_button_controller_->OnScreenBrightnessChanged(10.0);
898  PressPowerButton();
899  EXPECT_TRUE(test_api_->is_animating_lock());
900  ReleasePowerButton();
901}
902
903#if defined(OS_CHROMEOS)
904TEST_F(LockStateControllerTest, HonorPowerButtonInDockedMode) {
905  ScopedVector<const ui::DisplayMode> modes;
906  modes.push_back(new ui::DisplayMode(gfx::Size(1, 1), false, 60.0f));
907
908  // Create two outputs, the first internal and the second external.
909  ui::DisplayConfigurator::DisplayStateList outputs;
910  ui::DisplayConfigurator::DisplayState internal_output;
911  ui::TestDisplaySnapshot internal_display;
912  internal_display.set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL);
913  internal_display.set_modes(modes.get());
914  internal_output.display = &internal_display;
915  outputs.push_back(internal_output);
916
917  ui::DisplayConfigurator::DisplayState external_output;
918  ui::TestDisplaySnapshot external_display;
919  external_display.set_type(ui::DISPLAY_CONNECTION_TYPE_HDMI);
920  external_display.set_modes(modes.get());
921  external_output.display = &external_display;
922  outputs.push_back(external_output);
923
924  // When all of the displays are turned off (e.g. due to user inactivity), the
925  // power button should be ignored.
926  power_button_controller_->OnScreenBrightnessChanged(0.0);
927  static_cast<ui::TestDisplaySnapshot*>(outputs[0].display)
928      ->set_current_mode(NULL);
929  static_cast<ui::TestDisplaySnapshot*>(outputs[1].display)
930      ->set_current_mode(NULL);
931  power_button_controller_->OnDisplayModeChanged(outputs);
932  PressPowerButton();
933  EXPECT_FALSE(test_api_->is_animating_lock());
934  ReleasePowerButton();
935
936  // When the screen brightness is 0% but the external display is still turned
937  // on (indicating either docked mode or the user having manually decreased the
938  // brightness to 0%), the power button should still be handled.
939  static_cast<ui::TestDisplaySnapshot*>(outputs[1].display)
940      ->set_current_mode(modes[0]);
941  power_button_controller_->OnDisplayModeChanged(outputs);
942  PressPowerButton();
943  EXPECT_TRUE(test_api_->is_animating_lock());
944  ReleasePowerButton();
945}
946#endif
947
948// Test that hidden background appears and revers correctly on lock/cancel.
949TEST_F(LockStateControllerTest, TestHiddenBackgroundLockCancel) {
950  Initialize(false, user::LOGGED_IN_USER);
951  HideBackground();
952
953  ExpectUnlockedState();
954  PressPowerButton();
955
956  ExpectPreLockAnimationStarted();
957  ExpectBackgroundIsShowing();
958
959  // Forward only half way through.
960  AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
961
962  // Release the button before the lock timer fires.
963  ReleasePowerButton();
964  ExpectPreLockAnimationCancel();
965  ExpectBackgroundIsHiding();
966
967  Advance(SessionStateAnimator::ANIMATION_SPEED_UNDO_MOVE_WINDOWS);
968
969  // When the CancelPrelockAnimation sequence finishes it queues up a
970  // restore background visibilty sequence when the background is hidden.
971  ExpectRestoringBackgroundVisibility();
972
973  Advance(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE);
974
975  ExpectUnlockedState();
976}
977
978// Test that hidden background appears and revers correctly on lock/unlock.
979TEST_F(LockStateControllerTest, TestHiddenBackgroundLockUnlock) {
980  Initialize(false, user::LOGGED_IN_USER);
981  HideBackground();
982
983  ExpectUnlockedState();
984
985  // Press the power button and check that the lock timer is started and that we
986  // start lifting the non-screen-locker containers.
987  PressPowerButton();
988
989  ExpectPreLockAnimationStarted();
990  ExpectBackgroundIsShowing();
991
992  Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
993
994  ExpectPreLockAnimationFinished();
995
996  SystemLocks();
997
998  ReleasePowerButton();
999
1000  ExpectPostLockAnimationStarted();
1001  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1002  ExpectPostLockAnimationFinished();
1003
1004  ExpectLockedState();
1005
1006  SuccessfulAuthentication(NULL);
1007
1008  ExpectUnlockBeforeUIDestroyedAnimationStarted();
1009  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1010  ExpectUnlockBeforeUIDestroyedAnimationFinished();
1011
1012  SystemUnlocks();
1013
1014  ExpectUnlockAfterUIDestroyedAnimationStarted();
1015  ExpectBackgroundIsHiding();
1016
1017  Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1018
1019  // When the StartUnlockAnimationAfterUIDestroyed sequence finishes it queues
1020  // up a restore background visibilty sequence when the background is hidden.
1021  ExpectRestoringBackgroundVisibility();
1022
1023  Advance(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE);
1024
1025  ExpectUnlockAfterUIDestroyedAnimationFinished();
1026
1027  ExpectUnlockedState();
1028}
1029
1030TEST_F(LockStateControllerTest, Screenshot) {
1031  test::TestScreenshotDelegate* delegate = GetScreenshotDelegate();
1032  delegate->set_can_take_screenshot(true);
1033
1034  EnableMaximizeMode(false);
1035
1036  // Screenshot handling should not be active when not in maximize mode.
1037  ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1038  PressVolumeDown();
1039  PressPowerButton();
1040  ReleasePowerButton();
1041  ReleaseVolumeDown();
1042  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1043
1044  EnableMaximizeMode(true);
1045
1046  // Pressing power alone does not take a screenshot.
1047  PressPowerButton();
1048  ReleasePowerButton();
1049  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1050
1051  // Press & release volume then pressing power does not take a screenshot.
1052  ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1053  PressVolumeDown();
1054  ReleaseVolumeDown();
1055  PressPowerButton();
1056  ReleasePowerButton();
1057  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1058
1059  // Pressing power and then volume does not take a screenshot.
1060  ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1061  PressPowerButton();
1062  ReleasePowerButton();
1063  PressVolumeDown();
1064  ReleaseVolumeDown();
1065  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1066
1067  // Holding volume down and pressing power takes a screenshot.
1068  ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1069  PressVolumeDown();
1070  PressPowerButton();
1071  ReleasePowerButton();
1072  ReleaseVolumeDown();
1073  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
1074}
1075
1076// Tests that a lock action is cancellable when quick lock is turned on and
1077// maximize mode is not active.
1078TEST_F(LockStateControllerTest, QuickLockWhileNotInMaximizeMode) {
1079  Initialize(false, user::LOGGED_IN_USER);
1080  power_button_controller_->set_enable_quick_lock_for_test(true);
1081  EnableMaximizeMode(false);
1082
1083  PressPowerButton();
1084
1085  ExpectPreLockAnimationStarted();
1086  EXPECT_TRUE(test_api_->is_animating_lock());
1087  EXPECT_TRUE(lock_state_controller_->CanCancelLockAnimation());
1088
1089  ReleasePowerButton();
1090
1091  EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
1092}
1093
1094// Tests that a lock action is not cancellable when quick lock is turned on and
1095// maximize mode is active.
1096TEST_F(LockStateControllerTest, QuickLockWhileInMaximizeMode) {
1097  Initialize(false, user::LOGGED_IN_USER);
1098  power_button_controller_->set_enable_quick_lock_for_test(true);
1099  EnableMaximizeMode(true);
1100
1101  PressPowerButton();
1102
1103  ExpectPreLockAnimationStarted();
1104  EXPECT_TRUE(test_api_->is_animating_lock());
1105  EXPECT_FALSE(lock_state_controller_->CanCancelLockAnimation());
1106
1107  ReleasePowerButton();
1108
1109  ExpectPreLockAnimationStarted();
1110
1111  test_animator_->CompleteAllAnimations(true);
1112  EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
1113}
1114
1115}  // namespace test
1116}  // namespace ash
1117