oobe_base_test.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
5#include "chrome/browser/chromeos/login/test/oobe_base_test.h"
6
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"
28
29namespace chromeos {
30
31OobeBaseTest::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;
37}
38
39OobeBaseTest::~OobeBaseTest() {
40}
41
42void 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();
52
53  ExtensionApiTest::SetUp();
54}
55
56void 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_->SetDefaultNetworkPathForTesting(
61      FakeShillManagerClient::kFakeEthernetNetworkPath,
62      FakeShillManagerClient::kFakeEthernetNetworkPath /* guid */);
63
64  ExtensionApiTest::SetUpInProcessBrowserTestFixture();
65}
66
67void OobeBaseTest::SetUpOnMainThread() {
68  // Restart the thread as the sandbox host process has already been spawned.
69  embedded_test_server()->RestartThreadAndListen();
70
71  ExtensionApiTest::SetUpOnMainThread();
72}
73
74void OobeBaseTest::CleanUpOnMainThread() {
75  // If the login display is still showing, exit gracefully.
76  if (LoginDisplayHostImpl::default_host()) {
77    base::MessageLoop::current()->PostTask(FROM_HERE,
78                                           base::Bind(&chrome::AttemptExit));
79    content::RunMessageLoop();
80  }
81
82  ExtensionApiTest::CleanUpOnMainThread();
83}
84
85void OobeBaseTest::SetUpCommandLine(CommandLine* command_line) {
86  ExtensionApiTest::SetUpCommandLine(command_line);
87  command_line->AppendSwitch(chromeos::switches::kLoginManager);
88  command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
89  if (!needs_background_networking_)
90    command_line->AppendSwitch(::switches::kDisableBackgroundNetworking);
91  command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
92
93  // Create gaia and webstore URL from test server url but using different
94  // host names. This is to avoid gaia response being tagged as from
95  // webstore in chrome_resource_dispatcher_host_delegate.cc.
96  const GURL& server_url = embedded_test_server()->base_url();
97
98  std::string gaia_host("gaia");
99  GURL::Replacements replace_gaia_host;
100  replace_gaia_host.SetHostStr(gaia_host);
101  GURL gaia_url = server_url.ReplaceComponents(replace_gaia_host);
102  command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec());
103  command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec());
104  command_line->AppendSwitchASCII(::switches::kGoogleApisUrl,
105                                  gaia_url.spec());
106  fake_gaia_->Initialize();
107}
108
109void OobeBaseTest::SimulateNetworkOffline() {
110  NetworkPortalDetector::CaptivePortalState offline_state;
111  offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
112  network_portal_detector_->SetDetectionResultsForTesting(
113      FakeShillManagerClient::kFakeEthernetNetworkPath, offline_state);
114  network_portal_detector_->NotifyObserversForTesting();
115}
116
117base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() {
118  return base::Bind(&OobeBaseTest::SimulateNetworkOffline,
119                    base::Unretained(this));
120}
121
122void 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::kFakeEthernetNetworkPath, online_state);
128  network_portal_detector_->NotifyObserversForTesting();
129}
130
131base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() {
132  return base::Bind(&OobeBaseTest::SimulateNetworkOnline,
133                    base::Unretained(this));
134}
135
136void OobeBaseTest::SimulateNetworkPortal() {
137  NetworkPortalDetector::CaptivePortalState portal_state;
138  portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
139  network_portal_detector_->SetDetectionResultsForTesting(
140      FakeShillManagerClient::kFakeEthernetNetworkPath, portal_state);
141  network_portal_detector_->NotifyObserversForTesting();
142}
143
144base::Closure OobeBaseTest::SimulateNetworkPortalClosure() {
145  return base::Bind(&OobeBaseTest::SimulateNetworkPortal,
146                    base::Unretained(this));
147}
148
149void OobeBaseTest::JsExpect(const std::string& expression) {
150  bool result;
151  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
152      GetLoginUI()->GetWebContents(),
153      "window.domAutomationController.send(!!(" + expression + "));",
154       &result));
155  ASSERT_TRUE(result) << expression;
156}
157
158content::WebUI* OobeBaseTest::GetLoginUI() {
159  return static_cast<chromeos::LoginDisplayHostImpl*>(
160      chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
161}
162
163SigninScreenHandler* OobeBaseTest::GetSigninScreenHandler() {
164  return static_cast<chromeos::LoginDisplayHostImpl*>(
165      chromeos::LoginDisplayHostImpl::default_host())
166      ->GetOobeUI()
167      ->signin_screen_handler_for_test();
168}
169
170WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() {
171  ExistingUserController* controller =
172      ExistingUserController::current_controller();
173  CHECK(controller);
174  return static_cast<WebUILoginDisplay*>(
175      controller->login_display());
176}
177
178}  // namespace chromeos
179