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