OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits.h> | 5 #include <limits.h> |
6 #include <stddef.h> | 6 #include <stddef.h> |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 27 matching lines...) Expand all Loading... |
38 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" | 38 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
39 #include "chrome/test/base/ui_test_utils.h" | 39 #include "chrome/test/base/ui_test_utils.h" |
40 #include "components/prefs/pref_service.h" | 40 #include "components/prefs/pref_service.h" |
41 #include "content/public/browser/browser_context.h" | 41 #include "content/public/browser/browser_context.h" |
42 #include "content/public/browser/notification_service.h" | 42 #include "content/public/browser/notification_service.h" |
43 #include "content/public/browser/render_frame_host.h" | 43 #include "content/public/browser/render_frame_host.h" |
44 #include "content/public/browser/storage_partition.h" | 44 #include "content/public/browser/storage_partition.h" |
45 #include "content/public/common/page_zoom.h" | 45 #include "content/public/common/page_zoom.h" |
46 #include "content/public/common/url_constants.h" | 46 #include "content/public/common/url_constants.h" |
47 #include "content/public/test/browser_test_utils.h" | 47 #include "content/public/test/browser_test_utils.h" |
| 48 #include "content/public/test/test_navigation_observer.h" |
48 #include "extensions/browser/api_test_utils.h" | 49 #include "extensions/browser/api_test_utils.h" |
49 #include "extensions/browser/app_window/app_window.h" | 50 #include "extensions/browser/app_window/app_window.h" |
50 #include "extensions/browser/app_window/app_window_registry.h" | 51 #include "extensions/browser/app_window/app_window_registry.h" |
51 #include "extensions/common/manifest_constants.h" | 52 #include "extensions/common/manifest_constants.h" |
52 #include "extensions/common/test_util.h" | 53 #include "extensions/common/test_util.h" |
53 #include "extensions/test/extension_test_message_listener.h" | 54 #include "extensions/test/extension_test_message_listener.h" |
54 #include "extensions/test/result_catcher.h" | 55 #include "extensions/test/result_catcher.h" |
55 #include "net/test/embedded_test_server/embedded_test_server.h" | 56 #include "net/test/embedded_test_server/embedded_test_server.h" |
56 #include "ui/base/window_open_disposition.h" | 57 #include "ui/base/window_open_disposition.h" |
57 #include "ui/gfx/geometry/rect.h" | 58 #include "ui/gfx/geometry/rect.h" |
(...skipping 2084 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2142 browser()->tab_strip_model()->ActivateTabAt(0, true); | 2143 browser()->tab_strip_model()->ActivateTabAt(0, true); |
2143 EXPECT_EQ(first_web_contents, | 2144 EXPECT_EQ(first_web_contents, |
2144 browser()->tab_strip_model()->GetActiveWebContents()); | 2145 browser()->tab_strip_model()->GetActiveWebContents()); |
2145 browser()->tab_strip_model()->ActivateTabAt(1, true); | 2146 browser()->tab_strip_model()->ActivateTabAt(1, true); |
2146 EXPECT_EQ(second_web_contents, | 2147 EXPECT_EQ(second_web_contents, |
2147 browser()->tab_strip_model()->GetActiveWebContents()); | 2148 browser()->tab_strip_model()->GetActiveWebContents()); |
2148 | 2149 |
2149 EXPECT_EQ(url, second_web_contents->GetVisibleURL()); | 2150 EXPECT_EQ(url, second_web_contents->GetVisibleURL()); |
2150 } | 2151 } |
2151 | 2152 |
2152 // Window created by chrome.windows.create should be in the same SiteInstance | 2153 // Tests how chrome.windows.create behaves when setSelfAsOpener parameter is |
2153 // and BrowsingInstance as the opener - this is a regression test for | 2154 // used. setSelfAsOpener was introduced as a fix for https://crbug.com/713888 |
2154 // https://crbug.com/597750. | 2155 // and https://crbug.com/718489. This is a (slightly morphed) regression test |
2155 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowsCreateVsSiteInstance) { | 2156 // for https://crbug.com/597750. |
| 2157 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowsCreate_WithOpener) { |
2156 const extensions::Extension* extension = | 2158 const extensions::Extension* extension = |
2157 LoadExtension(test_data_dir_.AppendASCII("../simple_with_file")); | 2159 LoadExtension(test_data_dir_.AppendASCII("../simple_with_file")); |
2158 ASSERT_TRUE(extension); | 2160 ASSERT_TRUE(extension); |
2159 | 2161 |
2160 // Navigate a tab to an extension page. | 2162 // Navigate a tab to an extension page. |
2161 GURL extension_url = extension->GetResourceURL("file.html"); | 2163 GURL extension_url = extension->GetResourceURL("file.html"); |
2162 ui_test_utils::NavigateToURL(browser(), extension_url); | 2164 ui_test_utils::NavigateToURL(browser(), extension_url); |
2163 content::WebContents* old_contents = | 2165 content::WebContents* old_contents = |
2164 browser()->tab_strip_model()->GetActiveWebContents(); | 2166 browser()->tab_strip_model()->GetActiveWebContents(); |
2165 | 2167 |
2166 // Execute chrome.windows.create and store the new tab in |new_contents|. | 2168 // Execute chrome.windows.create and store the new tab in |new_contents|. |
2167 content::WebContents* new_contents = nullptr; | 2169 content::WebContents* new_contents = nullptr; |
2168 { | 2170 { |
2169 content::WebContentsAddedObserver observer; | 2171 content::WebContentsAddedObserver observer; |
2170 ASSERT_TRUE(content::ExecuteScript(old_contents, | 2172 std::string script = base::StringPrintf( |
2171 "window.name = 'old-contents';\n" | 2173 R"( window.name = 'old-contents'; |
2172 "chrome.windows.create({url: '" + | 2174 chrome.windows.create({url: '%s', setSelfAsOpener: true}); )", |
2173 extension_url.spec() + "'})")); | 2175 extension_url.spec().c_str()); |
| 2176 ASSERT_TRUE(content::ExecuteScript(old_contents, script)); |
2174 new_contents = observer.GetWebContents(); | 2177 new_contents = observer.GetWebContents(); |
2175 ASSERT_TRUE(content::WaitForLoadStop(new_contents)); | 2178 ASSERT_TRUE(content::WaitForLoadStop(new_contents)); |
2176 } | 2179 } |
2177 | 2180 |
2178 // Verify that the old and new tab are in the same process and SiteInstance. | 2181 // Navigate the old and the new tab to a web URL. |
2179 // Note: both test assertions are important - one observed failure mode was | 2182 ASSERT_TRUE(StartEmbeddedTestServer()); |
2180 // having the same process, but different SiteInstance. | 2183 GURL web_url1 = embedded_test_server()->GetURL("/title1.html"); |
| 2184 GURL web_url2 = embedded_test_server()->GetURL("/title2.html"); |
| 2185 { |
| 2186 content::TestNavigationObserver nav_observer(new_contents, 1); |
| 2187 ASSERT_TRUE(content::ExecuteScript( |
| 2188 new_contents, "window.location = '" + web_url1.spec() + "';")); |
| 2189 nav_observer.Wait(); |
| 2190 } |
| 2191 { |
| 2192 content::TestNavigationObserver nav_observer(old_contents, 1); |
| 2193 ASSERT_TRUE(content::ExecuteScript( |
| 2194 old_contents, "window.location = '" + web_url2.spec() + "';")); |
| 2195 nav_observer.Wait(); |
| 2196 } |
| 2197 EXPECT_EQ(web_url1, new_contents->GetMainFrame()->GetLastCommittedURL()); |
| 2198 EXPECT_EQ(web_url2, old_contents->GetMainFrame()->GetLastCommittedURL()); |
| 2199 |
| 2200 // Verify that the old and new tab are in the same process. |
2181 EXPECT_EQ(old_contents->GetMainFrame()->GetProcess(), | 2201 EXPECT_EQ(old_contents->GetMainFrame()->GetProcess(), |
2182 new_contents->GetMainFrame()->GetProcess()); | 2202 new_contents->GetMainFrame()->GetProcess()); |
2183 EXPECT_EQ(old_contents->GetMainFrame()->GetSiteInstance(), | |
2184 new_contents->GetMainFrame()->GetSiteInstance()); | |
2185 | 2203 |
2186 // Verify that the |new_contents| doesn't have a |window.opener| set. | 2204 // Verify the old and new contents are in the same BrowsingInstance. |
2187 bool window_opener_cast_to_bool = true; | 2205 EXPECT_TRUE( |
2188 EXPECT_TRUE(ExecuteScriptAndExtractBool( | 2206 old_contents->GetMainFrame()->GetSiteInstance()->IsRelatedSiteInstance( |
2189 new_contents, "window.domAutomationController.send(!!window.opener)", | 2207 new_contents->GetMainFrame()->GetSiteInstance())); |
2190 &window_opener_cast_to_bool)); | 2208 |
2191 EXPECT_FALSE(window_opener_cast_to_bool); | 2209 // Verify that the |new_contents| has |window.opener| set. |
| 2210 std::string location_of_opener; |
| 2211 EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 2212 new_contents, |
| 2213 "window.domAutomationController.send(window.opener.location.href)", |
| 2214 &location_of_opener)); |
| 2215 EXPECT_EQ(old_contents->GetMainFrame()->GetLastCommittedURL().spec(), |
| 2216 location_of_opener); |
2192 | 2217 |
2193 // Verify that |new_contents| can find |old_contents| using window.open/name. | 2218 // Verify that |new_contents| can find |old_contents| using window.open/name. |
2194 std::string location_of_other_window; | 2219 std::string location_of_other_window; |
2195 EXPECT_TRUE(ExecuteScriptAndExtractString( | 2220 EXPECT_TRUE(ExecuteScriptAndExtractString( |
2196 new_contents, | 2221 new_contents, |
2197 "var w = window.open('', 'old-contents');\n" | 2222 "var w = window.open('', 'old-contents');\n" |
2198 "window.domAutomationController.send(w.location.href);", | 2223 "window.domAutomationController.send(w.location.href);", |
2199 &location_of_other_window)); | 2224 &location_of_other_window)); |
2200 EXPECT_EQ(old_contents->GetMainFrame()->GetLastCommittedURL().spec(), | 2225 EXPECT_EQ(old_contents->GetMainFrame()->GetLastCommittedURL().spec(), |
2201 location_of_other_window); | 2226 location_of_other_window); |
2202 } | 2227 } |
2203 | 2228 |
| 2229 // Tests how chrome.windows.create behaves when setSelfAsOpener parameter is not |
| 2230 // used. setSelfAsOpener was introduced as a fix for https://crbug.com/713888 |
| 2231 // and https://crbug.com/718489. |
| 2232 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowsCreate_NoOpener) { |
| 2233 const extensions::Extension* extension = |
| 2234 LoadExtension(test_data_dir_.AppendASCII("../simple_with_file")); |
| 2235 ASSERT_TRUE(extension); |
| 2236 |
| 2237 // Navigate a tab to an extension page. |
| 2238 GURL extension_url = extension->GetResourceURL("file.html"); |
| 2239 ui_test_utils::NavigateToURL(browser(), extension_url); |
| 2240 content::WebContents* old_contents = |
| 2241 browser()->tab_strip_model()->GetActiveWebContents(); |
| 2242 |
| 2243 // Execute chrome.windows.create and store the new tab in |new_contents|. |
| 2244 content::WebContents* new_contents = nullptr; |
| 2245 { |
| 2246 content::WebContentsAddedObserver observer; |
| 2247 std::string script = base::StringPrintf( |
| 2248 R"( window.name = 'old-contents'; |
| 2249 chrome.windows.create({url: '%s'}); )", |
| 2250 extension_url.spec().c_str()); |
| 2251 ASSERT_TRUE(content::ExecuteScript(old_contents, script)); |
| 2252 new_contents = observer.GetWebContents(); |
| 2253 ASSERT_TRUE(content::WaitForLoadStop(new_contents)); |
| 2254 } |
| 2255 |
| 2256 // Navigate the new tab to a web URL. |
| 2257 // TODO(lukasza): This part of the test works around not yet fixed |
| 2258 // http://crbug.com/718489 (which means that frames that share the same |
| 2259 // renderer process can find each other even if they are not in the same |
| 2260 // browsing instance). |
| 2261 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 2262 GURL web_url = embedded_test_server()->GetURL("/title1.html"); |
| 2263 { |
| 2264 content::TestNavigationObserver nav_observer(new_contents, 1); |
| 2265 ASSERT_TRUE(content::ExecuteScript( |
| 2266 new_contents, "window.location = '" + web_url.spec() + "';")); |
| 2267 nav_observer.Wait(); |
| 2268 } |
| 2269 EXPECT_EQ(web_url, new_contents->GetMainFrame()->GetLastCommittedURL()); |
| 2270 |
| 2271 // Verify the old and new contents are NOT in the same BrowsingInstance. |
| 2272 EXPECT_FALSE( |
| 2273 old_contents->GetMainFrame()->GetSiteInstance()->IsRelatedSiteInstance( |
| 2274 new_contents->GetMainFrame()->GetSiteInstance())); |
| 2275 |
| 2276 // Verify that the |new_contents| doesn't have |window.opener| set. |
| 2277 bool opener_as_bool; |
| 2278 EXPECT_TRUE(ExecuteScriptAndExtractBool( |
| 2279 new_contents, "window.domAutomationController.send(!!window.opener)", |
| 2280 &opener_as_bool)); |
| 2281 EXPECT_FALSE(opener_as_bool); |
| 2282 |
| 2283 // Verify that |new_contents| can find |old_contents| using window.open/name. |
| 2284 content::WebContentsAddedObserver newly_opened_window_observer; |
| 2285 std::string location_of_opened_window; |
| 2286 EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 2287 new_contents, |
| 2288 "w = window.open('', 'old-contents');" |
| 2289 "window.domAutomationController.send(w.location.href);", |
| 2290 &location_of_opened_window)); |
| 2291 EXPECT_EQ(url::kAboutBlankURL, location_of_opened_window); |
| 2292 ASSERT_TRUE(newly_opened_window_observer.GetWebContents()); |
| 2293 } |
| 2294 |
2204 } // namespace extensions | 2295 } // namespace extensions |
OLD | NEW |