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->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); | |
119 command_line->AppendSwitchASCII(switches::kAuthExtensionPath, "gaia_auth"); | |
120 } | |
121 | |
122 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
123 content_browser_client_.reset(new TestContentBrowserClient()); | |
124 original_content_browser_client_ = content::SetBrowserClientForTesting( | |
125 content_browser_client_.get()); | |
126 base::FilePath test_data_dir; | |
127 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | |
128 CHECK(file_util::ReadFileToString(test_data_dir.Append(kServiceLogin), | |
129 &service_login_response_)); | |
130 } | |
131 | |
132 virtual void SetUpOnMainThread() OVERRIDE { | |
133 test_server_ = new HttpServer(); // Constructor wants UI thread. | |
134 CHECK(test_server_->InitializeAndWaitUntilReady()); | |
135 test_server_->RegisterRequestHandler( | |
136 base::Bind(&OobeTest::HandleRequest, base::Unretained(this))); | |
137 LOG(INFO) << "Set up http server at " << test_server_->base_url(); | |
138 | |
139 const std::string gaia_url = | |
140 "http://localhost:" + test_server_->base_url().port(); | |
Nikita (slow)
2013/04/08 19:56:41
This won't work as there's a hardcoded localhost:8
glotov
2013/04/10 13:03:24
Done.
| |
141 content_browser_client_->browser_main_extra_parts_->set_gaia_url(gaia_url); | |
142 } | |
143 | |
144 virtual void CleanUpOnMainThread() OVERRIDE { | |
145 LOG(INFO) << "Stopping the http server."; | |
146 test_server_->ShutdownAndWaitUntilComplete(); | |
147 delete test_server_; // Destructor wants UI thread. | |
148 } | |
149 | |
150 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { | |
151 GURL url = test_server_->GetURL(request.relative_url); | |
152 LOG(INFO) << "Http request: " << url.spec(); | |
153 | |
154 scoped_ptr<HttpResponse> http_response(new HttpResponse()); | |
155 if (url.path() == "/ServiceLogin") { | |
156 http_response->set_code(test_server::SUCCESS); | |
157 http_response->set_content(service_login_response_); | |
158 http_response->set_content_type("text/html"); | |
159 } else if (url.path() == "/ServiceLoginAuth") { | |
160 LOG(INFO) << "Params: " << request.content; | |
161 static const char kContinueParam[] = "continue="; | |
162 int continue_arg_begin = request.content.find(kContinueParam) + arraysize( kContinueParam) - 1; | |
163 int continue_arg_end = request.content.find("&", continue_arg_begin); | |
164 const std::string continue_url = request.content.substr( | |
165 continue_arg_begin, continue_arg_end - continue_arg_begin); | |
166 http_response->set_code(test_server::SUCCESS); | |
167 const std::string redirect_js = | |
168 "document.location.href = unescape('" + continue_url + "');"; | |
169 http_response->set_content( | |
170 "<HTML><HEAD><SCRIPT>\n" + redirect_js + "\n</SCRIPT></HEAD></HTML>"); | |
171 http_response->set_content_type("text/html"); | |
172 } else { | |
173 NOTREACHED() << url.path(); | |
174 } | |
175 return http_response.Pass(); | |
176 } | |
177 | |
178 scoped_ptr<TestContentBrowserClient> content_browser_client_; | |
179 content::ContentBrowserClient* original_content_browser_client_; | |
180 std::string service_login_response_; | |
181 HttpServer* test_server_; // cant use scoped_ptr because destructor | |
182 // needs UI thread. | |
183 }; | |
184 | |
185 IN_PROC_BROWSER_TEST_F(OobeTest, NewUser) { | |
186 chromeos::WizardController::SkipPostLoginScreensForTesting(); | |
187 chromeos::WizardController* wizard_controller = | |
188 chromeos::WizardController::default_controller(); | |
189 CHECK(wizard_controller); | |
190 wizard_controller->SkipToLoginForTesting(); | |
191 | |
192 scoped_refptr<content::MessageLoopRunner> runner = | |
193 new content::MessageLoopRunner; | |
194 content_browser_client_->browser_main_extra_parts_->set_quit_task( | |
195 runner->QuitClosure()); | |
196 runner->Run(); | |
197 } | |
198 | |
199 } | |
OLD | NEW |