• 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_FORMFILLINFO* info,
55                               FPDF_DOCUMENT document,
56                               int page_index);
57   };
58 
59   EmbedderTest();
60   virtual ~EmbedderTest();
61 
62   void SetUp() override;
63   void TearDown() override;
64 
65 #ifdef PDF_ENABLE_V8
66   // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
SetExternalIsolate(void * isolate)67   void SetExternalIsolate(void* isolate) {
68     external_isolate_ = static_cast<v8::Isolate*>(isolate);
69   }
70 #endif  // PDF_ENABLE_V8
71 
SetDelegate(Delegate * delegate)72   void SetDelegate(Delegate* delegate) {
73     delegate_ = delegate ? delegate : default_delegate_.get();
74   }
75 
document()76   FPDF_DOCUMENT document() { return document_; }
form_handle()77   FPDF_FORMHANDLE form_handle() { return form_handle_; }
78 
79   // Create an empty document, and its form fill environment. Returns true
80   // on success or false on failure.
81   virtual bool CreateEmptyDocument();
82 
83   // Open the document specified by |filename|, and create its form fill
84   // environment, or return false on failure.
85   // The filename is relative to the test data directory where we store all the
86   // test files.
87   virtual bool OpenDocument(const std::string& filename,
88                             const char* password = nullptr,
89                             bool must_linearize = false);
90 
91   // Perform JavaScript actions that are to run at document open time.
92   virtual void DoOpenActions();
93 
94   // Determine the page numbers present in the document.
95   virtual int GetFirstPageNum();
96   virtual int GetPageCount();
97 
98   // Load a specific page of the open document.
99   virtual FPDF_PAGE LoadPage(int page_number);
100 
101   // Convert a loaded page into a bitmap.
102   virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
103 
104   // Relese the resources obtained from LoadPage(). Further use of |page|
105   // is prohibited after this call is made.
106   virtual void UnloadPage(FPDF_PAGE page);
107 
108   // Check |bitmap| to make sure it has the right dimensions and content.
109   static void CompareBitmap(FPDF_BITMAP bitmap,
110                             int expected_width,
111                             int expected_height,
112                             const char* expected_md5sum);
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 #endif  // PDF_ENABLE_V8
128   void* external_isolate_;
129   TestLoader* loader_;
130   size_t file_length_;
131   std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
132   std::map<int, FPDF_PAGE> page_map_;
133   std::map<FPDF_PAGE, int> page_reverse_map_;
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