app_process_apitest.cc revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// 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 5#include "base/utf_string_conversions.h" 6#include "chrome/browser/browser_list.h" 7#include "chrome/browser/extensions/extension_apitest.h" 8#include "chrome/browser/extensions/extension_host.h" 9#include "chrome/browser/extensions/extension_process_manager.h" 10#include "chrome/browser/profile.h" 11#include "chrome/browser/renderer_host/render_view_host.h" 12#include "chrome/browser/tab_contents/tab_contents.h" 13#include "chrome/browser/ui/browser.h" 14#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 { 19}; 20 21// Simulates a page calling window.open on an URL, and waits for the navigation. 22static void WindowOpenHelper(Browser* browser, 23 RenderViewHost* opener_host, 24 const GURL& url, 25 bool newtab_process_should_equal_opener) { 26 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( 27 opener_host, L"", L"window.open('" + UTF8ToWide(url.spec()) + L"');")); 28 29 // 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); 36 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()); 40 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()); 44} 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; 49 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( 50 contents->render_view_host(), L"", 51 L"window.addEventListener('unload', function() {" 52 L" window.domAutomationController.send(true);" 53 L"}, false);" 54 L"window.location = '" + UTF8ToWide(url.spec()) + L"';", 55 &result)); 56 ASSERT_TRUE(result); 57 58 if (!contents->controller().GetLastCommittedEntry() || 59 contents->controller().GetLastCommittedEntry()->url() != url) 60 ui_test_utils::WaitForNavigation(&contents->controller()); 61 EXPECT_EQ(url, contents->controller().GetLastCommittedEntry()->url()); 62} 63 64#if defined(OS_WIN) 65// AppProcess sometimes hangs on Windows 66// http://crbug.com/58810 67#define MAYBE_AppProcess DISABLED_AppProcess 68#else 69#define MAYBE_AppProcess AppProcess 70#endif 71IN_PROC_BROWSER_TEST_F(AppApiTest, MAYBE_AppProcess) { 72 CommandLine::ForCurrentProcess()->AppendSwitch( 73 switches::kDisablePopupBlocking); 74 75 host_resolver()->AddRule("*", "127.0.0.1"); 76 ASSERT_TRUE(test_server()->Start()); 77 78 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process"))); 79 80 // Open two tabs in the app, one outside it. 81 GURL base_url = test_server()->GetURL( 82 "files/extensions/api_test/app_process/"); 83 84 // The app under test acts on URLs whose host is "localhost", 85 // so the URLs we navigate to must have host "localhost". 86 GURL::Replacements replace_host; 87 replace_host.SetHostStr("localhost"); 88 base_url = base_url.ReplaceComponents(replace_host); 89 90 browser()->NewTab(); 91 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); 92 browser()->NewTab(); 93 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html")); 94 browser()->NewTab(); 95 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path3/empty.html")); 96 97 // The extension should have opened 3 new tabs. Including the original blank 98 // tab, we now have 4 tabs. Two should be part of the extension app, and 99 // grouped in the same process. 100 ASSERT_EQ(4, browser()->tab_count()); 101 RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host(); 102 103 EXPECT_EQ(host->process(), 104 browser()->GetTabContentsAt(2)->render_view_host()->process()); 105 EXPECT_NE(host->process(), 106 browser()->GetTabContentsAt(3)->render_view_host()->process()); 107 108 // Now let's do the same using window.open. The same should happen. 109 ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile())); 110 WindowOpenHelper(browser(), host, 111 base_url.Resolve("path1/empty.html"), true); 112 WindowOpenHelper(browser(), host, 113 base_url.Resolve("path2/empty.html"), true); 114 // TODO(creis): This should open in a new process (i.e., false for the last 115 // argument), but we temporarily avoid swapping processes away from an app 116 // until we're able to restore window.opener if the page later returns to an 117 // in-app URL. See crbug.com/65953. 118 WindowOpenHelper(browser(), host, 119 base_url.Resolve("path3/empty.html"), true); 120 121 // Now let's have these pages navigate, into or out of the extension web 122 // extent. They should switch processes. 123 const GURL& app_url(base_url.Resolve("path1/empty.html")); 124 const GURL& non_app_url(base_url.Resolve("path3/empty.html")); 125 NavigateTabHelper(browser()->GetTabContentsAt(2), non_app_url); 126 NavigateTabHelper(browser()->GetTabContentsAt(3), app_url); 127 // TODO(creis): This should swap out of the app's process (i.e., EXPECT_NE), 128 // but we temporarily avoid swapping away from an app in case it needs to 129 // communicate with window.opener later. See crbug.com/65953. 130 EXPECT_EQ(host->process(), 131 browser()->GetTabContentsAt(2)->render_view_host()->process()); 132 EXPECT_EQ(host->process(), 133 browser()->GetTabContentsAt(3)->render_view_host()->process()); 134 135 // If one of the popup tabs navigates back to the app, window.opener should 136 // be valid. 137 NavigateTabHelper(browser()->GetTabContentsAt(6), app_url); 138 EXPECT_EQ(host->process(), 139 browser()->GetTabContentsAt(6)->render_view_host()->process()); 140 bool windowOpenerValid = false; 141 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( 142 browser()->GetTabContentsAt(6)->render_view_host(), L"", 143 L"window.domAutomationController.send(window.opener != null)", 144 &windowOpenerValid)); 145 ASSERT_TRUE(windowOpenerValid); 146} 147