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_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/lock/screen_locker.h"
14 #include "chrome/browser/chromeos/login/lock/webui_screen_locker.h"
15 #include "chromeos/login/auth/auth_status_consumer.h"
16 #include "chromeos/login/auth/fake_extended_authenticator.h"
17 #include "chromeos/login/auth/stub_authenticator.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 "testing/gtest/include/gtest/gtest.h"
24 #include "ui/views/controls/button/button.h"
25 #include "ui/views/controls/label.h"
26 #include "ui/views/controls/textfield/textfield.h"
27 #include "ui/views/widget/root_view.h"
29 using content::WebContents
;
33 // This class is used to observe state of the global ScreenLocker instance,
34 // which can go away as a result of a successful authentication. As such,
35 // it needs to directly reference the global ScreenLocker.
36 class LoginAttemptObserver
: public chromeos::AuthStatusConsumer
{
38 LoginAttemptObserver();
39 ~LoginAttemptObserver() override
;
41 void WaitForAttempt();
43 // Overridden from AuthStatusConsumer:
44 void OnAuthFailure(const chromeos::AuthFailure
& error
) override
{
48 void OnAuthSuccess(const chromeos::UserContext
& credentials
) override
{
53 void LoginAttempted();
55 bool login_attempted_
;
58 DISALLOW_COPY_AND_ASSIGN(LoginAttemptObserver
);
61 LoginAttemptObserver::LoginAttemptObserver()
62 : chromeos::AuthStatusConsumer(), login_attempted_(false), waiting_(false) {
63 chromeos::ScreenLocker::default_screen_locker()->SetLoginStatusConsumer(this);
66 LoginAttemptObserver::~LoginAttemptObserver() {
67 chromeos::ScreenLocker
* global_locker
=
68 chromeos::ScreenLocker::default_screen_locker();
70 global_locker
->SetLoginStatusConsumer(NULL
);
73 void LoginAttemptObserver::WaitForAttempt() {
74 if (!login_attempted_
) {
76 content::RunMessageLoop();
79 ASSERT_TRUE(login_attempted_
);
82 void LoginAttemptObserver::LoginAttempted() {
83 login_attempted_
= true;
85 base::MessageLoopForUI::current()->Quit();
88 } // anyonymous namespace
94 class WebUIScreenLockerTester
: public ScreenLockerTester
{
96 // ScreenLockerTester overrides:
97 void SetPassword(const std::string
& password
) override
;
98 std::string
GetPassword() override
;
99 void EnterPassword(const std::string
& password
) override
;
100 void EmulateWindowManagerReady() override
;
101 views::Widget
* GetWidget() const override
;
102 views::Widget
* GetChildWidget() const override
;
105 friend class chromeos::ScreenLocker
;
107 WebUIScreenLockerTester() {}
109 content::RenderViewHost
* RenderViewHost() const;
111 // Returns the ScreenLockerWebUI object.
112 WebUIScreenLocker
* webui_screen_locker() const;
114 // Returns the WebUI object from the screen locker.
115 content::WebUI
* webui() const;
117 DISALLOW_COPY_AND_ASSIGN(WebUIScreenLockerTester
);
120 void WebUIScreenLockerTester::SetPassword(const std::string
& password
) {
121 webui()->GetWebContents()->GetMainFrame()->ExecuteJavaScriptForTests(
122 base::ASCIIToUTF16(base::StringPrintf(
123 "$('pod-row').pods[0].passwordElement.value = '%s';",
127 std::string
WebUIScreenLockerTester::GetPassword() {
129 scoped_ptr
<base::Value
> v
= content::ExecuteScriptAndGetValue(
130 RenderViewHost()->GetMainFrame(),
131 "$('pod-row').pods[0].passwordElement.value;");
132 CHECK(v
->GetAsString(&result
));
136 void WebUIScreenLockerTester::EnterPassword(const std::string
& password
) {
138 SetPassword(password
);
140 // Verify password is set.
141 ASSERT_EQ(password
, GetPassword());
143 // Verify that "reauth" warning is hidden.
144 scoped_ptr
<base::Value
> v
= content::ExecuteScriptAndGetValue(
145 RenderViewHost()->GetMainFrame(),
146 "window.getComputedStyle("
147 " $('pod-row').pods[0].querySelector('.reauth-hint-container'))"
148 " .display == 'none'");
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::InjectStubUserContext(
204 const UserContext
& user_context
) {
205 DCHECK(ScreenLocker::screen_locker_
);
206 ScreenLocker::screen_locker_
->SetAuthenticator(
207 new StubAuthenticator(ScreenLocker::screen_locker_
, user_context
));
208 ScreenLocker::screen_locker_
->extended_authenticator_
=
209 new FakeExtendedAuthenticator(ScreenLocker::screen_locker_
, user_context
);
214 test::ScreenLockerTester
* ScreenLocker::GetTester() {
215 return new test::WebUIScreenLockerTester();
218 } // namespace chromeos