| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/command_line.h" |
| 6 #include "chrome/browser/ui/browser.h" |
| 7 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 8 #include "chrome/test/base/in_process_browser_test.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/content_browser_test_utils.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "net/dns/mock_host_resolver.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 class SitePerProcessInteractiveBrowserTest : public InProcessBrowserTest { |
| 20 public: |
| 21 SitePerProcessInteractiveBrowserTest() {} |
| 22 ~SitePerProcessInteractiveBrowserTest() override {} |
| 23 |
| 24 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 25 content::IsolateAllSitesForTesting(command_line); |
| 26 } |
| 27 |
| 28 void SetUpOnMainThread() override { |
| 29 host_resolver()->AddRule("*", "127.0.0.1"); |
| 30 |
| 31 // Add content/test/data for cross_site_iframe_factory.html |
| 32 embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data"); |
| 33 |
| 34 ASSERT_TRUE(embedded_test_server()->Start()); |
| 35 } |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(SitePerProcessInteractiveBrowserTest); |
| 39 }; |
| 40 |
| 41 // Check that document.hasFocus() works properly with out-of-process iframes. |
| 42 // The test builds a page with four cross-site frames and then focuses them one |
| 43 // by one, checking the value of document.hasFocus() in all frames. For any |
| 44 // given focused frame, document.hasFocus() should return true for that frame |
| 45 // and all its ancestor frames. |
| 46 IN_PROC_BROWSER_TEST_F(SitePerProcessInteractiveBrowserTest, DocumentHasFocus) { |
| 47 GURL main_url(embedded_test_server()->GetURL( |
| 48 "a.com", "/cross_site_iframe_factory.html?a(b(c),d)")); |
| 49 ui_test_utils::NavigateToURL(browser(), main_url); |
| 50 |
| 51 content::WebContents* web_contents = |
| 52 browser()->tab_strip_model()->GetActiveWebContents(); |
| 53 |
| 54 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 55 content::RenderFrameHost* child1 = ChildFrameAt(main_frame, 0); |
| 56 ASSERT_NE(nullptr, child1); |
| 57 content::RenderFrameHost* child2 = ChildFrameAt(main_frame, 1); |
| 58 ASSERT_NE(nullptr, child2); |
| 59 content::RenderFrameHost* grandchild = ChildFrameAt(child1, 0); |
| 60 ASSERT_NE(nullptr, grandchild); |
| 61 |
| 62 EXPECT_NE(main_frame->GetSiteInstance(), child1->GetSiteInstance()); |
| 63 EXPECT_NE(main_frame->GetSiteInstance(), child2->GetSiteInstance()); |
| 64 EXPECT_NE(child1->GetSiteInstance(), grandchild->GetSiteInstance()); |
| 65 |
| 66 // Helper function to check document.hasFocus() for a given frame. |
| 67 auto document_has_focus = [](content::RenderFrameHost* rfh) -> bool { |
| 68 bool has_focus = false; |
| 69 EXPECT_TRUE(ExecuteScriptAndExtractBool( |
| 70 rfh, |
| 71 "window.domAutomationController.send(document.hasFocus())", |
| 72 &has_focus)); |
| 73 return has_focus; |
| 74 }; |
| 75 |
| 76 // The main frame should be focused to start with. |
| 77 EXPECT_EQ(main_frame, web_contents->GetFocusedFrame()); |
| 78 |
| 79 EXPECT_TRUE(document_has_focus(main_frame)); |
| 80 EXPECT_FALSE(document_has_focus(child1)); |
| 81 EXPECT_FALSE(document_has_focus(grandchild)); |
| 82 EXPECT_FALSE(document_has_focus(child2)); |
| 83 |
| 84 EXPECT_TRUE(ExecuteScript(child1, "window.focus();")); |
| 85 EXPECT_EQ(child1, web_contents->GetFocusedFrame()); |
| 86 |
| 87 EXPECT_TRUE(document_has_focus(main_frame)); |
| 88 EXPECT_TRUE(document_has_focus(child1)); |
| 89 EXPECT_FALSE(document_has_focus(grandchild)); |
| 90 EXPECT_FALSE(document_has_focus(child2)); |
| 91 |
| 92 EXPECT_TRUE(ExecuteScript(grandchild, "window.focus();")); |
| 93 EXPECT_EQ(grandchild, web_contents->GetFocusedFrame()); |
| 94 |
| 95 EXPECT_TRUE(document_has_focus(main_frame)); |
| 96 EXPECT_TRUE(document_has_focus(child1)); |
| 97 EXPECT_TRUE(document_has_focus(grandchild)); |
| 98 EXPECT_FALSE(document_has_focus(child2)); |
| 99 |
| 100 EXPECT_TRUE(ExecuteScript(child2, "window.focus();")); |
| 101 EXPECT_EQ(child2, web_contents->GetFocusedFrame()); |
| 102 |
| 103 EXPECT_TRUE(document_has_focus(main_frame)); |
| 104 EXPECT_FALSE(document_has_focus(child1)); |
| 105 EXPECT_FALSE(document_has_focus(grandchild)); |
| 106 EXPECT_TRUE(document_has_focus(child2)); |
| 107 } |
| 108 |
| OLD | NEW |