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/net/network_portal_detector_test_impl.h"
13 #include "chrome/browser/lifetime/application_lifetime.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "chromeos/dbus/fake_shill_manager_client.h"
18 #include "components/user_manager/fake_user_manager.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"
30 OobeBaseTest::OobeBaseTest()
31 : fake_gaia_(new FakeGaia()),
32 network_portal_detector_(NULL
),
33 needs_background_networking_(false) {
34 set_exit_when_last_browser_closes(false);
35 set_chromeos_user_
= false;
38 OobeBaseTest::~OobeBaseTest() {
41 void OobeBaseTest::SetUp() {
42 base::FilePath test_data_dir
;
43 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
44 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
45 embedded_test_server()->RegisterRequestHandler(
46 base::Bind(&FakeGaia::HandleRequest
, base::Unretained(fake_gaia_
.get())));
47 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
48 // Stop IO thread here because no threads are allowed while
49 // spawning sandbox host process. See crbug.com/322732.
50 embedded_test_server()->StopThread();
52 ExtensionApiTest::SetUp();
55 void OobeBaseTest::SetUpInProcessBrowserTestFixture() {
56 host_resolver()->AddRule("*", "127.0.0.1");
57 network_portal_detector_
= new NetworkPortalDetectorTestImpl();
58 NetworkPortalDetector::InitializeForTesting(network_portal_detector_
);
59 network_portal_detector_
->SetDefaultNetworkForTesting(
60 FakeShillManagerClient::kFakeEthernetNetworkGuid
);
62 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
65 void OobeBaseTest::SetUpOnMainThread() {
66 // Restart the thread as the sandbox host process has already been spawned.
67 embedded_test_server()->RestartThreadAndListen();
69 ExtensionApiTest::SetUpOnMainThread();
72 void OobeBaseTest::TearDownOnMainThread() {
73 // If the login display is still showing, exit gracefully.
74 if (LoginDisplayHostImpl::default_host()) {
75 base::MessageLoop::current()->PostTask(FROM_HERE
,
76 base::Bind(&chrome::AttemptExit
));
77 content::RunMessageLoop();
80 ExtensionApiTest::TearDownOnMainThread();
83 void OobeBaseTest::SetUpCommandLine(base::CommandLine
* command_line
) {
84 ExtensionApiTest::SetUpCommandLine(command_line
);
85 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
86 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
87 if (!needs_background_networking_
)
88 command_line
->AppendSwitch(::switches::kDisableBackgroundNetworking
);
89 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
, "user");
91 // Create gaia and webstore URL from test server url but using different
92 // host names. This is to avoid gaia response being tagged as from
93 // webstore in chrome_resource_dispatcher_host_delegate.cc.
94 const GURL
& server_url
= embedded_test_server()->base_url();
96 GURL::Replacements replace_gaia_host
;
97 replace_gaia_host
.SetHostStr("gaia");
98 GURL gaia_url
= server_url
.ReplaceComponents(replace_gaia_host
);
99 command_line
->AppendSwitchASCII(::switches::kGaiaUrl
, gaia_url
.spec());
100 command_line
->AppendSwitchASCII(::switches::kLsoUrl
, gaia_url
.spec());
101 command_line
->AppendSwitchASCII(::switches::kGoogleApisUrl
,
103 fake_gaia_
->Initialize();
106 void OobeBaseTest::SimulateNetworkOffline() {
107 NetworkPortalDetector::CaptivePortalState offline_state
;
108 offline_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE
;
109 network_portal_detector_
->SetDetectionResultsForTesting(
110 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
112 network_portal_detector_
->NotifyObserversForTesting();
115 base::Closure
OobeBaseTest::SimulateNetworkOfflineClosure() {
116 return base::Bind(&OobeBaseTest::SimulateNetworkOffline
,
117 base::Unretained(this));
120 void OobeBaseTest::SimulateNetworkOnline() {
121 NetworkPortalDetector::CaptivePortalState online_state
;
122 online_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
123 online_state
.response_code
= 204;
124 network_portal_detector_
->SetDetectionResultsForTesting(
125 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
127 network_portal_detector_
->NotifyObserversForTesting();
130 base::Closure
OobeBaseTest::SimulateNetworkOnlineClosure() {
131 return base::Bind(&OobeBaseTest::SimulateNetworkOnline
,
132 base::Unretained(this));
135 void OobeBaseTest::SimulateNetworkPortal() {
136 NetworkPortalDetector::CaptivePortalState portal_state
;
137 portal_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
138 network_portal_detector_
->SetDetectionResultsForTesting(
139 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
141 network_portal_detector_
->NotifyObserversForTesting();
144 base::Closure
OobeBaseTest::SimulateNetworkPortalClosure() {
145 return base::Bind(&OobeBaseTest::SimulateNetworkPortal
,
146 base::Unretained(this));
149 void OobeBaseTest::JsExpect(const std::string
& expression
) {
151 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
152 GetLoginUI()->GetWebContents(),
153 "window.domAutomationController.send(!!(" + expression
+ "));",
155 ASSERT_TRUE(result
) << expression
;
158 content::WebUI
* OobeBaseTest::GetLoginUI() {
159 return static_cast<chromeos::LoginDisplayHostImpl
*>(
160 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
163 WebUILoginDisplay
* OobeBaseTest::GetLoginDisplay() {
164 ExistingUserController
* controller
=
165 ExistingUserController::current_controller();
167 return static_cast<WebUILoginDisplay
*>(
168 controller
->login_display());
171 } // namespace chromeos