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