Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / signin / inline_login_ui_browsertest.cc
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.
4
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"
26
27 using ::testing::_;
28
29 namespace {
30
31 struct ContentInfo {
32   ContentInfo(int pid, content::StoragePartition* storage_partition) {
33     this->pid = pid;
34     this->storage_partition = storage_partition;
35   }
36
37   int pid;
38   content::StoragePartition* storage_partition;
39 };
40
41 ContentInfo NavigateAndGetInfo(
42     Browser* browser,
43     const GURL& url,
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());
52 }
53
54 // Returns a new WebUI object for the WebContents from |arg0|.
55 ACTION(ReturnNewWebUI) {
56   return new content::WebUIController(arg0);
57 }
58
59 // Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
60 // not called as expected.
61 class FooWebUIProvider
62     : public TestChromeWebUIControllerFactory::WebUIProvider {
63  public:
64   MOCK_METHOD2(NewWebUI, content::WebUIController*(content::WebUI* web_ui,
65                                                    const GURL& url));
66 };
67
68 const char kFooWebUIURL[] = "chrome://foo/";
69
70 }  // namespace
71
72 class InlineLoginUIBrowserTest : public InProcessBrowserTest {
73  public:
74   InlineLoginUIBrowserTest() {}
75 };
76
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")));
81
82   ContentInfo info1 =
83       NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
84   ContentInfo info2 =
85       NavigateAndGetInfo(browser(),
86                          signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
87                          CURRENT_TAB);
88   NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
89   ContentInfo info3 =
90       NavigateAndGetInfo(browser(),
91                          signin::GetPromoURL( signin::SOURCE_START_PAGE, false),
92                          NEW_FOREGROUND_TAB);
93
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);
98 }
99
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!")));
107
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);
111
112   ContentInfo info1 =
113       NavigateAndGetInfo(browser(), test_url_1, CURRENT_TAB);
114   ContentInfo info2 =
115       NavigateAndGetInfo(browser(), test_url_2, CURRENT_TAB);
116   ContentInfo info3 =
117       NavigateAndGetInfo(browser(),
118                          signin::GetPromoURL( signin::SOURCE_START_PAGE, false),
119                          CURRENT_TAB);
120
121   ASSERT_EQ(info1.pid, info2.pid);
122   ASSERT_NE(info1.pid, info3.pid);
123 }
124
125 class InlineLoginUISafeIframeBrowserTest : public InProcessBrowserTest {
126  public:
127   FooWebUIProvider& foo_provider() { return foo_provider_; }
128
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');"
138         "};"
139         "if (inline.login.isAuthReady())"
140         "  handler();"
141         "else"
142         "  inline.login.getAuthExtHost().addEventListener('ready', handler);"));
143
144     std::string message;
145     do {
146       ASSERT_TRUE(message_queue.WaitForMessage(&message));
147     } while (message != "\"ready\"");
148   }
149
150  private:
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_);
158   }
159
160   virtual void TearDownOnMainThread() OVERRIDE {
161     test_factory_->RemoveFactoryOverride(GURL(kFooWebUIURL).host());
162     content::WebUIControllerFactory::UnregisterFactoryForTesting(
163         test_factory_.get());
164     test_factory_.reset();
165   }
166
167   FooWebUIProvider foo_provider_;
168   scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
169 };
170
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));
178 }
179
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);
187 }
188
189 // Flaky on CrOS, http://crbug.com/364759.
190 #if defined(OS_CHROMEOS)
191 #define MAYBE_TopFrameNavigationDisallowed DISABLED_TopFrameNavigationDisallowed
192 #else
193 #define MAYBE_TopFrameNavigationDisallowed TopFrameNavigationDisallowed
194 #endif
195
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);
207   WaitUntilUIReady();
208
209   content::WebContents* contents =
210       browser()->tab_strip_model()->GetActiveWebContents();
211   EXPECT_EQ(url, contents->GetVisibleURL());
212
213   content::NavigationController& controller = contents->GetController();
214   EXPECT_TRUE(controller.GetPendingEntry() == NULL);
215 }
216
217 // Flaky on CrOS, http://crbug.com/364759.
218 #if defined(OS_CHROMEOS)
219 #define MAYBE_NavigationToOtherChromeURLDisallowed \
220     DISABLED_NavigationToOtherChromeURLDisallowed
221 #else
222 #define MAYBE_NavigationToOtherChromeURLDisallowed \
223     NavigationToOtherChromeURLDisallowed
224 #endif
225
226 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
227     MAYBE_NavigationToOtherChromeURLDisallowed) {
228   ui_test_utils::NavigateToURL(
229       browser(), signin::GetPromoURL(signin::SOURCE_START_PAGE, false));
230   WaitUntilUIReady();
231
232   content::WebContents* contents =
233       browser()->tab_strip_model()->GetActiveWebContents();
234   ASSERT_TRUE(content::ExecuteScript(
235       contents, "window.location.href = 'chrome://foo'"));
236
237   content::TestNavigationObserver navigation_observer(contents, 1);
238   navigation_observer.Wait();
239
240   EXPECT_EQ(GURL("about:blank"), contents->GetVisibleURL());
241 }