blob: fb3ea428f16e1e2cc0ac4a8f2a973597f68bab60 [file] [log] [blame]
Tom Sepez96d13342015-01-16 14:59:26 -08001// Copyright (c) 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
Tom Sepez396e8722015-09-09 10:16:08 -07008#include <map>
Tom Sepez96d13342015-01-16 14:59:26 -08009#include <string>
10
Tom Sepez1ed8a212015-05-11 15:25:39 -070011#include "../public/fpdf_dataavail.h"
12#include "../public/fpdf_ext.h"
13#include "../public/fpdf_formfill.h"
14#include "../public/fpdfview.h"
Tom Sepeza72e8e22015-10-07 10:17:53 -070015#include "../third_party/base/nonstd_unique_ptr.h"
Tom Sepez96d13342015-01-16 14:59:26 -080016#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070017
18#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080019#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070020#endif // PDF_ENABLE_v8
Tom Sepez96d13342015-01-16 14:59:26 -080021
22class TestLoader;
23
24// This class is used to load a PDF document, and then run programatic
25// API tests against it.
Tom Sepez4cb0fa72015-02-25 16:08:18 -080026class EmbedderTest : public ::testing::Test,
27 public UNSUPPORT_INFO,
28 public IPDF_JSPLATFORM,
29 public FPDF_FORMFILLINFO {
Tom Sepez96d13342015-01-16 14:59:26 -080030 public:
Tom Sepez4cb0fa72015-02-25 16:08:18 -080031 class Delegate {
32 public:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 virtual ~Delegate() {}
Tom Sepez96d13342015-01-16 14:59:26 -080034
Tom Sepez4cb0fa72015-02-25 16:08:18 -080035 // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 virtual void UnsupportedHandler(int type) {}
Tom Sepez4cb0fa72015-02-25 16:08:18 -080037
38 // Equivalent to IPDF_JSPLATFORM::app_alert().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 virtual int Alert(FPDF_WIDESTRING message,
40 FPDF_WIDESTRING title,
41 int type,
42 int icon) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080043 return 0;
44 }
Tom Sepez6efc0ad2015-06-02 17:11:18 -070045
46 // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
47 virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
48
49 // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 virtual void KillTimer(int id) {}
Tom Sepez396e8722015-09-09 10:16:08 -070051
52 // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage().
53 virtual FPDF_PAGE GetPage(FPDF_FORMHANDLE form_handle,
54 FPDF_DOCUMENT document,
55 int page_index);
56
57 private:
58 std::map<int, FPDF_PAGE> m_pageMap;
Tom Sepez4cb0fa72015-02-25 16:08:18 -080059 };
60
61 EmbedderTest();
62 virtual ~EmbedderTest();
Tom Sepez96d13342015-01-16 14:59:26 -080063
64 void SetUp() override;
65 void TearDown() override;
66
Tom Sepez452b4f32015-10-13 09:27:27 -070067#ifdef PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
Tom Sepez452b4f32015-10-13 09:27:27 -070069 void SetExternalIsolate(void* isolate) {
70 external_isolate_ = static_cast<v8::Isolate*>(isolate);
71 }
72#endif // PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070073
Tom Sepez4cb0fa72015-02-25 16:08:18 -080074 void SetDelegate(Delegate* delegate) {
Tom Sepeza72e8e22015-10-07 10:17:53 -070075 delegate_ = delegate ? delegate : default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080076 }
77
Tom Sepez96d13342015-01-16 14:59:26 -080078 FPDF_DOCUMENT document() { return document_; }
Tom Sepezda8189e2015-01-30 14:41:50 -080079 FPDF_FORMHANDLE form_handle() { return form_handle_; }
Tom Sepez96d13342015-01-16 14:59:26 -080080
Tom Sepezda8189e2015-01-30 14:41:50 -080081 // Open the document specified by |filename|, and create its form fill
82 // environment, or return false on failure.
Tom Sepez96d13342015-01-16 14:59:26 -080083 virtual bool OpenDocument(const std::string& filename);
84
Tom Sepez96d13342015-01-16 14:59:26 -080085 // Perform JavaScript actions that are to run at document open time.
Tom Sepezda8189e2015-01-30 14:41:50 -080086 virtual void DoOpenActions();
Tom Sepez96d13342015-01-16 14:59:26 -080087
88 // Determine the page numbers present in the document.
89 virtual int GetFirstPageNum();
90 virtual int GetPageCount();
91
92 // Load a specific page of the open document.
Tom Sepezda8189e2015-01-30 14:41:50 -080093 virtual FPDF_PAGE LoadPage(int page_number);
Tom Sepez96d13342015-01-16 14:59:26 -080094
Tom Sepez396e8722015-09-09 10:16:08 -070095 // Load a specific page of the open document using delegate_->GetPage.
96 // delegate_->GetPage also caches loaded page.
97 virtual FPDF_PAGE LoadAndCachePage(int page_number);
98
Tom Sepez96d13342015-01-16 14:59:26 -080099 // Convert a loaded page into a bitmap.
Tom Sepezda8189e2015-01-30 14:41:50 -0800100 virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800101
102 // Relese the resources obtained from LoadPage(). Further use of |page|
103 // is prohibited after this call is made.
Tom Sepezda8189e2015-01-30 14:41:50 -0800104 virtual void UnloadPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800105
Tom Sepez2255a1b2015-01-23 15:33:44 -0800106 protected:
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800107 Delegate* delegate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700108 nonstd::unique_ptr<Delegate> default_delegate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800109 FPDF_DOCUMENT document_;
Tom Sepezda8189e2015-01-30 14:41:50 -0800110 FPDF_FORMHANDLE form_handle_;
Tom Sepez96d13342015-01-16 14:59:26 -0800111 FPDF_AVAIL avail_;
112 FX_DOWNLOADHINTS hints_;
113 FPDF_FILEACCESS file_access_;
114 FX_FILEAVAIL file_avail_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700115#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700116 v8::Platform* platform_;
Tom Sepez96d13342015-01-16 14:59:26 -0800117 v8::StartupData natives_;
118 v8::StartupData snapshot_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700119#endif // PDF_ENABLE_V8
120 void* external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800121 TestLoader* loader_;
122 size_t file_length_;
123 char* file_contents_;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800124
125 private:
126 static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
128 FPDF_WIDESTRING message,
129 FPDF_WIDESTRING title,
130 int type,
131 int icon);
132 static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
133 int msecs,
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700134 TimerCallback fn);
135 static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
Tom Sepez396e8722015-09-09 10:16:08 -0700136 static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
137 FPDF_DOCUMENT document,
138 int page_index);
Tom Sepez96d13342015-01-16 14:59:26 -0800139};
140
141#endif // TESTING_EMBEDDER_TEST_H_