ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / login / lock / screen_locker_browsertest.cc
blobd4c218cc08f11299872384e7306960b22ff6c49e
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.
5 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
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/exclusive_access/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 "chromeos/chromeos_switches.h"
21 #include "chromeos/dbus/dbus_thread_manager.h"
22 #include "chromeos/dbus/fake_session_manager_client.h"
23 #include "chromeos/login/auth/key.h"
24 #include "chromeos/login/auth/stub_authenticator.h"
25 #include "chromeos/login/auth/user_context.h"
26 #include "chromeos/login/user_names.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/test/test_utils.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"
36 using testing::_;
37 using testing::AnyNumber;
38 using testing::Return;
40 namespace {
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());
56 ~Waiter() override {}
58 void Observe(int type,
59 const content::NotificationSource& source,
60 const content::NotificationDetails& details) override {
61 DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED ||
62 type == chrome::NOTIFICATION_FULLSCREEN_CHANGED);
63 if (running_)
64 base::MessageLoop::current()->Quit();
67 // Wait until the two conditions are met.
68 void Wait(bool locker_state, bool fullscreen) {
69 running_ = true;
70 scoped_ptr<chromeos::test::ScreenLockerTester>
71 tester(chromeos::ScreenLocker::GetTester());
72 while (tester->IsLocked() != locker_state ||
73 browser_->window()->IsFullscreen() != fullscreen) {
74 content::RunMessageLoop();
76 // Make sure all pending tasks are executed.
77 content::RunAllPendingInMessageLoop();
78 running_ = false;
81 private:
82 Browser* browser_;
83 content::NotificationRegistrar registrar_;
85 // Are we currently running the message loop?
86 bool running_;
88 DISALLOW_COPY_AND_ASSIGN(Waiter);
91 } // namespace
93 namespace chromeos {
95 class ScreenLockerTest : public InProcessBrowserTest {
96 public:
97 ScreenLockerTest() : fake_session_manager_client_(NULL) {
100 protected:
101 FakeSessionManagerClient* fake_session_manager_client_;
103 void LockScreen(test::ScreenLockerTester* tester) {
104 ScreenLocker::Show();
105 tester->EmulateWindowManagerReady();
106 content::WindowedNotificationObserver lock_state_observer(
107 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
108 content::NotificationService::AllSources());
109 if (!tester->IsLocked())
110 lock_state_observer.Wait();
111 EXPECT_TRUE(tester->IsLocked());
114 // Verifies if LockScreenDismissed() was called once.
115 bool VerifyLockScreenDismissed() {
116 return 1 == fake_session_manager_client_->
117 notify_lock_screen_dismissed_call_count();
120 private:
121 void SetUpInProcessBrowserTestFixture() override {
122 fake_session_manager_client_ = new FakeSessionManagerClient;
123 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
124 scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
126 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
127 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
128 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
131 void SetUpCommandLine(base::CommandLine* command_line) override {
132 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
135 scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
137 DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
140 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) {
141 ScreenLocker::Show();
142 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
143 tester->EmulateWindowManagerReady();
144 content::WindowedNotificationObserver lock_state_observer(
145 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
146 content::NotificationService::AllSources());
147 if (!tester->IsLocked())
148 lock_state_observer.Wait();
150 // Test to make sure that the widget is actually appearing and is of
151 // reasonable size, preventing a regression of
152 // http://code.google.com/p/chromium-os/issues/detail?id=5987
153 gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowBoundsInScreen();
154 EXPECT_GT(lock_bounds.width(), 10);
155 EXPECT_GT(lock_bounds.height(), 10);
157 UserContext user_context(chromeos::login::kStubUser);
158 user_context.SetKey(Key("pass"));
159 tester->InjectStubUserContext(user_context);
160 EXPECT_TRUE(tester->IsLocked());
161 tester->EnterPassword("fail");
162 content::RunAllPendingInMessageLoop();
163 EXPECT_TRUE(tester->IsLocked());
164 tester->EnterPassword("pass");
165 content::RunAllPendingInMessageLoop();
166 // Successful authentication clears the lock screen and tells the
167 // SessionManager to announce this over DBus.
168 EXPECT_FALSE(tester->IsLocked());
169 EXPECT_EQ(
171 fake_session_manager_client_->notify_lock_screen_shown_call_count());
173 EXPECT_TRUE(VerifyLockScreenDismissed());
176 // Test how locking the screen affects an active fullscreen window.
177 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
178 // 1) If the active browser window is in fullscreen and the fullscreen window
179 // does not have all the pixels (e.g. the shelf is auto hidden instead of
180 // hidden), locking the screen should not exit fullscreen. The shelf is
181 // auto hidden when in immersive fullscreen.
182 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
183 BrowserWindow* browser_window = browser()->window();
184 ash::wm::WindowState* window_state = ash::wm::GetWindowState(
185 browser_window->GetNativeWindow());
187 Waiter waiter(browser());
188 browser()
189 ->exclusive_access_manager()
190 ->fullscreen_controller()
191 ->ToggleBrowserFullscreenMode();
192 waiter.Wait(false /* not locked */, true /* full screen */);
193 EXPECT_TRUE(browser_window->IsFullscreen());
194 EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
195 EXPECT_FALSE(tester->IsLocked());
198 Waiter waiter(browser());
199 ScreenLocker::Show();
200 tester->EmulateWindowManagerReady();
201 waiter.Wait(true /* locked */, true /* full screen */);
202 EXPECT_TRUE(browser_window->IsFullscreen());
203 EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
204 EXPECT_TRUE(tester->IsLocked());
206 UserContext user_context(chromeos::login::kStubUser);
207 user_context.SetKey(Key("pass"));
208 tester->InjectStubUserContext(user_context);
209 tester->EnterPassword("pass");
210 content::RunAllPendingInMessageLoop();
211 EXPECT_FALSE(tester->IsLocked());
213 Waiter waiter(browser());
214 browser()
215 ->exclusive_access_manager()
216 ->fullscreen_controller()
217 ->ToggleBrowserFullscreenMode();
218 waiter.Wait(false /* not locked */, false /* fullscreen */);
219 EXPECT_FALSE(browser_window->IsFullscreen());
222 // 2) If the active browser window is in fullscreen and the fullscreen window
223 // has all of the pixels, locking the screen should exit fullscreen. The
224 // fullscreen window has all of the pixels when in tab fullscreen.
226 Waiter waiter(browser());
227 content::WebContents* web_contents =
228 browser()->tab_strip_model()->GetActiveWebContents();
229 browser()
230 ->exclusive_access_manager()
231 ->fullscreen_controller()
232 ->EnterFullscreenModeForTab(web_contents, GURL());
233 waiter.Wait(false /* not locked */, true /* fullscreen */);
234 EXPECT_TRUE(browser_window->IsFullscreen());
235 EXPECT_TRUE(window_state->hide_shelf_when_fullscreen());
236 EXPECT_FALSE(tester->IsLocked());
239 Waiter waiter(browser());
240 ScreenLocker::Show();
241 tester->EmulateWindowManagerReady();
242 waiter.Wait(true /* locked */, false /* full screen */);
243 EXPECT_FALSE(browser_window->IsFullscreen());
244 EXPECT_TRUE(tester->IsLocked());
247 tester->EnterPassword("pass");
248 content::RunAllPendingInMessageLoop();
249 EXPECT_FALSE(tester->IsLocked());
251 EXPECT_EQ(
253 fake_session_manager_client_->notify_lock_screen_shown_call_count());
254 EXPECT_EQ(
256 fake_session_manager_client_->notify_lock_screen_dismissed_call_count());
259 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) {
260 ui_controls::SendKeyPress(widget->GetNativeWindow(),
261 key_code, false, false, false, false);
264 void UnlockKeyPress(views::Widget* widget) {
265 SimulateKeyPress(widget, ui::VKEY_SPACE);
268 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
269 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
270 LockScreen(tester.get());
272 // Calling Show again simply send LockCompleted signal.
273 ScreenLocker::Show();
274 EXPECT_TRUE(tester->IsLocked());
275 EXPECT_EQ(
277 fake_session_manager_client_->notify_lock_screen_shown_call_count());
280 // Close the locker to match expectations.
281 ScreenLocker::Hide();
282 content::RunAllPendingInMessageLoop();
283 EXPECT_FALSE(tester->IsLocked());
284 EXPECT_TRUE(VerifyLockScreenDismissed());
287 // TODO(flackr): Find out why the RenderView isn't getting the escape press
288 // and re-enable this test (currently this test is flaky).
289 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) {
290 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
291 LockScreen(tester.get());
293 EXPECT_EQ(
295 fake_session_manager_client_->notify_lock_screen_shown_call_count());
297 tester->SetPassword("password");
298 EXPECT_EQ("password", tester->GetPassword());
299 // Escape clears the password.
300 SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE);
301 content::RunAllPendingInMessageLoop();
302 EXPECT_EQ("", tester->GetPassword());
304 // Close the locker to match expectations.
305 ScreenLocker::Hide();
306 content::RunAllPendingInMessageLoop();
307 EXPECT_FALSE(tester->IsLocked());
308 EXPECT_TRUE(VerifyLockScreenDismissed());
311 } // namespace chromeos