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.
6 #include "base/command_line.h"
7 #include "base/strings/string_util.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/chromeos/login/login_manager_test.h"
10 #include "chrome/browser/chromeos/login/login_wizard.h"
11 #include "chrome/browser/chromeos/login/startup_utils.h"
12 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
13 #include "chrome/browser/chromeos/login/wizard_controller.h"
14 #include "chrome/browser/chromeos/settings/cros_settings.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/profiles/profiles_state.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/interactive_test_utils.h"
22 #include "chrome/test/base/tracing.h"
23 #include "chrome/test/base/ui_test_utils.h"
24 #include "chromeos/chromeos_switches.h"
25 #include "chromeos/login/user_names.h"
26 #include "chromeos/settings/cros_settings_names.h"
27 #include "content/public/browser/render_frame_host.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "content/public/test/test_utils.h"
30 #include "extensions/browser/extension_system.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h"
35 using ::testing::AnyNumber
;
36 using ::testing::Return
;
40 const char kTestUser
[] = "test-user@gmail.com";
41 const char kPassword
[] = "password";
43 void FilterFrameByName(std::set
<content::RenderFrameHost
*>* frame_set
,
44 const std::string
& frame_name
,
45 content::RenderFrameHost
* frame
) {
46 if (frame
->GetFrameName() == frame_name
)
47 frame_set
->insert(frame
);
50 class LoginUserTest
: public InProcessBrowserTest
{
52 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
53 command_line
->AppendSwitchASCII(
54 chromeos::switches::kLoginUser
, "TestUser@gmail.com");
55 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
,
60 class LoginGuestTest
: public InProcessBrowserTest
{
62 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
63 command_line
->AppendSwitch(chromeos::switches::kGuestSession
);
64 command_line
->AppendSwitch(::switches::kIncognito
);
65 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
,
67 command_line
->AppendSwitchASCII(chromeos::switches::kLoginUser
,
68 chromeos::login::kGuestUserName
);
72 class LoginCursorTest
: public InProcessBrowserTest
{
74 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
75 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
79 class LoginSigninTest
: public InProcessBrowserTest
{
81 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
82 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
83 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
86 void SetUpOnMainThread() override
{
87 ASSERT_TRUE(tracing::BeginTracingWithWatch(
88 "ui", "ui", "ShowLoginWebUI", 1));
92 class LoginTest
: public chromeos::LoginManagerTest
{
94 LoginTest() : LoginManagerTest(true) {}
95 ~LoginTest() override
{}
97 content::RenderFrameHost
* GetNamedFrame(const std::string
& frame_name
) {
98 std::set
<content::RenderFrameHost
*> frame_set
;
99 web_contents()->ForEachFrame(
100 base::Bind(&FilterFrameByName
, &frame_set
, frame_name
));
101 return frame_set
.empty() ? NULL
: *frame_set
.begin();
104 void ExecuteJsInGaiaAuthFrame(const std::string
& js
) {
105 content::RenderFrameHost
* frame
= GetNamedFrame("signin-frame");
107 ASSERT_TRUE(content::ExecuteScript(frame
, js
));
110 void StartGaiaAuthOffline() {
111 content::DOMMessageQueue message_queue
;
112 const std::string js
= "(function() {"
113 "var frame = $('signin-frame');"
114 "var onload= function() {"
115 "frame.removeEventListener('load', onload);"
116 "console.error('#### onload frame.src=' + frame.src);"
117 "window.domAutomationController.setAutomationId(0);"
118 "window.domAutomationController.send('frameLoaded');"
120 "frame.addEventListener('load', onload);"
121 "$('error-offline-login-link').onclick();"
122 "console.error('#### original frame.src=' + frame.src);"
124 ASSERT_TRUE(content::ExecuteScript(web_contents(), js
));
128 ASSERT_TRUE(message_queue
.WaitForMessage(&message
));
129 } while (message
!= "\"frameLoaded\"");
132 void SubmitGaiaAuthOfflineForm(const std::string
& user_email
,
133 const std::string
& password
) {
134 // Note the input elements must match gaia_auth/offline.html.
137 "document.getElementsByName('email')[0].value = '$Email';"
138 "document.getElementsByName('password')[0].value = '$Password';"
139 "document.getElementById('submit-button').click();"
141 ReplaceSubstringsAfterOffset(&js
, 0, "$Email", user_email
);
142 ReplaceSubstringsAfterOffset(&js
, 0, "$Password", password
);
143 ExecuteJsInGaiaAuthFrame(js
);
147 // After a chrome crash, the session manager will restart chrome with
148 // the -login-user flag indicating that the user is already logged in.
149 // This profile should NOT be an OTR profile.
150 IN_PROC_BROWSER_TEST_F(LoginUserTest
, UserPassed
) {
151 Profile
* profile
= browser()->profile();
152 std::string
profile_base_path("hash");
153 profile_base_path
.insert(0, chrome::kProfileDirPrefix
);
154 EXPECT_EQ(profile_base_path
, profile
->GetPath().BaseName().value());
155 EXPECT_FALSE(profile
->IsOffTheRecord());
158 // Verifies the cursor is not hidden at startup when user is logged in.
159 IN_PROC_BROWSER_TEST_F(LoginUserTest
, CursorShown
) {
160 EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
163 // After a guest login, we should get the OTR default profile.
164 IN_PROC_BROWSER_TEST_F(LoginGuestTest
, GuestIsOTR
) {
165 Profile
* profile
= browser()->profile();
166 EXPECT_TRUE(profile
->IsOffTheRecord());
167 // Ensure there's extension service for this profile.
168 EXPECT_TRUE(extensions::ExtensionSystem::Get(profile
)->extension_service());
171 // Verifies the cursor is not hidden at startup when running guest session.
172 IN_PROC_BROWSER_TEST_F(LoginGuestTest
, CursorShown
) {
173 EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
176 // Verifies the cursor is hidden at startup on login screen.
177 IN_PROC_BROWSER_TEST_F(LoginCursorTest
, CursorHidden
) {
178 // Login screen needs to be shown explicitly when running test.
179 chromeos::ShowLoginWizard(chromeos::WizardController::kLoginScreenName
);
181 // Cursor should be hidden at startup
182 EXPECT_FALSE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
184 // Cursor should be shown after cursor is moved.
185 EXPECT_TRUE(ui_test_utils::SendMouseMoveSync(gfx::Point()));
186 EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
188 base::MessageLoop::current()->DeleteSoon(
189 FROM_HERE
, chromeos::LoginDisplayHostImpl::default_host());
192 // Verifies that the webui for login comes up successfully.
193 IN_PROC_BROWSER_TEST_F(LoginSigninTest
, WebUIVisible
) {
194 base::TimeDelta no_timeout
;
195 EXPECT_TRUE(tracing::WaitForWatchEvent(no_timeout
));
196 std::string json_events
;
197 ASSERT_TRUE(tracing::EndTracing(&json_events
));
200 IN_PROC_BROWSER_TEST_F(LoginTest
, PRE_GaiaAuthOffline
) {
201 RegisterUser(kTestUser
);
202 chromeos::StartupUtils::MarkOobeCompleted();
203 chromeos::CrosSettings::Get()->SetBoolean(
204 chromeos::kAccountsPrefShowUserNamesOnSignIn
, false);
207 IN_PROC_BROWSER_TEST_F(LoginTest
, GaiaAuthOffline
) {
209 ASSERT_TRUE(chromeos::CrosSettings::Get()->GetBoolean(
210 chromeos::kAccountsPrefShowUserNamesOnSignIn
, &show_user
));
211 ASSERT_FALSE(show_user
);
213 StartGaiaAuthOffline();
215 chromeos::UserContext
user_context(kTestUser
);
216 user_context
.SetKey(chromeos::Key(kPassword
));
217 SetExpectedCredentials(user_context
);
219 SubmitGaiaAuthOfflineForm(kTestUser
, kPassword
);
221 content::WindowedNotificationObserver(
222 chrome::NOTIFICATION_SESSION_STARTED
,
223 content::NotificationService::AllSources()).Wait();