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 "chrome/browser/signin/signin_promo.h"
6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/tabs/tab_strip_model.h"
8 #include "chrome/common/chrome_switches.h"
9 #include "chrome/common/url_constants.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/session_storage_namespace.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_ui_controller.h"
19 #include "content/public/common/url_constants.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
29 ContentInfo(int pid, content::StoragePartition* storage_partition) {
31 this->storage_partition = storage_partition;
35 content::StoragePartition* storage_partition;
38 ContentInfo NavigateAndGetInfo(
41 WindowOpenDisposition disposition) {
42 ui_test_utils::NavigateToURLWithDisposition(
43 browser, url, disposition,
44 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
45 content::WebContents* contents =
46 browser->tab_strip_model()->GetActiveWebContents();
47 content::RenderProcessHost* process = contents->GetRenderProcessHost();
48 return ContentInfo(process->GetID(), process->GetStoragePartition());
51 // Returns a new WebUI object for the WebContents from |arg0|.
52 ACTION(ReturnNewWebUI) {
53 return new content::WebUIController(arg0);
56 // Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
57 // not called as expected.
58 class FooWebUIProvider
59 : public TestChromeWebUIControllerFactory::WebUIProvider {
61 MOCK_METHOD2(NewWebUI, content::WebUIController*(content::WebUI* web_ui,
65 const char kFooWebUIURL[] = "chrome://foo/";
69 class InlineLoginUIBrowserTest : public InProcessBrowserTest {
71 InlineLoginUIBrowserTest() {}
74 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, DifferentStorageId) {
75 GURL test_url = ui_test_utils::GetTestUrl(
76 base::FilePath(base::FilePath::kCurrentDirectory),
77 base::FilePath(FILE_PATH_LITERAL("title1.html")));
80 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
82 NavigateAndGetInfo(browser(),
83 signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
85 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
87 NavigateAndGetInfo(browser(),
88 signin::GetPromoURL( signin::SOURCE_START_PAGE, false),
91 // The info for signin should be the same.
92 ASSERT_EQ(info2.storage_partition, info3.storage_partition);
93 // The info for test_url and signin should be different.
94 ASSERT_NE(info1.storage_partition, info2.storage_partition);
97 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, OneProcessLimit) {
98 GURL test_url_1 = ui_test_utils::GetTestUrl(
99 base::FilePath(base::FilePath::kCurrentDirectory),
100 base::FilePath(FILE_PATH_LITERAL("title1.html")));
101 GURL test_url_2 = ui_test_utils::GetTestUrl(
102 base::FilePath(base::FilePath::kCurrentDirectory),
103 base::FilePath(FILE_PATH_LITERAL("data:text/html,Hello world!")));
105 // Even when the process limit is set to one, the signin process should
106 // still be given its own process and storage partition.
107 content::RenderProcessHost::SetMaxRendererProcessCount(1);
110 NavigateAndGetInfo(browser(), test_url_1, CURRENT_TAB);
112 NavigateAndGetInfo(browser(), test_url_2, CURRENT_TAB);
114 NavigateAndGetInfo(browser(),
115 signin::GetPromoURL( signin::SOURCE_START_PAGE, false),
118 ASSERT_EQ(info1.pid, info2.pid);
119 ASSERT_NE(info1.pid, info3.pid);
122 class InlineLoginUISafeIframeBrowserTest : public InProcessBrowserTest {
124 FooWebUIProvider& foo_provider() { return foo_provider_; }
127 virtual void SetUpOnMainThread() OVERRIDE {
128 content::WebUIControllerFactory::UnregisterFactoryForTesting(
129 ChromeWebUIControllerFactory::GetInstance());
130 test_factory_.reset(new TestChromeWebUIControllerFactory);
131 content::WebUIControllerFactory::RegisterFactory(test_factory_.get());
132 test_factory_->AddFactoryOverride(
133 GURL(kFooWebUIURL).host(), &foo_provider_);
136 virtual void CleanUpOnMainThread() OVERRIDE {
137 test_factory_->RemoveFactoryOverride(GURL(kFooWebUIURL).host());
138 content::WebUIControllerFactory::UnregisterFactoryForTesting(
139 test_factory_.get());
140 test_factory_.reset();
143 FooWebUIProvider foo_provider_;
144 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
147 // Make sure that the foo webui handler is working properly and that it gets
148 // created when navigated to normally.
149 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, Basic) {
150 const GURL kUrl(kFooWebUIURL);
151 EXPECT_CALL(foo_provider(), NewWebUI(_, ::testing::Eq(kUrl)))
152 .WillOnce(ReturnNewWebUI());
153 ui_test_utils::NavigateToURL(browser(), GURL(kFooWebUIURL));
156 // Make sure that the foo webui handler does not get created when we try to
157 // load it inside the iframe of the login ui.
158 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, NoWebUIInIframe) {
159 GURL url = signin::GetPromoURL(signin::SOURCE_START_PAGE, false).
160 Resolve("?source=0&frameUrl=chrome://foo");
161 EXPECT_CALL(foo_provider(), NewWebUI(_, _)).Times(0);
162 ui_test_utils::NavigateToURL(browser(), url);