1 // Copyright (c) 2014 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.
5 #include "base/command_line.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/browser/accessibility/browser_accessibility.h"
9 #include "content/browser/accessibility/browser_accessibility_manager.h"
10 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
11 #include "content/browser/frame_host/cross_process_frame_connector.h"
12 #include "content/browser/frame_host/frame_tree.h"
13 #include "content/browser/frame_host/render_frame_proxy_host.h"
14 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/site_per_process_browsertest.h"
17 #include "content/browser/web_contents/web_contents_impl.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/web_contents_observer.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/test/content_browser_test.h"
24 #include "content/public/test/content_browser_test_utils.h"
25 #include "content/public/test/test_utils.h"
26 #include "content/shell/browser/shell.h"
27 #include "content/test/accessibility_browser_test_utils.h"
28 #include "content/test/content_browser_test_utils_internal.h"
29 #include "net/dns/mock_host_resolver.h"
31 #include "url/url_constants.h"
35 class SitePerProcessAccessibilityBrowserTest
36 : public SitePerProcessBrowserTest
{
38 SitePerProcessAccessibilityBrowserTest() {}
41 bool AccessibilityTreeContainsDocTitle(
42 BrowserAccessibility
* node
,
43 const std::string
& title
) {
44 if (node
->GetStringAttribute(ui::AX_ATTR_DOC_TITLE
) == title
)
46 for (unsigned i
= 0; i
< node
->PlatformChildCount(); i
++) {
47 if (AccessibilityTreeContainsDocTitle(node
->PlatformGetChild(i
), title
))
53 // Utility function to determine if an accessibility tree has finished loading
54 // or if the tree represents a page that hasn't finished loading yet.
55 bool AccessibilityTreeIsLoaded(BrowserAccessibilityManager
* manager
) {
56 BrowserAccessibility
* root
= manager
->GetRoot();
57 return (root
->GetFloatAttribute(ui::AX_ATTR_DOC_LOADING_PROGRESS
) == 1.0 &&
58 root
->GetStringAttribute(ui::AX_ATTR_DOC_URL
) != url::kAboutBlankURL
);
61 // Times out on Android, not clear if it's an actual bug or just slow.
62 #if defined(OS_ANDROID)
63 #define MAYBE_CrossSiteIframeAccessibility DISABLED_CrossSiteIframeAccessibility
65 #define MAYBE_CrossSiteIframeAccessibility CrossSiteIframeAccessibility
67 IN_PROC_BROWSER_TEST_F(SitePerProcessAccessibilityBrowserTest
,
68 MAYBE_CrossSiteIframeAccessibility
) {
69 // Enable full accessibility for all current and future WebContents.
70 BrowserAccessibilityState::GetInstance()->EnableAccessibility();
72 host_resolver()->AddRule("*", "127.0.0.1");
73 ASSERT_TRUE(test_server()->Start());
74 GURL
main_url(test_server()->GetURL("files/site_per_process_main.html"));
75 NavigateToURL(shell(), main_url
);
77 // It is safe to obtain the root frame tree node here, as it doesn't change.
79 static_cast<WebContentsImpl
*>(shell()->web_contents())->
80 GetFrameTree()->root();
82 // Load same-site page into iframe.
83 FrameTreeNode
* child
= root
->child_at(0);
84 GURL
http_url(test_server()->GetURL("files/title1.html"));
85 NavigateFrameToURL(child
, http_url
);
87 // Load cross-site page into iframe.
88 RenderFrameHostImpl
* child_rfh
=
89 child
->render_manager()->current_frame_host();
90 RenderFrameDeletedObserver
deleted_observer(child_rfh
);
91 GURL::Replacements replace_host
;
92 GURL
cross_site_url(test_server()->GetURL("files/title2.html"));
93 replace_host
.SetHostStr("foo.com");
94 cross_site_url
= cross_site_url
.ReplaceComponents(replace_host
);
95 NavigateFrameToURL(root
->child_at(0), cross_site_url
);
97 // Ensure that we have created a new process for the subframe.
98 ASSERT_EQ(2U, root
->child_count());
99 SiteInstance
* site_instance
= child
->current_frame_host()->GetSiteInstance();
100 EXPECT_NE(shell()->web_contents()->GetSiteInstance(), site_instance
);
102 // Wait until the iframe completes the swap.
103 deleted_observer
.WaitUntilDeleted();
105 RenderFrameHostImpl
* main_frame
= static_cast<RenderFrameHostImpl
*>(
106 shell()->web_contents()->GetMainFrame());
107 BrowserAccessibilityManager
* main_frame_manager
=
108 main_frame
->browser_accessibility_manager();
109 VLOG(1) << "Main frame accessibility tree:\n"
110 << main_frame_manager
->SnapshotAXTreeForTesting().ToString();
112 RenderFrameHostImpl
* child_frame
= static_cast<RenderFrameHostImpl
*>(
113 child
->current_frame_host());
114 while (!AccessibilityTreeContainsDocTitle(main_frame_manager
->GetRoot(),
115 "Title Of Awesomeness")) {
116 AccessibilityNotificationWaiter
accessibility_waiter(main_frame
,
118 accessibility_waiter
.ListenToAdditionalFrame(child_frame
);
119 accessibility_waiter
.WaitForNotification();
122 // Assert that we can walk from the main frame down into the child frame
123 // directly, getting correct roles and data along the way.
124 BrowserAccessibility
* ax_root
= main_frame_manager
->GetRoot();
125 EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA
, ax_root
->GetRole());
126 ASSERT_EQ(1U, ax_root
->PlatformChildCount());
128 BrowserAccessibility
* ax_group
= ax_root
->PlatformGetChild(0);
129 EXPECT_EQ(ui::AX_ROLE_GROUP
, ax_group
->GetRole());
130 ASSERT_EQ(2U, ax_group
->PlatformChildCount());
132 BrowserAccessibility
* ax_iframe
= ax_group
->PlatformGetChild(0);
133 EXPECT_EQ(ui::AX_ROLE_IFRAME
, ax_iframe
->GetRole());
134 ASSERT_EQ(1U, ax_iframe
->PlatformChildCount());
136 BrowserAccessibility
* ax_child_frame_root
= ax_iframe
->PlatformGetChild(0);
137 EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA
, ax_child_frame_root
->GetRole());
138 ASSERT_EQ(1U, ax_child_frame_root
->PlatformChildCount());
139 EXPECT_EQ("Title Of Awesomeness",
140 ax_child_frame_root
->GetStringAttribute(ui::AX_ATTR_NAME
));
142 BrowserAccessibility
* ax_child_frame_group
=
143 ax_child_frame_root
->PlatformGetChild(0);
144 EXPECT_EQ(ui::AX_ROLE_GROUP
, ax_child_frame_group
->GetRole());
145 ASSERT_EQ(1U, ax_child_frame_group
->PlatformChildCount());
147 BrowserAccessibility
* ax_child_frame_static_text
=
148 ax_child_frame_group
->PlatformGetChild(0);
149 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT
, ax_child_frame_static_text
->GetRole());
150 ASSERT_EQ(0U, ax_child_frame_static_text
->PlatformChildCount());
152 // Last, check that the parent of the child frame root is correct.
153 EXPECT_EQ(ax_child_frame_root
->GetParent(), ax_iframe
);
156 } // namespace content