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/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/users/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 "chromeos/dbus/fake_shill_manager_client.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "google_apis/gaia/gaia_switches.h"
25 #include "net/dns/mock_host_resolver.h"
26 #include "net/test/embedded_test_server/http_request.h"
27 #include "net/test/embedded_test_server/http_response.h"
31 OobeBaseTest::OobeBaseTest()
32 : fake_gaia_(new FakeGaia()),
33 network_portal_detector_(NULL),
34 needs_background_networking_(false) {
35 set_exit_when_last_browser_closes(false);
36 set_chromeos_user_ = false;
39 OobeBaseTest::~OobeBaseTest() {
42 void OobeBaseTest::SetUp() {
43 base::FilePath test_data_dir;
44 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
45 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
46 embedded_test_server()->RegisterRequestHandler(
47 base::Bind(&FakeGaia::HandleRequest, base::Unretained(fake_gaia_.get())));
48 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
49 // Stop IO thread here because no threads are allowed while
50 // spawning sandbox host process. See crbug.com/322732.
51 embedded_test_server()->StopThread();
53 ExtensionApiTest::SetUp();
56 void OobeBaseTest::SetUpInProcessBrowserTestFixture() {
57 host_resolver()->AddRule("*", "127.0.0.1");
58 network_portal_detector_ = new NetworkPortalDetectorTestImpl();
59 NetworkPortalDetector::InitializeForTesting(network_portal_detector_);
60 network_portal_detector_->SetDefaultNetworkForTesting(
61 FakeShillManagerClient::kFakeEthernetNetworkGuid);
63 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
66 void OobeBaseTest::SetUpOnMainThread() {
67 // Restart the thread as the sandbox host process has already been spawned.
68 embedded_test_server()->RestartThreadAndListen();
70 ExtensionApiTest::SetUpOnMainThread();
73 void OobeBaseTest::TearDownOnMainThread() {
74 // If the login display is still showing, exit gracefully.
75 if (LoginDisplayHostImpl::default_host()) {
76 base::MessageLoop::current()->PostTask(FROM_HERE,
77 base::Bind(&chrome::AttemptExit));
78 content::RunMessageLoop();
81 ExtensionApiTest::TearDownOnMainThread();
84 void OobeBaseTest::SetUpCommandLine(CommandLine* command_line) {
85 ExtensionApiTest::SetUpCommandLine(command_line);
86 command_line->AppendSwitch(chromeos::switches::kLoginManager);
87 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
88 if (!needs_background_networking_)
89 command_line->AppendSwitch(::switches::kDisableBackgroundNetworking);
90 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
92 // Create gaia and webstore URL from test server url but using different
93 // host names. This is to avoid gaia response being tagged as from
94 // webstore in chrome_resource_dispatcher_host_delegate.cc.
95 const GURL& server_url = embedded_test_server()->base_url();
97 std::string gaia_host("gaia");
98 GURL::Replacements replace_gaia_host;
99 replace_gaia_host.SetHostStr(gaia_host);
100 GURL gaia_url = server_url.ReplaceComponents(replace_gaia_host);
101 command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec());
102 command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec());
103 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl,
105 fake_gaia_->Initialize();
108 void OobeBaseTest::SimulateNetworkOffline() {
109 NetworkPortalDetector::CaptivePortalState offline_state;
110 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
111 network_portal_detector_->SetDetectionResultsForTesting(
112 FakeShillManagerClient::kFakeEthernetNetworkGuid,
114 network_portal_detector_->NotifyObserversForTesting();
117 base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() {
118 return base::Bind(&OobeBaseTest::SimulateNetworkOffline,
119 base::Unretained(this));
122 void OobeBaseTest::SimulateNetworkOnline() {
123 NetworkPortalDetector::CaptivePortalState online_state;
124 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
125 online_state.response_code = 204;
126 network_portal_detector_->SetDetectionResultsForTesting(
127 FakeShillManagerClient::kFakeEthernetNetworkGuid,
129 network_portal_detector_->NotifyObserversForTesting();
132 base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() {
133 return base::Bind(&OobeBaseTest::SimulateNetworkOnline,
134 base::Unretained(this));
137 void OobeBaseTest::SimulateNetworkPortal() {
138 NetworkPortalDetector::CaptivePortalState portal_state;
139 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
140 network_portal_detector_->SetDetectionResultsForTesting(
141 FakeShillManagerClient::kFakeEthernetNetworkGuid,
143 network_portal_detector_->NotifyObserversForTesting();
146 base::Closure OobeBaseTest::SimulateNetworkPortalClosure() {
147 return base::Bind(&OobeBaseTest::SimulateNetworkPortal,
148 base::Unretained(this));
151 void OobeBaseTest::JsExpect(const std::string& expression) {
153 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
154 GetLoginUI()->GetWebContents(),
155 "window.domAutomationController.send(!!(" + expression + "));",
157 ASSERT_TRUE(result) << expression;
160 content::WebUI* OobeBaseTest::GetLoginUI() {
161 return static_cast<chromeos::LoginDisplayHostImpl*>(
162 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
165 SigninScreenHandler* OobeBaseTest::GetSigninScreenHandler() {
166 return static_cast<chromeos::LoginDisplayHostImpl*>(
167 chromeos::LoginDisplayHostImpl::default_host())
169 ->signin_screen_handler_for_test();
172 WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() {
173 ExistingUserController* controller =
174 ExistingUserController::current_controller();
176 return static_cast<WebUILoginDisplay*>(
177 controller->login_display());
180 } // namespace chromeos