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/auth/key.h"
     13 #include "chrome/browser/chromeos/login/auth/mock_authenticator.h"
     14 #include "chrome/browser/chromeos/login/auth/user_context.h"
     15 #include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
     16 #include "chrome/browser/chromeos/login/users/user_manager.h"
     17 #include "chrome/browser/profiles/profile_manager.h"
     18 #include "chrome/browser/ui/browser.h"
     19 #include "chrome/browser/ui/browser_window.h"
     20 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
     21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     22 #include "chrome/common/chrome_switches.h"
     23 #include "chrome/test/base/in_process_browser_test.h"
     24 #include "chrome/test/base/ui_test_utils.h"
     25 #include "chromeos/chromeos_switches.h"
     26 #include "chromeos/dbus/fake_dbus_thread_manager.h"
     27 #include "chromeos/dbus/fake_session_manager_client.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     FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
    124     fake_dbus_thread_manager->SetFakeClients();
    125     fake_session_manager_client_ = new FakeSessionManagerClient;
    126     fake_dbus_thread_manager->SetSessionManagerClient(
    127         scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
    128     DBusThreadManager::SetInstanceForTesting(fake_dbus_thread_manager);
    129 
    130     InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
    131     zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
    132         ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
    133   }
    134 
    135   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    136     command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
    137   }
    138 
    139   scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
    140 
    141   DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
    142 };
    143 
    144 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) {
    145   ScreenLocker::Show();
    146   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    147   tester->EmulateWindowManagerReady();
    148   content::WindowedNotificationObserver lock_state_observer(
    149       chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
    150       content::NotificationService::AllSources());
    151   if (!tester->IsLocked())
    152     lock_state_observer.Wait();
    153 
    154   // Test to make sure that the widget is actually appearing and is of
    155   // reasonable size, preventing a regression of
    156   // http://code.google.com/p/chromium-os/issues/detail?id=5987
    157   gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowBoundsInScreen();
    158   EXPECT_GT(lock_bounds.width(), 10);
    159   EXPECT_GT(lock_bounds.height(), 10);
    160 
    161   UserContext user_context(UserManager::kStubUser);
    162   user_context.SetKey(Key("pass"));
    163   tester->InjectMockAuthenticator(user_context);
    164   EXPECT_TRUE(tester->IsLocked());
    165   tester->EnterPassword("fail");
    166   content::RunAllPendingInMessageLoop();
    167   EXPECT_TRUE(tester->IsLocked());
    168   tester->EnterPassword("pass");
    169   content::RunAllPendingInMessageLoop();
    170   // Successful authentication clears the lock screen and tells the
    171   // SessionManager to announce this over DBus.
    172   EXPECT_FALSE(tester->IsLocked());
    173   EXPECT_EQ(
    174       1,
    175       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    176 
    177   EXPECT_TRUE(VerifyLockScreenDismissed());
    178 }
    179 
    180 // Test how locking the screen affects an active fullscreen window.
    181 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
    182   // 1) If the active browser window is in fullscreen and the fullscreen window
    183   // does not have all the pixels (e.g. the shelf is auto hidden instead of
    184   // hidden), locking the screen should not exit fullscreen. The shelf is
    185   // auto hidden when in immersive fullscreen.
    186   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    187   BrowserWindow* browser_window = browser()->window();
    188   ash::wm::WindowState* window_state = ash::wm::GetWindowState(
    189       browser_window->GetNativeWindow());
    190   {
    191     Waiter waiter(browser());
    192     browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
    193     waiter.Wait(false /* not locked */, true /* full screen */);
    194     EXPECT_TRUE(browser_window->IsFullscreen());
    195     EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
    196     EXPECT_FALSE(tester->IsLocked());
    197   }
    198   {
    199     Waiter waiter(browser());
    200     ScreenLocker::Show();
    201     tester->EmulateWindowManagerReady();
    202     waiter.Wait(true /* locked */, true /* full screen */);
    203     EXPECT_TRUE(browser_window->IsFullscreen());
    204     EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
    205     EXPECT_TRUE(tester->IsLocked());
    206   }
    207   UserContext user_context(UserManager::kStubUser);
    208   user_context.SetKey(Key("pass"));
    209   tester->InjectMockAuthenticator(user_context);
    210   tester->EnterPassword("pass");
    211   content::RunAllPendingInMessageLoop();
    212   EXPECT_FALSE(tester->IsLocked());
    213   {
    214     Waiter waiter(browser());
    215     browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
    216     waiter.Wait(false /* not locked */, false /* fullscreen */);
    217     EXPECT_FALSE(browser_window->IsFullscreen());
    218   }
    219 
    220   // 2) If the active browser window is in fullscreen and the fullscreen window
    221   // has all of the pixels, locking the screen should exit fullscreen. The
    222   // fullscreen window has all of the pixels when in tab fullscreen.
    223   {
    224     Waiter waiter(browser());
    225     content::WebContents* web_contents =
    226         browser()->tab_strip_model()->GetActiveWebContents();
    227     browser()->fullscreen_controller()->ToggleFullscreenModeForTab(
    228         web_contents, true);
    229     waiter.Wait(false /* not locked */, true /* fullscreen */);
    230     EXPECT_TRUE(browser_window->IsFullscreen());
    231     EXPECT_TRUE(window_state->hide_shelf_when_fullscreen());
    232     EXPECT_FALSE(tester->IsLocked());
    233   }
    234   {
    235     Waiter waiter(browser());
    236     ScreenLocker::Show();
    237     tester->EmulateWindowManagerReady();
    238     waiter.Wait(true /* locked */, false /* full screen */);
    239     EXPECT_FALSE(browser_window->IsFullscreen());
    240     EXPECT_TRUE(tester->IsLocked());
    241   }
    242 
    243   tester->EnterPassword("pass");
    244   content::RunAllPendingInMessageLoop();
    245   EXPECT_FALSE(tester->IsLocked());
    246 
    247   EXPECT_EQ(
    248       2,
    249       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    250   EXPECT_EQ(
    251       2,
    252       fake_session_manager_client_->notify_lock_screen_dismissed_call_count());
    253 }
    254 
    255 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) {
    256   ui_controls::SendKeyPress(widget->GetNativeWindow(),
    257                             key_code, false, false, false, false);
    258 }
    259 
    260 void UnlockKeyPress(views::Widget* widget) {
    261   SimulateKeyPress(widget, ui::VKEY_SPACE);
    262 }
    263 
    264 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
    265   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    266   LockScreen(tester.get());
    267 
    268   // Calling Show again simply send LockCompleted signal.
    269   ScreenLocker::Show();
    270   EXPECT_TRUE(tester->IsLocked());
    271   EXPECT_EQ(
    272       2,
    273       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    274 
    275 
    276   // Close the locker to match expectations.
    277   ScreenLocker::Hide();
    278   content::RunAllPendingInMessageLoop();
    279   EXPECT_FALSE(tester->IsLocked());
    280   EXPECT_TRUE(VerifyLockScreenDismissed());
    281 }
    282 
    283 // TODO(flackr): Find out why the RenderView isn't getting the escape press
    284 // and re-enable this test (currently this test is flaky).
    285 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) {
    286   scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
    287   LockScreen(tester.get());
    288 
    289   EXPECT_EQ(
    290       1,
    291       fake_session_manager_client_->notify_lock_screen_shown_call_count());
    292 
    293   tester->SetPassword("password");
    294   EXPECT_EQ("password", tester->GetPassword());
    295   // Escape clears the password.
    296   SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE);
    297   content::RunAllPendingInMessageLoop();
    298   EXPECT_EQ("", tester->GetPassword());
    299 
    300   // Close the locker to match expectations.
    301   ScreenLocker::Hide();
    302   content::RunAllPendingInMessageLoop();
    303   EXPECT_FALSE(tester->IsLocked());
    304   EXPECT_TRUE(VerifyLockScreenDismissed());
    305 }
    306 
    307 }  // namespace chromeos
    308