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_SUPPORT_H_
      6 #define TESTING_EMBEDDER_TEST_SUPPORT_H_
      7 
      8 #include <stdlib.h>
      9 #include <memory>
     10 #include <string>
     11 
     12 #include "public/fpdfview.h"
     13 #include "public/fpdf_save.h"
     14 
     15 #ifdef PDF_ENABLE_V8
     16 #include "v8/include/v8.h"
     17 #endif  // PDF_ENABLE_V8
     18 
     19 namespace pdfium {
     20 
     21 // Used with std::unique_ptr to free() objects that can't be deleted.
     22 struct FreeDeleter {
     23   inline void operator()(void* ptr) const { free(ptr); }
     24 };
     25 
     26 }  // namespace pdfium
     27 
     28 // Reads the entire contents of a file into a newly alloc'd buffer.
     29 std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename,
     30                                                            size_t* retlen);
     31 
     32 // Converts a FPDF_WIDESTRING to a std::wstring.
     33 // Deals with differences between UTF16LE and wchar_t.
     34 std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr);
     35 
     36 // Returns a newly allocated FPDF_WIDESTRING.
     37 // Deals with differences between UTF16LE and wchar_t.
     38 std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
     39     const std::wstring& wstr);
     40 
     41 #ifdef PDF_ENABLE_V8
     42 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
     43 bool InitializeV8ForPDFium(const std::string& exe_path,
     44                            const std::string& bin_dir,
     45                            v8::StartupData* natives_blob,
     46                            v8::StartupData* snapshot_blob,
     47                            v8::Platform** platform);
     48 #else   // V8_USE_EXTERNAL_STARTUP_DATA
     49 bool InitializeV8ForPDFium(v8::Platform** platform);
     50 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
     51 #endif  // PDF_ENABLE_V8
     52 
     53 class TestLoader {
     54  public:
     55   TestLoader(const char* pBuf, size_t len);
     56   static int GetBlock(void* param,
     57                       unsigned long pos,
     58                       unsigned char* pBuf,
     59                       unsigned long size);
     60 
     61  private:
     62   const char* const m_pBuf;
     63   const size_t m_Len;
     64 };
     65 
     66 class TestSaver : public FPDF_FILEWRITE {
     67  public:
     68   TestSaver();
     69 
     70   void ClearString();
     71   const std::string& GetString() const { return m_String; }
     72 
     73  private:
     74   static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
     75                                 const void* data,
     76                                 unsigned long size);
     77 
     78   std::string m_String;
     79 };
     80 
     81 #endif  // TESTING_EMBEDDER_TEST_SUPPORT_H_
     82