1 // Copyright 2019 The PDFium Authors. All rights reserved.
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 "fpdfsdk/cpdfsdk_annotiterator.h"
6 #include "fpdfsdk/cpdfsdk_baannothandler.h"
7 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
8 #include "fpdfsdk/cpdfsdk_helpers.h"
9 #include "fpdfsdk/cpdfsdk_pageview.h"
10 #include "public/fpdf_annot.h"
11 #include "testing/embedder_test.h"
12 
13 class CPDFSDK_BAAnnotHandlerTest : public EmbedderTest {
14  public:
SetUp()15   void SetUp() override {
16     // Test behaviour with currently supported annot i.e. Widget.
17     // TODO(crbug.com/994500): Add an API that can set list of focusable
18     // subtypes once other annots(links & highlights) are also supported.
19     EmbedderTest::SetUp();
20     SetUpBAAnnotHandler();
21   }
22 
TearDown()23   void TearDown() override {
24     UnloadPage(m_page);
25     EmbedderTest::TearDown();
26   }
27 
SetUpBAAnnotHandler()28   void SetUpBAAnnotHandler() {
29     EXPECT_TRUE(OpenDocument("links_highlights_annots.pdf"));
30     m_page = LoadPage(0);
31     ASSERT_TRUE(m_page);
32 
33     CPDFSDK_FormFillEnvironment* pFormFillEnv =
34         CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
35     ASSERT_TRUE(pFormFillEnv);
36     m_pPageView = pFormFillEnv->GetPageView(IPDFPageFromFPDFPage(m_page), true);
37     ASSERT_TRUE(m_pPageView);
38 
39     CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
40         pFormFillEnv->GetAnnotHandlerMgr();
41     ASSERT_TRUE(pAnnotHandlerMgr);
42     m_pBAAnnotHandler = pAnnotHandlerMgr->m_pBAAnnotHandler.get();
43     ASSERT_TRUE(m_pBAAnnotHandler);
44   }
45 
GetPageView() const46   CPDFSDK_PageView* GetPageView() const { return m_pPageView; }
GetBAAnnotHandler() const47   CPDFSDK_BAAnnotHandler* GetBAAnnotHandler() const {
48     return m_pBAAnnotHandler;
49   }
50 
51  private:
52   FPDF_PAGE m_page = nullptr;
53   CPDFSDK_PageView* m_pPageView = nullptr;
54   CPDFSDK_BAAnnotHandler* m_pBAAnnotHandler = nullptr;
55 };
56 
TEST_F(CPDFSDK_BAAnnotHandlerTest,TabToLinkOrHighlightAnnot)57 TEST_F(CPDFSDK_BAAnnotHandlerTest, TabToLinkOrHighlightAnnot) {
58   // TODO(crbug.com/994500): Create annot iterator with list of supported
59   // focusable subtypes as provided by host.
60   CPDFSDK_AnnotIterator ai(GetPageView(), CPDF_Annot::Subtype::LINK);
61   CPDFSDK_Annot* pAnnot = ai.GetFirstAnnot();
62   ASSERT_TRUE(pAnnot);
63   EXPECT_EQ(pAnnot->GetAnnotSubtype(), CPDF_Annot::Subtype::LINK);
64 
65   ObservedPtr<CPDFSDK_Annot> pNonWidgetAnnot(pAnnot);
66 
67   // TODO(crbug.com/994500): Change expected value as true once
68   // links & highlights are supported.
69   EXPECT_FALSE(GetBAAnnotHandler()->OnSetFocus(&pNonWidgetAnnot, 0));
70 
71   EXPECT_FALSE(GetBAAnnotHandler()->OnKillFocus(&pNonWidgetAnnot, 0));
72 }
73 
TEST_F(CPDFSDK_BAAnnotHandlerTest,TabToInvalidAnnot)74 TEST_F(CPDFSDK_BAAnnotHandlerTest, TabToInvalidAnnot) {
75   // TODO(crbug.com/994500): Create annot iterator with list of supported
76   // focusable subtypes as provided by host.
77   CPDFSDK_AnnotIterator ai(GetPageView(), CPDF_Annot::Subtype::WIDGET);
78   CPDFSDK_Annot* pAnnot = ai.GetFirstAnnot();
79   ASSERT_TRUE(pAnnot);
80   EXPECT_EQ(pAnnot->GetAnnotSubtype(), CPDF_Annot::Subtype::WIDGET);
81 
82   ObservedPtr<CPDFSDK_Annot> pWidgetAnnot(pAnnot);
83 
84   // Passing wrong subtype to BAAnnotHandler, API should return false.
85   EXPECT_FALSE(GetBAAnnotHandler()->OnSetFocus(&pWidgetAnnot, 0));
86 
87   EXPECT_FALSE(GetBAAnnotHandler()->OnKillFocus(&pWidgetAnnot, 0));
88 }
89