1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297

content / browser / accessibility / site_per_process_accessibility_browsertest.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/cross_process_frame_connector.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/renderer_host/render_frame_proxy_host.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
#include "content/browser/site_per_process_browsertest.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/accessibility_notification_waiter.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/scoped_accessibility_mode_override.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/render_document_feature.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/accessibility/platform/browser_accessibility.h"
#include "ui/accessibility/platform/browser_accessibility_manager.h"
#include "url/gurl.h"
#include "url/url_constants.h"

// These tests time out on Android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_SitePerProcessAccessibilityBrowserTest \
  DISABLED_SitePerProcessAccessibilityBrowserTest
#else
#define MAYBE_SitePerProcessAccessibilityBrowserTest \
  SitePerProcessAccessibilityBrowserTest
#endif
// "All/DISABLED_SitePerProcessAccessibilityBrowserTest" does not work. We need
// "DISABLED_All/...". TODO(crbug.com/40136187) delete when fixed.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_All DISABLED_All
#else
#define MAYBE_All All
#endif

namespace content {

class MAYBE_SitePerProcessAccessibilityBrowserTest
    : public SitePerProcessBrowserTest {
 public:
  MAYBE_SitePerProcessAccessibilityBrowserTest() {}

  void LoadCrossSitePageIntoFrame(FrameTreeNode* frame_tree_node,
                                  const std::string& relative_url,
                                  const std::string& host) {
    // Load cross-site page into iframe.
    RenderFrameHostImpl* child_rfh =
        frame_tree_node->render_manager()->current_frame_host();
    RenderFrameDeletedObserver deleted_observer(child_rfh);
    GURL cross_site_url(embedded_test_server()->GetURL(host, relative_url));
    EXPECT_TRUE(NavigateToURLFromRenderer(frame_tree_node, cross_site_url));

    // Ensure that we have created a new process for the subframe.
    SiteInstance* site_instance =
        frame_tree_node->current_frame_host()->GetSiteInstance();
    EXPECT_NE(shell()->web_contents()->GetSiteInstance(), site_instance);

    // Wait until the iframe completes the swap.
    deleted_observer.WaitUntilDeleted();
  }
};

IN_PROC_BROWSER_TEST_P(MAYBE_SitePerProcessAccessibilityBrowserTest,
                       CrossSiteIframeAccessibility) {
  // Enable full accessibility for all current and future WebContents.
  ScopedAccessibilityModeOverride mode_override(ui::kAXModeComplete);

  GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  // It is safe to obtain the root frame tree node here, as it doesn't change.
  FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
                            ->GetPrimaryFrameTree()
                            .root();

  // Load same-site page into iframe.
  FrameTreeNode* child = root->child_at(0);
  GURL http_url(embedded_test_server()->GetURL("/title1.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(child, http_url));

  // Load cross-site page into iframe and wait for text from that
  // page to appear in the accessibility tree.
  LoadCrossSitePageIntoFrame(child, "/title2.html", "foo.com");
  WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
                                                "Title Of Awesomeness");

  RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>(
      shell()->web_contents()->GetPrimaryMainFrame());
  ui::BrowserAccessibilityManager* main_frame_manager =
      main_frame->browser_accessibility_manager();
  VLOG(1) << "Main frame accessibility tree:\n"
          << main_frame_manager->SnapshotAXTreeForTesting().ToString();

  // Assert that we can walk from the main frame down into the child frame
  // directly, getting correct roles and data along the way.
  ui::BrowserAccessibility* ax_root =
      main_frame_manager->GetBrowserAccessibilityRoot();
  EXPECT_EQ(ax::mojom::Role::kRootWebArea, ax_root->GetRole());
  ASSERT_EQ(1U, ax_root->PlatformChildCount());

  ui::BrowserAccessibility* ax_group = ax_root->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kGenericContainer, ax_group->GetRole());
  ASSERT_EQ(2U, ax_group->PlatformChildCount());

  ui::BrowserAccessibility* ax_iframe = ax_group->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kIframe, ax_iframe->GetRole());
  ASSERT_EQ(1U, ax_iframe->PlatformChildCount());

  ui::BrowserAccessibility* ax_child_frame_root =
      ax_iframe->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kRootWebArea, ax_child_frame_root->GetRole());
  ASSERT_EQ(1U, ax_child_frame_root->PlatformChildCount());
  EXPECT_EQ("Title Of Awesomeness", ax_child_frame_root->GetStringAttribute(
                                        ax::mojom::StringAttribute::kName));

  ui::BrowserAccessibility* ax_child_frame_group =
      ax_child_frame_root->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kGenericContainer,
            ax_child_frame_group->GetRole());
  ASSERT_EQ(1U, ax_child_frame_group->PlatformChildCount());

  ui::BrowserAccessibility* ax_child_frame_static_text =
      ax_child_frame_group->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kStaticText,
            ax_child_frame_static_text->GetRole());
  ASSERT_EQ(0U, ax_child_frame_static_text->PlatformChildCount());

  // Last, check that the parent of the child frame root is correct.
  EXPECT_EQ(ax_child_frame_root->PlatformGetParent(), ax_iframe);
}

// TODO(aboxhall): Flaky test, discuss with dmazzoni
IN_PROC_BROWSER_TEST_P(MAYBE_SitePerProcessAccessibilityBrowserTest,
                       DISABLED_TwoCrossSiteNavigations) {
  // Enable full accessibility for all current and future WebContents.
  ScopedAccessibilityModeOverride mode_override(ui::kAXModeComplete);

  GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  // It is safe to obtain the root frame tree node here, as it doesn't change.
  FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
                            ->GetPrimaryFrameTree()
                            .root();

  // Load first cross-site page into iframe and wait for text from that
  // page to appear in the accessibility tree.
  FrameTreeNode* child = root->child_at(0);
  LoadCrossSitePageIntoFrame(child, "/title1.html", "foo.com");
  WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
                                                "This page has no title.");

  // Load second cross-site page into iframe and wait for text from that
  // page to appear in the accessibility tree. If this succeeds and doesn't
  // time out, the test passes.
  LoadCrossSitePageIntoFrame(child, "/title2.html", "bar.com");
  WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
                                                "Title Of Awesomeness");
}

// Ensure that enabling accessibility and doing a remote-to-local main frame
// navigation doesn't crash.  See https://crbug.com/762824.
IN_PROC_BROWSER_TEST_P(MAYBE_SitePerProcessAccessibilityBrowserTest,
                       RemoteToLocalMainFrameNavigation) {
  // Enable full accessibility for all current and future WebContents.
  ScopedAccessibilityModeOverride mode_override(ui::kAXModeComplete);

  GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));
  WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
                                                "This page has no title.");

  // Open a new tab for b.com and wait for it to load.  This will create a
  // proxy in b.com for the original (opener) tab.
  GURL b_url(embedded_test_server()->GetURL("b.com", "/title2.html"));
  Shell* new_shell = OpenPopup(shell()->web_contents(), b_url, "popup");
  WaitForAccessibilityTreeToContainNodeWithName(new_shell->web_contents(),
                                                "Title Of Awesomeness");

  // Navigate the original tab to b.com as well.  This performs a
  // remote-to-local main frame navigation in b.com and shouldn't crash.
  EXPECT_TRUE(NavigateToURL(shell(), b_url));
  WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
                                                "Title Of Awesomeness");
}

INSTANTIATE_TEST_SUITE_P(
    MAYBE_All,
    MAYBE_SitePerProcessAccessibilityBrowserTest,
    ::testing::ValuesIn(RenderDocumentFeatureLevelValues()));

class MAYBE_SitePerProcessAccessibilityDeviceScaleFactorBrowserTest
    : public MAYBE_SitePerProcessAccessibilityBrowserTest {
 public:
  MAYBE_SitePerProcessAccessibilityDeviceScaleFactorBrowserTest() = default;

  void SetUp() override {
    EnablePixelOutput(device_scale_factor_);
    MAYBE_SitePerProcessAccessibilityBrowserTest::SetUp();
  }

 protected:
  static constexpr float device_scale_factor_ = 2.f;
};

IN_PROC_BROWSER_TEST_P(
    MAYBE_SitePerProcessAccessibilityDeviceScaleFactorBrowserTest,
    CrossSiteIframeCoordinates) {
  // Enable full accessibility for all current and future WebContents.
  ScopedAccessibilityModeOverride mode_override(ui::kAXModeComplete);

  GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  // It is safe to obtain the root frame tree node here, as it doesn't change.
  FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
                            ->GetPrimaryFrameTree()
                            .root();

  // Load cross-site page into iframe and wait for text from that
  // page to appear in the accessibility tree.
  FrameTreeNode* child = root->child_at(0);
  LoadCrossSitePageIntoFrame(child, "/title2.html", "foo.com");
  WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
                                                "Title Of Awesomeness");
  child = root->child_at(0);

  RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>(
      shell()->web_contents()->GetPrimaryMainFrame());
  ui::BrowserAccessibilityManager* main_frame_manager =
      main_frame->browser_accessibility_manager();
  VLOG(1) << "Main frame accessibility tree:\n"
          << main_frame_manager->SnapshotAXTreeForTesting().ToString();

  // Assert that we can walk from the main frame down into the child frame
  // directly, getting correct roles and data along the way.
  ui::BrowserAccessibility* ax_root =
      main_frame_manager->GetBrowserAccessibilityRoot();
  EXPECT_EQ(ax::mojom::Role::kRootWebArea, ax_root->GetRole());
  ASSERT_EQ(1U, ax_root->PlatformChildCount());

  ui::BrowserAccessibility* ax_group = ax_root->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kGenericContainer, ax_group->GetRole());
  ASSERT_EQ(2U, ax_group->PlatformChildCount());

  ui::BrowserAccessibility* ax_iframe = ax_group->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kIframe, ax_iframe->GetRole());
  ASSERT_EQ(1U, ax_iframe->PlatformChildCount());

  ui::BrowserAccessibility* ax_child_frame_root =
      ax_iframe->PlatformGetChild(0);
  EXPECT_EQ(ax::mojom::Role::kRootWebArea, ax_child_frame_root->GetRole());
  ASSERT_EQ(1U, ax_child_frame_root->PlatformChildCount());

  // Get the relative iframe rect in blink pixels.
  gfx::Rect iframe_rect_root_relative_blink_pixels = ax_iframe->GetBoundsRect(
      ui::AXCoordinateSystem::kRootFrame, ui::AXClippingBehavior::kUnclipped);
  gfx::Rect iframe_rect_root_relative_physical_pixels;
  iframe_rect_root_relative_physical_pixels =
      iframe_rect_root_relative_blink_pixels;

  // Get the view bounds in screen coordinate DIPs, then ensure the offsetting
  // done by ui::AXCoordinateSystem::kScreenPhysicalPixels produces the correct
  // rect.
  gfx::Rect view_bounds = main_frame->GetView()->GetViewBounds();
  gfx::Rect iframe_rect_physical_screen_pixels =
      iframe_rect_root_relative_physical_pixels +
      gfx::ScaleToFlooredPoint(view_bounds.origin(), device_scale_factor_)
          .OffsetFromOrigin();

  EXPECT_EQ(iframe_rect_physical_screen_pixels.origin(),
            ax_child_frame_root
                ->GetBoundsRect(ui::AXCoordinateSystem::kScreenPhysicalPixels,
                                ui::AXClippingBehavior::kUnclipped)
                .origin());
}

INSTANTIATE_TEST_SUITE_P(
    MAYBE_All,
    MAYBE_SitePerProcessAccessibilityDeviceScaleFactorBrowserTest,
    ::testing::ValuesIn(RenderDocumentFeatureLevelValues()));
}  // namespace content