OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/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/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 |
| 28 namespace chromeos { |
| 29 |
| 30 namespace { |
| 31 |
| 32 // Note the path name must be the same as in shill stub. |
| 33 const char kStubEthernetServicePath[] = "eth1"; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 OobeBaseTest::OobeBaseTest() : network_portal_detector_(NULL) { |
| 38 set_exit_when_last_browser_closes(false); |
| 39 } |
| 40 |
| 41 OobeBaseTest::~OobeBaseTest() { |
| 42 } |
| 43 |
| 44 void OobeBaseTest::SetUp() { |
| 45 base::FilePath test_data_dir; |
| 46 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 47 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 48 embedded_test_server()->RegisterRequestHandler( |
| 49 base::Bind(&FakeGaia::HandleRequest, base::Unretained(&fake_gaia_))); |
| 50 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 51 // Stop IO thread here because no threads are allowed while |
| 52 // spawning sandbox host process. See crbug.com/322732. |
| 53 embedded_test_server()->StopThread(); |
| 54 |
| 55 InProcessBrowserTest::SetUp(); |
| 56 } |
| 57 |
| 58 void OobeBaseTest::SetUpInProcessBrowserTestFixture() { |
| 59 host_resolver()->AddRule("*", "127.0.0.1"); |
| 60 network_portal_detector_ = new NetworkPortalDetectorTestImpl(); |
| 61 NetworkPortalDetector::InitializeForTesting(network_portal_detector_); |
| 62 network_portal_detector_->SetDefaultNetworkPathForTesting( |
| 63 kStubEthernetServicePath); |
| 64 } |
| 65 |
| 66 void OobeBaseTest::SetUpOnMainThread() { |
| 67 // Restart the thread as the sandbox host process has already been spawned. |
| 68 embedded_test_server()->RestartThreadAndListen(); |
| 69 } |
| 70 |
| 71 void OobeBaseTest::CleanUpOnMainThread() { |
| 72 // If the login display is still showing, exit gracefully. |
| 73 if (LoginDisplayHostImpl::default_host()) { |
| 74 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 75 base::Bind(&chrome::AttemptExit)); |
| 76 content::RunMessageLoop(); |
| 77 } |
| 78 } |
| 79 |
| 80 void OobeBaseTest::SetUpCommandLine(CommandLine* command_line) { |
| 81 command_line->AppendSwitch(chromeos::switches::kLoginManager); |
| 82 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); |
| 83 command_line->AppendSwitch(::switches::kDisableBackgroundNetworking); |
| 84 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); |
| 85 |
| 86 // Create gaia and webstore URL from test server url but using different |
| 87 // host names. This is to avoid gaia response being tagged as from |
| 88 // webstore in chrome_resource_dispatcher_host_delegate.cc. |
| 89 const GURL& server_url = embedded_test_server()->base_url(); |
| 90 |
| 91 std::string gaia_host("gaia"); |
| 92 GURL::Replacements replace_gaia_host; |
| 93 replace_gaia_host.SetHostStr(gaia_host); |
| 94 GURL gaia_url = server_url.ReplaceComponents(replace_gaia_host); |
| 95 command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec()); |
| 96 command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec()); |
| 97 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl, |
| 98 gaia_url.spec()); |
| 99 fake_gaia_.Initialize(); |
| 100 } |
| 101 |
| 102 void OobeBaseTest::SimulateNetworkOffline() { |
| 103 NetworkPortalDetector::CaptivePortalState offline_state; |
| 104 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 105 network_portal_detector_->SetDetectionResultsForTesting( |
| 106 kStubEthernetServicePath, offline_state); |
| 107 network_portal_detector_->NotifyObserversForTesting(); |
| 108 } |
| 109 |
| 110 base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() { |
| 111 return base::Bind(&OobeBaseTest::SimulateNetworkOffline, |
| 112 base::Unretained(this)); |
| 113 } |
| 114 |
| 115 void OobeBaseTest::SimulateNetworkOnline() { |
| 116 NetworkPortalDetector::CaptivePortalState online_state; |
| 117 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; |
| 118 online_state.response_code = 204; |
| 119 network_portal_detector_->SetDetectionResultsForTesting( |
| 120 kStubEthernetServicePath, online_state); |
| 121 network_portal_detector_->NotifyObserversForTesting(); |
| 122 } |
| 123 |
| 124 base::Closure OobeBaseTest::SimulateNetworkOnlineClosure() { |
| 125 return base::Bind(&OobeBaseTest::SimulateNetworkOnline, |
| 126 base::Unretained(this)); |
| 127 } |
| 128 |
| 129 void OobeBaseTest::SimulateNetworkPortal() { |
| 130 NetworkPortalDetector::CaptivePortalState portal_state; |
| 131 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL; |
| 132 network_portal_detector_->SetDetectionResultsForTesting( |
| 133 kStubEthernetServicePath, portal_state); |
| 134 network_portal_detector_->NotifyObserversForTesting(); |
| 135 } |
| 136 |
| 137 base::Closure OobeBaseTest::SimulateNetworkPortalClosure() { |
| 138 return base::Bind(&OobeBaseTest::SimulateNetworkPortal, |
| 139 base::Unretained(this)); |
| 140 } |
| 141 |
| 142 void OobeBaseTest::JsExpect(const std::string& expression) { |
| 143 bool result; |
| 144 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 145 GetLoginUI()->GetWebContents(), |
| 146 "window.domAutomationController.send(!!(" + expression + "));", |
| 147 &result)); |
| 148 ASSERT_TRUE(result) << expression; |
| 149 } |
| 150 |
| 151 content::WebUI* OobeBaseTest::GetLoginUI() { |
| 152 return static_cast<chromeos::LoginDisplayHostImpl*>( |
| 153 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI()->web_ui(); |
| 154 } |
| 155 |
| 156 SigninScreenHandler* OobeBaseTest::GetSigninScreenHandler() { |
| 157 return static_cast<chromeos::LoginDisplayHostImpl*>( |
| 158 chromeos::LoginDisplayHostImpl::default_host()) |
| 159 ->GetOobeUI() |
| 160 ->signin_screen_handler_for_test(); |
| 161 } |
| 162 |
| 163 WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() { |
| 164 ExistingUserController* controller = |
| 165 ExistingUserController::current_controller(); |
| 166 CHECK(controller); |
| 167 return static_cast<WebUILoginDisplay*>( |
| 168 controller->login_display()); |
| 169 } |
| 170 |
| 171 } // namespace chromeos |
OLD | NEW |