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