Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / test / oobe_base_test.cc
bloba0b04bee0fb8b56d3eb83761a4bd02751fec041d
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/session/user_session_manager.h"
13 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
14 #include "chrome/browser/chromeos/login/test/https_forwarder.h"
15 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
16 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
17 #include "chrome/browser/lifetime/application_lifetime.h"
18 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chromeos/chromeos_switches.h"
22 #include "chromeos/dbus/fake_shill_manager_client.h"
23 #include "components/policy/core/common/policy_switches.h"
24 #include "components/user_manager/fake_user_manager.h"
25 #include "content/public/browser/notification_observer.h"
26 #include "content/public/browser/notification_registrar.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "google_apis/gaia/gaia_switches.h"
30 #include "net/dns/mock_host_resolver.h"
31 #include "net/test/embedded_test_server/http_request.h"
32 #include "net/test/embedded_test_server/http_response.h"
34 namespace chromeos {
36 namespace {
37 const char kGAIAHost[] = "accounts.google.com";
40 // static
41 const char OobeBaseTest::kFakeUserEmail[] = "fake-email@gmail.com";
42 const char OobeBaseTest::kFakeUserPassword[] = "fake-password";
43 const char OobeBaseTest::kFakeSIDCookie[] = "fake-SID-cookie";
44 const char OobeBaseTest::kFakeLSIDCookie[] = "fake-LSID-cookie";
46 OobeBaseTest::OobeBaseTest()
47 : fake_gaia_(new FakeGaia()),
48 network_portal_detector_(NULL),
49 needs_background_networking_(false),
50 gaia_frame_parent_("signin-frame"),
51 initialize_fake_merge_session_(true) {
52 set_exit_when_last_browser_closes(false);
53 set_chromeos_user_ = false;
56 OobeBaseTest::~OobeBaseTest() {
59 void OobeBaseTest::SetUp() {
60 base::FilePath test_data_dir;
61 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
62 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
64 RegisterAdditionalRequestHandlers();
66 embedded_test_server()->RegisterRequestHandler(
67 base::Bind(&FakeGaia::HandleRequest, base::Unretained(fake_gaia_.get())));
69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
71 // Start https wrapper here so that the URLs can be pointed at it in
72 // SetUpCommandLine().
73 InitHttpsForwarders();
75 // Stop IO thread here because no threads are allowed while
76 // spawning sandbox host process. See crbug.com/322732.
77 embedded_test_server()->StopThread();
79 ExtensionApiTest::SetUp();
82 void OobeBaseTest::SetUpInProcessBrowserTestFixture() {
83 host_resolver()->AddRule("*", "127.0.0.1");
84 network_portal_detector_ = new NetworkPortalDetectorTestImpl();
85 NetworkPortalDetector::InitializeForTesting(network_portal_detector_);
86 network_portal_detector_->SetDefaultNetworkForTesting(
87 FakeShillManagerClient::kFakeEthernetNetworkGuid);
89 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
92 void OobeBaseTest::SetUpOnMainThread() {
93 if (initialize_fake_merge_session()) {
94 fake_gaia_->SetFakeMergeSessionParams(kFakeUserEmail, kFakeSIDCookie,
95 kFakeLSIDCookie);
98 // Restart the thread as the sandbox host process has already been spawned.
99 embedded_test_server()->RestartThreadAndListen();
101 login_screen_load_observer_.reset(new content::WindowedNotificationObserver(
102 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
103 content::NotificationService::AllSources()));
105 js_checker_.set_web_contents(LoginDisplayHostImpl::default_host()
106 ->GetWebUILoginView()
107 ->GetWebContents());
109 test::UserSessionManagerTestApi session_manager_test_api(
110 UserSessionManager::GetInstance());
111 session_manager_test_api.SetShouldObtainTokenHandleInTests(false);
113 ExtensionApiTest::SetUpOnMainThread();
116 void OobeBaseTest::TearDownOnMainThread() {
117 // If the login display is still showing, exit gracefully.
118 if (LoginDisplayHostImpl::default_host()) {
119 base::MessageLoop::current()->PostTask(FROM_HERE,
120 base::Bind(&chrome::AttemptExit));
121 content::RunMessageLoop();
123 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
125 ExtensionApiTest::TearDownOnMainThread();
128 void OobeBaseTest::SetUpCommandLine(base::CommandLine* command_line) {
129 ExtensionApiTest::SetUpCommandLine(command_line);
131 command_line->AppendSwitch(chromeos::switches::kLoginManager);
132 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
133 if (!needs_background_networking_)
134 command_line->AppendSwitch(::switches::kDisableBackgroundNetworking);
135 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
137 GURL gaia_url = gaia_https_forwarder_.GetURLForSSLHost(std::string());
138 command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec());
139 command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec());
140 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl,
141 gaia_url.spec());
143 fake_gaia_->Initialize();
144 fake_gaia_->set_issue_oauth_code_cookie(true);
147 void OobeBaseTest::InitHttpsForwarders() {
148 ASSERT_TRUE(gaia_https_forwarder_.Initialize(
149 kGAIAHost, embedded_test_server()->base_url()));
152 void OobeBaseTest::RegisterAdditionalRequestHandlers() {
155 void OobeBaseTest::SimulateNetworkOffline() {
156 NetworkPortalDetector::CaptivePortalState offline_state;
157 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
158 network_portal_detector_->SetDetectionResultsForTesting(
159 FakeShillManagerClient::kFakeEthernetNetworkGuid,
160 offline_state);
161 network_portal_detector_->NotifyObserversForTesting();
164 base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() {
165 return base::Bind(&OobeBaseTest::SimulateNetworkOffline,
166 base::Unretained(this));
169 void OobeBaseTest::SimulateNetworkOnline() {
170 NetworkPortalDetector::CaptivePortalState online_state;
171 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
172 online_state.response_code = 204;
173 network_portal_detector_->SetDetectionResultsForTesting(
174 FakeShillManagerClient::kFakeEthernetNetworkGuid,
175 online_state);
176 network_portal_detector_->NotifyObserversForTesting();
179 base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() {
180 return base::Bind(&OobeBaseTest::SimulateNetworkOnline,
181 base::Unretained(this));
184 void OobeBaseTest::SimulateNetworkPortal() {
185 NetworkPortalDetector::CaptivePortalState portal_state;
186 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
187 network_portal_detector_->SetDetectionResultsForTesting(
188 FakeShillManagerClient::kFakeEthernetNetworkGuid,
189 portal_state);
190 network_portal_detector_->NotifyObserversForTesting();
193 base::Closure OobeBaseTest::SimulateNetworkPortalClosure() {
194 return base::Bind(&OobeBaseTest::SimulateNetworkPortal,
195 base::Unretained(this));
198 void OobeBaseTest::JsExpect(const std::string& expression) {
199 JS().ExpectTrue(expression);
202 content::WebUI* OobeBaseTest::GetLoginUI() {
203 return static_cast<chromeos::LoginDisplayHostImpl*>(
204 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
207 WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() {
208 ExistingUserController* controller =
209 ExistingUserController::current_controller();
210 CHECK(controller);
211 return static_cast<WebUILoginDisplay*>(
212 controller->login_display());
215 void OobeBaseTest::WaitForGaiaPageLoad() {
216 WaitForSigninScreen();
218 JS().Evaluate(
219 "$('gaia-signin').gaiaAuthHost_.addEventListener('ready',"
220 "function() {"
221 "window.domAutomationController.setAutomationId(0);"
222 "window.domAutomationController.send('GaiaReady');"
223 "});");
225 content::DOMMessageQueue message_queue;
226 std::string message;
227 do {
228 ASSERT_TRUE(message_queue.WaitForMessage(&message));
229 } while (message != "\"GaiaReady\"");
232 void OobeBaseTest::WaitForSigninScreen() {
233 WizardController* wizard_controller = WizardController::default_controller();
234 if (wizard_controller)
235 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
237 WizardController::SkipPostLoginScreensForTesting();
239 login_screen_load_observer_->Wait();
242 void OobeBaseTest::ExecuteJsInSigninFrame(const std::string& js) {
243 content::RenderFrameHost* frame = InlineLoginUI::GetAuthFrame(
244 GetLoginUI()->GetWebContents(), GURL(), gaia_frame_parent_);
245 ASSERT_TRUE(content::ExecuteScript(frame, js));
248 void OobeBaseTest::SetSignFormField(const std::string& field_id,
249 const std::string& field_value) {
250 std::string js =
251 "(function(){"
252 "document.getElementById('$FieldId').value = '$FieldValue';"
253 "var e = new Event('input');"
254 "document.getElementById('$FieldId').dispatchEvent(e);"
255 "})();";
256 base::ReplaceSubstringsAfterOffset(&js, 0, "$FieldId", field_id);
257 base::ReplaceSubstringsAfterOffset(&js, 0, "$FieldValue", field_value);
258 ExecuteJsInSigninFrame(js);
261 } // namespace chromeos