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