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 "content/public/test/test_navigation_observer.h"
22 #include "net/base/url_util.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
32 ContentInfo(int pid, content::StoragePartition* storage_partition) {
34 this->storage_partition = storage_partition;
38 content::StoragePartition* storage_partition;
41 ContentInfo NavigateAndGetInfo(
44 WindowOpenDisposition disposition) {
45 ui_test_utils::NavigateToURLWithDisposition(
46 browser, url, disposition,
47 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
48 content::WebContents* contents =
49 browser->tab_strip_model()->GetActiveWebContents();
50 content::RenderProcessHost* process = contents->GetRenderProcessHost();
51 return ContentInfo(process->GetID(), process->GetStoragePartition());
54 // Returns a new WebUI object for the WebContents from |arg0|.
55 ACTION(ReturnNewWebUI) {
56 return new content::WebUIController(arg0);
59 // Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
60 // not called as expected.
61 class FooWebUIProvider
62 : public TestChromeWebUIControllerFactory::WebUIProvider {
64 MOCK_METHOD2(NewWebUI, content::WebUIController*(content::WebUI* web_ui,
68 const char kFooWebUIURL[] = "chrome://foo/";
72 class InlineLoginUIBrowserTest : public InProcessBrowserTest {
74 InlineLoginUIBrowserTest() {}
77 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, DifferentStorageId) {
78 GURL test_url = ui_test_utils::GetTestUrl(
79 base::FilePath(base::FilePath::kCurrentDirectory),
80 base::FilePath(FILE_PATH_LITERAL("title1.html")));
83 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
85 NavigateAndGetInfo(browser(),
86 signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
88 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
90 NavigateAndGetInfo(browser(),
91 signin::GetPromoURL( signin::SOURCE_START_PAGE, false),
94 // The info for signin should be the same.
95 ASSERT_EQ(info2.storage_partition, info3.storage_partition);
96 // The info for test_url and signin should be different.
97 ASSERT_NE(info1.storage_partition, info2.storage_partition);
100 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, OneProcessLimit) {
101 GURL test_url_1 = ui_test_utils::GetTestUrl(
102 base::FilePath(base::FilePath::kCurrentDirectory),
103 base::FilePath(FILE_PATH_LITERAL("title1.html")));
104 GURL test_url_2 = ui_test_utils::GetTestUrl(
105 base::FilePath(base::FilePath::kCurrentDirectory),
106 base::FilePath(FILE_PATH_LITERAL("data:text/html,Hello world!")));
108 // Even when the process limit is set to one, the signin process should
109 // still be given its own process and storage partition.
110 content::RenderProcessHost::SetMaxRendererProcessCount(1);
113 NavigateAndGetInfo(browser(), test_url_1, CURRENT_TAB);
115 NavigateAndGetInfo(browser(), test_url_2, CURRENT_TAB);
117 NavigateAndGetInfo(browser(),
118 signin::GetPromoURL( signin::SOURCE_START_PAGE, false),
121 ASSERT_EQ(info1.pid, info2.pid);
122 ASSERT_NE(info1.pid, info3.pid);
125 class InlineLoginUISafeIframeBrowserTest : public InProcessBrowserTest {
127 FooWebUIProvider& foo_provider() { return foo_provider_; }
129 void WaitUntilUIReady() {
130 content::DOMMessageQueue message_queue;
131 ASSERT_TRUE(content::ExecuteScript(
132 browser()->tab_strip_model()->GetActiveWebContents(),
133 "if (!inline.login.getAuthExtHost())"
134 " inline.login.initialize();"
135 "var handler = function() {"
136 " window.domAutomationController.setAutomationId(0);"
137 " window.domAutomationController.send('ready');"
139 "if (inline.login.isAuthReady())"
142 " inline.login.getAuthExtHost().addEventListener('ready', handler);"));
146 ASSERT_TRUE(message_queue.WaitForMessage(&message));
147 } while (message != "\"ready\"");
151 virtual void SetUpOnMainThread() OVERRIDE {
152 content::WebUIControllerFactory::UnregisterFactoryForTesting(
153 ChromeWebUIControllerFactory::GetInstance());
154 test_factory_.reset(new TestChromeWebUIControllerFactory);
155 content::WebUIControllerFactory::RegisterFactory(test_factory_.get());
156 test_factory_->AddFactoryOverride(
157 GURL(kFooWebUIURL).host(), &foo_provider_);
160 virtual void TearDownOnMainThread() OVERRIDE {
161 test_factory_->RemoveFactoryOverride(GURL(kFooWebUIURL).host());
162 content::WebUIControllerFactory::UnregisterFactoryForTesting(
163 test_factory_.get());
164 test_factory_.reset();
167 FooWebUIProvider foo_provider_;
168 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
171 // Make sure that the foo webui handler is working properly and that it gets
172 // created when navigated to normally.
173 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, Basic) {
174 const GURL kUrl(kFooWebUIURL);
175 EXPECT_CALL(foo_provider(), NewWebUI(_, ::testing::Eq(kUrl)))
176 .WillOnce(ReturnNewWebUI());
177 ui_test_utils::NavigateToURL(browser(), GURL(kFooWebUIURL));
180 // Make sure that the foo webui handler does not get created when we try to
181 // load it inside the iframe of the login ui.
182 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, NoWebUIInIframe) {
183 GURL url = signin::GetPromoURL(signin::SOURCE_START_PAGE, false).
184 Resolve("?source=0&frameUrl=chrome://foo");
185 EXPECT_CALL(foo_provider(), NewWebUI(_, _)).Times(0);
186 ui_test_utils::NavigateToURL(browser(), url);
189 // Flaky on CrOS, http://crbug.com/364759.
190 #if defined(OS_CHROMEOS)
191 #define MAYBE_TopFrameNavigationDisallowed DISABLED_TopFrameNavigationDisallowed
193 #define MAYBE_TopFrameNavigationDisallowed TopFrameNavigationDisallowed
196 // Make sure that the gaia iframe cannot trigger top-frame navigation.
197 // TODO(guohui): flaky on trybot crbug/364759.
198 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
199 MAYBE_TopFrameNavigationDisallowed) {
200 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
201 // Loads into gaia iframe a web page that attempts to deframe on load.
202 GURL deframe_url(embedded_test_server()->GetURL("/login/deframe.html"));
203 GURL url(net::AppendOrReplaceQueryParameter(
204 signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
205 "frameUrl", deframe_url.spec()));
206 ui_test_utils::NavigateToURL(browser(), url);
209 content::WebContents* contents =
210 browser()->tab_strip_model()->GetActiveWebContents();
211 EXPECT_EQ(url, contents->GetVisibleURL());
213 content::NavigationController& controller = contents->GetController();
214 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
217 // Flaky on CrOS, http://crbug.com/364759.
218 #if defined(OS_CHROMEOS)
219 #define MAYBE_NavigationToOtherChromeURLDisallowed \
220 DISABLED_NavigationToOtherChromeURLDisallowed
222 #define MAYBE_NavigationToOtherChromeURLDisallowed \
223 NavigationToOtherChromeURLDisallowed
226 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
227 MAYBE_NavigationToOtherChromeURLDisallowed) {
228 ui_test_utils::NavigateToURL(
229 browser(), signin::GetPromoURL(signin::SOURCE_START_PAGE, false));
232 content::WebContents* contents =
233 browser()->tab_strip_model()->GetActiveWebContents();
234 ASSERT_TRUE(content::ExecuteScript(
235 contents, "window.location.href = 'chrome://foo'"));
237 content::TestNavigationObserver navigation_observer(contents, 1);
238 navigation_observer.Wait();
240 EXPECT_EQ(GURL("about:blank"), contents->GetVisibleURL());