1 // Copyright (c) 2013 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/oobe_base_test.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/chromeos/login/existing_user_controller.h"
12 #include "chrome/browser/chromeos/login/fake_user_manager.h"
13 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
14 #include "chrome/browser/lifetime/application_lifetime.h"
15 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chromeos/chromeos_switches.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "google_apis/gaia/gaia_switches.h"
24 #include "net/dns/mock_host_resolver.h"
25 #include "net/test/embedded_test_server/http_request.h"
26 #include "net/test/embedded_test_server/http_response.h"
32 // Note the path name must be the same as in shill stub.
33 const char kStubEthernetServicePath
[] = "eth1";
37 OobeBaseTest::OobeBaseTest()
38 : fake_gaia_(new FakeGaia()),
39 network_portal_detector_(NULL
) {
40 set_exit_when_last_browser_closes(false);
41 set_chromeos_user_
= false;
44 OobeBaseTest::~OobeBaseTest() {
47 void OobeBaseTest::SetUp() {
48 base::FilePath test_data_dir
;
49 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
50 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
51 embedded_test_server()->RegisterRequestHandler(
52 base::Bind(&FakeGaia::HandleRequest
, base::Unretained(fake_gaia_
.get())));
53 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
54 // Stop IO thread here because no threads are allowed while
55 // spawning sandbox host process. See crbug.com/322732.
56 embedded_test_server()->StopThread();
58 ExtensionApiTest::SetUp();
61 void OobeBaseTest::SetUpInProcessBrowserTestFixture() {
62 host_resolver()->AddRule("*", "127.0.0.1");
63 network_portal_detector_
= new NetworkPortalDetectorTestImpl();
64 NetworkPortalDetector::InitializeForTesting(network_portal_detector_
);
65 network_portal_detector_
->SetDefaultNetworkPathForTesting(
66 kStubEthernetServicePath
);
68 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
71 void OobeBaseTest::SetUpOnMainThread() {
72 // Restart the thread as the sandbox host process has already been spawned.
73 embedded_test_server()->RestartThreadAndListen();
75 ExtensionApiTest::SetUpOnMainThread();
78 void OobeBaseTest::CleanUpOnMainThread() {
79 // If the login display is still showing, exit gracefully.
80 if (LoginDisplayHostImpl::default_host()) {
81 base::MessageLoop::current()->PostTask(FROM_HERE
,
82 base::Bind(&chrome::AttemptExit
));
83 content::RunMessageLoop();
86 ExtensionApiTest::CleanUpOnMainThread();
89 void OobeBaseTest::SetUpCommandLine(CommandLine
* command_line
) {
90 ExtensionApiTest::SetUpCommandLine(command_line
);
91 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
92 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
93 command_line
->AppendSwitch(::switches::kDisableBackgroundNetworking
);
94 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
, "user");
96 // Create gaia and webstore URL from test server url but using different
97 // host names. This is to avoid gaia response being tagged as from
98 // webstore in chrome_resource_dispatcher_host_delegate.cc.
99 const GURL
& server_url
= embedded_test_server()->base_url();
101 std::string
gaia_host("gaia");
102 GURL::Replacements replace_gaia_host
;
103 replace_gaia_host
.SetHostStr(gaia_host
);
104 GURL gaia_url
= server_url
.ReplaceComponents(replace_gaia_host
);
105 command_line
->AppendSwitchASCII(::switches::kGaiaUrl
, gaia_url
.spec());
106 command_line
->AppendSwitchASCII(::switches::kLsoUrl
, gaia_url
.spec());
107 command_line
->AppendSwitchASCII(::switches::kGoogleApisUrl
,
109 fake_gaia_
->Initialize();
112 void OobeBaseTest::SimulateNetworkOffline() {
113 NetworkPortalDetector::CaptivePortalState offline_state
;
114 offline_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE
;
115 network_portal_detector_
->SetDetectionResultsForTesting(
116 kStubEthernetServicePath
, offline_state
);
117 network_portal_detector_
->NotifyObserversForTesting();
120 base::Closure
OobeBaseTest::SimulateNetworkOfflineClosure() {
121 return base::Bind(&OobeBaseTest::SimulateNetworkOffline
,
122 base::Unretained(this));
125 void OobeBaseTest::SimulateNetworkOnline() {
126 NetworkPortalDetector::CaptivePortalState online_state
;
127 online_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
128 online_state
.response_code
= 204;
129 network_portal_detector_
->SetDetectionResultsForTesting(
130 kStubEthernetServicePath
, online_state
);
131 network_portal_detector_
->NotifyObserversForTesting();
134 base::Closure
OobeBaseTest::SimulateNetworkOnlineClosure() {
135 return base::Bind(&OobeBaseTest::SimulateNetworkOnline
,
136 base::Unretained(this));
139 void OobeBaseTest::SimulateNetworkPortal() {
140 NetworkPortalDetector::CaptivePortalState portal_state
;
141 portal_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
142 network_portal_detector_
->SetDetectionResultsForTesting(
143 kStubEthernetServicePath
, portal_state
);
144 network_portal_detector_
->NotifyObserversForTesting();
147 base::Closure
OobeBaseTest::SimulateNetworkPortalClosure() {
148 return base::Bind(&OobeBaseTest::SimulateNetworkPortal
,
149 base::Unretained(this));
152 void OobeBaseTest::JsExpect(const std::string
& expression
) {
154 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
155 GetLoginUI()->GetWebContents(),
156 "window.domAutomationController.send(!!(" + expression
+ "));",
158 ASSERT_TRUE(result
) << expression
;
161 content::WebUI
* OobeBaseTest::GetLoginUI() {
162 return static_cast<chromeos::LoginDisplayHostImpl
*>(
163 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
166 SigninScreenHandler
* OobeBaseTest::GetSigninScreenHandler() {
167 return static_cast<chromeos::LoginDisplayHostImpl
*>(
168 chromeos::LoginDisplayHostImpl::default_host())
170 ->signin_screen_handler_for_test();
173 WebUILoginDisplay
* OobeBaseTest::GetLoginDisplay() {
174 ExistingUserController
* controller
=
175 ExistingUserController::current_controller();
177 return static_cast<WebUILoginDisplay
*>(
178 controller
->login_display());
181 } // namespace chromeos