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_->SetDefaultNetworkForTesting(
61      FakeShillManagerClient::kFakeEthernetNetworkGuid);
62
63  ExtensionApiTest::SetUpInProcessBrowserTestFixture();
64}
65
66void OobeBaseTest::SetUpOnMainThread() {
67  // Restart the thread as the sandbox host process has already been spawned.
68  embedded_test_server()->RestartThreadAndListen();
69
70  ExtensionApiTest::SetUpOnMainThread();
71}
72
73void 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();
79  }
80
81  ExtensionApiTest::TearDownOnMainThread();
82}
83
84void 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");
91
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();
96
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,
104                                  gaia_url.spec());
105  fake_gaia_->Initialize();
106}
107
108void OobeBaseTest::SimulateNetworkOffline() {
109  NetworkPortalDetector::CaptivePortalState offline_state;
110  offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
111  network_portal_detector_->SetDetectionResultsForTesting(
112      FakeShillManagerClient::kFakeEthernetNetworkGuid,
113      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::kFakeEthernetNetworkGuid,
128      online_state);
129  network_portal_detector_->NotifyObserversForTesting();
130}
131
132base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() {
133  return base::Bind(&OobeBaseTest::SimulateNetworkOnline,
134                    base::Unretained(this));
135}
136
137void OobeBaseTest::SimulateNetworkPortal() {
138  NetworkPortalDetector::CaptivePortalState portal_state;
139  portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
140  network_portal_detector_->SetDetectionResultsForTesting(
141      FakeShillManagerClient::kFakeEthernetNetworkGuid,
142      portal_state);
143  network_portal_detector_->NotifyObserversForTesting();
144}
145
146base::Closure OobeBaseTest::SimulateNetworkPortalClosure() {
147  return base::Bind(&OobeBaseTest::SimulateNetworkPortal,
148                    base::Unretained(this));
149}
150
151void OobeBaseTest::JsExpect(const std::string& expression) {
152  bool result;
153  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
154      GetLoginUI()->GetWebContents(),
155      "window.domAutomationController.send(!!(" + expression + "));",
156       &result));
157  ASSERT_TRUE(result) << expression;
158}
159
160content::WebUI* OobeBaseTest::GetLoginUI() {
161  return static_cast<chromeos::LoginDisplayHostImpl*>(
162      chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui();
163}
164
165SigninScreenHandler* OobeBaseTest::GetSigninScreenHandler() {
166  return static_cast<chromeos::LoginDisplayHostImpl*>(
167      chromeos::LoginDisplayHostImpl::default_host())
168      ->GetOobeUI()
169      ->signin_screen_handler_for_test();
170}
171
172WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() {
173  ExistingUserController* controller =
174      ExistingUserController::current_controller();
175  CHECK(controller);
176  return static_cast<WebUILoginDisplay*>(
177      controller->login_display());
178}
179
180}  // namespace chromeos
181