[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 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] | ce7f62e3 | 2010-08-10 23:43:59 | [diff] [blame] | 5 | #include "base/utf_string_conversions.h" |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 6 | #include "chrome/browser/extensions/extension_apitest.h" |
| 7 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_service.h" |
| 9 | #include "chrome/browser/extensions/process_map.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser.h" |
[email protected] | 71b73f0 | 2011-04-06 15:57:29 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | ae67374 | 2011-08-24 19:48:37 | [diff] [blame] | 14 | #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 15 | #include "chrome/common/chrome_switches.h" |
[email protected] | 814a7bf0f | 2011-08-13 05:30:59 | [diff] [blame] | 16 | #include "chrome/common/extensions/extension.h" |
[email protected] | c2f36e3a | 2011-12-14 01:27:19 | [diff] [blame] | 17 | #include "chrome/common/extensions/extension_file_util.h" |
[email protected] | 36a5c4c | 2011-12-14 16:34:50 | [diff] [blame] | 18 | #include "chrome/common/string_ordinal.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 19 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 20 | #include "content/public/browser/navigation_entry.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 21 | #include "content/public/browser/notification_service.h" |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 22 | #include "content/public/browser/render_process_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 23 | #include "content/public/browser/render_view_host.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 25 | #include "content/test/test_navigation_observer.h" |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 26 | #include "net/base/mock_host_resolver.h" |
| 27 | |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 28 | using content::NavigationController; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 29 | using content::RenderViewHost; |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 30 | using content::WebContents; |
| 31 | |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 32 | // Simulates a page calling window.open on an URL, and waits for the navigation. |
| 33 | static void WindowOpenHelper(Browser* browser, |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 34 | RenderViewHost* opener_host, |
| 35 | const GURL& url, |
| 36 | bool newtab_process_should_equal_opener) { |
[email protected] | 3114db2c | 2011-09-12 20:09:05 | [diff] [blame] | 37 | ui_test_utils::WindowedNotificationObserver observer( |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 38 | content::NOTIFICATION_LOAD_STOP, |
| 39 | content::NotificationService::AllSources()); |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 40 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
| 41 | opener_host, L"", L"window.open('" + UTF8ToWide(url.spec()) + L"');")); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 42 | |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 43 | // The above window.open call is not user-initiated, it will create |
| 44 | // a popup window instead of a new tab in current window. |
| 45 | // Now the active tab in last active window should be the new tab. |
| 46 | Browser* last_active_browser = BrowserList::GetLastActive(); |
| 47 | EXPECT_TRUE(last_active_browser); |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 48 | WebContents* newtab = last_active_browser->GetSelectedWebContents(); |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 49 | EXPECT_TRUE(newtab); |
[email protected] | 3114db2c | 2011-09-12 20:09:05 | [diff] [blame] | 50 | observer.Wait(); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 51 | EXPECT_EQ(url, newtab->GetController().GetLastCommittedEntry()->GetURL()); |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 52 | if (newtab_process_should_equal_opener) |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 53 | EXPECT_EQ(opener_host->GetProcess(), newtab->GetRenderProcessHost()); |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 54 | else |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 55 | EXPECT_NE(opener_host->GetProcess(), newtab->GetRenderProcessHost()); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Simulates a page navigating itself to an URL, and waits for the navigation. |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 59 | static void NavigateTabHelper(WebContents* contents, const GURL& url) { |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 60 | bool result = false; |
[email protected] | 3114db2c | 2011-09-12 20:09:05 | [diff] [blame] | 61 | ui_test_utils::WindowedNotificationObserver observer( |
| 62 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 63 | content::NotificationService::AllSources()); |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 64 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 65 | contents->GetRenderViewHost(), L"", |
[email protected] | d38f83f | 2010-04-30 23:25:57 | [diff] [blame] | 66 | L"window.addEventListener('unload', function() {" |
| 67 | L" window.domAutomationController.send(true);" |
| 68 | L"}, false);" |
| 69 | L"window.location = '" + UTF8ToWide(url.spec()) + L"';", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 70 | &result)); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 71 | ASSERT_TRUE(result); |
[email protected] | 3114db2c | 2011-09-12 20:09:05 | [diff] [blame] | 72 | observer.Wait(); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 73 | EXPECT_EQ(url, contents->GetController().GetLastCommittedEntry()->GetURL()); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 7b54ca0 | 2012-03-02 18:06:53 | [diff] [blame] | 76 | class AppApiTest : public ExtensionApiTest { |
| 77 | protected: |
| 78 | // Gets the base URL for files for a specific test, making sure that it uses |
| 79 | // "localhost" as the hostname, since that is what the extent is declared |
| 80 | // as in the test apps manifests. |
| 81 | GURL GetTestBaseURL(std::string test_directory) { |
| 82 | GURL::Replacements replace_host; |
| 83 | std::string host_str("localhost"); // must stay in scope with replace_host |
| 84 | replace_host.SetHostStr(host_str); |
| 85 | GURL base_url = test_server()->GetURL( |
| 86 | "files/extensions/api_test/" + test_directory + "/"); |
| 87 | return base_url.ReplaceComponents(replace_host); |
| 88 | } |
| 89 | |
| 90 | // Pass flags to make testing apps easier. |
| 91 | void SetUpCommandLine(CommandLine* command_line) { |
| 92 | ExtensionApiTest::SetUpCommandLine(command_line); |
| 93 | CommandLine::ForCurrentProcess()->AppendSwitch( |
| 94 | switches::kDisablePopupBlocking); |
| 95 | CommandLine::ForCurrentProcess()->AppendSwitch( |
| 96 | switches::kAllowHTTPBackgroundPage); |
| 97 | } |
| 98 | |
| 99 | // Helper function to test that independent tabs of the named app are loaded |
| 100 | // into separate processes. |
| 101 | void TestAppInstancesHelper(std::string app_name) { |
| 102 | LOG(INFO) << "Start of test."; |
| 103 | |
| 104 | extensions::ProcessMap* process_map = |
| 105 | browser()->profile()->GetExtensionService()->process_map(); |
| 106 | |
| 107 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 108 | ASSERT_TRUE(test_server()->Start()); |
| 109 | |
| 110 | ASSERT_TRUE(LoadExtension( |
| 111 | test_data_dir_.AppendASCII(app_name))); |
| 112 | |
| 113 | // Open two tabs in the app, one outside it. |
| 114 | GURL base_url = GetTestBaseURL(app_name); |
| 115 | |
| 116 | // Test both opening a URL in a new tab, and opening a tab and then |
| 117 | // navigating it. Either way, app tabs should be considered extension |
| 118 | // processes, but they have no elevated privileges and thus should not |
| 119 | // have WebUI bindings. |
| 120 | ui_test_utils::NavigateToURLWithDisposition( |
| 121 | browser(), base_url.Resolve("path1/empty.html"), NEW_FOREGROUND_TAB, |
| 122 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 123 | LOG(INFO) << "Nav 1."; |
| 124 | EXPECT_TRUE(process_map->Contains( |
| 125 | browser()->GetWebContentsAt(1)->GetRenderProcessHost()->GetID())); |
| 126 | EXPECT_FALSE(browser()->GetWebContentsAt(1)->GetWebUI()); |
| 127 | |
| 128 | ui_test_utils::WindowedNotificationObserver tab_added_observer( |
| 129 | content::NOTIFICATION_TAB_ADDED, |
| 130 | content::NotificationService::AllSources()); |
| 131 | browser()->NewTab(); |
| 132 | tab_added_observer.Wait(); |
| 133 | LOG(INFO) << "New tab."; |
| 134 | ui_test_utils::NavigateToURL(browser(), |
| 135 | base_url.Resolve("path2/empty.html")); |
| 136 | LOG(INFO) << "Nav 2."; |
| 137 | EXPECT_TRUE(process_map->Contains( |
| 138 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()->GetID())); |
| 139 | EXPECT_FALSE(browser()->GetWebContentsAt(2)->GetWebUI()); |
| 140 | |
| 141 | // We should have opened 2 new extension tabs. Including the original blank |
| 142 | // tab, we now have 3 tabs. The two app tabs should not be in the same |
| 143 | // process, since they do not have the background permission. (Thus, we |
| 144 | // want to separate them to improve responsiveness.) |
| 145 | ASSERT_EQ(3, browser()->tab_count()); |
| 146 | RenderViewHost* host1 = browser()->GetWebContentsAt(1)->GetRenderViewHost(); |
| 147 | RenderViewHost* host2 = browser()->GetWebContentsAt(2)->GetRenderViewHost(); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 148 | EXPECT_NE(host1->GetProcess(), host2->GetProcess()); |
[email protected] | 7b54ca0 | 2012-03-02 18:06:53 | [diff] [blame] | 149 | |
| 150 | // Opening tabs with window.open should keep the page in the opener's |
| 151 | // process. |
| 152 | ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile())); |
| 153 | WindowOpenHelper(browser(), host1, |
| 154 | base_url.Resolve("path1/empty.html"), true); |
| 155 | LOG(INFO) << "WindowOpenHelper 1."; |
| 156 | WindowOpenHelper(browser(), host2, |
| 157 | base_url.Resolve("path2/empty.html"), true); |
| 158 | LOG(INFO) << "End of test."; |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | // Tests that hosted apps with the background permission get a process-per-app |
| 163 | // model, since all pages need to be able to script the background page. |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 164 | IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcess) { |
| 165 | LOG(INFO) << "Start of test."; |
[email protected] | 9b60083 | 2011-10-26 20:31:59 | [diff] [blame] | 166 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 167 | extensions::ProcessMap* process_map = |
| 168 | browser()->profile()->GetExtensionService()->process_map(); |
[email protected] | 718eab6 | 2011-10-05 21:16:52 | [diff] [blame] | 169 | |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 170 | host_resolver()->AddRule("*", "127.0.0.1"); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 171 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 172 | |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 173 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process"))); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 174 | |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 175 | LOG(INFO) << "Loaded extension."; |
| 176 | |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 177 | // Open two tabs in the app, one outside it. |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 178 | GURL base_url = GetTestBaseURL("app_process"); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 179 | |
[email protected] | f0e1333 | 2011-05-20 22:41:14 | [diff] [blame] | 180 | // Test both opening a URL in a new tab, and opening a tab and then navigating |
| 181 | // it. Either way, app tabs should be considered extension processes, but |
| 182 | // they have no elevated privileges and thus should not have WebUI bindings. |
| 183 | ui_test_utils::NavigateToURLWithDisposition( |
| 184 | browser(), base_url.Resolve("path1/empty.html"), NEW_FOREGROUND_TAB, |
| 185 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 186 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 187 | browser()->GetWebContentsAt(1)->GetRenderProcessHost()->GetID())); |
| 188 | EXPECT_FALSE(browser()->GetWebContentsAt(1)->GetWebUI()); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 189 | LOG(INFO) << "Nav 1."; |
| 190 | |
| 191 | ui_test_utils::NavigateToURLWithDisposition( |
| 192 | browser(), base_url.Resolve("path2/empty.html"), NEW_FOREGROUND_TAB, |
| 193 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 194 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 195 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()->GetID())); |
| 196 | EXPECT_FALSE(browser()->GetWebContentsAt(2)->GetWebUI()); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 197 | LOG(INFO) << "Nav 2."; |
| 198 | |
| 199 | ui_test_utils::WindowedNotificationObserver tab_added_observer( |
| 200 | content::NOTIFICATION_TAB_ADDED, |
| 201 | content::NotificationService::AllSources()); |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 202 | browser()->NewTab(); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 203 | tab_added_observer.Wait(); |
| 204 | LOG(INFO) << "New tab."; |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 205 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path3/empty.html")); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 206 | LOG(INFO) << "Nav 3."; |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 207 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 208 | browser()->GetWebContentsAt(3)->GetRenderProcessHost()->GetID())); |
| 209 | EXPECT_FALSE(browser()->GetWebContentsAt(3)->GetWebUI()); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 210 | |
[email protected] | 056ad2a | 2011-07-12 02:13:55 | [diff] [blame] | 211 | // We should have opened 3 new extension tabs. Including the original blank |
| 212 | // tab, we now have 4 tabs. Because the app_process app has the background |
| 213 | // permission, all of its instances are in the same process. Thus two tabs |
| 214 | // should be part of the extension app and grouped in the same process. |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 215 | ASSERT_EQ(4, browser()->tab_count()); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 216 | RenderViewHost* host = browser()->GetWebContentsAt(1)->GetRenderViewHost(); |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 217 | |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 218 | EXPECT_EQ(host->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 219 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 220 | EXPECT_NE(host->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 221 | browser()->GetWebContentsAt(3)->GetRenderProcessHost()); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 222 | |
| 223 | // Now let's do the same using window.open. The same should happen. |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 224 | ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile())); |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 225 | WindowOpenHelper(browser(), host, |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 226 | base_url.Resolve("path1/empty.html"), true); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 227 | LOG(INFO) << "WindowOpenHelper 1."; |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 228 | WindowOpenHelper(browser(), host, |
[email protected] | 12ea9b27 | 2010-08-24 11:31:40 | [diff] [blame] | 229 | base_url.Resolve("path2/empty.html"), true); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 230 | LOG(INFO) << "WindowOpenHelper 2."; |
[email protected] | 361a5f1f | 2011-10-05 20:11:15 | [diff] [blame] | 231 | // TODO(creis): This should open in a new process (i.e., false for the last |
| 232 | // argument), but we temporarily avoid swapping processes away from an app |
| 233 | // until we're able to support cross-process postMessage calls. |
| 234 | // See crbug.com/59285. |
[email protected] | cbf4d191 | 2010-08-12 18:24:57 | [diff] [blame] | 235 | WindowOpenHelper(browser(), host, |
[email protected] | 361a5f1f | 2011-10-05 20:11:15 | [diff] [blame] | 236 | base_url.Resolve("path3/empty.html"), true); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 237 | LOG(INFO) << "WindowOpenHelper 3."; |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 238 | |
| 239 | // Now let's have these pages navigate, into or out of the extension web |
| 240 | // extent. They should switch processes. |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame] | 241 | const GURL& app_url(base_url.Resolve("path1/empty.html")); |
| 242 | const GURL& non_app_url(base_url.Resolve("path3/empty.html")); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 243 | NavigateTabHelper(browser()->GetWebContentsAt(2), non_app_url); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 244 | LOG(INFO) << "NavigateTabHelper 1."; |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 245 | NavigateTabHelper(browser()->GetWebContentsAt(3), app_url); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 246 | LOG(INFO) << "NavigateTabHelper 2."; |
[email protected] | 361a5f1f | 2011-10-05 20:11:15 | [diff] [blame] | 247 | // TODO(creis): This should swap out of the app's process (i.e., EXPECT_NE), |
| 248 | // but we temporarily avoid swapping away from an app in case the window |
| 249 | // tries to send a postMessage to the app. See crbug.com/59285. |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 250 | EXPECT_EQ(host->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 251 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 252 | EXPECT_EQ(host->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 253 | browser()->GetWebContentsAt(3)->GetRenderProcessHost()); |
[email protected] | 08e94b8 | 2010-12-15 22:51:04 | [diff] [blame] | 254 | |
| 255 | // If one of the popup tabs navigates back to the app, window.opener should |
| 256 | // be valid. |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 257 | NavigateTabHelper(browser()->GetWebContentsAt(6), app_url); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 258 | LOG(INFO) << "NavigateTabHelper 3."; |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 259 | EXPECT_EQ(host->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 260 | browser()->GetWebContentsAt(6)->GetRenderProcessHost()); |
[email protected] | 08e94b8 | 2010-12-15 22:51:04 | [diff] [blame] | 261 | bool windowOpenerValid = false; |
| 262 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 263 | browser()->GetWebContentsAt(6)->GetRenderViewHost(), L"", |
[email protected] | 08e94b8 | 2010-12-15 22:51:04 | [diff] [blame] | 264 | L"window.domAutomationController.send(window.opener != null)", |
| 265 | &windowOpenerValid)); |
| 266 | ASSERT_TRUE(windowOpenerValid); |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 267 | |
| 268 | LOG(INFO) << "End of test."; |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 269 | } |
[email protected] | faf407b | 2011-01-05 01:24:32 | [diff] [blame] | 270 | |
[email protected] | 056ad2a | 2011-07-12 02:13:55 | [diff] [blame] | 271 | // Test that hosted apps without the background permission use a process per app |
| 272 | // instance model, such that separate instances are in separate processes. |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 273 | IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcessInstances) { |
[email protected] | 7b54ca0 | 2012-03-02 18:06:53 | [diff] [blame] | 274 | TestAppInstancesHelper("app_process_instances"); |
| 275 | } |
[email protected] | 87c7c29 | 2011-10-27 16:16:41 | [diff] [blame] | 276 | |
[email protected] | 7b54ca0 | 2012-03-02 18:06:53 | [diff] [blame] | 277 | // Test that hosted apps with the background permission but that set |
| 278 | // allow_js_access to false also use a process per app instance model. |
| 279 | // Separate instances should be in separate processes. |
| 280 | IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcessBackgroundInstances) { |
| 281 | TestAppInstancesHelper("app_process_background_instances"); |
[email protected] | 056ad2a | 2011-07-12 02:13:55 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 284 | // Tests that bookmark apps do not use the app process model and are treated |
| 285 | // like normal web pages instead. http://crbug.com/104636. |
[email protected] | c2f36e3a | 2011-12-14 01:27:19 | [diff] [blame] | 286 | IN_PROC_BROWSER_TEST_F(AppApiTest, BookmarkAppGetsNormalProcess) { |
[email protected] | c2f36e3a | 2011-12-14 01:27:19 | [diff] [blame] | 287 | ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 288 | extensions::ProcessMap* process_map = service->process_map(); |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 289 | |
| 290 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 291 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 292 | GURL base_url = GetTestBaseURL("app_process"); |
| 293 | |
[email protected] | c2f36e3a | 2011-12-14 01:27:19 | [diff] [blame] | 294 | // Load an app as a bookmark app. |
| 295 | std::string error; |
| 296 | scoped_refptr<const Extension> extension(extension_file_util::LoadExtension( |
| 297 | test_data_dir_.AppendASCII("app_process"), |
| 298 | Extension::LOAD, |
| 299 | Extension::FROM_BOOKMARK, |
| 300 | &error)); |
[email protected] | 36a5c4c | 2011-12-14 16:34:50 | [diff] [blame] | 301 | service->OnExtensionInstalled(extension, false, |
| 302 | StringOrdinal::CreateInitialOrdinal()); |
[email protected] | c2f36e3a | 2011-12-14 01:27:19 | [diff] [blame] | 303 | ASSERT_TRUE(extension.get()); |
| 304 | ASSERT_TRUE(extension->from_bookmark()); |
| 305 | |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 306 | // Test both opening a URL in a new tab, and opening a tab and then navigating |
| 307 | // it. Either way, bookmark app tabs should be considered normal processes |
| 308 | // with no elevated privileges and no WebUI bindings. |
| 309 | ui_test_utils::NavigateToURLWithDisposition( |
| 310 | browser(), base_url.Resolve("path1/empty.html"), NEW_FOREGROUND_TAB, |
| 311 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 312 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 313 | browser()->GetWebContentsAt(1)->GetRenderProcessHost()->GetID())); |
| 314 | EXPECT_FALSE(browser()->GetWebContentsAt(1)->GetWebUI()); |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 315 | |
| 316 | ui_test_utils::WindowedNotificationObserver tab_added_observer( |
| 317 | content::NOTIFICATION_TAB_ADDED, |
| 318 | content::NotificationService::AllSources()); |
| 319 | browser()->NewTab(); |
| 320 | tab_added_observer.Wait(); |
| 321 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html")); |
| 322 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 323 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()->GetID())); |
| 324 | EXPECT_FALSE(browser()->GetWebContentsAt(2)->GetWebUI()); |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 325 | |
| 326 | // We should have opened 2 new bookmark app tabs. Including the original blank |
| 327 | // tab, we now have 3 tabs. Because normal pages use the |
| 328 | // process-per-site-instance model, each should be in its own process. |
| 329 | ASSERT_EQ(3, browser()->tab_count()); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 330 | RenderViewHost* host = browser()->GetWebContentsAt(1)->GetRenderViewHost(); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 331 | EXPECT_NE(host->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 332 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()); |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 333 | |
| 334 | // Now let's do the same using window.open. The same should happen. |
| 335 | ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile())); |
| 336 | WindowOpenHelper(browser(), host, |
| 337 | base_url.Resolve("path1/empty.html"), true); |
| 338 | WindowOpenHelper(browser(), host, |
| 339 | base_url.Resolve("path2/empty.html"), true); |
| 340 | |
| 341 | // Now let's have a tab navigate out of and back into the app's web |
| 342 | // extent. Neither navigation should switch processes. |
| 343 | const GURL& app_url(base_url.Resolve("path1/empty.html")); |
| 344 | const GURL& non_app_url(base_url.Resolve("path3/empty.html")); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 345 | RenderViewHost* host2 = browser()->GetWebContentsAt(2)->GetRenderViewHost(); |
| 346 | NavigateTabHelper(browser()->GetWebContentsAt(2), non_app_url); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 347 | EXPECT_EQ(host2->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 348 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()); |
| 349 | NavigateTabHelper(browser()->GetWebContentsAt(2), app_url); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 350 | EXPECT_EQ(host2->GetProcess(), |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 351 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()); |
[email protected] | 15877ca | 2011-11-18 22:40:52 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | faf407b | 2011-01-05 01:24:32 | [diff] [blame] | 354 | // Tests that app process switching works properly in the following scenario: |
| 355 | // 1. navigate to a page1 in the app |
| 356 | // 2. page1 redirects to a page2 outside the app extent (ie, "/server-redirect") |
| 357 | // 3. page2 redirects back to a page in the app |
| 358 | // The final navigation should end up in the app process. |
| 359 | // See http://crbug.com/61757 |
[email protected] | 678d702 | 2011-12-30 10:18:54 | [diff] [blame] | 360 | // This test doesn't complete on WebKit Win (dbg). See crbug.com/108853. |
[email protected] | eea84c0 | 2012-01-25 06:15:25 | [diff] [blame] | 361 | #if defined(OS_WIN) && !defined(NDEBUG) |
[email protected] | c374f8a8 | 2011-12-01 00:45:08 | [diff] [blame] | 362 | #define MAYBE_AppProcessRedirectBack DISABLED_AppProcessRedirectBack |
| 363 | #else |
| 364 | #define MAYBE_AppProcessRedirectBack AppProcessRedirectBack |
| 365 | #endif |
| 366 | IN_PROC_BROWSER_TEST_F(AppApiTest, MAYBE_AppProcessRedirectBack) { |
[email protected] | faf407b | 2011-01-05 01:24:32 | [diff] [blame] | 367 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 368 | ASSERT_TRUE(test_server()->Start()); |
| 369 | |
| 370 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process"))); |
| 371 | |
| 372 | // Open two tabs in the app. |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 373 | GURL base_url = GetTestBaseURL("app_process"); |
[email protected] | faf407b | 2011-01-05 01:24:32 | [diff] [blame] | 374 | |
| 375 | browser()->NewTab(); |
| 376 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); |
| 377 | browser()->NewTab(); |
[email protected] | 4ad5d77d | 2011-12-03 02:00:48 | [diff] [blame] | 378 | // Wait until the second tab finishes its redirect train (3 hops). |
| 379 | // 1. We navigate to redirect.html |
| 380 | // 2. Renderer navigates and finishes, counting as a load stop. |
| 381 | // 3. Renderer issues the meta refresh to navigate to server-redirect. |
| 382 | // 4. Renderer is now in a "provisional load", waiting for navigation to |
| 383 | // complete. |
| 384 | // 5. Browser sees a redirect response from server-redirect to empty.html, and |
| 385 | // transfers that to a new navigation, using RequestTransferURL. |
| 386 | // 6. We navigate to empty.html. |
| 387 | // 7. Renderer is still in a provisional load to server-redirect, so that is |
| 388 | // cancelled, and counts as a load stop |
| 389 | // 8. Renderer navigates to empty.html, and finishes loading, counting as the |
| 390 | // third load stop |
[email protected] | 4ad5d77d | 2011-12-03 02:00:48 | [diff] [blame] | 391 | ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 392 | browser(), base_url.Resolve("path1/redirect.html"), 3); |
[email protected] | faf407b | 2011-01-05 01:24:32 | [diff] [blame] | 393 | |
| 394 | // 3 tabs, including the initial about:blank. The last 2 should be the same |
| 395 | // process. |
| 396 | ASSERT_EQ(3, browser()->tab_count()); |
[email protected] | 089e8c33 | 2011-01-06 21:37:29 | [diff] [blame] | 397 | EXPECT_EQ("/files/extensions/api_test/app_process/path1/empty.html", |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 398 | browser()->GetWebContentsAt(2)->GetController(). |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 399 | GetLastCommittedEntry()->GetURL().path()); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 400 | EXPECT_EQ(browser()->GetWebContentsAt(1)->GetRenderProcessHost(), |
| 401 | browser()->GetWebContentsAt(2)->GetRenderProcessHost()); |
[email protected] | faf407b | 2011-01-05 01:24:32 | [diff] [blame] | 402 | } |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 403 | |
| 404 | // Ensure that reloading a URL after installing or uninstalling it as an app |
| 405 | // correctly swaps the process. (http://crbug.com/80621) |
[email protected] | 1153596 | 2012-02-09 21:02:10 | [diff] [blame] | 406 | IN_PROC_BROWSER_TEST_F(AppApiTest, ReloadIntoAppProcess) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 407 | extensions::ProcessMap* process_map = |
| 408 | browser()->profile()->GetExtensionService()->process_map(); |
[email protected] | 718eab6 | 2011-10-05 21:16:52 | [diff] [blame] | 409 | |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 410 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 411 | ASSERT_TRUE(test_server()->Start()); |
| 412 | |
| 413 | // The app under test acts on URLs whose host is "localhost", |
| 414 | // so the URLs we navigate to must have host "localhost". |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 415 | GURL base_url = GetTestBaseURL("app_process"); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 416 | |
| 417 | // Load an app URL before loading the app. |
| 418 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 419 | WebContents* contents = browser()->GetWebContentsAt(0); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 420 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 421 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 422 | |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 423 | // Load app and navigate to the page. |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 424 | const Extension* app = |
| 425 | LoadExtension(test_data_dir_.AppendASCII("app_process")); |
| 426 | ASSERT_TRUE(app); |
| 427 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 428 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 429 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 430 | |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 431 | // Disable app and navigate to the page. |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 432 | DisableExtension(app->id()); |
| 433 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 434 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 435 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 436 | |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 437 | // Enable app and reload the page. |
| 438 | EnableExtension(app->id()); |
| 439 | ui_test_utils::WindowedNotificationObserver reload_observer( |
| 440 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 441 | content::Source<NavigationController>( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 442 | &browser()->GetSelectedTabContentsWrapper()->web_contents()-> |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 443 | GetController())); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 444 | browser()->Reload(CURRENT_TAB); |
| 445 | reload_observer.Wait(); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 446 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 447 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 448 | |
| 449 | // Disable app and reload the page. |
| 450 | DisableExtension(app->id()); |
| 451 | ui_test_utils::WindowedNotificationObserver reload_observer2( |
| 452 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 453 | content::Source<NavigationController>( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 454 | &browser()->GetSelectedTabContentsWrapper()->web_contents()-> |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 455 | GetController())); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 456 | browser()->Reload(CURRENT_TAB); |
| 457 | reload_observer2.Wait(); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 458 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 459 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 460 | |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 461 | // Enable app and reload via JavaScript. |
| 462 | EnableExtension(app->id()); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 463 | ui_test_utils::WindowedNotificationObserver js_reload_observer( |
| 464 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 465 | content::Source<NavigationController>( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 466 | &browser()->GetSelectedTabContentsWrapper()->web_contents()-> |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 467 | GetController())); |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 468 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(contents->GetRenderViewHost(), |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 469 | L"", L"location.reload();")); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 470 | js_reload_observer.Wait(); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 471 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 472 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 473 | |
| 474 | // Disable app and reload via JavaScript. |
| 475 | DisableExtension(app->id()); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 476 | ui_test_utils::WindowedNotificationObserver js_reload_observer2( |
| 477 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 478 | content::Source<NavigationController>( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 479 | &browser()->GetSelectedTabContentsWrapper()->web_contents()-> |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 480 | GetController())); |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 481 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(contents->GetRenderViewHost(), |
[email protected] | bcd90448 | 2012-02-01 01:54:22 | [diff] [blame] | 482 | L"", L"location = location;")); |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 483 | js_reload_observer2.Wait(); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 484 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 485 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 486 | } |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 487 | |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 488 | // Tests that if we have a non-app process (path3/container.html) that has an |
| 489 | // iframe with a URL in the app's extent (path1/iframe.html), then opening a |
| 490 | // link from that iframe to a new window to a URL in the app's extent (path1/ |
| 491 | // empty.html) results in the new window being in an app process. See |
| 492 | // http://crbug.com/89272 for more details. |
| 493 | IN_PROC_BROWSER_TEST_F(AppApiTest, OpenAppFromIframe) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 494 | extensions::ProcessMap* process_map = |
| 495 | browser()->profile()->GetExtensionService()->process_map(); |
[email protected] | 718eab6 | 2011-10-05 21:16:52 | [diff] [blame] | 496 | |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 497 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 498 | ASSERT_TRUE(test_server()->Start()); |
| 499 | |
| 500 | GURL base_url = GetTestBaseURL("app_process"); |
| 501 | |
| 502 | // Load app and start URL (not in the app). |
| 503 | const Extension* app = |
| 504 | LoadExtension(test_data_dir_.AppendASCII("app_process")); |
| 505 | ASSERT_TRUE(app); |
| 506 | ui_test_utils::NavigateToURLWithDisposition( |
| 507 | browser(), |
| 508 | base_url.Resolve("path3/container.html"), |
| 509 | CURRENT_TAB, |
| 510 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION | |
| 511 | ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 512 | EXPECT_FALSE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 513 | browser()->GetWebContentsAt(0)->GetRenderProcessHost()->GetID())); |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 514 | |
| 515 | // Wait for popup window to appear. |
| 516 | GURL app_url = base_url.Resolve("path1/empty.html"); |
| 517 | Browser* last_active_browser = BrowserList::GetLastActive(); |
| 518 | EXPECT_TRUE(last_active_browser); |
| 519 | ASSERT_NE(browser(), last_active_browser); |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 520 | WebContents* newtab = last_active_browser->GetSelectedWebContents(); |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 521 | EXPECT_TRUE(newtab); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 522 | if (!newtab->GetController().GetLastCommittedEntry() || |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 523 | newtab->GetController().GetLastCommittedEntry()->GetURL() != app_url) { |
[email protected] | 160f17f1 | 2011-10-19 00:40:00 | [diff] [blame] | 524 | // TODO(gbillock): This still looks racy. Need to make a custom |
| 525 | // observer to intercept new window creation and then look for |
| 526 | // NAV_ENTRY_COMMITTED on the new tab there. |
| 527 | ui_test_utils::WindowedNotificationObserver observer( |
| 528 | content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 529 | content::Source<NavigationController>(&(newtab->GetController()))); |
[email protected] | 160f17f1 | 2011-10-19 00:40:00 | [diff] [blame] | 530 | observer.Wait(); |
| 531 | } |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 532 | |
| 533 | // Popup window should be in the app's process. |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 534 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 535 | last_active_browser->GetWebContentsAt(0)->GetRenderProcessHost()-> |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 536 | GetID())); |
[email protected] | 118d312 | 2011-08-10 17:09:53 | [diff] [blame] | 537 | } |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 538 | |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 539 | // Tests that if an extension launches an app via chrome.tabs.create with an URL |
| 540 | // that's not in the app's extent but that redirects to it, we still end up with |
| 541 | // an app process. See http://crbug.com/99349 for more details. |
| 542 | IN_PROC_BROWSER_TEST_F(AppApiTest, OpenAppFromExtension) { |
| 543 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 544 | ASSERT_TRUE(StartTestServer()); |
| 545 | |
| 546 | LoadExtension(test_data_dir_.AppendASCII("app_process")); |
| 547 | const Extension* launcher = |
| 548 | LoadExtension(test_data_dir_.AppendASCII("app_launcher")); |
| 549 | |
| 550 | // There should be three navigations by the time the app page is loaded. |
| 551 | // 1. The extension launcher page. |
| 552 | // 2. The URL that the extension launches, which redirects. |
| 553 | // 3. The app's URL. |
| 554 | TestNavigationObserver test_navigation_observer( |
| 555 | content::NotificationService::AllSources(), |
| 556 | NULL, |
| 557 | 3); |
| 558 | |
| 559 | // Load the launcher extension, which should launch the app. |
| 560 | ui_test_utils::NavigateToURLWithDisposition( |
| 561 | browser(), |
| 562 | launcher->GetResourceURL("main.html"), |
| 563 | CURRENT_TAB, |
| 564 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 565 | |
| 566 | // Wait for app tab to be created and loaded. |
| 567 | test_navigation_observer.WaitForObservation( |
| 568 | base::Bind(&ui_test_utils::RunMessageLoop), |
| 569 | base::Bind(&MessageLoop::Quit, |
| 570 | base::Unretained(MessageLoopForUI::current()))); |
| 571 | |
| 572 | // App has loaded, and chrome.app.isInstalled should be true. |
| 573 | bool is_installed = false; |
| 574 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 575 | browser()->GetSelectedWebContents()->GetRenderViewHost(), L"", |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 576 | L"window.domAutomationController.send(chrome.app.isInstalled)", |
| 577 | &is_installed)); |
| 578 | ASSERT_TRUE(is_installed); |
| 579 | } |
| 580 | |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 581 | // Tests that if we have an app process (path1/container.html) with a non-app |
| 582 | // iframe (path3/iframe.html), then opening a link from that iframe to a new |
| 583 | // window to a same-origin non-app URL (path3/empty.html) should keep the window |
| 584 | // in the app process. |
| 585 | // This is in contrast to OpenAppFromIframe, since here the popup will not be |
| 586 | // missing special permissions and should be scriptable from the iframe. |
| 587 | // See http://crbug.com/92669 for more details. |
| 588 | IN_PROC_BROWSER_TEST_F(AppApiTest, OpenWebPopupFromWebIframe) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 589 | extensions::ProcessMap* process_map = |
| 590 | browser()->profile()->GetExtensionService()->process_map(); |
[email protected] | 718eab6 | 2011-10-05 21:16:52 | [diff] [blame] | 591 | |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 592 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 593 | ASSERT_TRUE(test_server()->Start()); |
| 594 | |
| 595 | GURL base_url = GetTestBaseURL("app_process"); |
| 596 | |
| 597 | // Load app and start URL (in the app). |
| 598 | const Extension* app = |
| 599 | LoadExtension(test_data_dir_.AppendASCII("app_process")); |
| 600 | ASSERT_TRUE(app); |
[email protected] | 160f17f1 | 2011-10-19 00:40:00 | [diff] [blame] | 601 | ui_test_utils::WindowedNotificationObserver observer( |
| 602 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 603 | content::NotificationService::AllSources()); |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 604 | ui_test_utils::NavigateToURLWithDisposition( |
| 605 | browser(), |
| 606 | base_url.Resolve("path1/container.html"), |
| 607 | CURRENT_TAB, |
| 608 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION | |
| 609 | ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 610 | content::RenderProcessHost* process = |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 611 | browser()->GetWebContentsAt(0)->GetRenderProcessHost(); |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 612 | EXPECT_TRUE(process_map->Contains(process->GetID())); |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 613 | |
| 614 | // Wait for popup window to appear. The new Browser may not have been |
| 615 | // added with SetLastActive, in which case we need to show it first. |
| 616 | // This is necessary for popup windows without a cross-site transition. |
| 617 | if (browser() == BrowserList::GetLastActive()) { |
| 618 | // Grab the second window and show it. |
| 619 | ASSERT_TRUE(BrowserList::size() == 2); |
| 620 | Browser* popup_browser = *(++BrowserList::begin()); |
| 621 | popup_browser->window()->Show(); |
| 622 | } |
| 623 | Browser* last_active_browser = BrowserList::GetLastActive(); |
| 624 | EXPECT_TRUE(last_active_browser); |
| 625 | ASSERT_NE(browser(), last_active_browser); |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 626 | WebContents* newtab = last_active_browser->GetSelectedWebContents(); |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 627 | EXPECT_TRUE(newtab); |
| 628 | GURL non_app_url = base_url.Resolve("path3/empty.html"); |
[email protected] | 160f17f1 | 2011-10-19 00:40:00 | [diff] [blame] | 629 | observer.Wait(); |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 630 | |
| 631 | // Popup window should be in the app's process. |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 632 | content::RenderProcessHost* popup_process = |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 633 | last_active_browser->GetWebContentsAt(0)->GetRenderProcessHost(); |
[email protected] | d55c238 | 2011-08-18 23:10:36 | [diff] [blame] | 634 | EXPECT_EQ(process, popup_process); |
| 635 | } |
| 636 | |
[email protected] | a344b76 | 2012-03-16 18:53:49 | [diff] [blame^] | 637 | // http://crbug.com/118502 |
| 638 | #if defined(OS_MACOSX) || defined(OS_LINUX) |
| 639 | #define MAYBE_ReloadAppAfterCrash DISABLED_ReloadAppAfterCrash |
| 640 | #else |
| 641 | #define MAYBE_ReloadAppAfterCrash ReloadAppAfterCrash |
| 642 | #endif |
| 643 | IN_PROC_BROWSER_TEST_F(AppApiTest, MAYBE_ReloadAppAfterCrash) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 644 | extensions::ProcessMap* process_map = |
| 645 | browser()->profile()->GetExtensionService()->process_map(); |
| 646 | |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 647 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 648 | ASSERT_TRUE(test_server()->Start()); |
| 649 | |
| 650 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process"))); |
| 651 | |
| 652 | GURL base_url = GetTestBaseURL("app_process"); |
| 653 | |
| 654 | // Load the app, chrome.app.isInstalled should be true. |
| 655 | ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 656 | WebContents* contents = browser()->GetWebContentsAt(0); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 657 | EXPECT_TRUE(process_map->Contains( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 658 | contents->GetRenderProcessHost()->GetID())); |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 659 | bool is_installed = false; |
| 660 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 661 | contents->GetRenderViewHost(), L"", |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 662 | L"window.domAutomationController.send(chrome.app.isInstalled)", |
| 663 | &is_installed)); |
| 664 | ASSERT_TRUE(is_installed); |
| 665 | |
| 666 | // Crash the tab and reload it, chrome.app.isInstalled should still be true. |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 667 | ui_test_utils::CrashTab(browser()->GetSelectedWebContents()); |
[email protected] | ae67374 | 2011-08-24 19:48:37 | [diff] [blame] | 668 | ui_test_utils::WindowedNotificationObserver observer( |
| 669 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 670 | content::Source<NavigationController>( |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 671 | &browser()->GetSelectedTabContentsWrapper()->web_contents()-> |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 672 | GetController())); |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 673 | browser()->Reload(CURRENT_TAB); |
[email protected] | ae67374 | 2011-08-24 19:48:37 | [diff] [blame] | 674 | observer.Wait(); |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 675 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 676 | contents->GetRenderViewHost(), L"", |
[email protected] | a09add5 | 2011-08-12 03:59:23 | [diff] [blame] | 677 | L"window.domAutomationController.send(chrome.app.isInstalled)", |
| 678 | &is_installed)); |
| 679 | ASSERT_TRUE(is_installed); |
| 680 | } |