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.
5 #include "chrome/browser/chromeos/login/screen_locker_tester.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/login/login_status_consumer.h"
14 #include "chrome/browser/chromeos/login/mock_authenticator.h"
15 #include "chrome/browser/chromeos/login/screen_locker.h"
16 #include "chrome/browser/chromeos/login/webui_screen_locker.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_ui.h"
22 #include "content/public/test/test_utils.h"
23 #include "ui/views/controls/button/button.h"
24 #include "ui/views/controls/label.h"
25 #include "ui/views/controls/textfield/textfield.h"
26 #include "ui/views/widget/root_view.h"
28 using content::WebContents
;
32 // This class is used to observe state of the global ScreenLocker instance,
33 // which can go away as a result of a successful authentication. As such,
34 // it needs to directly reference the global ScreenLocker.
35 class LoginAttemptObserver
: public chromeos::LoginStatusConsumer
{
37 LoginAttemptObserver();
38 virtual ~LoginAttemptObserver();
40 void WaitForAttempt();
42 // Overridden from LoginStatusConsumer:
43 virtual void OnLoginFailure(const chromeos::LoginFailure
& error
) OVERRIDE
{
47 virtual void OnLoginSuccess(
48 const chromeos::UserContext
& credentials
) OVERRIDE
{
53 void LoginAttempted();
55 bool login_attempted_
;
58 DISALLOW_COPY_AND_ASSIGN(LoginAttemptObserver
);
61 LoginAttemptObserver::LoginAttemptObserver()
62 : chromeos::LoginStatusConsumer(),
63 login_attempted_(false),
65 chromeos::ScreenLocker::default_screen_locker()->SetLoginStatusConsumer(this);
68 LoginAttemptObserver::~LoginAttemptObserver() {
69 chromeos::ScreenLocker
* global_locker
=
70 chromeos::ScreenLocker::default_screen_locker();
72 global_locker
->SetLoginStatusConsumer(NULL
);
75 void LoginAttemptObserver::WaitForAttempt() {
76 if (!login_attempted_
) {
78 content::RunMessageLoop();
81 ASSERT_TRUE(login_attempted_
);
84 void LoginAttemptObserver::LoginAttempted() {
85 login_attempted_
= true;
87 base::MessageLoopForUI::current()->Quit();
90 } // anyonymous namespace
96 class WebUIScreenLockerTester
: public ScreenLockerTester
{
98 // ScreenLockerTester overrides:
99 virtual void SetPassword(const std::string
& password
) OVERRIDE
;
100 virtual std::string
GetPassword() OVERRIDE
;
101 virtual void EnterPassword(const std::string
& password
) OVERRIDE
;
102 virtual void EmulateWindowManagerReady() OVERRIDE
;
103 virtual views::Widget
* GetWidget() const OVERRIDE
;
104 virtual views::Widget
* GetChildWidget() const OVERRIDE
;
107 friend class chromeos::ScreenLocker
;
109 WebUIScreenLockerTester() {}
111 content::RenderViewHost
* RenderViewHost() const;
113 // Returns the ScreenLockerWebUI object.
114 WebUIScreenLocker
* webui_screen_locker() const;
116 // Returns the WebUI object from the screen locker.
117 content::WebUI
* webui() const;
119 DISALLOW_COPY_AND_ASSIGN(WebUIScreenLockerTester
);
122 void WebUIScreenLockerTester::SetPassword(const std::string
& password
) {
123 webui()->GetWebContents()->GetMainFrame()->ExecuteJavaScript(
124 base::ASCIIToUTF16(base::StringPrintf(
125 "$('pod-row').pods[0].passwordElement.value = '%s';",
129 std::string
WebUIScreenLockerTester::GetPassword() {
131 scoped_ptr
<base::Value
> v
= content::ExecuteScriptAndGetValue(
132 RenderViewHost()->GetMainFrame(),
133 "$('pod-row').pods[0].passwordElement.value;");
134 CHECK(v
->GetAsString(&result
));
138 void WebUIScreenLockerTester::EnterPassword(const std::string
& password
) {
140 SetPassword(password
);
142 // Verify password is set.
143 ASSERT_EQ(password
, GetPassword());
145 // Verify that "signin" button is hidden.
146 scoped_ptr
<base::Value
> v
= content::ExecuteScriptAndGetValue(
147 RenderViewHost()->GetMainFrame(),
148 "$('pod-row').pods[0].signinButtonElement.hidden;");
149 ASSERT_TRUE(v
->GetAsBoolean(&result
));
152 // Attempt to sign in.
153 LoginAttemptObserver login
;
154 v
= content::ExecuteScriptAndGetValue(
155 RenderViewHost()->GetMainFrame(),
156 "$('pod-row').pods[0].activate();");
157 ASSERT_TRUE(v
->GetAsBoolean(&result
));
160 // Wait for login attempt.
161 login
.WaitForAttempt();
164 void WebUIScreenLockerTester::EmulateWindowManagerReady() {
167 views::Widget
* WebUIScreenLockerTester::GetWidget() const {
168 return webui_screen_locker()->lock_window_
;
171 views::Widget
* WebUIScreenLockerTester::GetChildWidget() const {
172 return webui_screen_locker()->lock_window_
;
175 content::RenderViewHost
* WebUIScreenLockerTester::RenderViewHost() const {
176 return webui()->GetWebContents()->GetRenderViewHost();
179 WebUIScreenLocker
* WebUIScreenLockerTester::webui_screen_locker() const {
180 DCHECK(ScreenLocker::screen_locker_
);
181 return static_cast<WebUIScreenLocker
*>(
182 ScreenLocker::screen_locker_
->delegate_
.get());
185 content::WebUI
* WebUIScreenLockerTester::webui() const {
186 DCHECK(webui_screen_locker()->webui_ready_
);
187 content::WebUI
* webui
= webui_screen_locker()->GetWebUI();
192 ScreenLockerTester::ScreenLockerTester() {
195 ScreenLockerTester::~ScreenLockerTester() {
198 bool ScreenLockerTester::IsLocked() {
199 return ScreenLocker::screen_locker_
&&
200 ScreenLocker::screen_locker_
->locked_
;
203 void ScreenLockerTester::InjectMockAuthenticator(
204 const std::string
& user
, const std::string
& password
) {
205 DCHECK(ScreenLocker::screen_locker_
);
206 ScreenLocker::screen_locker_
->SetAuthenticator(
207 new MockAuthenticator(ScreenLocker::screen_locker_
, user
, password
));
212 test::ScreenLockerTester
* ScreenLocker::GetTester() {
213 return new test::WebUIScreenLockerTester();
216 } // namespace chromeos