forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdf_ink_module_unittest.cc
3073 lines (2607 loc) · 124 KB
/
pdf_ink_module_unittest.cc
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/pdf_ink_module.h"
#include <array>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/span.h"
#include "base/containers/to_vector.h"
#include "base/files/file_path.h"
#include "base/strings/to_string.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "pdf/pdf_features.h"
#include "pdf/pdf_ink_brush.h"
#include "pdf/pdf_ink_conversions.h"
#include "pdf/pdf_ink_metrics_handler.h"
#include "pdf/pdf_ink_module_client.h"
#include "pdf/pdf_ink_transform.h"
#include "pdf/pdfium/pdfium_ink_reader.h"
#include "pdf/test/mouse_event_builder.h"
#include "pdf/test/pdf_ink_test_helpers.h"
#include "pdf/ui/thumbnail.h"
#include "printing/units.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/common/input/web_touch_event.h"
#include "third_party/ink/src/ink/brush/brush.h"
#include "third_party/ink/src/ink/brush/type_matchers.h"
#include "third_party/ink/src/ink/geometry/affine_transform.h"
#include "third_party/ink/src/ink/strokes/input/type_matchers.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/mojom/cursor_type.mojom.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d_f.h"
using testing::_;
using testing::ElementsAre;
using testing::ElementsAreArray;
using testing::Field;
using testing::InSequence;
using testing::NiceMock;
using testing::Pair;
using testing::Pointwise;
using testing::Return;
using testing::SizeIs;
namespace chrome_pdf {
namespace {
// Some commonly used points with InitializeSimpleSinglePageBasicLayout().
constexpr gfx::PointF kLeftVerticalStrokePoint1(10.0f, 15.0f);
constexpr gfx::PointF kLeftVerticalStrokePoint2(10.0f, 35.0f);
constexpr gfx::PointF kRightVerticalStrokePoint1(40.0f, 15.0f);
constexpr gfx::PointF kRightVerticalStrokePoint2(40.0f, 35.0f);
// Constants to support a layout of 2 pages, arranged vertically with a small
// gap between them.
constexpr gfx::RectF kVerticalLayout2Pages[] = {
gfx::RectF(/*x=*/5.0f,
/*y=*/5.0f,
/*width=*/50.0f,
/*height=*/60.0f),
gfx::RectF(/*x=*/5.0f,
/*y=*/70.0f,
/*width=*/50.0f,
/*height=*/60.0f),
};
// Some commonly used points in relation to `kVerticalLayout2Pages`.
constexpr gfx::PointF kTwoPageVerticalLayoutPointOutsidePages(10.0f, 0.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint1InsidePage0(10.0f, 10.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint2InsidePage0(15.0f, 15.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint3InsidePage0(20.0f, 15.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint4InsidePage0(10.0f, 20.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint1InsidePage1(10.0f, 75.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint2InsidePage1(15.0f, 80.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutPoint3InsidePage1(20.0f, 80.0f);
// Canonical points after stroking horizontal & vertical lines with some
// commonly used points.
// Horizontal line uses: kTwoPageVerticalLayoutPoint2InsidePage0 to
// kTwoPageVerticalLayoutPoint3InsidePage0
// or: kTwoPageVerticalLayoutPoint2InsidePage1 to
// kTwoPageVerticalLayoutPoint3InsidePage1
// Vertical line uses: kTwoPageVerticalLayoutPoint1InsidePage0 to
// kTwoPageVerticalLayoutPoint4InsidePage0
constexpr gfx::PointF kTwoPageVerticalLayoutHorzLinePoint0Canonical(10.0f,
10.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutHorzLinePoint1Canonical(15.0f,
10.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutVertLinePoint0Canonical(5.0f, 5.0f);
constexpr gfx::PointF kTwoPageVerticalLayoutVertLinePoint1Canonical(5.0f,
15.0f);
// The inputs for a stroke that starts in first page, leaves the bounds of that
// page, but then moves back into the page results in one stroke with two
// segments.
constexpr gfx::PointF kTwoPageVerticalLayoutPageExitAndReentryPoints[] = {
gfx::PointF(10.0f, 5.0f), gfx::PointF(10.0f, 0.0f),
gfx::PointF(15.0f, 0.0f), gfx::PointF(15.0f, 5.0f),
gfx::PointF(15.0f, 10.0f)};
// The two segments created by the inputs above.
constexpr gfx::PointF kTwoPageVerticalLayoutPageExitAndReentrySegment1[] = {
gfx::PointF(5.0f, 5.0f), gfx::PointF(5.0f, 0.0f)};
constexpr gfx::PointF kTwoPageVerticalLayoutPageExitAndReentrySegment2[] = {
gfx::PointF(10.0f, 0.0f), gfx::PointF(10.0f, 5.0f),
gfx::PointF(15.0f, 10.0f)};
// The stroke inputs for vertical and horizontal lines in the pages. The
// `.time` fields intentionally get a common value, to match the behavior of
// `MouseEventBuilder`.
constexpr auto kTwoPageVerticalLayoutHorzLinePage0Inputs =
std::to_array<PdfInkInputData>({
{kTwoPageVerticalLayoutHorzLinePoint0Canonical, base::Seconds(0)},
{kTwoPageVerticalLayoutHorzLinePoint1Canonical, base::Seconds(0)},
});
constexpr auto kTwoPageVerticalLayoutVertLinePage0Inputs =
std::to_array<PdfInkInputData>({
{kTwoPageVerticalLayoutVertLinePoint0Canonical, base::Seconds(0)},
{kTwoPageVerticalLayoutVertLinePoint1Canonical, base::Seconds(0)},
});
constexpr auto kTwoPageVerticalLayoutHorzLinePage1Inputs =
std::to_array<PdfInkInputData>({
{kTwoPageVerticalLayoutHorzLinePoint0Canonical, base::Seconds(0)},
{kTwoPageVerticalLayoutHorzLinePoint1Canonical, base::Seconds(0)},
});
// Matcher for ink::Stroke objects against their expected brush and inputs.
MATCHER_P(InkStrokeEq, expected_brush, "") {
const auto& [actual_stroke, expected_inputs] = arg;
const auto brush_matcher = ink::BrushEq(expected_brush);
const auto input_matcher = ink::StrokeInputBatchEq(expected_inputs);
return testing::Matches(brush_matcher)(actual_stroke->GetBrush()) &&
testing::Matches(input_matcher)(actual_stroke->GetInputs());
}
// Matcher for ink::Stroke objects against an expected brush color.
MATCHER_P(InkStrokeBrushColorEq, expected_color, "") {
return chrome_pdf::GetSkColorFromInkBrush(arg.GetBrush()) == expected_color;
}
// Matcher for ink::Stroke objects against an expected brush size.
MATCHER_P(InkStrokeBrushSizeEq, expected_size, "") {
return arg.GetBrush().GetSize() == expected_size;
}
// Matcher for ink::Stroke objects against an expected drawing brush type.
// A pen is opaque while a highlighter has transparency, so a drawing
// brush type can be deduced from the ink::Stroke's brush coat.
MATCHER_P(InkStrokeDrawingBrushTypeEq, expected_type, "") {
const ink::Brush& ink_brush = arg.GetBrush();
const ink::BrushCoat& coat = ink_brush.GetCoats()[0];
float opacity = coat.tip.opacity_multiplier;
if (expected_type == PdfInkBrush::Type::kPen) {
return opacity == 1.0f;
}
CHECK(expected_type == PdfInkBrush::Type::kHighlighter);
return opacity == 0.4f;
}
// Matcher for cursor with a custom bitmap against expected dimensions.
MATCHER_P(CursorBitmapImageSizeEq, dimensions, "") {
return arg.type() == ui::mojom::CursorType::kCustom &&
arg.custom_bitmap().dimensions() == dimensions;
}
std::map<int, std::vector<raw_ref<const ink::Stroke>>> CollectVisibleStrokes(
PdfInkModule::PageInkStrokeIterator strokes_iter) {
std::map<int, std::vector<raw_ref<const ink::Stroke>>> visible_stroke_shapes;
for (auto page_stroke = strokes_iter.GetNextStrokeAndAdvance();
page_stroke.has_value();
page_stroke = strokes_iter.GetNextStrokeAndAdvance()) {
visible_stroke_shapes[page_stroke.value().page_index].push_back(
page_stroke.value().stroke);
}
return visible_stroke_shapes;
}
blink::WebMouseEvent CreateMouseMoveWithLeftButtonEventAtPoint(
const gfx::PointF& point) {
return MouseEventBuilder()
.SetType(blink::WebInputEvent::Type::kMouseMove)
.SetPosition(point)
.SetButton(blink::WebPointerProperties::Button::kLeft)
.Build();
}
base::Value::Dict CreateGetAnnotationBrushMessageForTesting(
const std::string& brush_type) {
base::Value::Dict message;
message.Set("type", "getAnnotationBrush");
message.Set("messageId", "foo");
if (!brush_type.empty()) {
message.Set("brushType", brush_type);
}
return message;
}
blink::WebTouchEvent CreateTouchEvent(blink::WebInputEvent::Type type,
base::span<const gfx::PointF> points) {
CHECK_LE(points.size(), blink::WebTouchEvent::kTouchesLengthCap);
constexpr int kNoModifiers = 0;
blink::WebTouchEvent touch_event(
type, kNoModifiers, blink::WebInputEvent::GetStaticTimeStampForTests());
for (size_t i = 0; i < points.size(); ++i) {
touch_event.touches[i].SetPositionInWidget(points[i]);
}
touch_event.touches_length = points.size();
return touch_event;
}
blink::WebTouchEvent CreatePenEvent(blink::WebInputEvent::Type type,
base::span<const gfx::PointF> points) {
blink::WebTouchEvent pen_event = CreateTouchEvent(type, points);
for (size_t i = 0; i < pen_event.touches_length; ++i) {
pen_event.touches[i].pointer_type =
blink::WebPointerProperties::PointerType::kPen;
}
return pen_event;
}
class FakeClient : public PdfInkModuleClient {
public:
FakeClient() = default;
FakeClient(const FakeClient&) = delete;
FakeClient& operator=(const FakeClient&) = delete;
~FakeClient() override = default;
// PdfInkModuleClient:
MOCK_METHOD(void,
DiscardStroke,
(int page_index, InkStrokeId id),
(override));
PageOrientation GetOrientation() const override { return orientation_; }
gfx::Size GetThumbnailSize(int page_index) override {
CHECK_GE(page_index, 0);
CHECK_LT(static_cast<size_t>(page_index), page_layouts_.size());
return Thumbnail::CalculateImageSize(page_layouts_[page_index].size(),
/*device_pixel_ratio=*/1);
}
gfx::Vector2dF GetViewportOriginOffset() override {
return viewport_origin_offset_;
}
gfx::Rect GetPageContentsRect(int page_index) override {
CHECK_GE(page_index, 0);
CHECK_LT(static_cast<size_t>(page_index), page_layouts_.size());
return gfx::ToEnclosedRect(page_layouts_[page_index]);
}
gfx::SizeF GetPageSizeInPoints(int page_index) override {
CHECK_GE(page_index, 0);
CHECK_LT(static_cast<size_t>(page_index), page_layouts_.size());
gfx::SizeF page_size = page_layouts_[page_index].size();
page_size.Scale(printing::kUnitConversionFactorPixelsToPoints);
return page_size;
}
float GetZoom() const override { return zoom_; }
void Invalidate(const gfx::Rect& rect) override {
invalidations_.push_back(rect);
}
bool IsPageVisible(int page_index) override {
return base::Contains(visible_page_indices_, page_index);
}
MOCK_METHOD(PdfInkModuleClient::DocumentV2InkPathShapesMap,
LoadV2InkPathsFromPdf,
(),
(override));
MOCK_METHOD(void, PostMessage, (base::Value::Dict message), (override));
MOCK_METHOD(void,
RequestThumbnail,
(int page_index, SendThumbnailCallback callback),
(override));
MOCK_METHOD(void,
StrokeAdded,
(int page_index, InkStrokeId id, const ink::Stroke& stroke),
(override));
void StrokeFinished() override { ++stroke_finished_count_; }
MOCK_METHOD(void, UpdateInkCursor, (const ui::Cursor&), (override));
MOCK_METHOD(void,
UpdateShapeActive,
(int page_index, InkModeledShapeId id, bool active),
(override));
MOCK_METHOD(void,
UpdateStrokeActive,
(int page_index, InkStrokeId id, bool active),
(override));
int VisiblePageIndexFromPoint(const gfx::PointF& point) override {
for (size_t i = 0; i < page_layouts_.size(); ++i) {
if (IsPageVisible(i) && page_layouts_[i].Contains(point)) {
return i;
}
}
// Point is not over a visible page in the viewer plane.
return -1;
}
int stroke_finished_count() const { return stroke_finished_count_; }
const std::vector<gfx::Rect>& invalidations() const { return invalidations_; }
// Provide the sequence of pages and the coordinates and dimensions for how
// they are laid out in a viewer plane. It is upon the caller to ensure the
// positioning makes sense (e.g., pages do not overlap).
void set_page_layouts(base::span<const gfx::RectF> page_layouts) {
page_layouts_ = base::ToVector(page_layouts);
}
// Marks pages as visible or not. The caller is responsible for making sure
// the values makes sense.
void set_page_visibility(int index, bool visible) {
if (visible) {
visible_page_indices_.insert(index);
} else {
visible_page_indices_.erase(index);
}
}
void set_orientation(PageOrientation orientation) {
orientation_ = orientation;
}
void set_viewport_origin_offset(const gfx::Vector2dF& offset) {
viewport_origin_offset_ = offset;
}
void set_zoom(float zoom) { zoom_ = zoom; }
private:
int stroke_finished_count_ = 0;
std::vector<gfx::RectF> page_layouts_;
std::set<int> visible_page_indices_;
PageOrientation orientation_ = PageOrientation::kOriginal;
gfx::Vector2dF viewport_origin_offset_;
float zoom_ = 1.0f;
std::vector<gfx::Rect> invalidations_;
};
struct PdfInkModuleTestVariation {
bool use_text_annotations;
bool use_text_highlighting;
};
constexpr PdfInkModuleTestVariation kPdfInkModuleTestVariationNoTextSupport{
/*use_text_annotations=*/false,
/*use_text_highlighting=*/false};
constexpr PdfInkModuleTestVariation kPdfInkModuleTestVariationTextHighlighting{
/*use_text_annotations=*/false,
/*use_text_highlighting=*/true};
constexpr PdfInkModuleTestVariation
kPdfInkModuleTestVariationTextHighlightingAndAnnotations{
/*use_text_annotations=*/true, /*use_text_highlighting=*/true};
// Variations of PdfInkModule tests to cover all features in development.
constexpr auto kPdfInkModuleTestVariations =
std::to_array<PdfInkModuleTestVariation>({
kPdfInkModuleTestVariationNoTextSupport,
kPdfInkModuleTestVariationTextHighlighting,
kPdfInkModuleTestVariationTextHighlightingAndAnnotations,
});
class PdfInkModuleTest
: public testing::TestWithParam<PdfInkModuleTestVariation> {
public:
void SetUp() override {
feature_list_.InitAndEnableFeatureWithParameters(
chrome_pdf::features::kPdfInk2,
{{features::kPdfInk2TextAnnotations.name,
base::ToString(UseTextAnnotations())},
{features::kPdfInk2TextHighlighting.name,
base::ToString(UseTextHighlighting())}});
ink_module_ = std::make_unique<PdfInkModule>(client_);
}
protected:
bool UseTextAnnotations() const { return GetParam().use_text_annotations; }
bool UseTextHighlighting() const { return GetParam().use_text_highlighting; }
void EnableAnnotationMode() {
EXPECT_TRUE(
ink_module().OnMessage(CreateSetAnnotationModeMessageForTesting(true)));
EXPECT_TRUE(ink_module().enabled());
}
FakeClient& client() { return client_; }
PdfInkModule& ink_module() { return *ink_module_; }
const PdfInkModule& ink_module() const { return *ink_module_; }
private:
base::test::ScopedFeatureList feature_list_;
NiceMock<FakeClient> client_;
std::unique_ptr<PdfInkModule> ink_module_;
};
} // namespace
TEST_P(PdfInkModuleTest, UnknownMessage) {
base::Value::Dict message;
message.Set("type", "nonInkMessage");
EXPECT_FALSE(ink_module().OnMessage(message));
}
// Verify that a get eraser message gets the eraser parameters.
TEST_P(PdfInkModuleTest, HandleGetAnnotationBrushMessageEraser) {
EnableAnnotationMode();
EXPECT_CALL(client(), PostMessage)
.WillOnce([](const base::Value::Dict& dict) {
auto expected = base::test::ParseJsonDict(R"({
"type": "getAnnotationBrushReply",
"messageId": "foo",
"data": {
"type": "eraser",
},
})");
EXPECT_THAT(dict, base::test::DictionaryHasValues(expected));
});
EXPECT_TRUE(ink_module().OnMessage(
CreateGetAnnotationBrushMessageForTesting("eraser")));
}
// Verify that a get pen message gets the pen brush parameters.
TEST_P(PdfInkModuleTest, HandleGetAnnotationBrushMessagePen) {
EnableAnnotationMode();
EXPECT_CALL(client(), PostMessage)
.WillOnce([](const base::Value::Dict& dict) {
auto expected = base::test::ParseJsonDict(R"({
"type": "getAnnotationBrushReply",
"messageId": "foo",
"data": {
"type": "pen",
"size": 3.0,
"color": {
"r": 0,
"g": 0,
"b": 0,
},
},
})");
EXPECT_THAT(dict, base::test::DictionaryHasValues(expected));
});
EXPECT_TRUE(
ink_module().OnMessage(CreateGetAnnotationBrushMessageForTesting("pen")));
}
// Verify that a get highlighter message gets the highlighter brush parameters.
TEST_P(PdfInkModuleTest, HandleGetAnnotationBrushMessageHighlighter) {
EnableAnnotationMode();
EXPECT_CALL(client(), PostMessage)
.WillOnce([](const base::Value::Dict& dict) {
auto expected = base::test::ParseJsonDict(R"({
"type": "getAnnotationBrushReply",
"messageId": "foo",
"data": {
"type": "highlighter",
"size": 8.0,
"color": {
"r": 242,
"g": 139,
"b": 130,
},
},
})");
EXPECT_THAT(dict, base::test::DictionaryHasValues(expected));
});
EXPECT_TRUE(ink_module().OnMessage(
CreateGetAnnotationBrushMessageForTesting("highlighter")));
}
// Verify that a get brush message without a parameter gets the default brush
// parameters.
TEST_P(PdfInkModuleTest, HandleGetAnnotationBrushMessageDefault) {
EnableAnnotationMode();
EXPECT_CALL(client(), PostMessage)
.WillOnce([](const base::Value::Dict& dict) {
auto expected = base::test::ParseJsonDict(R"({
"type": "getAnnotationBrushReply",
"messageId": "foo",
"data": {
"type": "pen",
"size": 3.0,
"color": {
"r": 0,
"g": 0,
"b": 0,
},
},
})");
EXPECT_THAT(dict, base::test::DictionaryHasValues(expected));
});
EXPECT_TRUE(
ink_module().OnMessage(CreateGetAnnotationBrushMessageForTesting("")));
}
// Verify that a get brush message without a parameter gets the current brush
// parameters if the brush has changed from the default brush.
TEST_P(PdfInkModuleTest, HandleGetAnnotationBrushMessageCurrent) {
EnableAnnotationMode();
// Set the brush to eraser.
EXPECT_TRUE(ink_module().OnMessage(
CreateSetAnnotationBrushMessageForTesting("eraser", nullptr)));
EXPECT_CALL(client(), PostMessage)
.WillOnce([](const base::Value::Dict& dict) {
auto expected = base::test::ParseJsonDict(R"({
"type": "getAnnotationBrushReply",
"messageId": "foo",
"data": {
"type": "eraser",
},
})");
EXPECT_THAT(dict, base::test::DictionaryHasValues(expected));
});
EXPECT_TRUE(
ink_module().OnMessage(CreateGetAnnotationBrushMessageForTesting("")));
}
// Verify that a set eraser message sets the annotation brush to an eraser. i.e.
// There is no `PdfInkBrush`.
TEST_P(PdfInkModuleTest, HandleSetAnnotationBrushMessageEraser) {
EnableAnnotationMode();
base::Value::Dict message =
CreateSetAnnotationBrushMessageForTesting("eraser", nullptr);
EXPECT_TRUE(ink_module().OnMessage(message));
const PdfInkBrush* brush = ink_module().GetPdfInkBrushForTesting();
EXPECT_FALSE(brush);
}
// Verify that a set pen message sets the annotation brush to a pen, with the
// given params.
TEST_P(PdfInkModuleTest, HandleSetAnnotationBrushMessagePen) {
EnableAnnotationMode();
TestAnnotationBrushMessageParams message_params{/*color_r=*/10,
/*color_g=*/255,
/*color_b=*/50, /*size=*/8.0};
base::Value::Dict message =
CreateSetAnnotationBrushMessageForTesting("pen", &message_params);
EXPECT_TRUE(ink_module().OnMessage(message));
const PdfInkBrush* brush = ink_module().GetPdfInkBrushForTesting();
ASSERT_TRUE(brush);
const ink::Brush& ink_brush = brush->ink_brush();
EXPECT_EQ(SkColorSetRGB(10, 255, 50), GetSkColorFromInkBrush(ink_brush));
EXPECT_EQ(8.0f, ink_brush.GetSize());
ASSERT_EQ(1u, ink_brush.CoatCount());
const ink::BrushCoat& coat = ink_brush.GetCoats()[0];
EXPECT_EQ(1.0f, coat.tip.corner_rounding);
EXPECT_EQ(1.0f, coat.tip.opacity_multiplier);
}
// Verify that a set highlighter message sets the annotation brush to a
// highlighter, with the given params.
TEST_P(PdfInkModuleTest, HandleSetAnnotationBrushMessageHighlighter) {
EnableAnnotationMode();
TestAnnotationBrushMessageParams message_params{/*color_r=*/240,
/*color_g=*/133,
/*color_b=*/0, /*size=*/4.5};
base::Value::Dict message =
CreateSetAnnotationBrushMessageForTesting("highlighter", &message_params);
EXPECT_TRUE(ink_module().OnMessage(message));
const PdfInkBrush* brush = ink_module().GetPdfInkBrushForTesting();
ASSERT_TRUE(brush);
const ink::Brush& ink_brush = brush->ink_brush();
EXPECT_EQ(SkColorSetRGB(240, 133, 0), GetSkColorFromInkBrush(ink_brush));
EXPECT_EQ(4.5f, ink_brush.GetSize());
ASSERT_EQ(1u, ink_brush.CoatCount());
const ink::BrushCoat& coat = ink_brush.GetCoats()[0];
EXPECT_EQ(0.0f, coat.tip.corner_rounding);
EXPECT_EQ(0.4f, coat.tip.opacity_multiplier);
}
// Verify that brushes with zero color values can be set as the annotation
// brush.
TEST_P(PdfInkModuleTest, HandleSetAnnotationBrushMessageColorZero) {
EnableAnnotationMode();
TestAnnotationBrushMessageParams message_params{/*color_r=*/0, /*color_g=*/0,
/*color_b=*/0, /*size=*/4.5};
base::Value::Dict message =
CreateSetAnnotationBrushMessageForTesting("pen", &message_params);
EXPECT_TRUE(ink_module().OnMessage(message));
const PdfInkBrush* brush = ink_module().GetPdfInkBrushForTesting();
ASSERT_TRUE(brush);
const ink::Brush& ink_brush = brush->ink_brush();
EXPECT_EQ(SK_ColorBLACK, GetSkColorFromInkBrush(ink_brush));
EXPECT_EQ(4.5f, ink_brush.GetSize());
ASSERT_EQ(1u, ink_brush.CoatCount());
const ink::BrushCoat& coat = ink_brush.GetCoats()[0];
EXPECT_EQ(1.0f, coat.tip.corner_rounding);
EXPECT_EQ(1.0f, coat.tip.opacity_multiplier);
}
TEST_P(PdfInkModuleTest, HandleSetAnnotationModeMessage) {
EXPECT_CALL(client(), LoadV2InkPathsFromPdf())
.WillOnce(Return(PdfInkModuleClient::DocumentV2InkPathShapesMap{
{0,
PdfInkModuleClient::PageV2InkPathShapesMap{
{InkModeledShapeId(0), ink::PartitionedMesh()},
{InkModeledShapeId(1), ink::PartitionedMesh()}}},
{3,
PdfInkModuleClient::PageV2InkPathShapesMap{
{InkModeledShapeId(2), ink::PartitionedMesh()}}},
}));
const auto kShapeMapMatcher = ElementsAre(
Pair(0, ElementsAre(Field(&PdfInkModule::LoadedV2ShapeState::id,
InkModeledShapeId(0)),
Field(&PdfInkModule::LoadedV2ShapeState::id,
InkModeledShapeId(1)))),
Pair(3, ElementsAre(Field(&PdfInkModule::LoadedV2ShapeState::id,
InkModeledShapeId(2)))));
EXPECT_FALSE(ink_module().enabled());
base::Value::Dict message =
CreateSetAnnotationModeMessageForTesting(/*enable=*/false);
EXPECT_TRUE(ink_module().OnMessage(message));
EXPECT_FALSE(ink_module().enabled());
EXPECT_TRUE(ink_module().loaded_v2_shapes_.empty());
message.Set("mode", "draw");
EXPECT_TRUE(ink_module().OnMessage(message));
EXPECT_TRUE(ink_module().enabled());
EXPECT_THAT(ink_module().loaded_v2_shapes_, kShapeMapMatcher);
message.Set("mode", "none");
EXPECT_TRUE(ink_module().OnMessage(message));
EXPECT_FALSE(ink_module().enabled());
EXPECT_THAT(ink_module().loaded_v2_shapes_, kShapeMapMatcher);
}
TEST_P(PdfInkModuleTest, MaybeSetCursorWhenTogglingAnnotationMode) {
EXPECT_FALSE(ink_module().enabled());
EXPECT_CALL(client(), UpdateInkCursor(_)).WillOnce([this]() {
EXPECT_TRUE(ink_module().enabled());
});
base::Value::Dict message =
CreateSetAnnotationModeMessageForTesting(/*enable=*/true);
EXPECT_TRUE(ink_module().OnMessage(message));
EXPECT_TRUE(ink_module().enabled());
message.Set("mode", "none");
EXPECT_TRUE(ink_module().OnMessage(message));
EXPECT_FALSE(ink_module().enabled());
}
TEST_P(PdfInkModuleTest, MaybeSetCursorWhenChangingBrushes) {
{
InSequence seq;
EXPECT_CALL(client(), UpdateInkCursor(_))
.WillOnce([](const ui::Cursor& cursor) {
ASSERT_EQ(ui::mojom::CursorType::kCustom, cursor.type());
const SkBitmap& bitmap = cursor.custom_bitmap();
EXPECT_EQ(6, bitmap.width());
EXPECT_EQ(6, bitmap.height());
});
EXPECT_CALL(client(), UpdateInkCursor(_))
.WillOnce([](const ui::Cursor& cursor) {
ASSERT_EQ(ui::mojom::CursorType::kCustom, cursor.type());
const SkBitmap& bitmap = cursor.custom_bitmap();
EXPECT_EQ(20, bitmap.width());
EXPECT_EQ(20, bitmap.height());
});
EXPECT_CALL(client(), UpdateInkCursor(_))
.WillOnce([](const ui::Cursor& cursor) {
ASSERT_EQ(ui::mojom::CursorType::kCustom, cursor.type());
const SkBitmap& bitmap = cursor.custom_bitmap();
EXPECT_EQ(6, bitmap.width());
EXPECT_EQ(6, bitmap.height());
});
}
EnableAnnotationMode();
TestAnnotationBrushMessageParams message_params{/*color_r=*/0,
/*color_g=*/255,
/*color_b=*/0, /*size=*/16.0};
base::Value::Dict message =
CreateSetAnnotationBrushMessageForTesting("pen", &message_params);
EXPECT_TRUE(ink_module().OnMessage(message));
message = CreateSetAnnotationBrushMessageForTesting("eraser", nullptr);
EXPECT_TRUE(ink_module().OnMessage(message));
}
TEST_P(PdfInkModuleTest, MaybeSetCursorWhenChangingZoom) {
{
InSequence seq;
EXPECT_CALL(client(), UpdateInkCursor(_))
.WillOnce([](const ui::Cursor& cursor) {
ASSERT_EQ(ui::mojom::CursorType::kCustom, cursor.type());
const SkBitmap& bitmap = cursor.custom_bitmap();
EXPECT_EQ(6, bitmap.width());
EXPECT_EQ(6, bitmap.height());
});
EXPECT_CALL(client(), UpdateInkCursor(_))
.WillOnce([](const ui::Cursor& cursor) {
ASSERT_EQ(ui::mojom::CursorType::kCustom, cursor.type());
const SkBitmap& bitmap = cursor.custom_bitmap();
EXPECT_EQ(20, bitmap.width());
EXPECT_EQ(20, bitmap.height());
});
EXPECT_CALL(client(), UpdateInkCursor(_))
.WillOnce([](const ui::Cursor& cursor) {
ASSERT_EQ(ui::mojom::CursorType::kCustom, cursor.type());
const SkBitmap& bitmap = cursor.custom_bitmap();
EXPECT_EQ(10, bitmap.width());
EXPECT_EQ(10, bitmap.height());
});
}
EnableAnnotationMode();
TestAnnotationBrushMessageParams message_params{/*color_r=*/0,
/*color_g=*/255,
/*color_b=*/0,
/*size=*/16.0};
base::Value::Dict message =
CreateSetAnnotationBrushMessageForTesting("pen", &message_params);
EXPECT_TRUE(ink_module().OnMessage(message));
client().set_zoom(0.5f);
ink_module().OnGeometryChanged();
}
TEST_P(PdfInkModuleTest, ContentFocusedPostMessage) {
EnableAnnotationMode();
blink::WebMouseEvent mouse_down_event =
MouseEventBuilder().CreateLeftClickAtPosition(gfx::PointF()).Build();
EXPECT_CALL(client(), PostMessage)
.WillOnce([](const base::Value::Dict& dict) {
auto expected = base::test::ParseJsonDict(R"({
"type": "contentFocused",
})");
EXPECT_THAT(dict, base::test::DictionaryHasValues(expected));
});
ink_module().HandleInputEvent(mouse_down_event);
}
class PdfInkModuleStrokeTest : public PdfInkModuleTest {
protected:
// Mouse locations used for `RunStrokeCheckTest()`.
// Touch events may use the same coordinates.
static constexpr gfx::PointF kMouseDownPoint = gfx::PointF(10.0f, 15.0f);
static constexpr gfx::PointF kMouseMovePoint = gfx::PointF(20.0f, 25.0f);
static constexpr gfx::PointF kMouseUpPoint = gfx::PointF(30.0f, 17.0f);
static constexpr gfx::PointF kMousePoints[] = {
kMouseDownPoint, kMouseMovePoint, kMouseUpPoint};
// PdfInkModuleTest:
void SetUp() override {
PdfInkModuleTest::SetUp();
EXPECT_CALL(client(), PostMessage)
.WillRepeatedly([&](const base::Value::Dict& dict) {
const std::string* type = dict.FindString("type");
ASSERT_TRUE(type);
if (*type != "updateInk2Thumbnail") {
return;
}
std::optional<int> page_number = dict.FindInt("pageNumber");
ASSERT_TRUE(page_number.has_value());
std::optional<bool> is_ink = dict.FindBool("isInk");
ASSERT_TRUE(is_ink.has_value());
auto& updated = is_ink.value() ? updated_ink_thumbnail_page_indices_
: updated_pdf_thumbnail_page_indices_;
updated.push_back(page_number.value() - 1);
});
}
void InitializeSimpleSinglePageBasicLayout() {
// Single page layout that matches visible area.
constexpr gfx::RectF kPage(0.0f, 0.0f, 50.0f, 60.0f);
client().set_page_layouts(base::span_from_ref(kPage));
client().set_page_visibility(0, true);
}
void InitializeScaledLandscapeSinglePageBasicLayout() {
// Single page layout that matches visible area.
constexpr gfx::RectF kPage(0.0f, 0.0f, 120.0f, 100.0f);
client().set_page_layouts(base::span_from_ref(kPage));
client().set_page_visibility(0, true);
}
void InitializeVerticalTwoPageLayout() {
// Page 2 is below page 1. Not side-by-side.
client().set_page_layouts(kVerticalLayout2Pages);
client().set_page_visibility(0, true);
client().set_page_visibility(1, true);
}
void ApplyStrokeWithMouseAtPoints(
const gfx::PointF& mouse_down_point,
base::span<const gfx::PointF> mouse_move_points,
const gfx::PointF& mouse_up_point) {
ApplyStrokeWithMouseAtPointsMaybeHandled(
mouse_down_point, mouse_move_points, mouse_up_point,
/*expect_mouse_events_handled=*/true);
}
void ApplyStrokeWithMouseAtPointsNotHandled(
const gfx::PointF& mouse_down_point,
base::span<const gfx::PointF> mouse_move_points,
const gfx::PointF& mouse_up_point) {
ApplyStrokeWithMouseAtPointsMaybeHandled(
mouse_down_point, mouse_move_points, mouse_up_point,
/*expect_mouse_events_handled=*/false);
}
void RunStrokeCheckTest(bool annotation_mode_enabled) {
EXPECT_TRUE(ink_module().OnMessage(
CreateSetAnnotationModeMessageForTesting(annotation_mode_enabled)));
EXPECT_EQ(annotation_mode_enabled, ink_module().enabled());
ApplyStrokeWithMouseAtPointsMaybeHandled(
kMouseDownPoint, base::span_from_ref(kMouseMovePoint), kMouseUpPoint,
/*expect_mouse_events_handled=*/annotation_mode_enabled);
ValidateRunStrokeCheckTest(
/*expect_stroke_success=*/annotation_mode_enabled);
}
void ApplyStrokeWithMouseAtMouseDownPoint() {
ApplyStrokeWithMouseAtPoints(
kMouseDownPoint, base::span_from_ref(kMouseDownPoint), kMouseDownPoint);
}
void ApplyStrokeWithTouchAtPoints(
base::span<const gfx::PointF> touch_start_points,
std::vector<base::span<const gfx::PointF>> all_touch_move_points,
base::span<const gfx::PointF> touch_end_points) {
ApplyStrokeWithTouchAtPointsMaybeHandled(
touch_start_points, all_touch_move_points, touch_end_points,
/*expect_touch_events_handled=*/true);
}
void ApplyStrokeWithTouchAtPointsNotHandled(
base::span<const gfx::PointF> touch_start_points,
std::vector<base::span<const gfx::PointF>> all_touch_move_points,
base::span<const gfx::PointF> touch_end_points) {
ApplyStrokeWithTouchAtPointsMaybeHandled(
touch_start_points, all_touch_move_points, touch_end_points,
/*expect_touch_events_handled=*/false);
}
// TODO(crbug.com/377733396): Consider refactoring to combine with
// RunStrokeCheckTest().
void RunStrokeTouchCheckTest(bool annotation_mode_enabled) {
EXPECT_TRUE(ink_module().OnMessage(
CreateSetAnnotationModeMessageForTesting(annotation_mode_enabled)));
EXPECT_EQ(annotation_mode_enabled, ink_module().enabled());
const std::vector<base::span<const gfx::PointF>> all_touch_move_points{
base::span_from_ref(kMouseMovePoint),
};
ApplyStrokeWithTouchAtPointsMaybeHandled(
base::span_from_ref(kMouseDownPoint), all_touch_move_points,
base::span_from_ref(kMouseUpPoint),
/*expect_touch_events_handled=*/annotation_mode_enabled);
ValidateRunStrokeCheckTest(
/*expect_stroke_success=*/annotation_mode_enabled);
}
// TODO(crbug.com/377733396): Consider refactoring to combine with
// RunStrokeCheckTest().
//
// Note that currently multi-touch is not handled, so the test expectations
// are different from the ones in RunStrokeTouchCheckTest().
void RunStrokeMultiTouchCheckTest(bool annotation_mode_enabled) {
EXPECT_TRUE(ink_module().OnMessage(
CreateSetAnnotationModeMessageForTesting(annotation_mode_enabled)));
EXPECT_EQ(annotation_mode_enabled, ink_module().enabled());
const std::vector<gfx::PointF> touch_start_points{kMouseDownPoint,
kMouseDownPoint};
const std::vector<gfx::PointF> touch_move_points{kMouseMovePoint,
kMouseMovePoint};
const std::vector<base::span<const gfx::PointF>> all_touch_move_points{
touch_move_points,
};
const std::vector<gfx::PointF> touch_end_points{kMouseUpPoint,
kMouseUpPoint};
ApplyStrokeWithTouchAtPointsMaybeHandled(
touch_start_points, all_touch_move_points, touch_end_points,
/*expect_touch_events_handled=*/false);
ValidateRunStrokeCheckTest(/*expect_stroke_success=*/false);
}
void ApplyStrokeWithPenAtPoints(
base::span<const gfx::PointF> pen_start_points,
std::vector<base::span<const gfx::PointF>> all_pen_move_points,
base::span<const gfx::PointF> pen_end_points) {
ApplyStrokeWithPenAtPointsMaybeHandled(pen_start_points,
all_pen_move_points, pen_end_points,
/*expect_pen_events_handled=*/true);
}
// TODO(crbug.com/377733396): Consider refactoring to combine with
// RunStrokeCheckTest().
void RunStrokePenCheckTest(bool annotation_mode_enabled) {
EXPECT_TRUE(ink_module().OnMessage(
CreateSetAnnotationModeMessageForTesting(annotation_mode_enabled)));
EXPECT_EQ(annotation_mode_enabled, ink_module().enabled());
const std::vector<base::span<const gfx::PointF>> all_pen_move_points{
base::span_from_ref(kMouseMovePoint),
};
ApplyStrokeWithPenAtPointsMaybeHandled(
base::span_from_ref(kMouseDownPoint), all_pen_move_points,
base::span_from_ref(kMouseUpPoint),
/*expect_pen_events_handled=*/annotation_mode_enabled);
ValidateRunStrokeCheckTest(
/*expect_stroke_success=*/annotation_mode_enabled);
}
void RunStrokeMissedEndEventThenMouseMoveTest() {
{
// Start a drawing or erase action.
blink::WebMouseEvent mouse_down_event =
MouseEventBuilder()
.CreateLeftClickAtPosition(kMouseDownPoint)
.Build();
EXPECT_TRUE(ink_module().HandleInputEvent(mouse_down_event));
// Simulate scenario where another view has taken the focus and consumed
// the mouse up event, such that subsequent mouse moves don't show the
// left mouse button being pressed. This should be handled, as it treats
// it as a signal to terminate the prior stroke.
blink::WebMouseEvent mouse_move_event =
MouseEventBuilder()
.SetType(blink::WebInputEvent::Type::kMouseMove)
.SetPosition(kMouseMovePoint)
.SetButton(blink::WebPointerProperties::Button::kNoButton)
.Build();