| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/test/oobe_base_test.h" | 5 #include "chrome/browser/chromeos/login/test/oobe_base_test.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/chromeos/login/existing_user_controller.h" | 12 #include "chrome/browser/chromeos/login/existing_user_controller.h" |
| 13 #include "chrome/browser/chromeos/login/test/https_forwarder.h" | 13 #include "chrome/browser/chromeos/login/test/https_forwarder.h" |
| 14 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h" | 14 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h" |
| 15 #include "chrome/browser/lifetime/application_lifetime.h" | 15 #include "chrome/browser/lifetime/application_lifetime.h" |
| 16 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" | 16 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" |
| 17 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 18 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "chromeos/chromeos_switches.h" | 21 #include "chromeos/chromeos_switches.h" |
| 22 #include "chromeos/dbus/fake_shill_manager_client.h" | 22 #include "chromeos/dbus/fake_shill_manager_client.h" |
| 23 #include "components/policy/core/common/policy_switches.h" |
| 23 #include "components/user_manager/fake_user_manager.h" | 24 #include "components/user_manager/fake_user_manager.h" |
| 24 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
| 25 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
| 26 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/test/browser_test_utils.h" | 28 #include "content/public/test/browser_test_utils.h" |
| 28 #include "google_apis/gaia/gaia_switches.h" | 29 #include "google_apis/gaia/gaia_switches.h" |
| 29 #include "net/dns/mock_host_resolver.h" | 30 #include "net/dns/mock_host_resolver.h" |
| 30 #include "net/test/embedded_test_server/http_request.h" | 31 #include "net/test/embedded_test_server/http_request.h" |
| 31 #include "net/test/embedded_test_server/http_response.h" | 32 #include "net/test/embedded_test_server/http_response.h" |
| 32 | 33 |
| 33 namespace chromeos { | 34 namespace chromeos { |
| 34 | 35 |
| 36 // static |
| 37 const char OobeBaseTest::kFakeUserEmail[] = "fake-email@gmail.com"; |
| 38 const char OobeBaseTest::kFakeUserPassword[] = "fake-password"; |
| 39 const char OobeBaseTest::kFakeSIDCookie[] = "fake-SID-cookie"; |
| 40 const char OobeBaseTest::kFakeLSIDCookie[] = "fake-LSID-cookie"; |
| 41 |
| 35 OobeBaseTest::OobeBaseTest() | 42 OobeBaseTest::OobeBaseTest() |
| 36 : fake_gaia_(new FakeGaia()), | 43 : fake_gaia_(new FakeGaia()), |
| 37 network_portal_detector_(NULL), | 44 network_portal_detector_(NULL), |
| 38 needs_background_networking_(false), | 45 needs_background_networking_(false), |
| 39 gaia_frame_parent_("signin-frame"), | 46 gaia_frame_parent_("signin-frame"), |
| 40 use_webview_(false) { | 47 use_webview_(false), |
| 48 initialize_fake_merge_session_(true) { |
| 41 set_exit_when_last_browser_closes(false); | 49 set_exit_when_last_browser_closes(false); |
| 42 set_chromeos_user_ = false; | 50 set_chromeos_user_ = false; |
| 43 } | 51 } |
| 44 | 52 |
| 45 OobeBaseTest::~OobeBaseTest() { | 53 OobeBaseTest::~OobeBaseTest() { |
| 46 } | 54 } |
| 47 | 55 |
| 48 void OobeBaseTest::SetUp() { | 56 void OobeBaseTest::SetUp() { |
| 49 base::FilePath test_data_dir; | 57 base::FilePath test_data_dir; |
| 50 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 58 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 51 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 59 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 52 | 60 |
| 61 RegisterAdditionalRequestHandlers(); |
| 62 |
| 53 embedded_test_server()->RegisterRequestHandler( | 63 embedded_test_server()->RegisterRequestHandler( |
| 54 base::Bind(&FakeGaia::HandleRequest, base::Unretained(fake_gaia_.get()))); | 64 base::Bind(&FakeGaia::HandleRequest, base::Unretained(fake_gaia_.get()))); |
| 55 | 65 |
| 56 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 66 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 57 | 67 |
| 58 // Start https wrapper here so that the URLs can be pointed at it in | 68 // Start https wrapper here so that the URLs can be pointed at it in |
| 59 // SetUpCommandLine(). | 69 // SetUpCommandLine(). |
| 60 InitHttpsForwarders(); | 70 InitHttpsForwarders(); |
| 61 | 71 |
| 62 // Stop IO thread here because no threads are allowed while | 72 // Stop IO thread here because no threads are allowed while |
| 63 // spawning sandbox host process. See crbug.com/322732. | 73 // spawning sandbox host process. See crbug.com/322732. |
| 64 embedded_test_server()->StopThread(); | 74 embedded_test_server()->StopThread(); |
| 65 | 75 |
| 66 ExtensionApiTest::SetUp(); | 76 ExtensionApiTest::SetUp(); |
| 67 } | 77 } |
| 68 | 78 |
| 69 bool OobeBaseTest::SetUpUserDataDirectory() { | 79 bool OobeBaseTest::SetUpUserDataDirectory() { |
| 70 if (use_webview_) { | 80 base::FilePath user_data_dir; |
| 71 // Fake Dev channel to enable webview signin. | 81 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
| 72 scoped_channel_.reset( | 82 base::FilePath local_state_path = |
| 73 new extensions::ScopedCurrentChannel(chrome::VersionInfo::CHANNEL_DEV)); | 83 user_data_dir.Append(chrome::kLocalStateFilename); |
| 74 | 84 |
| 75 base::FilePath user_data_dir; | 85 if (!use_webview()) { |
| 76 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); | 86 // Set webview disabled flag only when local state file does not exist. |
| 77 base::FilePath local_state_path = | |
| 78 user_data_dir.Append(chrome::kLocalStateFilename); | |
| 79 | |
| 80 // Set webview enabled flag only when local state file does not exist. | |
| 81 // Otherwise, we break PRE tests that leave state in it. | 87 // Otherwise, we break PRE tests that leave state in it. |
| 82 if (!base::PathExists(local_state_path)) { | 88 if (!base::PathExists(local_state_path)) { |
| 83 base::DictionaryValue local_state_dict; | 89 base::DictionaryValue local_state_dict; |
| 84 local_state_dict.SetBoolean(prefs::kWebviewSigninEnabled, true); | 90 |
| 85 // OobeCompleted to skip controller-pairing-screen which still uses | 91 // TODO(nkostylev): Fix tests that fail with webview signin. |
| 86 // iframe and ends up in a JS error in oobe page init. | 92 local_state_dict.SetBoolean(prefs::kWebviewSigninDisabled, true); |
| 87 // See http://crbug.com/467147 | |
| 88 local_state_dict.SetBoolean(prefs::kOobeComplete, true); | |
| 89 | 93 |
| 90 CHECK(JSONFileValueSerializer(local_state_path) | 94 CHECK(JSONFileValueSerializer(local_state_path) |
| 91 .Serialize(local_state_dict)); | 95 .Serialize(local_state_dict)); |
| 92 } | 96 } |
| 93 } | 97 } |
| 94 | 98 |
| 95 return ExtensionApiTest::SetUpUserDataDirectory(); | 99 return ExtensionApiTest::SetUpUserDataDirectory(); |
| 96 } | 100 } |
| 97 | 101 |
| 98 void OobeBaseTest::SetUpInProcessBrowserTestFixture() { | 102 void OobeBaseTest::SetUpInProcessBrowserTestFixture() { |
| 99 host_resolver()->AddRule("*", "127.0.0.1"); | 103 host_resolver()->AddRule("*", "127.0.0.1"); |
| 100 network_portal_detector_ = new NetworkPortalDetectorTestImpl(); | 104 network_portal_detector_ = new NetworkPortalDetectorTestImpl(); |
| 101 NetworkPortalDetector::InitializeForTesting(network_portal_detector_); | 105 NetworkPortalDetector::InitializeForTesting(network_portal_detector_); |
| 102 network_portal_detector_->SetDefaultNetworkForTesting( | 106 network_portal_detector_->SetDefaultNetworkForTesting( |
| 103 FakeShillManagerClient::kFakeEthernetNetworkGuid); | 107 FakeShillManagerClient::kFakeEthernetNetworkGuid); |
| 104 | 108 |
| 105 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | 109 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| 106 } | 110 } |
| 107 | 111 |
| 108 void OobeBaseTest::SetUpOnMainThread() { | 112 void OobeBaseTest::SetUpOnMainThread() { |
| 113 if (initialize_fake_merge_session()) { |
| 114 fake_gaia_->SetFakeMergeSessionParams(kFakeUserEmail, kFakeSIDCookie, |
| 115 kFakeLSIDCookie); |
| 116 } |
| 117 |
| 109 // Restart the thread as the sandbox host process has already been spawned. | 118 // Restart the thread as the sandbox host process has already been spawned. |
| 110 embedded_test_server()->RestartThreadAndListen(); | 119 embedded_test_server()->RestartThreadAndListen(); |
| 111 | 120 |
| 112 login_screen_load_observer_.reset(new content::WindowedNotificationObserver( | 121 login_screen_load_observer_.reset(new content::WindowedNotificationObserver( |
| 113 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 122 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 114 content::NotificationService::AllSources())); | 123 content::NotificationService::AllSources())); |
| 115 | 124 |
| 116 ExtensionApiTest::SetUpOnMainThread(); | 125 ExtensionApiTest::SetUpOnMainThread(); |
| 117 } | 126 } |
| 118 | 127 |
| 119 void OobeBaseTest::TearDownOnMainThread() { | 128 void OobeBaseTest::TearDownOnMainThread() { |
| 120 // If the login display is still showing, exit gracefully. | 129 // If the login display is still showing, exit gracefully. |
| 121 if (LoginDisplayHostImpl::default_host()) { | 130 if (LoginDisplayHostImpl::default_host()) { |
| 122 base::MessageLoop::current()->PostTask(FROM_HERE, | 131 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 123 base::Bind(&chrome::AttemptExit)); | 132 base::Bind(&chrome::AttemptExit)); |
| 124 content::RunMessageLoop(); | 133 content::RunMessageLoop(); |
| 125 } | 134 } |
| 135 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 126 | 136 |
| 127 ExtensionApiTest::TearDownOnMainThread(); | 137 ExtensionApiTest::TearDownOnMainThread(); |
| 128 } | 138 } |
| 129 | 139 |
| 130 void OobeBaseTest::SetUpCommandLine(base::CommandLine* command_line) { | 140 void OobeBaseTest::SetUpCommandLine(base::CommandLine* command_line) { |
| 131 ExtensionApiTest::SetUpCommandLine(command_line); | 141 ExtensionApiTest::SetUpCommandLine(command_line); |
| 132 | 142 |
| 133 command_line->AppendSwitch(chromeos::switches::kLoginManager); | 143 command_line->AppendSwitch(chromeos::switches::kLoginManager); |
| 134 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); | 144 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); |
| 135 if (!needs_background_networking_) | 145 if (!needs_background_networking_) |
| 136 command_line->AppendSwitch(::switches::kDisableBackgroundNetworking); | 146 command_line->AppendSwitch(::switches::kDisableBackgroundNetworking); |
| 137 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); | 147 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); |
| 138 | 148 |
| 139 GURL gaia_url = gaia_https_forwarder_->GetURL(""); | 149 GURL gaia_url = gaia_https_forwarder_->GetURL(std::string()); |
| 140 command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec()); | 150 command_line->AppendSwitchASCII(::switches::kGaiaUrl, gaia_url.spec()); |
| 141 command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec()); | 151 command_line->AppendSwitchASCII(::switches::kLsoUrl, gaia_url.spec()); |
| 142 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl, | 152 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl, |
| 143 gaia_url.spec()); | 153 gaia_url.spec()); |
| 144 | 154 |
| 145 fake_gaia_->Initialize(); | 155 fake_gaia_->Initialize(); |
| 146 fake_gaia_->set_issue_oauth_code_cookie(use_webview_); | 156 fake_gaia_->set_issue_oauth_code_cookie(use_webview()); |
| 147 } | 157 } |
| 148 | 158 |
| 149 void OobeBaseTest::InitHttpsForwarders() { | 159 void OobeBaseTest::InitHttpsForwarders() { |
| 150 gaia_https_forwarder_.reset( | 160 gaia_https_forwarder_.reset( |
| 151 new HTTPSForwarder(embedded_test_server()->base_url())); | 161 new HTTPSForwarder(embedded_test_server()->base_url())); |
| 152 ASSERT_TRUE(gaia_https_forwarder_->Start()); | 162 ASSERT_TRUE(gaia_https_forwarder_->Start()); |
| 153 } | 163 } |
| 154 | 164 |
| 165 void OobeBaseTest::RegisterAdditionalRequestHandlers() { |
| 166 } |
| 167 |
| 155 void OobeBaseTest::SimulateNetworkOffline() { | 168 void OobeBaseTest::SimulateNetworkOffline() { |
| 156 NetworkPortalDetector::CaptivePortalState offline_state; | 169 NetworkPortalDetector::CaptivePortalState offline_state; |
| 157 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE; | 170 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE; |
| 158 network_portal_detector_->SetDetectionResultsForTesting( | 171 network_portal_detector_->SetDetectionResultsForTesting( |
| 159 FakeShillManagerClient::kFakeEthernetNetworkGuid, | 172 FakeShillManagerClient::kFakeEthernetNetworkGuid, |
| 160 offline_state); | 173 offline_state); |
| 161 network_portal_detector_->NotifyObserversForTesting(); | 174 network_portal_detector_->NotifyObserversForTesting(); |
| 162 } | 175 } |
| 163 | 176 |
| 164 base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() { | 177 base::Closure OobeBaseTest::SimulateNetworkOfflineClosure() { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 } | 223 } |
| 211 | 224 |
| 212 WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() { | 225 WebUILoginDisplay* OobeBaseTest::GetLoginDisplay() { |
| 213 ExistingUserController* controller = | 226 ExistingUserController* controller = |
| 214 ExistingUserController::current_controller(); | 227 ExistingUserController::current_controller(); |
| 215 CHECK(controller); | 228 CHECK(controller); |
| 216 return static_cast<WebUILoginDisplay*>( | 229 return static_cast<WebUILoginDisplay*>( |
| 217 controller->login_display()); | 230 controller->login_display()); |
| 218 } | 231 } |
| 219 | 232 |
| 233 void OobeBaseTest::WaitForGaiaPageLoad() { |
| 234 WaitForSigninScreen(); |
| 235 |
| 236 if (!use_webview()) |
| 237 return; |
| 238 |
| 239 ASSERT_TRUE(content::ExecuteScript( |
| 240 GetLoginUI()->GetWebContents(), |
| 241 "$('gaia-signin').gaiaAuthHost_.addEventListener('ready'," |
| 242 "function() {" |
| 243 "window.domAutomationController.setAutomationId(0);" |
| 244 "window.domAutomationController.send('GaiaReady');" |
| 245 "});")); |
| 246 |
| 247 content::DOMMessageQueue message_queue; |
| 248 std::string message; |
| 249 do { |
| 250 ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 251 } while (message != "\"GaiaReady\""); |
| 252 } |
| 253 |
| 220 void OobeBaseTest::WaitForSigninScreen() { | 254 void OobeBaseTest::WaitForSigninScreen() { |
| 221 WizardController* wizard_controller = WizardController::default_controller(); | 255 WizardController* wizard_controller = WizardController::default_controller(); |
| 222 if (wizard_controller) { | 256 if (wizard_controller) |
| 223 wizard_controller->SkipToLoginForTesting(LoginScreenContext()); | 257 wizard_controller->SkipToLoginForTesting(LoginScreenContext()); |
| 224 } | 258 |
| 225 WizardController::SkipPostLoginScreensForTesting(); | 259 WizardController::SkipPostLoginScreensForTesting(); |
| 226 | 260 |
| 227 login_screen_load_observer_->Wait(); | 261 login_screen_load_observer_->Wait(); |
| 228 } | 262 } |
| 229 | 263 |
| 230 void OobeBaseTest::ExecuteJsInSigninFrame(const std::string& js) { | 264 void OobeBaseTest::ExecuteJsInSigninFrame(const std::string& js) { |
| 231 content::RenderFrameHost* frame = InlineLoginUI::GetAuthFrame( | 265 content::RenderFrameHost* frame = InlineLoginUI::GetAuthFrame( |
| 232 GetLoginUI()->GetWebContents(), GURL(), gaia_frame_parent_); | 266 GetLoginUI()->GetWebContents(), GURL(), gaia_frame_parent_); |
| 233 ASSERT_TRUE(content::ExecuteScript(frame, js)); | 267 ASSERT_TRUE(content::ExecuteScript(frame, js)); |
| 234 } | 268 } |
| 235 | 269 |
| 236 void OobeBaseTest::SetSignFormField(const std::string& field_id, | 270 void OobeBaseTest::SetSignFormField(const std::string& field_id, |
| 237 const std::string& field_value) { | 271 const std::string& field_value) { |
| 238 std::string js = | 272 std::string js = |
| 239 "(function(){" | 273 "(function(){" |
| 240 "document.getElementById('$FieldId').value = '$FieldValue';" | 274 "document.getElementById('$FieldId').value = '$FieldValue';" |
| 241 "var e = new Event('input');" | 275 "var e = new Event('input');" |
| 242 "document.getElementById('$FieldId').dispatchEvent(e);" | 276 "document.getElementById('$FieldId').dispatchEvent(e);" |
| 243 "})();"; | 277 "})();"; |
| 244 ReplaceSubstringsAfterOffset(&js, 0, "$FieldId", field_id); | 278 ReplaceSubstringsAfterOffset(&js, 0, "$FieldId", field_id); |
| 245 ReplaceSubstringsAfterOffset(&js, 0, "$FieldValue", field_value); | 279 ReplaceSubstringsAfterOffset(&js, 0, "$FieldValue", field_value); |
| 246 ExecuteJsInSigninFrame(js); | 280 ExecuteJsInSigninFrame(js); |
| 247 } | 281 } |
| 248 | 282 |
| 249 } // namespace chromeos | 283 } // namespace chromeos |
| OLD | NEW |