blob: c812b1a743722d98a590c95906b9ccb62d95b941 [file] [log] [blame]
[email protected]3a8eecb2010-04-22 23:56:301// Copyright (c) 2010 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
[email protected]ce7f62e32010-08-10 23:43:595#include "base/utf_string_conversions.h"
[email protected]12ea9b272010-08-24 11:31:406#include "chrome/browser/browser_list.h"
[email protected]3a8eecb2010-04-22 23:56:307#include "chrome/browser/extensions/extension_apitest.h"
8#include "chrome/browser/extensions/extension_host.h"
9#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3310#include "chrome/browser/profiles/profile.h"
[email protected]3a8eecb2010-04-22 23:56:3011#include "chrome/browser/renderer_host/render_view_host.h"
12#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]7b5dc002010-11-16 23:08:1013#include "chrome/browser/ui/browser.h"
[email protected]3a8eecb2010-04-22 23:56:3014#include "chrome/common/chrome_switches.h"
15#include "chrome/test/ui_test_utils.h"
16#include "net/base/mock_host_resolver.h"
17
18class AppApiTest : public ExtensionApiTest {
[email protected]3a8eecb2010-04-22 23:56:3019};
20
21// Simulates a page calling window.open on an URL, and waits for the navigation.
22static void WindowOpenHelper(Browser* browser,
[email protected]12ea9b272010-08-24 11:31:4023 RenderViewHost* opener_host,
24 const GURL& url,
25 bool newtab_process_should_equal_opener) {
[email protected]9fabbf72010-09-30 21:50:0526 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
27 opener_host, L"", L"window.open('" + UTF8ToWide(url.spec()) + L"');"));
[email protected]3a8eecb2010-04-22 23:56:3028
[email protected]12ea9b272010-08-24 11:31:4029 // The above window.open call is not user-initiated, it will create
30 // a popup window instead of a new tab in current window.
31 // Now the active tab in last active window should be the new tab.
32 Browser* last_active_browser = BrowserList::GetLastActive();
33 EXPECT_TRUE(last_active_browser);
34 TabContents* newtab = last_active_browser->GetSelectedTabContents();
35 EXPECT_TRUE(newtab);
[email protected]3a8eecb2010-04-22 23:56:3036 if (!newtab->controller().GetLastCommittedEntry() ||
37 newtab->controller().GetLastCommittedEntry()->url() != url)
38 ui_test_utils::WaitForNavigation(&newtab->controller());
39 EXPECT_EQ(url, newtab->controller().GetLastCommittedEntry()->url());
[email protected]12ea9b272010-08-24 11:31:4040 if (newtab_process_should_equal_opener)
41 EXPECT_EQ(opener_host->process(), newtab->render_view_host()->process());
42 else
43 EXPECT_NE(opener_host->process(), newtab->render_view_host()->process());
[email protected]3a8eecb2010-04-22 23:56:3044}
45
46// Simulates a page navigating itself to an URL, and waits for the navigation.
47static void NavigateTabHelper(TabContents* contents, const GURL& url) {
48 bool result = false;
[email protected]9fabbf72010-09-30 21:50:0549 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
[email protected]3a8eecb2010-04-22 23:56:3050 contents->render_view_host(), L"",
[email protected]d38f83f2010-04-30 23:25:5751 L"window.addEventListener('unload', function() {"
52 L" window.domAutomationController.send(true);"
53 L"}, false);"
54 L"window.location = '" + UTF8ToWide(url.spec()) + L"';",
[email protected]9fabbf72010-09-30 21:50:0555 &result));
[email protected]3a8eecb2010-04-22 23:56:3056 ASSERT_TRUE(result);
57
[email protected]d38f83f2010-04-30 23:25:5758 if (!contents->controller().GetLastCommittedEntry() ||
[email protected]3a8eecb2010-04-22 23:56:3059 contents->controller().GetLastCommittedEntry()->url() != url)
60 ui_test_utils::WaitForNavigation(&contents->controller());
61 EXPECT_EQ(url, contents->controller().GetLastCommittedEntry()->url());
62}
63
[email protected]faf407b2011-01-05 01:24:3264IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcess) {
[email protected]12ea9b272010-08-24 11:31:4065 CommandLine::ForCurrentProcess()->AppendSwitch(
66 switches::kDisablePopupBlocking);
67
[email protected]3a8eecb2010-04-22 23:56:3068 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]95409e12010-08-17 20:07:1169 ASSERT_TRUE(test_server()->Start());
[email protected]3a8eecb2010-04-22 23:56:3070
[email protected]cbf4d1912010-08-12 18:24:5771 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process")));
[email protected]3a8eecb2010-04-22 23:56:3072
[email protected]cbf4d1912010-08-12 18:24:5773 // Open two tabs in the app, one outside it.
[email protected]fe3048872010-10-18 14:58:5974 GURL base_url = test_server()->GetURL(
75 "files/extensions/api_test/app_process/");
76
77 // The app under test acts on URLs whose host is "localhost",
78 // so the URLs we navigate to must have host "localhost".
79 GURL::Replacements replace_host;
[email protected]6101c342010-12-16 22:44:3780 std::string host_str("localhost"); // must stay in scope with replace_host
81 replace_host.SetHostStr(host_str);
[email protected]fe3048872010-10-18 14:58:5982 base_url = base_url.ReplaceComponents(replace_host);
83
[email protected]cbf4d1912010-08-12 18:24:5784 browser()->NewTab();
85 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
86 browser()->NewTab();
87 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html"));
88 browser()->NewTab();
89 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path3/empty.html"));
[email protected]3a8eecb2010-04-22 23:56:3090
91 // The extension should have opened 3 new tabs. Including the original blank
92 // tab, we now have 4 tabs. Two should be part of the extension app, and
[email protected]583d45c12010-08-31 02:48:1293 // grouped in the same process.
[email protected]3a8eecb2010-04-22 23:56:3094 ASSERT_EQ(4, browser()->tab_count());
[email protected]cbf4d1912010-08-12 18:24:5795 RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host();
[email protected]cbf4d1912010-08-12 18:24:5796
97 EXPECT_EQ(host->process(),
[email protected]3a8eecb2010-04-22 23:56:3098 browser()->GetTabContentsAt(2)->render_view_host()->process());
[email protected]cbf4d1912010-08-12 18:24:5799 EXPECT_NE(host->process(),
[email protected]3a8eecb2010-04-22 23:56:30100 browser()->GetTabContentsAt(3)->render_view_host()->process());
101
102 // Now let's do the same using window.open. The same should happen.
[email protected]12ea9b272010-08-24 11:31:40103 ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile()));
[email protected]cbf4d1912010-08-12 18:24:57104 WindowOpenHelper(browser(), host,
[email protected]12ea9b272010-08-24 11:31:40105 base_url.Resolve("path1/empty.html"), true);
[email protected]cbf4d1912010-08-12 18:24:57106 WindowOpenHelper(browser(), host,
[email protected]12ea9b272010-08-24 11:31:40107 base_url.Resolve("path2/empty.html"), true);
[email protected]08e94b82010-12-15 22:51:04108 // TODO(creis): This should open in a new process (i.e., false for the last
109 // argument), but we temporarily avoid swapping processes away from an app
110 // until we're able to restore window.opener if the page later returns to an
111 // in-app URL. See crbug.com/65953.
[email protected]cbf4d1912010-08-12 18:24:57112 WindowOpenHelper(browser(), host,
[email protected]08e94b82010-12-15 22:51:04113 base_url.Resolve("path3/empty.html"), true);
[email protected]3a8eecb2010-04-22 23:56:30114
115 // Now let's have these pages navigate, into or out of the extension web
116 // extent. They should switch processes.
[email protected]9a1e6d42010-04-26 22:29:36117 const GURL& app_url(base_url.Resolve("path1/empty.html"));
118 const GURL& non_app_url(base_url.Resolve("path3/empty.html"));
[email protected]cbf4d1912010-08-12 18:24:57119 NavigateTabHelper(browser()->GetTabContentsAt(2), non_app_url);
[email protected]3a8eecb2010-04-22 23:56:30120 NavigateTabHelper(browser()->GetTabContentsAt(3), app_url);
[email protected]08e94b82010-12-15 22:51:04121 // TODO(creis): This should swap out of the app's process (i.e., EXPECT_NE),
122 // but we temporarily avoid swapping away from an app in case it needs to
123 // communicate with window.opener later. See crbug.com/65953.
124 EXPECT_EQ(host->process(),
[email protected]cbf4d1912010-08-12 18:24:57125 browser()->GetTabContentsAt(2)->render_view_host()->process());
126 EXPECT_EQ(host->process(),
[email protected]3a8eecb2010-04-22 23:56:30127 browser()->GetTabContentsAt(3)->render_view_host()->process());
[email protected]08e94b82010-12-15 22:51:04128
129 // If one of the popup tabs navigates back to the app, window.opener should
130 // be valid.
131 NavigateTabHelper(browser()->GetTabContentsAt(6), app_url);
132 EXPECT_EQ(host->process(),
133 browser()->GetTabContentsAt(6)->render_view_host()->process());
134 bool windowOpenerValid = false;
135 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
136 browser()->GetTabContentsAt(6)->render_view_host(), L"",
137 L"window.domAutomationController.send(window.opener != null)",
138 &windowOpenerValid));
139 ASSERT_TRUE(windowOpenerValid);
[email protected]3a8eecb2010-04-22 23:56:30140}
[email protected]faf407b2011-01-05 01:24:32141
142// Tests that app process switching works properly in the following scenario:
143// 1. navigate to a page1 in the app
144// 2. page1 redirects to a page2 outside the app extent (ie, "/server-redirect")
145// 3. page2 redirects back to a page in the app
146// The final navigation should end up in the app process.
147// See http://crbug.com/61757
148IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcessRedirectBack) {
149 CommandLine::ForCurrentProcess()->AppendSwitch(
150 switches::kDisablePopupBlocking);
151
152 host_resolver()->AddRule("*", "127.0.0.1");
153 ASSERT_TRUE(test_server()->Start());
154
155 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process")));
156
157 // Open two tabs in the app.
158 GURL base_url = test_server()->GetURL(
159 "files/extensions/api_test/app_process/");
160
161 // The app under test acts on URLs whose host is "localhost",
162 // so the URLs we navigate to must have host "localhost".
163 GURL::Replacements replace_host;
164 std::string host_str("localhost"); // must stay in scope with replace_host
165 replace_host.SetHostStr(host_str);
166 base_url = base_url.ReplaceComponents(replace_host);
167
168 browser()->NewTab();
169 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
170 browser()->NewTab();
[email protected]faf407b2011-01-05 01:24:32171 // Wait until the second tab finishes its redirect train (2 hops).
[email protected]089e8c332011-01-06 21:37:29172 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
173 browser(), base_url.Resolve("path1/redirect.html"), 2);
[email protected]faf407b2011-01-05 01:24:32174
175 // 3 tabs, including the initial about:blank. The last 2 should be the same
176 // process.
177 ASSERT_EQ(3, browser()->tab_count());
[email protected]089e8c332011-01-06 21:37:29178 EXPECT_EQ("/files/extensions/api_test/app_process/path1/empty.html",
179 browser()->GetTabContentsAt(2)->controller().
180 GetLastCommittedEntry()->url().path());
[email protected]faf407b2011-01-05 01:24:32181 RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host();
182 EXPECT_EQ(host->process(),
183 browser()->GetTabContentsAt(2)->render_view_host()->process());
184}