xref: /aosp_15_r20/external/pdfium/fpdfsdk/pwl/cpwl_special_button_embeddertest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2020 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 "fpdfsdk/cpdfsdk_annotiterator.h"
6 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
7 #include "fpdfsdk/cpdfsdk_helpers.h"
8 #include "fpdfsdk/cpdfsdk_widget.h"
9 #include "fpdfsdk/formfiller/cffl_formfield.h"
10 #include "fpdfsdk/pwl/cpwl_special_button.h"
11 #include "fpdfsdk/pwl/cpwl_wnd.h"
12 #include "testing/embedder_test.h"
13 
14 class CPWLSpecialButtonEmbedderTest : public EmbedderTest {
15  protected:
SetUp()16   void SetUp() override {
17     EmbedderTest::SetUp();
18     CreateAndInitializeFormPDF();
19   }
20 
TearDown()21   void TearDown() override {
22     UnloadPage(page_);
23     EmbedderTest::TearDown();
24   }
25 
CreateAndInitializeFormPDF()26   void CreateAndInitializeFormPDF() {
27     ASSERT_TRUE(OpenDocument("click_form.pdf"));
28 
29     page_ = LoadPage(0);
30     ASSERT_TRUE(page_);
31 
32     formfill_env_ = CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
33     CPDFSDK_AnnotIterator it(formfill_env_->GetPageViewAtIndex(0),
34                              {CPDF_Annot::Subtype::WIDGET});
35 
36     // Read only check box.
37     widget_readonly_checkbox_ = ToCPDFSDKWidget(it.GetFirstAnnot());
38     ASSERT_TRUE(widget_readonly_checkbox_);
39     ASSERT_EQ(CPDF_Annot::Subtype::WIDGET,
40               widget_readonly_checkbox_->GetAnnotSubtype());
41 
42     // Check box.
43     widget_checkbox_ =
44         ToCPDFSDKWidget(it.GetNextAnnot(widget_readonly_checkbox_));
45     ASSERT_TRUE(widget_checkbox_);
46     ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, widget_checkbox_->GetAnnotSubtype());
47 
48     // Read only radio button.
49     widget_readonly_radiobutton_ =
50         ToCPDFSDKWidget(it.GetNextAnnot(widget_checkbox_));
51     ASSERT_TRUE(widget_readonly_radiobutton_);
52     ASSERT_EQ(CPDF_Annot::Subtype::WIDGET,
53               widget_readonly_radiobutton_->GetAnnotSubtype());
54 
55     // Tabbing three times from read only radio button to unselected normal
56     // radio button.
57     widget_radiobutton_ = widget_readonly_radiobutton_;
58     ASSERT_TRUE(widget_radiobutton_);
59     for (int i = 0; i < 3; i++) {
60       widget_radiobutton_ =
61           ToCPDFSDKWidget(it.GetNextAnnot(widget_radiobutton_));
62       ASSERT_TRUE(widget_radiobutton_);
63     }
64 
65     ASSERT_EQ(CPDF_Annot::Subtype::WIDGET,
66               widget_radiobutton_->GetAnnotSubtype());
67   }
68 
FormFillerAndWindowSetup(CPDFSDK_Widget * widget)69   void FormFillerAndWindowSetup(CPDFSDK_Widget* widget) {
70     CFFL_InteractiveFormFiller* interactive_formfiller =
71         formfill_env_->GetInteractiveFormFiller();
72     {
73       ObservedPtr<CPDFSDK_Widget> observed(widget);
74       EXPECT_TRUE(interactive_formfiller->OnSetFocus(observed, {}));
75     }
76 
77     form_filler_ = interactive_formfiller->GetFormFieldForTesting(widget);
78     ASSERT_TRUE(form_filler_);
79 
80     window_ = form_filler_->CreateOrUpdatePWLWindow(
81         formfill_env_->GetPageViewAtIndex(0));
82     ASSERT_TRUE(window_);
83   }
84 
GetCPDFSDKWidgetCheckBox() const85   CPDFSDK_Widget* GetCPDFSDKWidgetCheckBox() const { return widget_checkbox_; }
GetCPDFSDKWidgetReadOnlyCheckBox() const86   CPDFSDK_Widget* GetCPDFSDKWidgetReadOnlyCheckBox() const {
87     return widget_readonly_checkbox_;
88   }
GetCPDFSDKWidgetRadioButton() const89   CPDFSDK_Widget* GetCPDFSDKWidgetRadioButton() const {
90     return widget_radiobutton_;
91   }
GetCPDFSDKWidgetReadOnlyRadioButton() const92   CPDFSDK_Widget* GetCPDFSDKWidgetReadOnlyRadioButton() const {
93     return widget_readonly_radiobutton_;
94   }
GetCPDFSDKFormFillEnv() const95   CPDFSDK_FormFillEnvironment* GetCPDFSDKFormFillEnv() const {
96     return formfill_env_;
97   }
GetWindow() const98   CPWL_Wnd* GetWindow() const { return window_; }
99 
100  private:
101   FPDF_PAGE page_;
102   CFFL_FormField* form_filler_;
103   CPDFSDK_Widget* widget_checkbox_;
104   CPDFSDK_Widget* widget_readonly_checkbox_;
105   CPDFSDK_Widget* widget_radiobutton_;
106   CPDFSDK_Widget* widget_readonly_radiobutton_;
107   CPDFSDK_FormFillEnvironment* formfill_env_;
108   CPWL_Wnd* window_;
109 };
110 
TEST_F(CPWLSpecialButtonEmbedderTest,EnterOnReadOnlyCheckBox)111 TEST_F(CPWLSpecialButtonEmbedderTest, EnterOnReadOnlyCheckBox) {
112   FormFillerAndWindowSetup(GetCPDFSDKWidgetReadOnlyCheckBox());
113   CPWL_CheckBox* check_box = static_cast<CPWL_CheckBox*>(GetWindow());
114   EXPECT_TRUE(check_box->IsChecked());
115   EXPECT_TRUE(GetCPDFSDKFormFillEnv()->GetInteractiveFormFiller()->OnChar(
116       GetCPDFSDKWidgetReadOnlyCheckBox(), '\r', {}));
117   EXPECT_TRUE(check_box->IsChecked());
118 }
119 
TEST_F(CPWLSpecialButtonEmbedderTest,EnterOnCheckBox)120 TEST_F(CPWLSpecialButtonEmbedderTest, EnterOnCheckBox) {
121   FormFillerAndWindowSetup(GetCPDFSDKWidgetCheckBox());
122   CPWL_CheckBox* check_box = static_cast<CPWL_CheckBox*>(GetWindow());
123   EXPECT_TRUE(GetCPDFSDKFormFillEnv()->GetInteractiveFormFiller()->OnChar(
124       GetCPDFSDKWidgetCheckBox(), '\r', {}));
125   EXPECT_TRUE(check_box->IsChecked());
126 
127   EXPECT_TRUE(GetCPDFSDKFormFillEnv()->GetInteractiveFormFiller()->OnChar(
128       GetCPDFSDKWidgetCheckBox(), '\r', {}));
129   EXPECT_FALSE(check_box->IsChecked());
130 }
131 
TEST_F(CPWLSpecialButtonEmbedderTest,EnterOnReadOnlyRadioButton)132 TEST_F(CPWLSpecialButtonEmbedderTest, EnterOnReadOnlyRadioButton) {
133   FormFillerAndWindowSetup(GetCPDFSDKWidgetReadOnlyRadioButton());
134   CPWL_RadioButton* radio_button = static_cast<CPWL_RadioButton*>(GetWindow());
135   EXPECT_FALSE(radio_button->IsChecked());
136   EXPECT_TRUE(GetCPDFSDKFormFillEnv()->GetInteractiveFormFiller()->OnChar(
137       GetCPDFSDKWidgetReadOnlyRadioButton(), '\r', {}));
138   EXPECT_FALSE(radio_button->IsChecked());
139 }
140 
TEST_F(CPWLSpecialButtonEmbedderTest,EnterOnRadioButton)141 TEST_F(CPWLSpecialButtonEmbedderTest, EnterOnRadioButton) {
142   FormFillerAndWindowSetup(GetCPDFSDKWidgetRadioButton());
143   CPWL_RadioButton* radio_button = static_cast<CPWL_RadioButton*>(GetWindow());
144   EXPECT_TRUE(GetCPDFSDKFormFillEnv()->GetInteractiveFormFiller()->OnChar(
145       GetCPDFSDKWidgetRadioButton(), '\r', {}));
146   EXPECT_TRUE(radio_button->IsChecked());
147 }
148