Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / ui / webui / signin / inline_login_ui_browsertest.cc
blob5ec0bbc7b95fae159e2174cb49f63cf042cb7d9b
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 "chrome/browser/signin/signin_promo.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
10 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
11 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
12 #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/test_browser_window.h"
17 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "components/signin/core/common/profile_management_switches.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/session_storage_namespace.h"
24 #include "content/public/browser/storage_partition.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_ui_controller.h"
27 #include "content/public/common/url_constants.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "content/public/test/test_navigation_observer.h"
30 #include "extensions/browser/guest_view/guest_view_manager.h"
31 #include "google_apis/gaia/fake_gaia.h"
32 #include "google_apis/gaia/gaia_switches.h"
33 #include "net/base/url_util.h"
34 #include "net/test/embedded_test_server/embedded_test_server.h"
35 #include "net/test/embedded_test_server/http_request.h"
36 #include "net/test/embedded_test_server/http_response.h"
37 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gtest/include/gtest/gtest.h"
40 using ::testing::_;
41 using ::testing::Invoke;
42 using ::testing::InvokeWithoutArgs;
44 using login_ui_test_utils::ExecuteJsToSigninInSigninFrame;
45 using login_ui_test_utils::WaitUntilUIReady;
47 namespace {
49 struct ContentInfo {
50 ContentInfo(content::WebContents* contents,
51 int pid,
52 content::StoragePartition* storage_partition) {
53 this->contents = contents;
54 this->pid = pid;
55 this->storage_partition = storage_partition;
58 content::WebContents* contents;
59 int pid;
60 content::StoragePartition* storage_partition;
63 ContentInfo NavigateAndGetInfo(
64 Browser* browser,
65 const GURL& url,
66 WindowOpenDisposition disposition) {
67 ui_test_utils::NavigateToURLWithDisposition(
68 browser, url, disposition,
69 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
70 content::WebContents* contents =
71 browser->tab_strip_model()->GetActiveWebContents();
72 content::RenderProcessHost* process = contents->GetRenderProcessHost();
73 return ContentInfo(contents, process->GetID(),
74 process->GetStoragePartition());
77 // Returns a new WebUI object for the WebContents from |arg0|.
78 ACTION(ReturnNewWebUI) {
79 return new content::WebUIController(arg0);
82 // Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
83 // not called as expected.
84 class FooWebUIProvider
85 : public TestChromeWebUIControllerFactory::WebUIProvider {
86 public:
87 MOCK_METHOD2(NewWebUI, content::WebUIController*(content::WebUI* web_ui,
88 const GURL& url));
91 class MockLoginUIObserver : public LoginUIService::Observer {
92 public:
93 MOCK_METHOD0(OnUntrustedLoginUIShown, void());
96 const char kFooWebUIURL[] = "chrome://foo/";
98 bool AddToSet(std::set<content::WebContents*>* set,
99 content::WebContents* web_contents) {
100 set->insert(web_contents);
101 return false;
104 } // namespace
106 class InlineLoginUIBrowserTest : public InProcessBrowserTest {
107 public:
108 InlineLoginUIBrowserTest() {}
111 #if defined(OS_LINUX) || defined(OS_WIN)
112 // crbug.com/422868
113 #define MAYBE_DifferentStorageId DISABLED_DifferentStorageId
114 #else
115 #define MAYBE_DifferentStorageId DifferentStorageId
116 #endif
117 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, MAYBE_DifferentStorageId) {
118 if (switches::IsEnableWebviewBasedSignin()) {
119 ContentInfo info = NavigateAndGetInfo(
120 browser(),
121 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
122 CURRENT_TAB);
123 WaitUntilUIReady(browser());
125 // Make sure storage partition of embedded webview is different from
126 // parent.
127 std::set<content::WebContents*> set;
128 extensions::GuestViewManager* manager =
129 extensions::GuestViewManager::FromBrowserContext(
130 info.contents->GetBrowserContext());
131 manager->ForEachGuest(info.contents, base::Bind(&AddToSet, &set));
132 ASSERT_EQ(1u, set.size());
133 content::WebContents* webview_contents = *set.begin();
134 content::RenderProcessHost* process =
135 webview_contents->GetRenderProcessHost();
136 ASSERT_NE(info.pid, process->GetID());
137 ASSERT_NE(info.storage_partition, process->GetStoragePartition());
138 } else {
139 GURL test_url = ui_test_utils::GetTestUrl(
140 base::FilePath(base::FilePath::kCurrentDirectory),
141 base::FilePath(FILE_PATH_LITERAL("title1.html")));
143 ContentInfo info1 =
144 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
145 ContentInfo info2 = NavigateAndGetInfo(
146 browser(),
147 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
148 CURRENT_TAB);
149 NavigateAndGetInfo(browser(), test_url, CURRENT_TAB);
150 ContentInfo info3 = NavigateAndGetInfo(
151 browser(),
152 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
153 NEW_FOREGROUND_TAB);
155 // The info for signin should be the same.
156 ASSERT_EQ(info2.storage_partition, info3.storage_partition);
157 // The info for test_url and signin should be different.
158 ASSERT_NE(info1.storage_partition, info2.storage_partition);
162 IN_PROC_BROWSER_TEST_F(InlineLoginUIBrowserTest, OneProcessLimit) {
163 GURL test_url_1 = ui_test_utils::GetTestUrl(
164 base::FilePath(base::FilePath::kCurrentDirectory),
165 base::FilePath(FILE_PATH_LITERAL("title1.html")));
166 GURL test_url_2 = ui_test_utils::GetTestUrl(
167 base::FilePath(base::FilePath::kCurrentDirectory),
168 base::FilePath(FILE_PATH_LITERAL("data:text/html,Hello world!")));
170 // Even when the process limit is set to one, the signin process should
171 // still be given its own process and storage partition.
172 content::RenderProcessHost::SetMaxRendererProcessCount(1);
174 ContentInfo info1 =
175 NavigateAndGetInfo(browser(), test_url_1, CURRENT_TAB);
176 ContentInfo info2 =
177 NavigateAndGetInfo(browser(), test_url_2, CURRENT_TAB);
178 ContentInfo info3 = NavigateAndGetInfo(
179 browser(),
180 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
181 CURRENT_TAB);
183 ASSERT_EQ(info1.pid, info2.pid);
184 ASSERT_NE(info1.pid, info3.pid);
187 class InlineLoginUISafeIframeBrowserTest : public InProcessBrowserTest {
188 public:
189 FooWebUIProvider& foo_provider() { return foo_provider_; }
191 private:
192 void SetUp() override {
193 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
195 // EmbeddedTestServer spawns a thread to initialize socket.
196 // Stop IO thread in preparation for fork and exec.
197 embedded_test_server()->StopThread();
199 InProcessBrowserTest::SetUp();
202 void SetUpCommandLine(base::CommandLine* command_line) override {
203 const GURL& base_url = embedded_test_server()->base_url();
204 command_line->AppendSwitchASCII(::switches::kGaiaUrl, base_url.spec());
205 command_line->AppendSwitchASCII(::switches::kLsoUrl, base_url.spec());
206 command_line->AppendSwitchASCII(::switches::kGoogleApisUrl,
207 base_url.spec());
210 void SetUpOnMainThread() override {
211 embedded_test_server()->RestartThreadAndListen();
213 content::WebUIControllerFactory::UnregisterFactoryForTesting(
214 ChromeWebUIControllerFactory::GetInstance());
215 test_factory_.reset(new TestChromeWebUIControllerFactory);
216 content::WebUIControllerFactory::RegisterFactory(test_factory_.get());
217 test_factory_->AddFactoryOverride(
218 GURL(kFooWebUIURL).host(), &foo_provider_);
221 void TearDownOnMainThread() override {
222 test_factory_->RemoveFactoryOverride(GURL(kFooWebUIURL).host());
223 content::WebUIControllerFactory::UnregisterFactoryForTesting(
224 test_factory_.get());
225 test_factory_.reset();
226 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
229 FooWebUIProvider foo_provider_;
230 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
233 // Make sure that the foo webui handler is working properly and that it gets
234 // created when navigated to normally.
235 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, Basic) {
236 const GURL kUrl(kFooWebUIURL);
237 EXPECT_CALL(foo_provider(), NewWebUI(_, ::testing::Eq(kUrl)))
238 .WillOnce(ReturnNewWebUI());
239 ui_test_utils::NavigateToURL(browser(), GURL(kFooWebUIURL));
242 // Make sure that the foo webui handler does not get created when we try to
243 // load it inside the iframe of the login ui.
244 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest, NoWebUIInIframe) {
245 GURL url = signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false).
246 Resolve("?source=0&frameUrl=chrome://foo");
247 EXPECT_CALL(foo_provider(), NewWebUI(_, _)).Times(0);
248 ui_test_utils::NavigateToURL(browser(), url);
251 // Flaky on CrOS, http://crbug.com/364759.
252 #if defined(OS_CHROMEOS)
253 #define MAYBE_TopFrameNavigationDisallowed DISABLED_TopFrameNavigationDisallowed
254 #else
255 #define MAYBE_TopFrameNavigationDisallowed TopFrameNavigationDisallowed
256 #endif
258 // Make sure that the gaia iframe cannot trigger top-frame navigation.
259 // TODO(guohui): flaky on trybot crbug/364759.
260 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
261 MAYBE_TopFrameNavigationDisallowed) {
262 // Loads into gaia iframe a web page that attempts to deframe on load.
263 GURL deframe_url(embedded_test_server()->GetURL("/login/deframe.html"));
264 GURL url(net::AppendOrReplaceQueryParameter(
265 signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false),
266 "frameUrl", deframe_url.spec()));
267 ui_test_utils::NavigateToURL(browser(), url);
268 WaitUntilUIReady(browser());
270 content::WebContents* contents =
271 browser()->tab_strip_model()->GetActiveWebContents();
272 EXPECT_EQ(url, contents->GetVisibleURL());
274 content::NavigationController& controller = contents->GetController();
275 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
278 // Flaky on CrOS, http://crbug.com/364759.
279 // Also flaky on Mac, http://crbug.com/442674.
280 // Also flaky on Linux which is just too flaky
281 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
282 DISABLED_NavigationToOtherChromeURLDisallowed) {
283 ui_test_utils::NavigateToURL(
284 browser(), signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false));
285 WaitUntilUIReady(browser());
287 content::WebContents* contents =
288 browser()->tab_strip_model()->GetActiveWebContents();
289 ASSERT_TRUE(content::ExecuteScript(
290 contents, "window.location.href = 'chrome://foo'"));
292 content::TestNavigationObserver navigation_observer(contents, 1);
293 navigation_observer.Wait();
295 EXPECT_EQ(GURL("about:blank"), contents->GetVisibleURL());
298 #if !defined(OS_CHROMEOS)
299 IN_PROC_BROWSER_TEST_F(InlineLoginUISafeIframeBrowserTest,
300 ConfirmationRequiredForNonsecureSignin) {
301 FakeGaia fake_gaia;
302 fake_gaia.Initialize();
304 embedded_test_server()->RegisterRequestHandler(
305 base::Bind(&FakeGaia::HandleRequest,
306 base::Unretained(&fake_gaia)));
307 fake_gaia.SetFakeMergeSessionParams(
308 "email@gmail.com", "fake-sid-cookie", "fake-lsid-cookie");
310 // Navigates to the Chrome signin page which loads the fake gaia auth page.
311 // Since the fake gaia auth page is served over HTTP, thus expects to see an
312 // untrusted signin confirmation dialog upon submitting credentials below.
313 ui_test_utils::NavigateToURL(
314 browser(), signin::GetPromoURL(signin_metrics::SOURCE_START_PAGE, false));
315 WaitUntilUIReady(browser());
317 MockLoginUIObserver observer;
318 LoginUIServiceFactory::GetForProfile(browser()->profile())
319 ->AddObserver(&observer);
320 base::RunLoop run_loop;
321 EXPECT_CALL(observer, OnUntrustedLoginUIShown())
322 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
324 ExecuteJsToSigninInSigninFrame(browser(), "email@gmail.com", "password");
325 run_loop.Run();
326 base::MessageLoop::current()->RunUntilIdle();
328 #endif // OS_CHROMEOS