• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/fxcrt/check.h"
8 #include "core/fxcrt/check_op.h"
9 #include "fpdfsdk/cpdfsdk_annotiterator.h"
10 #include "fpdfsdk/cpdfsdk_baannot.h"
11 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
12 #include "fpdfsdk/cpdfsdk_helpers.h"
13 #include "fpdfsdk/cpdfsdk_pageview.h"
14 #include "testing/embedder_test.h"
15 
16 class CPDFSDKBAAnnotTest : public EmbedderTest {
17  public:
SetUp()18   void SetUp() override {
19     EmbedderTest::SetUp();
20     ASSERT_TRUE(OpenDocument("links_highlights_annots.pdf"));
21   }
22 
SetUpBAAnnot()23   ScopedEmbedderTestPage SetUpBAAnnot() {
24     ScopedEmbedderTestPage page = LoadScopedPage(0);
25     if (!page) {
26       ADD_FAILURE();
27       return ScopedEmbedderTestPage();
28     }
29     m_pFormFillEnv =
30         CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
31     if (!m_pFormFillEnv) {
32       ADD_FAILURE();
33       return ScopedEmbedderTestPage();
34     }
35 
36     m_pPageView =
37         m_pFormFillEnv->GetOrCreatePageView(IPDFPageFromFPDFPage(page.get()));
38 
39     if (!m_pPageView) {
40       ADD_FAILURE();
41       return ScopedEmbedderTestPage();
42     }
43 
44     return page;
45   }
46 
GetFormFillEnv() const47   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { return m_pFormFillEnv; }
GetPageView() const48   CPDFSDK_PageView* GetPageView() const { return m_pPageView; }
49 
GetNthFocusableAnnot(size_t n)50   CPDFSDK_Annot* GetNthFocusableAnnot(size_t n) {
51     DCHECK_NE(n, 0);
52     CPDFSDK_AnnotIterator ai(GetPageView(),
53                              m_pFormFillEnv->GetFocusableAnnotSubtypes());
54     CPDFSDK_Annot* pAnnot = ai.GetFirstAnnot();
55     DCHECK(pAnnot);
56 
57     for (size_t i = 1; i < n; i++) {
58       pAnnot = ai.GetNextAnnot(pAnnot);
59       DCHECK(pAnnot);
60     }
61 
62     return pAnnot;
63   }
64 
65  private:
66   CPDFSDK_PageView* m_pPageView = nullptr;
67   CPDFSDK_FormFillEnvironment* m_pFormFillEnv = nullptr;
68 };
69 
TEST_F(CPDFSDKBAAnnotTest,TabToLinkOrHighlightAnnot)70 TEST_F(CPDFSDKBAAnnotTest, TabToLinkOrHighlightAnnot) {
71   ScopedEmbedderTestPage page = SetUpBAAnnot();
72   ASSERT_TRUE(page);
73 
74   std::vector<CPDF_Annot::Subtype> focusable_annot_types = {
75       CPDF_Annot::Subtype::WIDGET, CPDF_Annot::Subtype::LINK,
76       CPDF_Annot::Subtype::HIGHLIGHT};
77 
78   GetFormFillEnv()->SetFocusableAnnotSubtypes(focusable_annot_types);
79 
80   // Get link annot.
81   CPDFSDK_Annot* pAnnot = GetNthFocusableAnnot(2);
82   ASSERT_TRUE(pAnnot);
83   EXPECT_EQ(pAnnot->GetAnnotSubtype(), CPDF_Annot::Subtype::LINK);
84 
85   {
86     ObservedPtr<CPDFSDK_Annot> observer(pAnnot);
87     EXPECT_TRUE(CPDFSDK_Annot::OnSetFocus(observer, {}));
88     EXPECT_TRUE(CPDFSDK_Annot::OnKillFocus(observer, {}));
89   }
90 
91   // Get highlight annot.
92   pAnnot = GetNthFocusableAnnot(4);
93   ASSERT_TRUE(pAnnot);
94   EXPECT_EQ(pAnnot->GetAnnotSubtype(), CPDF_Annot::Subtype::HIGHLIGHT);
95 
96   {
97     ObservedPtr<CPDFSDK_Annot> observer(pAnnot);
98     EXPECT_TRUE(CPDFSDK_Annot::OnSetFocus(observer, {}));
99     EXPECT_TRUE(CPDFSDK_Annot::OnKillFocus(observer, {}));
100   }
101 }
102