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/test/oobe_base_test.h"
7 #include "base/command_line.h"
8 #include "base/json/json_file_value_serializer.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/chromeos/login/existing_user_controller.h"
13 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
14 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
15 #include "chrome/browser/chromeos/login/test/https_forwarder.h"
16 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
17 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "chromeos/chromeos_switches.h"
25 #include "chromeos/dbus/fake_shill_manager_client.h"
26 #include "components/policy/core/common/policy_switches.h"
27 #include "components/user_manager/fake_user_manager.h"
28 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "google_apis/gaia/gaia_switches.h"
33 #include "net/dns/mock_host_resolver.h"
34 #include "net/test/embedded_test_server/http_request.h"
35 #include "net/test/embedded_test_server/http_response.h"
40 const char kGAIAHost
[] = "accounts.google.com";
44 const char OobeBaseTest::kFakeUserEmail
[] = "fake-email@gmail.com";
45 const char OobeBaseTest::kFakeUserPassword
[] = "fake-password";
46 const char OobeBaseTest::kFakeSIDCookie
[] = "fake-SID-cookie";
47 const char OobeBaseTest::kFakeLSIDCookie
[] = "fake-LSID-cookie";
49 OobeBaseTest::OobeBaseTest()
50 : fake_gaia_(new FakeGaia()),
51 network_portal_detector_(NULL
),
52 needs_background_networking_(false),
53 gaia_frame_parent_("signin-frame"),
55 initialize_fake_merge_session_(true) {
56 set_exit_when_last_browser_closes(false);
57 set_chromeos_user_
= false;
60 OobeBaseTest::~OobeBaseTest() {
63 void OobeBaseTest::SetUp() {
64 base::FilePath test_data_dir
;
65 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
66 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
68 RegisterAdditionalRequestHandlers();
70 embedded_test_server()->RegisterRequestHandler(
71 base::Bind(&FakeGaia::HandleRequest
, base::Unretained(fake_gaia_
.get())));
73 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
75 // Start https wrapper here so that the URLs can be pointed at it in
76 // SetUpCommandLine().
77 InitHttpsForwarders();
79 // Stop IO thread here because no threads are allowed while
80 // spawning sandbox host process. See crbug.com/322732.
81 embedded_test_server()->StopThread();
83 ExtensionApiTest::SetUp();
86 bool OobeBaseTest::SetUpUserDataDirectory() {
87 base::FilePath user_data_dir
;
88 CHECK(PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
));
89 base::FilePath local_state_path
=
90 user_data_dir
.Append(chrome::kLocalStateFilename
);
93 // Set webview disabled flag only when local state file does not exist.
94 // Otherwise, we break PRE tests that leave state in it.
95 if (!base::PathExists(local_state_path
)) {
96 base::DictionaryValue local_state_dict
;
98 // TODO(nkostylev): Fix tests that fail with webview signin.
99 local_state_dict
.SetBoolean(prefs::kWebviewSigninDisabled
, true);
101 CHECK(JSONFileValueSerializer(local_state_path
)
102 .Serialize(local_state_dict
));
106 return ExtensionApiTest::SetUpUserDataDirectory();
109 void OobeBaseTest::SetUpInProcessBrowserTestFixture() {
110 host_resolver()->AddRule("*", "127.0.0.1");
111 network_portal_detector_
= new NetworkPortalDetectorTestImpl();
112 NetworkPortalDetector::InitializeForTesting(network_portal_detector_
);
113 network_portal_detector_
->SetDefaultNetworkForTesting(
114 FakeShillManagerClient::kFakeEthernetNetworkGuid
);
116 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
119 void OobeBaseTest::SetUpOnMainThread() {
120 if (initialize_fake_merge_session()) {
121 fake_gaia_
->SetFakeMergeSessionParams(kFakeUserEmail
, kFakeSIDCookie
,
125 // Restart the thread as the sandbox host process has already been spawned.
126 embedded_test_server()->RestartThreadAndListen();
128 login_screen_load_observer_
.reset(new content::WindowedNotificationObserver(
129 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
130 content::NotificationService::AllSources()));
132 js_checker_
.set_web_contents(LoginDisplayHostImpl::default_host()
133 ->GetWebUILoginView()
136 test::UserSessionManagerTestApi
session_manager_test_api(
137 UserSessionManager::GetInstance());
138 session_manager_test_api
.SetShouldObtainTokenHandleInTests(false);
140 ExtensionApiTest::SetUpOnMainThread();
143 void OobeBaseTest::TearDownOnMainThread() {
144 // If the login display is still showing, exit gracefully.
145 if (LoginDisplayHostImpl::default_host()) {
146 base::MessageLoop::current()->PostTask(FROM_HERE
,
147 base::Bind(&chrome::AttemptExit
));
148 content::RunMessageLoop();
150 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
152 ExtensionApiTest::TearDownOnMainThread();
155 void OobeBaseTest::SetUpCommandLine(base::CommandLine
* command_line
) {
156 ExtensionApiTest::SetUpCommandLine(command_line
);
158 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
159 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
160 if (!needs_background_networking_
)
161 command_line
->AppendSwitch(::switches::kDisableBackgroundNetworking
);
162 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
, "user");
164 GURL gaia_url
= gaia_https_forwarder_
.GetURLForSSLHost(std::string());
165 command_line
->AppendSwitchASCII(::switches::kGaiaUrl
, gaia_url
.spec());
166 command_line
->AppendSwitchASCII(::switches::kLsoUrl
, gaia_url
.spec());
167 command_line
->AppendSwitchASCII(::switches::kGoogleApisUrl
,
170 fake_gaia_
->Initialize();
171 fake_gaia_
->set_issue_oauth_code_cookie(use_webview());
174 void OobeBaseTest::InitHttpsForwarders() {
175 ASSERT_TRUE(gaia_https_forwarder_
.Initialize(
176 kGAIAHost
, embedded_test_server()->base_url()));
179 void OobeBaseTest::RegisterAdditionalRequestHandlers() {
182 void OobeBaseTest::SimulateNetworkOffline() {
183 NetworkPortalDetector::CaptivePortalState offline_state
;
184 offline_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE
;
185 network_portal_detector_
->SetDetectionResultsForTesting(
186 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
188 network_portal_detector_
->NotifyObserversForTesting();
191 base::Closure
OobeBaseTest::SimulateNetworkOfflineClosure() {
192 return base::Bind(&OobeBaseTest::SimulateNetworkOffline
,
193 base::Unretained(this));
196 void OobeBaseTest::SimulateNetworkOnline() {
197 NetworkPortalDetector::CaptivePortalState online_state
;
198 online_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
199 online_state
.response_code
= 204;
200 network_portal_detector_
->SetDetectionResultsForTesting(
201 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
203 network_portal_detector_
->NotifyObserversForTesting();
206 base::Closure
OobeBaseTest::SimulateNetworkOnlineClosure() {
207 return base::Bind(&OobeBaseTest::SimulateNetworkOnline
,
208 base::Unretained(this));
211 void OobeBaseTest::SimulateNetworkPortal() {
212 NetworkPortalDetector::CaptivePortalState portal_state
;
213 portal_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
214 network_portal_detector_
->SetDetectionResultsForTesting(
215 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
217 network_portal_detector_
->NotifyObserversForTesting();
220 base::Closure
OobeBaseTest::SimulateNetworkPortalClosure() {
221 return base::Bind(&OobeBaseTest::SimulateNetworkPortal
,
222 base::Unretained(this));
225 void OobeBaseTest::JsExpect(const std::string
& expression
) {
226 JS().ExpectTrue(expression
);
229 content::WebUI
* OobeBaseTest::GetLoginUI() {
230 return static_cast<chromeos::LoginDisplayHostImpl
*>(
231 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
234 WebUILoginDisplay
* OobeBaseTest::GetLoginDisplay() {
235 ExistingUserController
* controller
=
236 ExistingUserController::current_controller();
238 return static_cast<WebUILoginDisplay
*>(
239 controller
->login_display());
242 void OobeBaseTest::WaitForGaiaPageLoad() {
243 WaitForSigninScreen();
249 "$('gaia-signin').gaiaAuthHost_.addEventListener('ready',"
251 "window.domAutomationController.setAutomationId(0);"
252 "window.domAutomationController.send('GaiaReady');"
255 content::DOMMessageQueue message_queue
;
258 ASSERT_TRUE(message_queue
.WaitForMessage(&message
));
259 } while (message
!= "\"GaiaReady\"");
262 void OobeBaseTest::WaitForSigninScreen() {
263 WizardController
* wizard_controller
= WizardController::default_controller();
264 if (wizard_controller
)
265 wizard_controller
->SkipToLoginForTesting(LoginScreenContext());
267 WizardController::SkipPostLoginScreensForTesting();
269 login_screen_load_observer_
->Wait();
272 void OobeBaseTest::ExecuteJsInSigninFrame(const std::string
& js
) {
273 content::RenderFrameHost
* frame
= InlineLoginUI::GetAuthFrame(
274 GetLoginUI()->GetWebContents(), GURL(), gaia_frame_parent_
);
275 ASSERT_TRUE(content::ExecuteScript(frame
, js
));
278 void OobeBaseTest::SetSignFormField(const std::string
& field_id
,
279 const std::string
& field_value
) {
282 "document.getElementById('$FieldId').value = '$FieldValue';"
283 "var e = new Event('input');"
284 "document.getElementById('$FieldId').dispatchEvent(e);"
286 base::ReplaceSubstringsAfterOffset(&js
, 0, "$FieldId", field_id
);
287 base::ReplaceSubstringsAfterOffset(&js
, 0, "$FieldValue", field_value
);
288 ExecuteJsInSigninFrame(js
);
291 } // namespace chromeos