File: pdf_extension_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (382 lines) | stat: -rw-r--r-- 15,915 bytes parent folder | download | duplicates (6)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2021 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/run_loop.h"
#include "base/test/run_until.h"
#include "base/test/with_feature_override.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/pdf/pdf_extension_test_base.h"
#include "chrome/browser/pdf/pdf_extension_test_util.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/pdf/browser/pdf_frame_util.h"
#include "content/public/browser/browser_plugin_guest_manager.h"
#include "content/public/browser/focused_node_details.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/focus_changed_observer.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "pdf/pdf_features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/input/focus_type.mojom-shared.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "url/gurl.h"

#if defined(TOOLKIT_VIEWS) && defined(USE_AURA)
#include "content/public/browser/touch_selection_controller_client_manager.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"
#include "ui/events/gesture_event_details.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/test/geometry_util.h"
#include "ui/gfx/selection_bound.h"
#include "ui/touch_selection/touch_selection_controller.h"
#include "ui/views/touchui/touch_selection_menu_views.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"
#endif  // defined(TOOLKIT_VIEWS) && defined(USE_AURA)

namespace {

using ::pdf_extension_test_util::ConvertPageCoordToScreenCoord;
using ::pdf_extension_test_util::SetInputFocusOnPlugin;

class PDFExtensionInteractiveUITest : public base::test::WithFeatureOverride,
                                      public PDFExtensionTestBase {
 public:
  PDFExtensionInteractiveUITest()
      : base::test::WithFeatureOverride(chrome_pdf::features::kPdfOopif) {}

  void SetUpCommandLine(base::CommandLine* command_line) override {
    PDFExtensionTestBase::SetUpCommandLine(command_line);

    content::IsolateAllSitesForTesting(command_line);
  }

  content::FocusedNodeDetails TabAndWait(content::WebContents* web_contents,
                                         bool forward) {
    content::FocusChangedObserver focus_observer(web_contents);
    if (!ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB,
                                         /*control=*/false,
                                         /*shift=*/!forward,
                                         /*alt=*/false,
                                         /*command=*/false)) {
      ADD_FAILURE() << "Failed to send key press";
      return {};
    }
    return focus_observer.Wait();
  }

  bool UseOopif() const override { return GetParam(); }
};

class TabChangedWaiter : public TabStripModelObserver {
 public:
  explicit TabChangedWaiter(Browser* browser) {
    browser->tab_strip_model()->AddObserver(this);
  }
  TabChangedWaiter(const TabChangedWaiter&) = delete;
  TabChangedWaiter& operator=(const TabChangedWaiter&) = delete;
  ~TabChangedWaiter() override = default;

  void Wait() { run_loop_.Run(); }

  // TabStripModelObserver:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override {
    if (change.type() == TabStripModelChange::kSelectionOnly)
      run_loop_.Quit();
  }

 private:
  base::RunLoop run_loop_;
};

}  // namespace

// TODO(crbug.com/333802743): re-enable the test
// For crbug.com/1038918
IN_PROC_BROWSER_TEST_P(PDFExtensionInteractiveUITest,
                       DISABLED_CtrlPageUpDownSwitchesTabs) {
  content::RenderFrameHost* extension_host = LoadPdfInNewTabGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test.pdf"));
  ASSERT_TRUE(extension_host);

  auto* tab_strip_model = browser()->tab_strip_model();
  ASSERT_EQ(2, tab_strip_model->count());
  EXPECT_EQ(1, tab_strip_model->active_index());

  SetInputFocusOnPlugin(extension_host, GetEmbedderWebContents());

  {
    TabChangedWaiter tab_changed_waiter(browser());
    ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_NEXT,
                                                /*control=*/true,
                                                /*shift=*/false,
                                                /*alt=*/false,
                                                /*command=*/false));
    tab_changed_waiter.Wait();
  }
  ASSERT_EQ(2, tab_strip_model->count());
  EXPECT_EQ(0, tab_strip_model->active_index());

  {
    TabChangedWaiter tab_changed_waiter(browser());
    ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_PRIOR,
                                                /*control=*/true,
                                                /*shift=*/false,
                                                /*alt=*/false,
                                                /*command=*/false));
    tab_changed_waiter.Wait();
  }
  ASSERT_EQ(2, tab_strip_model->count());
  EXPECT_EQ(1, tab_strip_model->active_index());
}

IN_PROC_BROWSER_TEST_P(PDFExtensionInteractiveUITest, FocusForwardTraversal) {
  content::RenderFrameHost* extension_host = LoadPdfInNewTabGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test.pdf#toolbar=0"));
  ASSERT_TRUE(extension_host);
  auto* target_web_contents =
      content::WebContents::FromRenderFrameHost(extension_host);

  // Tab in.
  content::FocusedNodeDetails details =
      TabAndWait(target_web_contents, /*forward=*/true);
  EXPECT_EQ(blink::mojom::FocusType::kForward, details.focus_type);

  // Tab out.
  details = TabAndWait(target_web_contents, /*forward=*/true);
  EXPECT_EQ(blink::mojom::FocusType::kNone, details.focus_type);
}

IN_PROC_BROWSER_TEST_P(PDFExtensionInteractiveUITest, FocusReverseTraversal) {
  content::RenderFrameHost* extension_host = LoadPdfInNewTabGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/test.pdf#toolbar=0"));
  ASSERT_TRUE(extension_host);
  auto* target_web_contents =
      content::WebContents::FromRenderFrameHost(extension_host);

  // Tab in.
  content::FocusedNodeDetails details =
      TabAndWait(target_web_contents, /*forward=*/false);
  EXPECT_EQ(blink::mojom::FocusType::kBackward, details.focus_type);

  // Tab out.
  details = TabAndWait(target_web_contents, /*forward=*/false);
  EXPECT_EQ(blink::mojom::FocusType::kNone, details.focus_type);
}

// Regression test for https://crbug.com/326275041
IN_PROC_BROWSER_TEST_P(PDFExtensionInteractiveUITest, SpaceKeyInForm) {
  content::RenderFrameHost* extension_host = LoadPdfGetExtensionHost(
      embedded_test_server()->GetURL("/pdf/text_form.pdf"));
  ASSERT_TRUE(extension_host);
  content::RenderFrameHost* plugin_host =
      pdf_frame_util::FindPdfChildFrame(extension_host);
  ASSERT_TRUE(plugin_host);

  static constexpr char kFocusChangeScript[] = R"(
    var form_focus_changed = false;
    const plugin = document.querySelector('embed');
    plugin.addEventListener('message', e => {
      if (e.data.type === 'formFocusChange') {
        form_focus_changed = true;
      }
    });
  )";
  ASSERT_TRUE(content::ExecJs(plugin_host, kFocusChangeScript));

  // Since the PDF contains a text form that takes up the entire PDF page, this
  // clicks into that form.
  auto* embedder_web_contents = GetEmbedderWebContents();
  ASSERT_TRUE(embedder_web_contents);
  SetInputFocusOnPlugin(extension_host, embedder_web_contents);

  // Wait for the input event to propagate into `plugin_host`.
  while (true) {
    // content::EvalJs() uses a base::RunLoop internally, so do not use
    // base::test::RunUntil() here to avoid a double run loop.
    if (content::EvalJs(plugin_host, "form_focus_changed").ExtractBool()) {
      break;
    }
  }

  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
      browser(), ui::VKEY_SPACE, /*control=*/false, /*shift=*/false,
      /*alt=*/false, /*command=*/false));

  content::RenderWidgetHostView* view = plugin_host->GetView();
  ASSERT_TRUE(view);
  EXPECT_TRUE(view->GetSelectedText().empty());

  // Wait for the key press to propagate from the browser to the PDF renderer.
  // Keep calling SelectAll(), as it may arrive ahead of the key press event.
  // Then wait for the text selection update to propagate from the PDF renderer
  // back to the browser.
  //
  // If the space key press did not register at all, this hangs.
  EXPECT_TRUE(base::test::RunUntil([embedder_web_contents, view]() {
    embedder_web_contents->SelectAll();
    return view->GetSelectedText() == u" ";
  }));
}

#if defined(TOOLKIT_VIEWS) && defined(USE_AURA)
namespace {

// Simulates a touch press event and touch release event on `contents` at
// `screen_pos`. Waits for the PDF viewer to notify `listener_host` that text
// has been selected in the PDF.
views::Widget* TouchSelectText(content::WebContents* contents,
                               content::RenderFrameHost* listener_host,
                               const gfx::Point& screen_pos) {
  views::NamedWidgetShownWaiter waiter(views::test::AnyWidgetTestPasskey{},
                                       "TouchSelectionMenuViews");
  content::SimulateTouchEventAt(contents, ui::EventType::kTouchPressed,
                                screen_pos);

  EXPECT_EQ(true, content::EvalJs(
                      listener_host,
                      "new Promise(resolve => {"
                      "  window.addEventListener('message', function(event) {"
                      "    if (event.data.type == 'touchSelectionOccurred')"
                      "      resolve(true);"
                      "  });"
                      "});"));

  content::SimulateTouchEventAt(contents, ui::EventType::kTouchReleased,
                                screen_pos);
  return waiter.WaitIfNeededAndGet();
}

}  // namespace

// On text selection, a touch selection menu should pop up. On clicking ellipsis
// icon on the menu, the context menu should open up.
IN_PROC_BROWSER_TEST_P(PDFExtensionInteractiveUITest,
                       ContextMenuOpensFromTouchSelectionMenu) {
  const GURL url = embedded_test_server()->GetURL("/pdf/text_large.pdf");
  content::RenderFrameHost* extension_host =
      LoadPdfInNewTabGetExtensionHost(url);
  ASSERT_TRUE(extension_host);

  content::WaitForHitTestData(extension_host);

  content::WebContents* contents = GetActiveWebContents();

  // For GuestView PDF viewer, the listener host can be the PDF embedder host.
  // For OOPIF PDF viewer, the listener host can't be the embedder host, since
  // the PDF extension host doesn't send it messages. Instead, the listener host
  // can be the extension host and listen for messages from the PDF content
  // host.
  content::RenderFrameHost* listener_host =
      UseOopif() ? extension_host : contents->GetPrimaryMainFrame();
  const gfx::Point point_in_root_coords =
      extension_host->GetView()->TransformPointToRootCoordSpace(
          ConvertPageCoordToScreenCoord(extension_host, {12, 12}));
  views::Widget* widget =
      TouchSelectText(contents, listener_host, point_in_root_coords);
  ASSERT_TRUE(widget);
  views::View* menu = widget->GetContentsView();
  ASSERT_TRUE(menu);
  views::View* ellipsis_button = menu->GetViewByID(
      views::TouchSelectionMenuViews::ButtonViewId::kEllipsisButton);
  ASSERT_TRUE(ellipsis_button);
  ContextMenuWaiter context_menu_observer;
  ui::GestureEvent tap(0, 0, 0, ui::EventTimeForNow(),
                       ui::GestureEventDetails(ui::EventType::kGestureTap));
  ellipsis_button->OnGestureEvent(&tap);
  context_menu_observer.WaitForMenuOpenAndClose();

  // Verify that the expected context menu items are present.
  //
  // Note that the assertion below doesn't use exact matching via
  // testing::ElementsAre, because some platforms may include unexpected extra
  // elements (e.g. an extra separator and IDC=100 has been observed on some Mac
  // bots).
  EXPECT_THAT(
      context_menu_observer.GetCapturedCommandIds(),
      testing::IsSupersetOf(
          {IDC_CONTENT_CONTEXT_COPY, IDC_CONTENT_CONTEXT_SEARCHWEBFOR,
           IDC_PRINT, IDC_CONTENT_CONTEXT_ROTATECW,
           IDC_CONTENT_CONTEXT_ROTATECCW, IDC_CONTENT_CONTEXT_INSPECTELEMENT}));
}

// TODO(crbug.com/40847318): Deflake this test.
#if BUILDFLAG(IS_WIN)
#define MAYBE_TouchSelectionBounds DISABLED_TouchSelectionBounds
#else
#define MAYBE_TouchSelectionBounds TouchSelectionBounds
#endif  // BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_P(PDFExtensionInteractiveUITest,
                       MAYBE_TouchSelectionBounds) {
  // Use test.pdf here because it has embedded font metrics. With a fixed zoom,
  // coordinates should be consistent across platforms.
  const GURL url = embedded_test_server()->GetURL("/pdf/test.pdf#zoom=100");
  content::RenderFrameHost* extension_host =
      LoadPdfInNewTabGetExtensionHost(url);
  ASSERT_TRUE(extension_host);
  content::RenderFrameHost* plugin_host =
      pdf_frame_util::FindPdfChildFrame(extension_host);
  ASSERT_TRUE(plugin_host);
  content::RenderWidgetHostView* view = plugin_host->GetView();
  ASSERT_TRUE(view);
  EXPECT_TRUE(view->GetSelectedText().empty());

  content::WaitForHitTestData(extension_host);

  content::WebContents* contents = GetActiveWebContents();

  // For GuestView PDF viewer, the listener host can be the PDF embedder host.
  // For OOPIF PDF viewer, the listener host can't be the embedder host, since
  // the PDF extension host doesn't send it messages. Instead, the listener host
  // can be the extension host and listen for messages from the PDF content
  // host.
  content::RenderFrameHost* listener_host =
      UseOopif() ? extension_host : contents->GetPrimaryMainFrame();
  views::Widget* widget = TouchSelectText(contents, listener_host, {473, 166});
  ASSERT_TRUE(widget);

  EXPECT_EQ(u"some", view->GetSelectedText());

  auto* touch_selection_controller =
      extension_host->GetView()
          ->GetTouchSelectionControllerClientManager()
          ->GetTouchSelectionController();

  gfx::SelectionBound start_bound = touch_selection_controller->start();
  EXPECT_EQ(gfx::SelectionBound::LEFT, start_bound.type());
  EXPECT_POINTF_NEAR(gfx::PointF(454.0f, 152.0f), start_bound.edge_start(),
                     1.0f);
  EXPECT_POINTF_NEAR(gfx::PointF(454.0f, 178.0f), start_bound.edge_end(), 1.0f);

  gfx::SelectionBound end_bound = touch_selection_controller->end();
  EXPECT_EQ(gfx::SelectionBound::RIGHT, end_bound.type());
  EXPECT_POINTF_NEAR(gfx::PointF(494.0f, 152.0f), end_bound.edge_start(), 1.0f);
  EXPECT_POINTF_NEAR(gfx::PointF(494.0f, 178.0f), end_bound.edge_end(), 1.0f);
}
#endif  // defined(TOOLKIT_VIEWS) && defined(USE_AURA)

// TODO(crbug.com/40268279): Stop testing both modes after OOPIF PDF viewer
// launches.
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(PDFExtensionInteractiveUITest);