File: pdf_document_helper_browsertest.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 (291 lines) | stat: -rw-r--r-- 10,763 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/pdf/browser/pdf_document_helper.h"

#include "base/test/metrics/user_action_tester.h"
#include "base/test/test_future.h"
#include "base/test/with_feature_override.h"
#include "build/build_config.h"
#include "components/pdf/browser/pdf_document_helper_client.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/touch_selection_controller_client_manager.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "pdf/mojom/pdf.mojom.h"
#include "pdf/pdf_features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/selection_bound.h"
#include "ui/touch_selection/touch_editing_controller.h"

namespace pdf {

namespace {

using ::testing::NiceMock;

class FakePdfListener : public pdf::mojom::PdfListener {
 public:
  FakePdfListener() = default;
  FakePdfListener(const FakePdfListener&) = delete;
  FakePdfListener& operator=(const FakePdfListener&) = delete;
  ~FakePdfListener() override = default;

  MOCK_METHOD(void, SetCaretPosition, (const gfx::PointF&), (override));
  MOCK_METHOD(void, MoveRangeSelectionExtent, (const gfx::PointF&), (override));
  MOCK_METHOD(void,
              SetSelectionBounds,
              (const gfx::PointF&, const gfx::PointF&),
              (override));
  MOCK_METHOD(void,
              GetPdfBytes,
              (uint32_t, GetPdfBytesCallback callback),
              (override));
  MOCK_METHOD(void,
              GetPageText,
              (int32_t, GetPageTextCallback callback),
              (override));
  MOCK_METHOD(void,
              GetMostVisiblePageIndex,
              (GetMostVisiblePageIndexCallback callback),
              (override));
};

class TestPDFDocumentHelperClient : public PDFDocumentHelperClient {
 public:
  TestPDFDocumentHelperClient() = default;
  ~TestPDFDocumentHelperClient() override = default;
  TestPDFDocumentHelperClient(const TestPDFDocumentHelperClient&) = delete;
  TestPDFDocumentHelperClient& operator=(const TestPDFDocumentHelperClient&) =
      delete;

  const gfx::SelectionBound& GetSelectionBoundStart() const { return start_; }
  const gfx::SelectionBound& GetSelectionBoundEnd() const { return end_; }

 private:
  // PDFDocumentHelperClient:
  void UpdateContentRestrictions(content::RenderFrameHost* render_frame_host,
                                 int content_restrictions) override {}
  void OnSaveURL(content::WebContents* contents) override {}
  void SetPluginCanSave(content::RenderFrameHost* render_frame_host,
                        bool can_save) override {}
  void OnDidScroll(const gfx::SelectionBound& start,
                   const gfx::SelectionBound& end) override {
    start_ = start;
    end_ = end;
  }
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
  void OnSearchifyStarted(content::WebContents* contents) override {}
#endif

 private:
  // The last bounds reported by PDFDocumentHelper.
  gfx::SelectionBound start_;
  gfx::SelectionBound end_;
};

}  // namespace

class PDFDocumentHelperTest : public base::test::WithFeatureOverride,
                              public content::ContentBrowserTest {
 public:
  PDFDocumentHelperTest()
      : base::test::WithFeatureOverride(chrome_pdf::features::kPdfOopif) {}
  ~PDFDocumentHelperTest() override = default;

 protected:
  void SelectionChanged(const gfx::PointF& left,
                        int32_t left_height,
                        const gfx::PointF& right,
                        int32_t right_height) {
    pdf_document_helper()->SelectionChanged(left, left_height, right,
                                            right_height);
  }

  PDFDocumentHelper* pdf_document_helper() {
    return PDFDocumentHelper::GetForCurrentDocument(
        shell()->web_contents()->GetPrimaryMainFrame());
  }

  content::RenderWidgetHostView* GetRenderWidgetHostView() {
    return shell()->web_contents()->GetRenderWidgetHostView();
  }

  TestPDFDocumentHelperClient* client() { return client_; }

  // content::ContentBrowserTest:
  void SetUpOnMainThread() override {
    content::ContentBrowserTest::SetUpOnMainThread();

    ASSERT_TRUE(content::NavigateToURL(shell(), GURL("about:blank")));
    auto client = std::make_unique<TestPDFDocumentHelperClient>();
    client_ = client.get();
    PDFDocumentHelper::CreateForCurrentDocument(
        shell()->web_contents()->GetPrimaryMainFrame(), std::move(client));
  }

  void TearDownOnMainThread() override {
    client_ = nullptr;
    content::ContentBrowserTest::TearDownOnMainThread();
  }

 private:
  raw_ptr<TestPDFDocumentHelperClient> client_ = nullptr;
};

IN_PROC_BROWSER_TEST_P(PDFDocumentHelperTest, SetListenerTwice) {
  NiceMock<FakePdfListener> listener;

  {
    mojo::Receiver<pdf::mojom::PdfListener> receiver(&listener);
    pdf_document_helper()->SetListener(receiver.BindNewPipeAndPassRemote());
  }

  {
    mojo::Receiver<pdf::mojom::PdfListener> receiver(&listener);
    pdf_document_helper()->SetListener(receiver.BindNewPipeAndPassRemote());
  }
}

// Tests that select-changed on a pdf text brings up selection handles and the
// quick menu in the reasonable position.
IN_PROC_BROWSER_TEST_P(PDFDocumentHelperTest, SelectionChanged) {
  gfx::SelectionBound initial_start = client()->GetSelectionBoundStart();
  gfx::SelectionBound initial_end = client()->GetSelectionBoundEnd();

  EXPECT_EQ(gfx::RectF(),
            gfx::RectFBetweenSelectionBounds(initial_start, initial_end));
  EXPECT_EQ(gfx::RectF(), gfx::RectFBetweenVisibleSelectionBounds(initial_start,
                                                                  initial_end));

  constexpr gfx::PointF kLeft(1.0f, 1.0f);
  constexpr gfx::PointF kRight(5.0f, 5.0f);
  constexpr int32_t kLeftHeight = 2;
  constexpr int32_t kRightHeight = 2;
  SelectionChanged(kLeft, kLeftHeight, kRight, kRightHeight);

  gfx::SelectionBound start = client()->GetSelectionBoundStart();
  gfx::SelectionBound end = client()->GetSelectionBoundEnd();

#if BUILDFLAG(IS_MAC)
  // Since macOS does not support Touch Selection Editing, the
  // SelectionChanged() call does not affect the selection bounds.
  EXPECT_EQ(start, initial_start);
  EXPECT_EQ(end, initial_end);
#else
  gfx::PointF origin_f;
  content::RenderWidgetHostView* view = GetRenderWidgetHostView();
  if (view) {
    origin_f = view->TransformPointToRootCoordSpaceF(gfx::PointF());
  }

  gfx::SelectionBound expected_start;
  {
    gfx::PointF edge_start(kLeft.x() + origin_f.x(), kLeft.y() + origin_f.y());
    gfx::PointF edge_end(kLeft.x() + origin_f.x(),
                         kLeft.y() + origin_f.y() + kLeftHeight);
    expected_start.SetEdge(edge_start, edge_end);
    expected_start.SetVisibleEdge(edge_start, edge_end);
  }

  gfx::SelectionBound expected_end;
  {
    gfx::PointF edge_start(kRight.x() + origin_f.x(),
                           kRight.y() + origin_f.y());
    gfx::PointF edge_end(kRight.x() + origin_f.x(),
                         kRight.y() + origin_f.y() + kRightHeight);
    expected_end.SetEdge(edge_start, edge_end);
    expected_end.SetVisibleEdge(edge_start, edge_end);
  }

  ASSERT_NE(expected_start, expected_end);

  expected_start.set_visible(true);
  expected_start.set_type(gfx::SelectionBound::LEFT);
  EXPECT_EQ(expected_start, start);

  expected_end.set_visible(true);
  expected_end.set_type(gfx::SelectionBound::RIGHT);
  EXPECT_EQ(expected_end, end);

  gfx::RectF expected_rect(
      expected_start.edge_start().x(), expected_start.edge_start().y(),
      expected_end.edge_start().x() - expected_start.edge_start().x(),
      expected_end.edge_end().y() - expected_start.edge_start().y());

  // The rect between the visible selection bounds determines the position of
  // the quick menu.
  EXPECT_EQ(expected_rect, gfx::RectFBetweenSelectionBounds(start, end));
  EXPECT_EQ(expected_rect, gfx::RectFBetweenVisibleSelectionBounds(start, end));
#endif  // BUILDFLAG(IS_MAC)
}

// When selecting something, only the copy command id should be enabled.
IN_PROC_BROWSER_TEST_P(PDFDocumentHelperTest, IsCommandIdEnabledCopyEnabled) {
  EXPECT_FALSE(
      pdf_document_helper()->IsCommandIdEnabled(ui::TouchEditable::kCut));
  EXPECT_FALSE(
      pdf_document_helper()->IsCommandIdEnabled(ui::TouchEditable::kCopy));

  constexpr gfx::PointF kLeft(1.0f, 1.0f);
  constexpr gfx::PointF kRight(5.0f, 5.0f);
  constexpr int32_t kLeftHeight = 2;
  constexpr int32_t kRightHeight = 2;
  SelectionChanged(kLeft, kLeftHeight, kRight, kRightHeight);

  EXPECT_FALSE(
      pdf_document_helper()->IsCommandIdEnabled(ui::TouchEditable::kCut));

#if BUILDFLAG(IS_MAC)
  // Since macOS does not support Touch Selection Editing, the copy command is
  // not enabled.
  EXPECT_FALSE(
      pdf_document_helper()->IsCommandIdEnabled(ui::TouchEditable::kCopy));
#else
  EXPECT_TRUE(
      pdf_document_helper()->IsCommandIdEnabled(ui::TouchEditable::kCopy));
#endif  // BUILDFLAG(IS_MAC)
}

// Test that the copy command executes.
IN_PROC_BROWSER_TEST_P(PDFDocumentHelperTest, ExecuteCommandCopy) {
  base::UserActionTester action_tester;
  EXPECT_EQ(0, action_tester.GetActionCount("Copy"));

  pdf_document_helper()->ExecuteCommand(ui::TouchEditable::kCopy, 0);

  EXPECT_EQ(1, action_tester.GetActionCount("Copy"));
}

IN_PROC_BROWSER_TEST_P(PDFDocumentHelperTest, DefaultImplementation) {
  EXPECT_FALSE(pdf_document_helper()->SupportsAnimation());
  EXPECT_FALSE(pdf_document_helper()->CreateDrawable());
  EXPECT_FALSE(pdf_document_helper()->ShouldShowQuickMenu());
  EXPECT_TRUE(pdf_document_helper()->GetSelectedText().empty());
}

IN_PROC_BROWSER_TEST_P(PDFDocumentHelperTest, DocumentLoadComplete) {
  base::test::TestFuture<void> load_complete_future;
  EXPECT_FALSE(pdf_document_helper()->IsDocumentLoadComplete());
  pdf_document_helper()->RegisterForDocumentLoadComplete(
      load_complete_future.GetCallback());
  pdf_document_helper()->OnDocumentLoadComplete();
  EXPECT_TRUE(load_complete_future.WaitAndClear());
  EXPECT_TRUE(pdf_document_helper()->IsDocumentLoadComplete());

  // Immediately called when document is already load complete.
  pdf_document_helper()->RegisterForDocumentLoadComplete(
      load_complete_future.GetCallback());
  EXPECT_TRUE(load_complete_future.WaitAndClear());
}

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

}  // namespace pdf