Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / signin / inline_login_ui_browsertest.cc
blob3a7b0ed79766835e47ad2a19a54a43ee313e97f5
1 // Copyright 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.
5 #include "base/command_line.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/prefs/scoped_user_pref_update.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/content_settings/cookie_settings.h"
10 #include "chrome/browser/signin/signin_manager_factory.h"
11 #include "chrome/browser/signin/signin_promo.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h"
15 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
16 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
18 #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/test_browser_window.h"
25 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h"
26 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/ui_test_utils.h"
28 #include "components/guest_view/browser/guest_view_manager.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "components/signin/core/common/profile_management_switches.h"
31 #include "components/signin/core/common/signin_pref_names.h"
32 #include "content/public/browser/render_frame_host.h"
33 #include "content/public/browser/render_process_host.h"
34 #include "content/public/browser/session_storage_namespace.h"
35 #include "content/public/browser/storage_partition.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/browser/web_ui_controller.h"
38 #include "content/public/common/url_constants.h"
39 #include "content/public/test/browser_test_utils.h"
40 #include "content/public/test/test_navigation_observer.h"
41 #include "google_apis/gaia/fake_gaia.h"
42 #include "google_apis/gaia/gaia_switches.h"
43 #include "net/base/url_util.h"
44 #include "net/test/embedded_test_server/embedded_test_server.h"
45 #include "net/test/embedded_test_server/http_request.h"
46 #include "net/test/embedded_test_server/http_response.h"
47 #include "testing/gmock/include/gmock/gmock.h"
48 #include "testing/gtest/include/gtest/gtest.h"
49 #include "ui/base/l10n/l10n_util.h"
51 using ::testing::_;
52 using ::testing::AtLeast;
53 using ::testing::Invoke;
54 using ::testing::InvokeWithoutArgs;
55 using ::testing::Return;
57 using guest_view::GuestViewManager;
58 using login_ui_test_utils::ExecuteJsToSigninInSigninFrame;
59 using login_ui_test_utils::WaitUntilUIReady;
61 namespace {
63 struct ContentInfo {
64 ContentInfo(content::WebContents* contents,
65 int pid,
66 content::StoragePartition* storage_partition) {
67 this->contents = contents;
68 this->pid = pid;
69 this->storage_partition = storage_partition;
72 content::WebContents* contents;
73 int pid;
74 content::StoragePartition* storage_partition;
77 ContentInfo NavigateAndGetInfo(
78 Browser* browser,
79 const GURL& url,
80 WindowOpenDisposition disposition) {
81 ui_test_utils::NavigateToURLWithDisposition(
82 browser, url, disposition,
83 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
84 content::WebContents* contents =
85 browser->tab_strip_model()->GetActiveWebContents();
86 content::RenderProcessHost* process = contents->GetRenderProcessHost();
87 return ContentInfo(contents, process->GetID(),
88 process->GetStoragePartition());
91 // Returns a new WebUI object for the WebContents from |arg0|.
92 ACTION(ReturnNewWebUI) {
93 return new content::WebUIController(arg0);
96 // Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
97 // not called as expected.
98 class FooWebUIProvider
99 : public TestChromeWebUIControllerFactory::WebUIProvider {
100 public:
101 MOCK_METHOD2(NewWebUI, content::WebUIController*(content::WebUI* web_ui,
102 const GURL& url));
105 class MockLoginUIObserver : public LoginUIService::Observer {
106 public:
107 MOCK_METHOD0(OnUntrustedLoginUIShown, void());
110 const char kFooWebUIURL[] = "chrome://foo/";
112 bool AddToSet(std::set<content::WebContents*>* set,
113 content::WebContents* web_contents) {
114 set->insert(web_contents);
115 return false;
118 } // namespace
120 class InlineLoginUIBrowserTest : public InProcessBrowserTest {
121 public:
122 InlineLoginUIBrowserTest() {}
124 void SetUpSigninManager(const std::string& username);
125 void EnableSigninAllowed(bool enable);
126 void EnableOneClick(bool enable);
127 void AddEmailToOneClickRejectedList(const std::string& email);
128 void AllowSigninCookies(bool enable);
129 void SetAllowedUsernamePattern(const std::string& pattern);
131 protected:
132 content::WebContents* web_contents() { return nullptr; }
135 void InlineLoginUIBrowserTest::SetUpSigninManager(const std::string& username) {
136 if (username.empty())
137 return;
139 SigninManagerBase* signin_manager =
140 SigninManagerFactory::GetForProfile(browser()->profile());
141 signin_manager->SetAuthenticatedAccountInfo(username, username);
144 void InlineLoginUIBrowserTest::EnableSigninAllowed(bool enable) {
145 PrefService* pref_service = browser()->profile()->GetPrefs();
146 pref_service->SetBoolean(prefs::kSigninAllowed, enable);
149 void InlineLoginUIBrowserTest::EnableOneClick(bool enable) {
150 PrefService* pref_service = browser()->profile()->GetPrefs();
151 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable);
154 void InlineLoginUIBrowserTest::AddEmailToOneClickRejectedList(
155 const std::string& email) {
156 PrefService* pref_service = browser()->profile()->GetPrefs();
157 ListPrefUpdate updater(pref_service,
158 prefs::kReverseAutologinRejectedEmailList);
159 updater->AppendIfNotPresent(new base::StringValue(email));
162 void InlineLoginUIBrowserTest::AllowSigninCookies(bool enable) {
163 CookieSettings* cookie_settings =
164 CookieSettings::Factory::GetForProfile(browser()->profile()).get();
165 cookie_settings->SetDefaultCookieSetting(enable ? CONTENT_SETTING_ALLOW
166 : CONTENT_SETTING_BLOCK);
169 void InlineLoginUIBrowserTest::SetAllowedUsernamePattern(
170 const std::string& pattern) {
171 PrefService* local_state = g_browser_process->local_state();
172 local_state->SetString(prefs::kGoogleServicesUsernamePattern, pattern);
175 #if defined(OS_LINUX) || defined(OS_WIN)
176 // crbug.com/422868
177 #define MAYBE_DifferentStorageId DISABLED_DifferentStorageId
178 #else
179 #define MAYBE_DifferentStorageId DifferentStorageId
180 #endif
181 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, MAYBE_DifferentStorageId) {
182 if (switches::IsEnableWebviewBasedSignin()) {
183 ContentInfo info = NavigateAndGetInfo(
184 browser(),
185 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
186 CURRENT_TAB);
187 WaitUntilUIReady(browser());
189 // Make sure storage partition of embedded webview is different from
190 // parent.
191 std::set<content::WebContents*> set;
192 GuestViewManager* manager = GuestViewManager::FromBrowserContext(
193 info.contents->GetBrowserContext());
194 manager->ForEachGuest(info.contents, base::Bind(&AddToSet, &set));
195 ASSERT_EQ(1u, set.size());
196 content::WebContents* webview_contents = *set.begin();
197 content::RenderProcessHost* process =
198 webview_contents->GetRenderProcessHost();
199 ASSERT_NE(info.pid, process->GetID());
200 ASSERT_NE(info.storage_partition, process->GetStoragePartition());
201 } else {
202 GURL test_url = ui_test_utils::GetTestUrl(
203 base::FilePath(base::FilePath::kCurrentDirectory),
204 base::FilePath(FILE_PATH_LITERAL("title1.html")));
206 ContentInfo info1 =
207 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
208 ContentInfo info2 = NavigateAndGetInfo(
209 browser(),
210 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
211 CURRENT_TAB);
212 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
213 ContentInfo info3 = NavigateAndGetInfo(
214 browser(),
215 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
216 NEW_FOREGROUND_TAB);
218 // The info for signin should be the same.
219 ASSERT_EQ(info2.storage_partition, info3.storage_partition);
220 // The info for test_url and signin should be different.
221 ASSERT_NE(info1.storage_partition, info2.storage_partition);
225 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, OneProcessLimit) {
226 GURL test_url_1 = ui_test_utils::GetTestUrl(
227 base::FilePath(base::FilePath::kCurrentDirectory),
228 base::FilePath(FILE_PATH_LITERAL("title1.html")));
229 GURL test_url_2 = ui_test_utils::GetTestUrl(
230 base::FilePath(base::FilePath::kCurrentDirectory),
231 base::FilePath(FILE_PATH_LITERAL("data:text/html,Hello world!")));
233 // Even when the process limit is set to one, the signin process should
234 // still be given its own process and storage partition.
235 content::RenderProcessHost::SetMaxRendererProcessCount(1);
237 ContentInfo info1 =
238 NavigateAndGetInfo(browser(), test_url_1, CURRENT_TAB);
239 ContentInfo info2 =
240 NavigateAndGetInfo(browser(), test_url_2, CURRENT_TAB);
241 ContentInfo info3 = NavigateAndGetInfo(
242 browser(),
243 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
244 CURRENT_TAB);
246 ASSERT_EQ(info1.pid, info2.pid);
247 ASSERT_NE(info1.pid, info3.pid);
250 #if !defined(OS_CHROMEOS)
252 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, CanOfferNoProfile) {
253 std::string error_message;
254 EXPECT_FALSE(InlineLoginHandlerImpl::CanOffer(
255 NULL, InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
256 "12345", "user@gmail.com", &error_message));
257 EXPECT_EQ("", error_message);
260 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, CanOffer) {
261 EnableOneClick(true);
262 EXPECT_TRUE(InlineLoginHandlerImpl::CanOffer(
263 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
264 "12345", "user@gmail.com", NULL));
266 EnableOneClick(false);
268 std::string error_message;
270 EXPECT_TRUE(InlineLoginHandlerImpl::CanOffer(
271 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
272 "12345", "user@gmail.com", &error_message));
275 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, CanOfferProfileConnected) {
276 SetUpSigninManager("foo@gmail.com");
277 EnableSigninAllowed(true);
279 std::string error_message;
281 EXPECT_TRUE(InlineLoginHandlerImpl::CanOffer(
282 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
283 "12345", "foo@gmail.com", &error_message));
284 EXPECT_TRUE(InlineLoginHandlerImpl::CanOffer(
285 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
286 "12345", "foo", &error_message));
287 EXPECT_FALSE(InlineLoginHandlerImpl::CanOffer(
288 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
289 "12345", "user@gmail.com", &error_message));
290 EXPECT_EQ(l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
291 base::UTF8ToUTF16("foo@gmail.com")),
292 error_message);
295 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, CanOfferUsernameNotAllowed) {
296 SetAllowedUsernamePattern("*.google.com");
298 std::string error_message;
299 EXPECT_FALSE(InlineLoginHandlerImpl::CanOffer(
300 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
301 "12345", "foo@gmail.com", &error_message));
302 EXPECT_EQ(l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED),
303 error_message);
306 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, CanOfferWithRejectedEmail) {
307 EnableSigninAllowed(true);
309 AddEmailToOneClickRejectedList("foo@gmail.com");
310 AddEmailToOneClickRejectedList("user@gmail.com");
312 std::string error_message;
313 EXPECT_TRUE(InlineLoginHandlerImpl::CanOffer(
314 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
315 "12345", "foo@gmail.com", &error_message));
316 EXPECT_TRUE(InlineLoginHandlerImpl::CanOffer(
317 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
318 "12345", "user@gmail.com", &error_message));
321 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, CanOfferNoSigninCookies) {
322 AllowSigninCookies(false);
323 EnableSigninAllowed(true);
325 std::string error_message;
326 EXPECT_FALSE(InlineLoginHandlerImpl::CanOffer(
327 browser()->profile(), InlineLoginHandlerImpl::CAN_OFFER_FOR_ALL,
328 "12345", "user@gmail.com", &error_message));
329 EXPECT_EQ("", error_message);
332 #endif // OS_CHROMEOS
334 class InlineLoginUISafeIframeBrowserTest : public InProcessBrowserTest {
335 public:
336 FooWebUIProvider& foo_provider() { return foo_provider_; }
338 private:
339 void SetUp() override {
340 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
342 // EmbeddedTestServer spawns a thread to initialize socket.
343 // Stop IO thread in preparation for fork and exec.
344 embedded_test_server()->StopThread();
346 InProcessBrowserTest::SetUp();
349 void SetUpCommandLine(base::CommandLine* command_line) override {
350 const GURL& base_url = embedded_test_server()->base_url();
351 command_line->AppendSwitchASCII(::switches::kGaiaUrl, base_url.spec());
352 command_line->AppendSwitchASCII(::switches::kLsoUrl, base_url.spec());
353 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl,
354 base_url.spec());
357 void SetUpOnMainThread() override {
358 embedded_test_server()->RestartThreadAndListen();
360 content::WebUIControllerFactory::UnregisterFactoryForTesting(
361 ChromeWebUIControllerFactory::GetInstance());
362 test_factory_.reset(new TestChromeWebUIControllerFactory);
363 content::WebUIControllerFactory::RegisterFactory(test_factory_.get());
364 test_factory_->AddFactoryOverride(
365 GURL(kFooWebUIURL).host(), &foo_provider_);
368 void TearDownOnMainThread() override {
369 test_factory_->RemoveFactoryOverride(GURL(kFooWebUIURL).host());
370 content::WebUIControllerFactory::UnregisterFactoryForTesting(
371 test_factory_.get());
372 test_factory_.reset();
373 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
376 FooWebUIProvider foo_provider_;
377 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
380 // Make sure that the foo webui handler is working properly and that it gets
381 // created when navigated to normally.
382 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, Basic) {
383 const GURL kUrl(kFooWebUIURL);
384 EXPECT_CALL(foo_provider(), NewWebUI(_, ::testing::Eq(kUrl)))
385 .WillOnce(ReturnNewWebUI());
386 ui_test_utils::NavigateToURL(browser(), GURL(kFooWebUIURL));
389 // Make sure that the foo webui handler does not get created when we try to
390 // load it inside the iframe of the login ui.
391 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, NoWebUIInIframe) {
392 GURL url = signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false).
393 Resolve("?source=0&frameUrl=chrome://foo");
394 EXPECT_CALL(foo_provider(), NewWebUI(_, _)).Times(0);
395 ui_test_utils::NavigateToURL(browser(), url);
398 // Flaky on CrOS, http://crbug.com/364759.
399 #if defined(OS_CHROMEOS)
400 #define MAYBE_TopFrameNavigationDisallowed DISABLED_TopFrameNavigationDisallowed
401 #else
402 #define MAYBE_TopFrameNavigationDisallowed TopFrameNavigationDisallowed
403 #endif
405 // Make sure that the gaia iframe cannot trigger top-frame navigation.
406 // TODO(guohui): flaky on trybot crbug/364759.
407 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
408 MAYBE_TopFrameNavigationDisallowed) {
409 // Loads into gaia iframe a web page that attempts to deframe on load.
410 GURL deframe_url(embedded_test_server()->GetURL("/login/deframe.html"));
411 GURL url(net::AppendOrReplaceQueryParameter(
412 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
413 "frameUrl", deframe_url.spec()));
414 ui_test_utils::NavigateToURL(browser(), url);
415 WaitUntilUIReady(browser());
417 content::WebContents* contents =
418 browser()->tab_strip_model()->GetActiveWebContents();
419 EXPECT_EQ(url, contents->GetVisibleURL());
421 content::NavigationController& controller = contents->GetController();
422 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
425 // Flaky on CrOS, http://crbug.com/364759.
426 // Also flaky on Mac, http://crbug.com/442674.
427 // Also flaky on Linux which is just too flaky
428 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
429 DISABLED_NavigationToOtherChromeURLDisallowed) {
430 ui_test_utils::NavigateToURL(
431 browser(), signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false));
432 WaitUntilUIReady(browser());
434 content::WebContents* contents =
435 browser()->tab_strip_model()->GetActiveWebContents();
436 ASSERT_TRUE(content::ExecuteScript(
437 contents, "window.location.href = 'chrome://foo'"));
439 content::TestNavigationObserver navigation_observer(contents, 1);
440 navigation_observer.Wait();
442 EXPECT_EQ(GURL("about:blank"), contents->GetVisibleURL());
445 #if !defined(OS_CHROMEOS)
446 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
447 ConfirmationRequiredForNonsecureSignin) {
448 FakeGaia fake_gaia;
449 fake_gaia.Initialize();
451 embedded_test_server()->RegisterRequestHandler(
452 base::Bind(&FakeGaia::HandleRequest,
453 base::Unretained(&fake_gaia)));
454 fake_gaia.SetFakeMergeSessionParams(
455 "email@gmail.com", "fake-sid-cookie", "fake-lsid-cookie");
457 // Navigates to the Chrome signin page which loads the fake gaia auth page.
458 // Since the fake gaia auth page is served over HTTP, thus expects to see an
459 // untrusted signin confirmation dialog upon submitting credentials below.
460 ui_test_utils::NavigateToURL(
461 browser(), signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false));
462 WaitUntilUIReady(browser());
464 MockLoginUIObserver observer;
465 LoginUIServiceFactory::GetForProfile(browser()->profile())
466 ->AddObserver(&observer);
467 base::RunLoop run_loop;
468 EXPECT_CALL(observer, OnUntrustedLoginUIShown())
469 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
471 ExecuteJsToSigninInSigninFrame(browser(), "email@gmail.com", "password");
472 run_loop.Run();
473 base::MessageLoop::current()->RunUntilIdle();
475 #endif // OS_CHROMEOS