• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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 #ifndef TESTING_EMBEDDER_TEST_H_
6 #define TESTING_EMBEDDER_TEST_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 
12 #include "public/fpdf_dataavail.h"
13 #include "public/fpdf_ext.h"
14 #include "public/fpdf_formfill.h"
15 #include "public/fpdfview.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/test_support.h"
18 
19 #ifdef PDF_ENABLE_V8
20 #include "v8/include/v8.h"
21 #endif  // PDF_ENABLE_v8
22 
23 class TestLoader;
24 
25 // This class is used to load a PDF document, and then run programatic
26 // API tests against it.
27 class EmbedderTest : public ::testing::Test,
28                      public UNSUPPORT_INFO,
29                      public IPDF_JSPLATFORM,
30                      public FPDF_FORMFILLINFO {
31  public:
32   class Delegate {
33    public:
~Delegate()34     virtual ~Delegate() {}
35 
36     // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
UnsupportedHandler(int type)37     virtual void UnsupportedHandler(int type) {}
38 
39     // Equivalent to IPDF_JSPLATFORM::app_alert().
Alert(FPDF_WIDESTRING message,FPDF_WIDESTRING title,int type,int icon)40     virtual int Alert(FPDF_WIDESTRING message,
41                       FPDF_WIDESTRING title,
42                       int type,
43                       int icon) {
44       return 0;
45     }
46 
47     // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
SetTimer(int msecs,TimerCallback fn)48     virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
49 
50     // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
KillTimer(int id)51     virtual void KillTimer(int id) {}
52 
53     // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage().
54     virtual FPDF_PAGE GetPage(FPDF_FORMHANDLE form_handle,
55                               FPDF_DOCUMENT document,
56                               int page_index);
57 
58    private:
59     std::map<int, FPDF_PAGE> m_pageMap;
60   };
61 
62   EmbedderTest();
63   virtual ~EmbedderTest();
64 
65   void SetUp() override;
66   void TearDown() override;
67 
68 #ifdef PDF_ENABLE_V8
69   // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
SetExternalIsolate(void * isolate)70   void SetExternalIsolate(void* isolate) {
71     external_isolate_ = static_cast<v8::Isolate*>(isolate);
72   }
73 #endif  // PDF_ENABLE_V8
74 
SetDelegate(Delegate * delegate)75   void SetDelegate(Delegate* delegate) {
76     delegate_ = delegate ? delegate : default_delegate_.get();
77   }
78 
document()79   FPDF_DOCUMENT document() { return document_; }
form_handle()80   FPDF_FORMHANDLE form_handle() { return form_handle_; }
81 
82   // Create an empty document, and its form fill environment. Returns true
83   // on success or false on failure.
84   virtual bool CreateEmptyDocument();
85 
86   // Open the document specified by |filename|, and create its form fill
87   // environment, or return false on failure.
88   // The filename is relative to the test data directory where we store all the
89   // test files.
90   virtual bool OpenDocument(const std::string& filename,
91                             bool must_linearize = false);
92 
93   // Perform JavaScript actions that are to run at document open time.
94   virtual void DoOpenActions();
95 
96   // Determine the page numbers present in the document.
97   virtual int GetFirstPageNum();
98   virtual int GetPageCount();
99 
100   // Load a specific page of the open document.
101   virtual FPDF_PAGE LoadPage(int page_number);
102 
103   // Load a specific page of the open document using delegate_->GetPage.
104   // delegate_->GetPage also caches loaded page.
105   virtual FPDF_PAGE LoadAndCachePage(int page_number);
106 
107   // Convert a loaded page into a bitmap.
108   virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
109 
110   // Relese the resources obtained from LoadPage(). Further use of |page|
111   // is prohibited after this call is made.
112   virtual void UnloadPage(FPDF_PAGE page);
113 
114  protected:
115   void SetupFormFillEnvironment();
116 
117   Delegate* delegate_;
118   std::unique_ptr<Delegate> default_delegate_;
119   FPDF_DOCUMENT document_;
120   FPDF_FORMHANDLE form_handle_;
121   FPDF_AVAIL avail_;
122   FX_DOWNLOADHINTS hints_;
123   FPDF_FILEACCESS file_access_;
124   FX_FILEAVAIL file_avail_;
125 #ifdef PDF_ENABLE_V8
126   v8::Platform* platform_;
127   v8::StartupData natives_;
128   v8::StartupData snapshot_;
129 #endif  // PDF_ENABLE_V8
130   void* external_isolate_;
131   TestLoader* loader_;
132   size_t file_length_;
133   std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
134 
135  private:
136   static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
137   static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
138                              FPDF_WIDESTRING message,
139                              FPDF_WIDESTRING title,
140                              int type,
141                              int icon);
142   static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
143                                 int msecs,
144                                 TimerCallback fn);
145   static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
146   static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
147                                      FPDF_DOCUMENT document,
148                                      int page_index);
149 };
150 
151 #endif  // TESTING_EMBEDDER_TEST_H_
152