| 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 "content/public/browser/notification_observer.h" |
| 25 #include "content/public/browser/notification_registrar.h" |
| 26 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/test/test_utils.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 |
| 31 using namespace google_apis; |
| 32 using namespace google_apis::test_server; |
| 33 |
| 34 namespace { |
| 35 |
| 36 // Used to add an observer to NotificationService after it's created. |
| 37 class TestBrowserMainExtraParts |
| 38 : public ChromeBrowserMainExtraParts, |
| 39 public content::NotificationObserver { |
| 40 public: |
| 41 TestBrowserMainExtraParts() {} |
| 42 virtual ~TestBrowserMainExtraParts() {} |
| 43 |
| 44 // ChromeBrowserMainExtraParts implementation. |
| 45 virtual void PreEarlyInitialization() OVERRIDE { |
| 46 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
| 47 content::NotificationService::AllSources()); |
| 48 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
| 49 content::NotificationService::AllSources()); |
| 50 } |
| 51 |
| 52 void set_quit_task(const base::Closure& quit_task) { quit_task_ = quit_task; } |
| 53 |
| 54 private: |
| 55 // Overridden from content::NotificationObserver: |
| 56 virtual void Observe(int type, |
| 57 const content::NotificationSource& source, |
| 58 const content::NotificationDetails& details) OVERRIDE { |
| 59 if (type == chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE) { |
| 60 LOG(INFO) << "NOTIFICATION_LOGIN_WEBUI_VISIBLE"; |
| 61 chromeos::ExistingUserController* controller = |
| 62 chromeos::ExistingUserController::current_controller(); |
| 63 CHECK(controller); |
| 64 chromeos::WebUILoginDisplay* webui_login_display = |
| 65 static_cast<chromeos::WebUILoginDisplay*>( |
| 66 controller->login_display()); |
| 67 CHECK(webui_login_display); |
| 68 webui_login_display->ShowSigninScreenForCreds("username", "password"); |
| 69 } else if (type == chrome::NOTIFICATION_SESSION_STARTED) { |
| 70 LOG(INFO) << "chrome::NOTIFICATION_SESSION_STARTED"; |
| 71 quit_task_.Run(); |
| 72 } else { |
| 73 NOTREACHED(); |
| 74 } |
| 75 } |
| 76 |
| 77 content::NotificationRegistrar registrar_; |
| 78 base::Closure quit_task_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(TestBrowserMainExtraParts); |
| 81 }; |
| 82 |
| 83 class TestContentBrowserClient : public chrome::ChromeContentBrowserClient { |
| 84 public: |
| 85 TestContentBrowserClient() {} |
| 86 virtual ~TestContentBrowserClient() {} |
| 87 |
| 88 virtual content::BrowserMainParts* CreateBrowserMainParts( |
| 89 const content::MainFunctionParams& parameters) OVERRIDE { |
| 90 ChromeBrowserMainParts* main_parts = static_cast<ChromeBrowserMainParts*>( |
| 91 ChromeContentBrowserClient::CreateBrowserMainParts(parameters)); |
| 92 |
| 93 browser_main_extra_parts_ = new TestBrowserMainExtraParts(); |
| 94 main_parts->AddParts(browser_main_extra_parts_); |
| 95 return main_parts; |
| 96 } |
| 97 |
| 98 TestBrowserMainExtraParts* browser_main_extra_parts_; |
| 99 |
| 100 private: |
| 101 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient); |
| 102 }; |
| 103 |
| 104 const base::FilePath kServiceLogin("chromeos/service_login.html"); |
| 105 |
| 106 class OobeTest : public chromeos::CrosInProcessBrowserTest { |
| 107 protected: |
| 108 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 109 command_line->AppendSwitch(switches::kLoginManager); |
| 110 command_line->AppendSwitch(switches::kForceLoginManagerInTests); |
| 111 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); |
| 112 command_line->AppendSwitchASCII(switches::kAuthExtensionPath, "gaia_auth"); |
| 113 } |
| 114 |
| 115 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 116 // host_resolver()->AddRule("*", "127.0.0.1"); -- work in progress! |
| 117 content_browser_client_.reset(new TestContentBrowserClient()); |
| 118 original_content_browser_client_ = content::GetContentClient()->browser(); |
| 119 content::GetContentClient()->set_browser_for_testing( |
| 120 content_browser_client_.get()); |
| 121 base::FilePath test_data_dir; |
| 122 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 123 CHECK(file_util::ReadFileToString(test_data_dir.Append(kServiceLogin), |
| 124 &service_login_response_)); |
| 125 } |
| 126 |
| 127 scoped_ptr<TestContentBrowserClient> content_browser_client_; |
| 128 content::ContentBrowserClient* original_content_browser_client_; |
| 129 |
| 130 public: |
| 131 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { |
| 132 GURL url = test_server_->GetURL(request.relative_url); |
| 133 LOG(WARNING) << "Http request: " << url.spec(); |
| 134 |
| 135 scoped_ptr<HttpResponse> http_response(new HttpResponse()); |
| 136 if (url.path() == "/ServiceLogin") { |
| 137 http_response->set_code(test_server::SUCCESS); |
| 138 http_response->set_content(service_login_response_); |
| 139 http_response->set_content_type("text/html"); |
| 140 } else if (url.path() == "/ServiceLoginAuth") { |
| 141 int continue_arg_begin = request.content.find("continue=") + 9; |
| 142 int continue_arg_end = request.content.find("&", continue_arg_begin); |
| 143 const std::string continue_url = request.content.substr( |
| 144 continue_arg_begin, continue_arg_end - continue_arg_begin); |
| 145 http_response->set_code(test_server::SUCCESS); |
| 146 const std::string redirect_js = |
| 147 "document.location.href = unescape('" + continue_url + "');"; |
| 148 http_response->set_content( |
| 149 "<HTML><HEAD><SCRIPT>\n" + redirect_js + "\n</SCRIPT></HEAD></HTML>"); |
| 150 http_response->set_content_type("text/html"); |
| 151 } else { |
| 152 NOTREACHED() << url.path(); |
| 153 } |
| 154 return http_response.Pass(); |
| 155 } |
| 156 |
| 157 HttpServer* test_server_; |
| 158 std::string service_login_response_; |
| 159 }; |
| 160 |
| 161 IN_PROC_BROWSER_TEST_F(OobeTest, NewUser) { |
| 162 test_server_ = new HttpServer(); |
| 163 CHECK(test_server_->InitializeAndWaitUntilReady()); |
| 164 test_server_->RegisterRequestHandler( |
| 165 base::Bind(&OobeTest::HandleRequest, base::Unretained(this))); |
| 166 LOG(INFO) << "Set up http server at " << test_server_->base_url(); |
| 167 |
| 168 chromeos::WizardController::SkipPostLoginScreensForTesting(); |
| 169 chromeos::WizardController* wizard_controller = |
| 170 chromeos::WizardController::default_controller(); |
| 171 CHECK(wizard_controller); |
| 172 wizard_controller->SkipToLoginForTesting(); |
| 173 |
| 174 scoped_refptr<content::MessageLoopRunner> runner = |
| 175 new content::MessageLoopRunner; |
| 176 content_browser_client_->browser_main_extra_parts_->set_quit_task( |
| 177 runner->QuitClosure()); |
| 178 runner->Run(); |
| 179 |
| 180 LOG(INFO) << "Stopping the http server."; |
| 181 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI); |
| 182 test_server_->ShutdownAndWaitUntilComplete(); |
| 183 } |
| 184 |
| 185 } |
| OLD | NEW |