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 needs_background_networking_(false) {
41 set_exit_when_last_browser_closes(false);
42 set_chromeos_user_ = false;
45 OobeBaseTest::~OobeBaseTest() {
48 void OobeBaseTest::SetUp() {
49 base::FilePath test_data_dir;
50 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
51 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
52 embedded_test_server()->RegisterRequestHandler(
53 base::Bind(&FakeGaia::HandleRequest, base::Unretained(fake_gaia_.get())));
54 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
55 // Stop IO thread here because no threads are allowed while
56 // spawning sandbox host process. See crbug.com/322732.
57 embedded_test_server()->StopThread();
59 ExtensionApiTest::SetUp();
62 void OobeBaseTest::SetUpInProcessBrowserTestFixture() {
63 host_resolver()->AddRule("*", "127.0.0.1");
64 network_portal_detector_ = new NetworkPortalDetectorTestImpl();
65 NetworkPortalDetector::InitializeForTesting(network_portal_detector_);
66 network_portal_detector_->SetDefaultNetworkPathForTesting(
67 kStubEthernetServicePath);
69 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
72 void OobeBaseTest::SetUpOnMainThread() {
73 // Restart the thread as the sandbox host process has already been spawned.
74 embedded_test_server()->RestartThreadAndListen();
76 ExtensionApiTest::SetUpOnMainThread();
79 void OobeBaseTest::CleanUpOnMainThread() {
80 // If the login display is still showing, exit gracefully.
81 if (LoginDisplayHostImpl::default_host()) {
82 base::MessageLoop::current()->PostTask(FROM_HERE,
83 base::Bind(&chrome::AttemptExit));
84 content::RunMessageLoop();
87 ExtensionApiTest::CleanUpOnMainThread();
90 void OobeBaseTest::SetUpCommandLine(CommandLine* command_line) {
91 ExtensionApiTest::SetUpCommandLine(command_line);
92 command_line->AppendSwitch(chromeos::switches::kLoginManager);
93 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
94 if (!needs_background_networking_)
95 command_line->AppendSwitch(::switches::kDisableBackgroundNetworking);
96 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
98 // Create gaia and webstore URL from test server url but using different
99 // host names. This is to avoid gaia response being tagged as from
100 // webstore in chrome_resource_dispatcher_host_delegate.cc.
101 const GURL& server_url = embedded_test_server()->base_url();
103 std::string gaia_host("gaia");
104 GURL::Replacements replace_gaia_host;
105 replace_gaia_host.SetHostStr(gaia_host);
106 GURL gaia_url = server_url.ReplaceComponents(replace_gaia_host);
107 command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec());
108 command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec());
109 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl,
111 fake_gaia_->Initialize();
114 void OobeBaseTest::SimulateNetworkOffline() {
115 NetworkPortalDetector::CaptivePortalState offline_state;
116 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
117 network_portal_detector_->SetDetectionResultsForTesting(
118 kStubEthernetServicePath, offline_state);
119 network_portal_detector_->NotifyObserversForTesting();
122 base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() {
123 return base::Bind(&OobeBaseTest::SimulateNetworkOffline,
124 base::Unretained(this));
127 void OobeBaseTest::SimulateNetworkOnline() {
128 NetworkPortalDetector::CaptivePortalState online_state;
129 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
130 online_state.response_code = 204;
131 network_portal_detector_->SetDetectionResultsForTesting(
132 kStubEthernetServicePath, online_state);
133 network_portal_detector_->NotifyObserversForTesting();
136 base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() {
137 return base::Bind(&OobeBaseTest::SimulateNetworkOnline,
138 base::Unretained(this));
141 void OobeBaseTest::SimulateNetworkPortal() {
142 NetworkPortalDetector::CaptivePortalState portal_state;
143 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
144 network_portal_detector_->SetDetectionResultsForTesting(
145 kStubEthernetServicePath, portal_state);
146 network_portal_detector_->NotifyObserversForTesting();
149 base::Closure OobeBaseTest::SimulateNetworkPortalClosure() {
150 return base::Bind(&OobeBaseTest::SimulateNetworkPortal,
151 base::Unretained(this));
154 void OobeBaseTest::JsExpect(const std::string& expression) {
156 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
157 GetLoginUI()->GetWebContents(),
158 "window.domAutomationController.send(!!(" + expression + "));",
160 ASSERT_TRUE(result) << expression;
163 content::WebUI* OobeBaseTest::GetLoginUI() {
164 return static_cast<chromeos::LoginDisplayHostImpl*>(
165 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
168 SigninScreenHandler* OobeBaseTest::GetSigninScreenHandler() {
169 return static_cast<chromeos::LoginDisplayHostImpl*>(
170 chromeos::LoginDisplayHostImpl::default_host())
172 ->signin_screen_handler_for_test();
175 WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() {
176 ExistingUserController* controller =
177 ExistingUserController::current_controller();
179 return static_cast<WebUILoginDisplay*>(
180 controller->login_display());
183 } // namespace chromeos