oobe_base_test.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 "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"
27
28namespace chromeos {
29
30namespace {
31
32// Note the path name must be the same as in shill stub.
33const char kStubEthernetServicePath[] = "eth1";
34
35}  // namespace
36
37OobeBaseTest::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;
43}
44
45OobeBaseTest::~OobeBaseTest() {
46}
47
48void 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();
58
59  ExtensionApiTest::SetUp();
60}
61
62void 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);
68
69  ExtensionApiTest::SetUpInProcessBrowserTestFixture();
70}
71
72void OobeBaseTest::SetUpOnMainThread() {
73  // Restart the thread as the sandbox host process has already been spawned.
74  embedded_test_server()->RestartThreadAndListen();
75
76  ExtensionApiTest::SetUpOnMainThread();
77}
78
79void 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();
85  }
86
87  ExtensionApiTest::CleanUpOnMainThread();
88}
89
90void 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");
97
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();
102
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,
110                                  gaia_url.spec());
111  fake_gaia_->Initialize();
112}
113
114void 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();
120}
121
122base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() {
123  return base::Bind(&OobeBaseTest::SimulateNetworkOffline,
124                    base::Unretained(this));
125}
126
127void 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();
134}
135
136base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() {
137  return base::Bind(&OobeBaseTest::SimulateNetworkOnline,
138                    base::Unretained(this));
139}
140
141void 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();
147}
148
149base::Closure OobeBaseTest::SimulateNetworkPortalClosure() {
150  return base::Bind(&OobeBaseTest::SimulateNetworkPortal,
151                    base::Unretained(this));
152}
153
154void OobeBaseTest::JsExpect(const std::string& expression) {
155  bool result;
156  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
157      GetLoginUI()->GetWebContents(),
158      "window.domAutomationController.send(!!(" + expression + "));",
159       &result));
160  ASSERT_TRUE(result) << expression;
161}
162
163content::WebUI* OobeBaseTest::GetLoginUI() {
164  return static_cast<chromeos::LoginDisplayHostImpl*>(
165      chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
166}
167
168SigninScreenHandler* OobeBaseTest::GetSigninScreenHandler() {
169  return static_cast<chromeos::LoginDisplayHostImpl*>(
170      chromeos::LoginDisplayHostImpl::default_host())
171      ->GetOobeUI()
172      ->signin_screen_handler_for_test();
173}
174
175WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() {
176  ExistingUserController* controller =
177      ExistingUserController::current_controller();
178  CHECK(controller);
179  return static_cast<WebUILoginDisplay*>(
180      controller->login_display());
181}
182
183}  // namespace chromeos
184