Add EmbedderTest::LoadPageNoEvents() and UnloadPageNoEvents()

Call it in fpdf_annot_embeddertest.cpp. This fixes a couple of TODOs,
and allows the use of the rest of the embedder test mechanism on
these cases in a subsequent cl.

Change-Id: I41a7136b62c2f9d8010057f157a0e182ba12fdf7
Reviewed-on: https://pdfium-review.googlesource.com/c/45170
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_annot_embeddertest.cpp b/fpdfsdk/fpdf_annot_embeddertest.cpp
index 7be5d75..777d821 100644
--- a/fpdfsdk/fpdf_annot_embeddertest.cpp
+++ b/fpdfsdk/fpdf_annot_embeddertest.cpp
@@ -97,10 +97,7 @@
 TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
   // Open a file with one annotation and load its first page.
   ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
-  // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
-  // the FORM API calls in LoadPage() makes this test fail. So use
-  // FPDF_LoadPage() and FPDF_ClosePage() for now.
-  FPDF_PAGE page = FPDF_LoadPage(document(), 0);
+  FPDF_PAGE page = LoadPageNoEvents(0);
   ASSERT_TRUE(page);
 
   // Check that there is a total of 1 annotation on its first page.
@@ -174,16 +171,13 @@
     EXPECT_EQ(157.211182f, quadpoints.x4);
     EXPECT_EQ(706.264465f, quadpoints.y4);
   }
-  FPDF_ClosePage(page);
+  UnloadPageNoEvents(page);
 }
 
 TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
   // Open a file with three annotations and load its first page.
   ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
-  // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
-  // the FORM API calls in LoadPage() makes this test fail. So use
-  // FPDF_LoadPage() and FPDF_ClosePage() for now.
-  FPDF_PAGE page = FPDF_LoadPage(document(), 0);
+  FPDF_PAGE page = LoadPageNoEvents(0);
   ASSERT_TRUE(page);
 
   // Check that there is a total of 3 annotation on its first page.
@@ -220,7 +214,7 @@
     EXPECT_EQ(475.336090f, rect.right);
     EXPECT_EQ(681.535034f, rect.top);
   }
-  FPDF_ClosePage(page);
+  UnloadPageNoEvents(page);
 }
 
 TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
@@ -600,10 +594,7 @@
 TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
   // Open a file with 3 annotations on its first page.
   ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
-  // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
-  // the FORM API calls in LoadPage() makes this test fail. So use
-  // FPDF_LoadPage() and FPDF_ClosePage() for now.
-  FPDF_PAGE page = FPDF_LoadPage(document(), 0);
+  FPDF_PAGE page = LoadPageNoEvents(0);
   ASSERT_TRUE(page);
   EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
 
@@ -641,7 +632,7 @@
 
   // Save the document, closing the page and document.
   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
-  FPDF_ClosePage(page);
+  UnloadPageNoEvents(page);
 
   // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
   // Open the saved document.
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index 6dd13e2..a94b7ef 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -282,6 +282,14 @@
 }
 
 FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
+  return LoadPageCommon(page_number, true);
+}
+
+FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
+  return LoadPageCommon(page_number, false);
+}
+
+FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
   ASSERT(form_handle_);
   ASSERT(page_number >= 0);
   ASSERT(!pdfium::ContainsKey(page_map_, page_number));
@@ -290,25 +298,34 @@
   if (!page)
     return nullptr;
 
-  FORM_OnAfterLoadPage(page, form_handle_);
-  FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
+  if (do_events) {
+    FORM_OnAfterLoadPage(page, form_handle_);
+    FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
+  }
   page_map_[page_number] = page;
   return page;
 }
 
 void EmbedderTest::UnloadPage(FPDF_PAGE page) {
-  ASSERT(form_handle_);
+  UnloadPageCommon(page, true);
+}
 
+void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
+  UnloadPageCommon(page, false);
+}
+
+void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
+  ASSERT(form_handle_);
   int page_number = GetPageNumberForLoadedPage(page);
   if (page_number < 0) {
     NOTREACHED();
     return;
   }
-
-  FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
-  FORM_OnBeforeClosePage(page, form_handle_);
+  if (do_events) {
+    FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
+    FORM_OnBeforeClosePage(page, form_handle_);
+  }
   FPDF_ClosePage(page);
-
   page_map_.erase(page_number);
 }
 
diff --git a/testing/embedder_test.h b/testing/embedder_test.h
index 37c5ca8..4ba5d44 100644
--- a/testing/embedder_test.h
+++ b/testing/embedder_test.h
@@ -119,10 +119,16 @@
   // holds the page handle for that page.
   FPDF_PAGE LoadPage(int page_number);
 
+  // Same as LoadPage(), but does not fire form events.
+  FPDF_PAGE LoadPageNoEvents(int page_number);
+
   // Fire form unload events and release the resources for a |page| obtained
   // from LoadPage(). Further use of |page| is prohibited after calling this.
   void UnloadPage(FPDF_PAGE page);
 
+  // Same as UnloadPage(), but does not fire form events.
+  void UnloadPageNoEvents(FPDF_PAGE page);
+
   // RenderLoadedPageWithFlags() with no flags.
   ScopedFPDFBitmap RenderLoadedPage(FPDF_PAGE page);
 
@@ -258,6 +264,9 @@
   // Same as GetPageNumberForLoadedPage(), but with |saved_page_map_|.
   int GetPageNumberForSavedPage(FPDF_PAGE page) const;
 
+  void UnloadPageCommon(FPDF_PAGE page, bool do_events);
+  FPDF_PAGE LoadPageCommon(int page_number, bool do_events);
+
   std::string data_string_;
   std::string saved_document_file_data_;
   std::ofstream filestream_;