| 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 "base/command_line.h" |
| 6 #include "base/path_service.h" |
| 7 #include "chrome/browser/chrome_browser_main.h" |
| 8 #include "chrome/browser/chrome_browser_main_extra_parts.h" |
| 9 #include "chrome/browser/chrome_content_browser_client.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" |
| 11 #include "chrome/browser/chromeos/login/existing_user_controller.h" |
| 12 #include "chrome/browser/chromeos/login/webui_login_display.h" |
| 13 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 14 #include "chrome/browser/google_apis/test_server/http_request.h" |
| 15 #include "chrome/browser/google_apis/test_server/http_response.h" |
| 16 #include "chrome/browser/google_apis/test_server/http_server.h" |
| 17 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/test/base/in_process_browser_test.h" |
| 22 #include "chrome/test/base/interactive_test_utils.h" |
| 23 #include "chrome/test/base/ui_test_utils.h" |
| 24 #include "chromeos/chromeos_switches.h" |
| 25 #include "content/public/browser/notification_observer.h" |
| 26 #include "content/public/browser/notification_registrar.h" |
| 27 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/test/test_utils.h" |
| 29 #include "google_apis/gaia/gaia_switches.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 |
| 33 using namespace google_apis; |
| 34 using namespace google_apis::test_server; |
| 35 |
| 36 namespace { |
| 37 |
| 38 // Used to add an observer to NotificationService after it's created. |
| 39 class TestBrowserMainExtraParts |
| 40 : public ChromeBrowserMainExtraParts, |
| 41 public content::NotificationObserver { |
| 42 public: |
| 43 TestBrowserMainExtraParts() {} |
| 44 virtual ~TestBrowserMainExtraParts() {} |
| 45 |
| 46 // ChromeBrowserMainExtraParts implementation. |
| 47 virtual void PreEarlyInitialization() OVERRIDE { |
| 48 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
| 49 content::NotificationService::AllSources()); |
| 50 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
| 51 content::NotificationService::AllSources()); |
| 52 } |
| 53 |
| 54 void set_quit_task(const base::Closure& quit_task) { quit_task_ = quit_task; } |
| 55 void set_gaia_url(const std::string& url) { gaia_url_ = url; } |
| 56 |
| 57 private: |
| 58 // Overridden from content::NotificationObserver: |
| 59 virtual void Observe(int type, |
| 60 const content::NotificationSource& source, |
| 61 const content::NotificationDetails& details) OVERRIDE { |
| 62 if (type == chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE) { |
| 63 LOG(INFO) << "NOTIFICATION_LOGIN_WEBUI_VISIBLE"; |
| 64 chromeos::ExistingUserController* controller = |
| 65 chromeos::ExistingUserController::current_controller(); |
| 66 CHECK(controller); |
| 67 chromeos::WebUILoginDisplay* webui_login_display = |
| 68 static_cast<chromeos::WebUILoginDisplay*>( |
| 69 controller->login_display()); |
| 70 CHECK(webui_login_display); |
| 71 webui_login_display->SetGaiaOriginForTesting(gaia_url_); |
| 72 webui_login_display->ShowSigninScreenForCreds("username", "password"); |
| 73 // TODO(glotov): mock GAIA server (test_server_) should support |
| 74 // username/password configuration. |
| 75 } else if (type == chrome::NOTIFICATION_SESSION_STARTED) { |
| 76 LOG(INFO) << "chrome::NOTIFICATION_SESSION_STARTED"; |
| 77 quit_task_.Run(); |
| 78 } else { |
| 79 NOTREACHED(); |
| 80 } |
| 81 } |
| 82 |
| 83 content::NotificationRegistrar registrar_; |
| 84 base::Closure quit_task_; |
| 85 std::string gaia_url_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(TestBrowserMainExtraParts); |
| 88 }; |
| 89 |
| 90 class TestContentBrowserClient : public chrome::ChromeContentBrowserClient { |
| 91 public: |
| 92 TestContentBrowserClient() {} |
| 93 virtual ~TestContentBrowserClient() {} |
| 94 |
| 95 virtual content::BrowserMainParts* CreateBrowserMainParts( |
| 96 const content::MainFunctionParams& parameters) OVERRIDE { |
| 97 ChromeBrowserMainParts* main_parts = static_cast<ChromeBrowserMainParts*>( |
| 98 ChromeContentBrowserClient::CreateBrowserMainParts(parameters)); |
| 99 |
| 100 browser_main_extra_parts_ = new TestBrowserMainExtraParts(); |
| 101 main_parts->AddParts(browser_main_extra_parts_); |
| 102 return main_parts; |
| 103 } |
| 104 |
| 105 TestBrowserMainExtraParts* browser_main_extra_parts_; |
| 106 |
| 107 private: |
| 108 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient); |
| 109 }; |
| 110 |
| 111 const base::FilePath kServiceLogin("chromeos/service_login.html"); |
| 112 |
| 113 class OobeTest : public chromeos::CrosInProcessBrowserTest { |
| 114 protected: |
| 115 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 116 command_line->AppendSwitch(chromeos::switches::kLoginManager); |
| 117 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); |
| 118 command_line->AppendSwitch(switches::kDisableChromeCaptivePortalDetector); |
| 119 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); |
| 120 command_line->AppendSwitchASCII(switches::kAuthExtensionPath, "gaia_auth"); |
| 121 } |
| 122 |
| 123 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 124 content_browser_client_.reset(new TestContentBrowserClient()); |
| 125 original_content_browser_client_ = content::SetBrowserClientForTesting( |
| 126 content_browser_client_.get()); |
| 127 base::FilePath test_data_dir; |
| 128 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 129 CHECK(file_util::ReadFileToString(test_data_dir.Append(kServiceLogin), |
| 130 &service_login_response_)); |
| 131 } |
| 132 |
| 133 virtual void SetUpOnMainThread() OVERRIDE { |
| 134 test_server_ = new HttpServer(); // Constructor wants UI thread. |
| 135 CHECK(test_server_->InitializeAndWaitUntilReady()); |
| 136 test_server_->RegisterRequestHandler( |
| 137 base::Bind(&OobeTest::HandleRequest, base::Unretained(this))); |
| 138 LOG(INFO) << "Set up http server at " << test_server_->base_url(); |
| 139 CHECK(test_server_->port() >= 8040 && test_server_->port() < 8045) |
| 140 << "Current manifest_test.json for gaia_login restrictions " |
| 141 << "does not allow this port"; |
| 142 |
| 143 const std::string gaia_url = |
| 144 "http://localhost:" + test_server_->base_url().port(); |
| 145 content_browser_client_->browser_main_extra_parts_->set_gaia_url(gaia_url); |
| 146 } |
| 147 |
| 148 virtual void CleanUpOnMainThread() OVERRIDE { |
| 149 LOG(INFO) << "Stopping the http server."; |
| 150 test_server_->ShutdownAndWaitUntilComplete(); |
| 151 delete test_server_; // Destructor wants UI thread. |
| 152 } |
| 153 |
| 154 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { |
| 155 GURL url = test_server_->GetURL(request.relative_url); |
| 156 LOG(INFO) << "Http request: " << url.spec(); |
| 157 |
| 158 scoped_ptr<HttpResponse> http_response(new HttpResponse()); |
| 159 if (url.path() == "/ServiceLogin") { |
| 160 http_response->set_code(test_server::SUCCESS); |
| 161 http_response->set_content(service_login_response_); |
| 162 http_response->set_content_type("text/html"); |
| 163 } else if (url.path() == "/ServiceLoginAuth") { |
| 164 LOG(INFO) << "Params: " << request.content; |
| 165 static const char kContinueParam[] = "continue="; |
| 166 int continue_arg_begin = request.content.find(kContinueParam) + |
| 167 arraysize(kContinueParam) - 1; |
| 168 int continue_arg_end = request.content.find("&", continue_arg_begin); |
| 169 const std::string continue_url = request.content.substr( |
| 170 continue_arg_begin, continue_arg_end - continue_arg_begin); |
| 171 http_response->set_code(test_server::SUCCESS); |
| 172 const std::string redirect_js = |
| 173 "document.location.href = unescape('" + continue_url + "');"; |
| 174 http_response->set_content( |
| 175 "<HTML><HEAD><SCRIPT>\n" + redirect_js + "\n</SCRIPT></HEAD></HTML>"); |
| 176 http_response->set_content_type("text/html"); |
| 177 } else { |
| 178 NOTREACHED() << url.path(); |
| 179 } |
| 180 return http_response.Pass(); |
| 181 } |
| 182 |
| 183 scoped_ptr<TestContentBrowserClient> content_browser_client_; |
| 184 content::ContentBrowserClient* original_content_browser_client_; |
| 185 std::string service_login_response_; |
| 186 HttpServer* test_server_; // cant use scoped_ptr because destructor |
| 187 // needs UI thread. |
| 188 }; |
| 189 |
| 190 IN_PROC_BROWSER_TEST_F(OobeTest, NewUser) { |
| 191 chromeos::WizardController::SkipPostLoginScreensForTesting(); |
| 192 chromeos::WizardController* wizard_controller = |
| 193 chromeos::WizardController::default_controller(); |
| 194 CHECK(wizard_controller); |
| 195 wizard_controller->SkipToLoginForTesting(); |
| 196 |
| 197 scoped_refptr<content::MessageLoopRunner> runner = |
| 198 new content::MessageLoopRunner; |
| 199 content_browser_client_->browser_main_extra_parts_->set_quit_task( |
| 200 runner->QuitClosure()); |
| 201 runner->Run(); |
| 202 } |
| 203 |
| 204 } |
| OLD | NEW |