Home | History | Annotate | Download | only in testing
      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/fpdf_save.h"
     16 #include "public/fpdfview.h"
     17 #include "testing/fake_file_access.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 #include "testing/test_support.h"
     20 
     21 #ifdef PDF_ENABLE_V8
     22 #include "v8/include/v8.h"
     23 #endif  // PDF_ENABLE_v8
     24 
     25 class TestLoader;
     26 
     27 // This class is used to load a PDF document, and then run programatic
     28 // API tests against it.
     29 class EmbedderTest : public ::testing::Test,
     30                      public UNSUPPORT_INFO,
     31                      public IPDF_JSPLATFORM,
     32                      public FPDF_FORMFILLINFO,
     33                      public FPDF_FILEWRITE {
     34  public:
     35   class Delegate {
     36    public:
     37     virtual ~Delegate() {}
     38 
     39     // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
     40     virtual void UnsupportedHandler(int type) {}
     41 
     42     // Equivalent to IPDF_JSPLATFORM::app_alert().
     43     virtual int Alert(FPDF_WIDESTRING message,
     44                       FPDF_WIDESTRING title,
     45                       int type,
     46                       int icon) {
     47       return 0;
     48     }
     49 
     50     // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
     51     virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
     52 
     53     // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
     54     virtual void KillTimer(int id) {}
     55 
     56     // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage().
     57     virtual FPDF_PAGE GetPage(FPDF_FORMFILLINFO* info,
     58                               FPDF_DOCUMENT document,
     59                               int page_index);
     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.
     70   void SetExternalIsolate(void* isolate) {
     71     external_isolate_ = static_cast<v8::Isolate*>(isolate);
     72   }
     73 #endif  // PDF_ENABLE_V8
     74 
     75   void SetDelegate(Delegate* delegate) {
     76     delegate_ = delegate ? delegate : default_delegate_.get();
     77   }
     78 
     79   FPDF_DOCUMENT document() { return document_; }
     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   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   // |password| can be nullptr if there is none.
     91   virtual bool OpenDocumentWithOptions(const std::string& filename,
     92                                        const char* password,
     93                                        bool must_linearize);
     94 
     95   // Variants provided for convenience.
     96   bool OpenDocument(const std::string& filename);
     97   bool OpenDocumentLinearized(const std::string& filename);
     98   bool OpenDocumentWithPassword(const std::string& filename,
     99                                 const char* password);
    100 
    101   // Perform JavaScript actions that are to run at document open time.
    102   void DoOpenActions();
    103 
    104   // Determine the page numbers present in the document.
    105   int GetFirstPageNum();
    106   int GetPageCount();
    107 
    108   // Load a specific page of the open document.
    109   FPDF_PAGE LoadPage(int page_number);
    110 
    111   // Convert a loaded page into a bitmap.
    112   FPDF_BITMAP RenderPage(FPDF_PAGE page);
    113 
    114   // Convert a loaded page into a bitmap with page rendering flags specified.
    115   // See public/fpdfview.h for a list of page rendering flags.
    116   FPDF_BITMAP RenderPageWithFlags(FPDF_PAGE page,
    117                                   FPDF_FORMHANDLE handle,
    118                                   int flags);
    119 
    120   // Relese the resources obtained from LoadPage(). Further use of |page|
    121   // is prohibited after this call is made.
    122   void UnloadPage(FPDF_PAGE page);
    123 
    124  protected:
    125   bool OpenDocumentHelper(const char* password,
    126                           bool must_linearize,
    127                           FakeFileAccess* network_simulator,
    128                           FPDF_DOCUMENT* document,
    129                           FPDF_AVAIL* avail,
    130                           FPDF_FORMHANDLE* form_handle);
    131 
    132   FPDF_FORMHANDLE SetupFormFillEnvironment(FPDF_DOCUMENT doc);
    133 
    134   // Return the hash of |bitmap|.
    135   static std::string HashBitmap(FPDF_BITMAP bitmap);
    136 
    137 #ifndef NDEBUG
    138   // For debugging purposes.
    139   // Write |bitmap| to a png file.
    140   static void WriteBitmapToPng(FPDF_BITMAP bitmap, const std::string& filename);
    141 #endif
    142 
    143   // Check |bitmap| to make sure it has the right dimensions and content.
    144   static void CompareBitmap(FPDF_BITMAP bitmap,
    145                             int expected_width,
    146                             int expected_height,
    147                             const char* expected_md5sum);
    148 
    149   void ClearString() { m_String.clear(); }
    150   const std::string& GetString() const { return m_String; }
    151 
    152   static int GetBlockFromString(void* param,
    153                                 unsigned long pos,
    154                                 unsigned char* buf,
    155                                 unsigned long size);
    156 
    157   FPDF_DOCUMENT OpenSavedDocument(const char* password = nullptr);
    158   void CloseSavedDocument();
    159   FPDF_PAGE LoadSavedPage(int page_number);
    160   FPDF_BITMAP RenderSavedPage(FPDF_PAGE page);
    161   void CloseSavedPage(FPDF_PAGE page);
    162   void VerifySavedRendering(FPDF_PAGE page,
    163                             int width,
    164                             int height,
    165                             const char* md5);
    166   void VerifySavedDocument(int width, int height, const char* md5);
    167 
    168   void SetWholeFileAvailable();
    169 
    170   Delegate* delegate_;
    171   std::unique_ptr<Delegate> default_delegate_;
    172   FPDF_DOCUMENT document_;
    173   FPDF_FORMHANDLE form_handle_;
    174   FPDF_AVAIL avail_;
    175   FPDF_FILEACCESS file_access_;  // must outlive avail_.
    176   void* external_isolate_;
    177   TestLoader* loader_;
    178   size_t file_length_;
    179   std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
    180   std::map<int, FPDF_PAGE> page_map_;
    181   std::map<FPDF_PAGE, int> page_reverse_map_;
    182   FPDF_DOCUMENT m_SavedDocument;
    183   FPDF_FORMHANDLE m_SavedForm;
    184   FPDF_AVAIL m_SavedAvail;
    185   FPDF_FILEACCESS saved_file_access_;  // must outlive m_SavedAvail.
    186   std::unique_ptr<FakeFileAccess> fake_file_access_;  // must outlive avail_.
    187   std::unique_ptr<FakeFileAccess>
    188       saved_fake_file_access_;  // must outlive m_SavedAvail.
    189 
    190  private:
    191   static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
    192   static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
    193                              FPDF_WIDESTRING message,
    194                              FPDF_WIDESTRING title,
    195                              int type,
    196                              int icon);
    197   static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
    198                                 int msecs,
    199                                 TimerCallback fn);
    200   static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
    201   static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
    202                                      FPDF_DOCUMENT document,
    203                                      int page_index);
    204   static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
    205                                 const void* data,
    206                                 unsigned long size);
    207 
    208   std::string m_String;
    209 };
    210 
    211 #endif  // TESTING_EMBEDDER_TEST_H_
    212