Home | History | Annotate | Download | only in lock
      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 "chrome/browser/chromeos/login/lock/screen_locker.h"
      6 
      7 #include "ash/wm/window_state.h"
      8 #include "base/command_line.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "chrome/browser/chrome_notification_types.h"
     12 #include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
     13 #include "chrome/browser/profiles/profile_manager.h"
     14 #include "chrome/browser/ui/browser.h"
     15 #include "chrome/browser/ui/browser_window.h"
     16 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
     17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     18 #include "chrome/common/chrome_switches.h"
     19 #include "chrome/test/base/in_process_browser_test.h"
     20 #include "chrome/test/base/ui_test_utils.h"
     21 #include "chromeos/chromeos_switches.h"
     22 #include "chromeos/dbus/dbus_thread_manager.h"
     23 #include "chromeos/dbus/fake_session_manager_client.h"
     24 #include "chromeos/login/auth/key.h"
     25 #include "chromeos/login/auth/mock_authenticator.h"
     26 #include "chromeos/login/auth/user_context.h"
     27 #include "chromeos/login/user_names.h"
     28 #include "content/public/browser/notification_service.h"
     29 #include "testing/gmock/include/gmock/gmock.h"
     30 #include "testing/gtest/include/gtest/gtest.h"
     31 #include "ui/base/test/ui_controls.h"
     32 #include "ui/compositor/layer_animator.h"
     33 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
     34 #include "ui/views/widget/widget.h"
     35 
     36 using testing::_;
     37 using testing::AnyNumber;
     38 using testing::Return;
     39 
     40 namespace {
     41 
     42 // An object that wait for lock state and fullscreen state.
     43 class Waiter : public content::NotificationObserver {
     44  public:
     45   explicit Waiter(Browser* browser)
     46       : browser_(browser),
     47         running_(false) {
     48     registrar_.Add(this,
     49                    chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
     50                    content::NotificationService::AllSources());
     51     registrar_.Add(this,
     52                    chrome::NOTIFICATION_FULLSCREEN_CHANGED,
     53                    content::NotificationService::AllSources());
     54   }
     55 
     56   virtual ~Waiter() {
     57   }
     58 
     59   virtual void Observe(int type,
     60                        const content::NotificationSource& source,
     61                        const content::NotificationDetails& details) OVERRIDE {
     62     DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED ||
     63            type == chrome::NOTIFICATION_FULLSCREEN_CHANGED);
     64     if (running_)
     65       base::MessageLoop::current()->Quit();
     66   }
     67 
     68   // Wait until the two conditions are met.
     69   void Wait(bool locker_state, bool fullscreen) {
     70     running_ = true;
     71     scoped_ptr<chromeos::test::ScreenLockerTester>
     72         tester(chromeos::ScreenLocker::GetTester());
     73     while (tester->IsLocked() != locker_state ||
     74            browser_->window()->IsFullscreen() != fullscreen) {
     75       content::RunMessageLoop();
     76     }
     77     // Make sure all pending tasks are executed.
     78     content::RunAllPendingInMessageLoop();
     79     running_ = false;
     80   }
     81 
     82  private:
     83   Browser* browser_;
     84   content::NotificationRegistrar registrar_;
     85 
     86   // Are we currently running the message loop?
     87   bool running_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(Waiter);
     90 };
     91 
     92 }  // namespace
     93 
     94 namespace chromeos {
     95 
     96 class ScreenLockerTest : public InProcessBrowserTest {
     97  public:
     98   ScreenLockerTest() : fake_session_manager_client_(NULL) {
     99   }
    100 
    101  protected:
    102   FakeSessionManagerClient* fake_session_manager_client_;
    103 
    104   void LockScreen(test::ScreenLockerTester* tester) {
    105     ScreenLocker::Show();
    106     tester->EmulateWindowManagerReady();
    107     content::WindowedNotificationObserver lock_state_observer(
    108         chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
    109         content::NotificationService::AllSources());
    110     if (!tester->IsLocked())
    111       lock_state_observer.Wait();
    112     EXPECT_TRUE(tester->IsLocked());
    113   }
    114 
    115   // Verifies if LockScreenDismissed() was called once.
    116   bool VerifyLockScreenDismissed() {
    117     return 1 == fake_session_manager_client_->
    118                     notify_lock_screen_dismissed_call_count();
    119   }
    120 
    121  private:
    122   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
    123     fake_session_manager_client_ = new FakeSessionManagerClient;
    124     DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
    125         scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
    126 
    127     InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
    128     zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
    129         ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
    130   }
    131 
    132   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    133     command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
    134   }
    135 
    136   scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
    139 };
    140 
    141 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) {
    142   ScreenLocker::Show();
    143   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    144   tester->EmulateWindowManagerReady();
    145   content::WindowedNotificationObserver lock_state_observer(
    146       chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
    147       content::NotificationService::AllSources());
    148   if (!tester->IsLocked())
    149     lock_state_observer.Wait();
    150 
    151   // Test to make sure that the widget is actually appearing and is of
    152   // reasonable size, preventing a regression of
    153   // http://code.google.com/p/chromium-os/issues/detail?id=5987
    154   gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowBoundsInScreen();
    155   EXPECT_GT(lock_bounds.width(), 10);
    156   EXPECT_GT(lock_bounds.height(), 10);
    157 
    158   UserContext user_context(chromeos::login::kStubUser);
    159   user_context.SetKey(Key("pass"));
    160   tester->InjectMockAuthenticator(user_context);
    161   EXPECT_TRUE(tester->IsLocked());
    162   tester->EnterPassword("fail");
    163   content::RunAllPendingInMessageLoop();
    164   EXPECT_TRUE(tester->IsLocked());
    165   tester->EnterPassword("pass");
    166   content::RunAllPendingInMessageLoop();
    167   // Successful authentication clears the lock screen and tells the
    168   // SessionManager to announce this over DBus.
    169   EXPECT_FALSE(tester->IsLocked());
    170   EXPECT_EQ(
    171       1,
    172       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    173 
    174   EXPECT_TRUE(VerifyLockScreenDismissed());
    175 }
    176 
    177 // Test how locking the screen affects an active fullscreen window.
    178 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
    179   // 1) If the active browser window is in fullscreen and the fullscreen window
    180   // does not have all the pixels (e.g. the shelf is auto hidden instead of
    181   // hidden), locking the screen should not exit fullscreen. The shelf is
    182   // auto hidden when in immersive fullscreen.
    183   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    184   BrowserWindow* browser_window = browser()->window();
    185   ash::wm::WindowState* window_state = ash::wm::GetWindowState(
    186       browser_window->GetNativeWindow());
    187   {
    188     Waiter waiter(browser());
    189     browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
    190     waiter.Wait(false /* not locked */, true /* full screen */);
    191     EXPECT_TRUE(browser_window->IsFullscreen());
    192     EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
    193     EXPECT_FALSE(tester->IsLocked());
    194   }
    195   {
    196     Waiter waiter(browser());
    197     ScreenLocker::Show();
    198     tester->EmulateWindowManagerReady();
    199     waiter.Wait(true /* locked */, true /* full screen */);
    200     EXPECT_TRUE(browser_window->IsFullscreen());
    201     EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
    202     EXPECT_TRUE(tester->IsLocked());
    203   }
    204   UserContext user_context(chromeos::login::kStubUser);
    205   user_context.SetKey(Key("pass"));
    206   tester->InjectMockAuthenticator(user_context);
    207   tester->EnterPassword("pass");
    208   content::RunAllPendingInMessageLoop();
    209   EXPECT_FALSE(tester->IsLocked());
    210   {
    211     Waiter waiter(browser());
    212     browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
    213     waiter.Wait(false /* not locked */, false /* fullscreen */);
    214     EXPECT_FALSE(browser_window->IsFullscreen());
    215   }
    216 
    217   // 2) If the active browser window is in fullscreen and the fullscreen window
    218   // has all of the pixels, locking the screen should exit fullscreen. The
    219   // fullscreen window has all of the pixels when in tab fullscreen.
    220   {
    221     Waiter waiter(browser());
    222     content::WebContents* web_contents =
    223         browser()->tab_strip_model()->GetActiveWebContents();
    224     browser()->fullscreen_controller()->ToggleFullscreenModeForTab(
    225         web_contents, true);
    226     waiter.Wait(false /* not locked */, true /* fullscreen */);
    227     EXPECT_TRUE(browser_window->IsFullscreen());
    228     EXPECT_TRUE(window_state->hide_shelf_when_fullscreen());
    229     EXPECT_FALSE(tester->IsLocked());
    230   }
    231   {
    232     Waiter waiter(browser());
    233     ScreenLocker::Show();
    234     tester->EmulateWindowManagerReady();
    235     waiter.Wait(true /* locked */, false /* full screen */);
    236     EXPECT_FALSE(browser_window->IsFullscreen());
    237     EXPECT_TRUE(tester->IsLocked());
    238   }
    239 
    240   tester->EnterPassword("pass");
    241   content::RunAllPendingInMessageLoop();
    242   EXPECT_FALSE(tester->IsLocked());
    243 
    244   EXPECT_EQ(
    245       2,
    246       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    247   EXPECT_EQ(
    248       2,
    249       fake_session_manager_client_->notify_lock_screen_dismissed_call_count());
    250 }
    251 
    252 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) {
    253   ui_controls::SendKeyPress(widget->GetNativeWindow(),
    254                             key_code, false, false, false, false);
    255 }
    256 
    257 void UnlockKeyPress(views::Widget* widget) {
    258   SimulateKeyPress(widget, ui::VKEY_SPACE);
    259 }
    260 
    261 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
    262   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    263   LockScreen(tester.get());
    264 
    265   // Calling Show again simply send LockCompleted signal.
    266   ScreenLocker::Show();
    267   EXPECT_TRUE(tester->IsLocked());
    268   EXPECT_EQ(
    269       2,
    270       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    271 
    272 
    273   // Close the locker to match expectations.
    274   ScreenLocker::Hide();
    275   content::RunAllPendingInMessageLoop();
    276   EXPECT_FALSE(tester->IsLocked());
    277   EXPECT_TRUE(VerifyLockScreenDismissed());
    278 }
    279 
    280 // TODO(flackr): Find out why the RenderView isn't getting the escape press
    281 // and re-enable this test (currently this test is flaky).
    282 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) {
    283   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    284   LockScreen(tester.get());
    285 
    286   EXPECT_EQ(
    287       1,
    288       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    289 
    290   tester->SetPassword("password");
    291   EXPECT_EQ("password", tester->GetPassword());
    292   // Escape clears the password.
    293   SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE);
    294   content::RunAllPendingInMessageLoop();
    295   EXPECT_EQ("", tester->GetPassword());
    296 
    297   // Close the locker to match expectations.
    298   ScreenLocker::Hide();
    299   content::RunAllPendingInMessageLoop();
    300   EXPECT_FALSE(tester->IsLocked());
    301   EXPECT_TRUE(VerifyLockScreenDismissed());
    302 }
    303 
    304 }  // namespace chromeos
    305