1 // Copyright 2019 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "fpdfsdk/cpdfsdk_annotiterator.h"
8 #include "fpdfsdk/cpdfsdk_baannot.h"
9 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
10 #include "fpdfsdk/cpdfsdk_helpers.h"
11 #include "fpdfsdk/cpdfsdk_pageview.h"
12 #include "testing/embedder_test.h"
13 #include "third_party/base/check.h"
14 #include "third_party/base/check_op.h"
15
16 class CPDFSDK_BAAnnotTest : public EmbedderTest {
17 public:
SetUp()18 void SetUp() override {
19 EmbedderTest::SetUp();
20 SetUpBAAnnot();
21 }
22
TearDown()23 void TearDown() override {
24 UnloadPage(m_page);
25 EmbedderTest::TearDown();
26 }
27
SetUpBAAnnot()28 void SetUpBAAnnot() {
29 ASSERT_TRUE(OpenDocument("links_highlights_annots.pdf"));
30 m_page = LoadPage(0);
31 ASSERT_TRUE(m_page);
32
33 m_pFormFillEnv =
34 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
35 ASSERT_TRUE(m_pFormFillEnv);
36 m_pPageView =
37 m_pFormFillEnv->GetOrCreatePageView(IPDFPageFromFPDFPage(m_page));
38 ASSERT_TRUE(m_pPageView);
39 }
40
GetFormFillEnv() const41 CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { return m_pFormFillEnv; }
GetPageView() const42 CPDFSDK_PageView* GetPageView() const { return m_pPageView; }
43
GetNthFocusableAnnot(size_t n)44 CPDFSDK_Annot* GetNthFocusableAnnot(size_t n) {
45 DCHECK_NE(n, 0);
46 CPDFSDK_AnnotIterator ai(GetPageView(),
47 m_pFormFillEnv->GetFocusableAnnotSubtypes());
48 CPDFSDK_Annot* pAnnot = ai.GetFirstAnnot();
49 DCHECK(pAnnot);
50
51 for (size_t i = 1; i < n; i++) {
52 pAnnot = ai.GetNextAnnot(pAnnot);
53 DCHECK(pAnnot);
54 }
55
56 return pAnnot;
57 }
58
59 private:
60 FPDF_PAGE m_page = nullptr;
61 CPDFSDK_PageView* m_pPageView = nullptr;
62 CPDFSDK_FormFillEnvironment* m_pFormFillEnv = nullptr;
63 };
64
TEST_F(CPDFSDK_BAAnnotTest,TabToLinkOrHighlightAnnot)65 TEST_F(CPDFSDK_BAAnnotTest, TabToLinkOrHighlightAnnot) {
66 std::vector<CPDF_Annot::Subtype> focusable_annot_types = {
67 CPDF_Annot::Subtype::WIDGET, CPDF_Annot::Subtype::LINK,
68 CPDF_Annot::Subtype::HIGHLIGHT};
69
70 GetFormFillEnv()->SetFocusableAnnotSubtypes(focusable_annot_types);
71
72 // Get link annot.
73 CPDFSDK_Annot* pAnnot = GetNthFocusableAnnot(2);
74 ASSERT_TRUE(pAnnot);
75 EXPECT_EQ(pAnnot->GetAnnotSubtype(), CPDF_Annot::Subtype::LINK);
76
77 {
78 ObservedPtr<CPDFSDK_Annot> observer(pAnnot);
79 EXPECT_TRUE(CPDFSDK_Annot::OnSetFocus(observer, {}));
80 EXPECT_TRUE(CPDFSDK_Annot::OnKillFocus(observer, {}));
81 }
82
83 // Get highlight annot.
84 pAnnot = GetNthFocusableAnnot(4);
85 ASSERT_TRUE(pAnnot);
86 EXPECT_EQ(pAnnot->GetAnnotSubtype(), CPDF_Annot::Subtype::HIGHLIGHT);
87
88 {
89 ObservedPtr<CPDFSDK_Annot> observer(pAnnot);
90 EXPECT_TRUE(CPDFSDK_Annot::OnSetFocus(observer, {}));
91 EXPECT_TRUE(CPDFSDK_Annot::OnKillFocus(observer, {}));
92 }
93 }
94