blob: 6b814a726771c979792086dd46d7086ed5ee3f33 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// 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>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080010#include <string>
11
Lei Zhangb4e7f302015-11-06 15:52:32 -080012#include "public/fpdf_dataavail.h"
13#include "public/fpdf_ext.h"
14#include "public/fpdf_formfill.h"
15#include "public/fpdfview.h"
Tom Sepez96d13342015-01-16 14:59:26 -080016#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aa35312016-01-06 10:16:32 -080017#include "testing/test_support.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070018
19#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080020#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070021#endif // PDF_ENABLE_v8
Tom Sepez96d13342015-01-16 14:59:26 -080022
23class TestLoader;
24
25// This class is used to load a PDF document, and then run programatic
26// API tests against it.
Tom Sepez4cb0fa72015-02-25 16:08:18 -080027class EmbedderTest : public ::testing::Test,
28 public UNSUPPORT_INFO,
29 public IPDF_JSPLATFORM,
30 public FPDF_FORMFILLINFO {
Tom Sepez96d13342015-01-16 14:59:26 -080031 public:
Tom Sepez4cb0fa72015-02-25 16:08:18 -080032 class Delegate {
33 public:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 virtual ~Delegate() {}
Tom Sepez96d13342015-01-16 14:59:26 -080035
Tom Sepez4cb0fa72015-02-25 16:08:18 -080036 // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 virtual void UnsupportedHandler(int type) {}
Tom Sepez4cb0fa72015-02-25 16:08:18 -080038
39 // Equivalent to IPDF_JSPLATFORM::app_alert().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 virtual int Alert(FPDF_WIDESTRING message,
41 FPDF_WIDESTRING title,
42 int type,
43 int icon) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080044 return 0;
45 }
Tom Sepez6efc0ad2015-06-02 17:11:18 -070046
47 // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
48 virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
49
50 // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 virtual void KillTimer(int id) {}
Tom Sepez396e8722015-09-09 10:16:08 -070052
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;
Tom Sepez4cb0fa72015-02-25 16:08:18 -080060 };
61
62 EmbedderTest();
63 virtual ~EmbedderTest();
Tom Sepez96d13342015-01-16 14:59:26 -080064
65 void SetUp() override;
66 void TearDown() override;
67
Tom Sepez452b4f32015-10-13 09:27:27 -070068#ifdef PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070069 // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
Tom Sepez452b4f32015-10-13 09:27:27 -070070 void SetExternalIsolate(void* isolate) {
71 external_isolate_ = static_cast<v8::Isolate*>(isolate);
72 }
73#endif // PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070074
Tom Sepez4cb0fa72015-02-25 16:08:18 -080075 void SetDelegate(Delegate* delegate) {
Tom Sepeza72e8e22015-10-07 10:17:53 -070076 delegate_ = delegate ? delegate : default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080077 }
78
Tom Sepez96d13342015-01-16 14:59:26 -080079 FPDF_DOCUMENT document() { return document_; }
Tom Sepezda8189e2015-01-30 14:41:50 -080080 FPDF_FORMHANDLE form_handle() { return form_handle_; }
Tom Sepez96d13342015-01-16 14:59:26 -080081
Tom Sepezd483eb42016-01-06 10:03:59 -080082 // Create an empty document, and its form fill environment. Returns true
83 // on success or false on failure.
84 virtual bool CreateEmptyDocument();
85
Tom Sepezda8189e2015-01-30 14:41:50 -080086 // Open the document specified by |filename|, and create its form fill
87 // environment, or return false on failure.
Wei Li091f7a02015-11-09 12:09:55 -080088 // The filename is relative to the test data directory where we store all the
89 // test files.
Jun Fangdf7f3662015-11-10 18:29:18 +080090 virtual bool OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -070091 const char* password = nullptr,
Jun Fangdf7f3662015-11-10 18:29:18 +080092 bool must_linearize = false);
Tom Sepez96d13342015-01-16 14:59:26 -080093
Tom Sepez96d13342015-01-16 14:59:26 -080094 // Perform JavaScript actions that are to run at document open time.
Tom Sepezda8189e2015-01-30 14:41:50 -080095 virtual void DoOpenActions();
Tom Sepez96d13342015-01-16 14:59:26 -080096
97 // Determine the page numbers present in the document.
98 virtual int GetFirstPageNum();
99 virtual int GetPageCount();
100
101 // Load a specific page of the open document.
Tom Sepezda8189e2015-01-30 14:41:50 -0800102 virtual FPDF_PAGE LoadPage(int page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800103
Tom Sepez396e8722015-09-09 10:16:08 -0700104 // Load a specific page of the open document using delegate_->GetPage.
105 // delegate_->GetPage also caches loaded page.
106 virtual FPDF_PAGE LoadAndCachePage(int page_number);
107
Tom Sepez96d13342015-01-16 14:59:26 -0800108 // Convert a loaded page into a bitmap.
Tom Sepezda8189e2015-01-30 14:41:50 -0800109 virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800110
111 // Relese the resources obtained from LoadPage(). Further use of |page|
112 // is prohibited after this call is made.
Tom Sepezda8189e2015-01-30 14:41:50 -0800113 virtual void UnloadPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800114
Tom Sepez2255a1b2015-01-23 15:33:44 -0800115 protected:
Tom Sepezd483eb42016-01-06 10:03:59 -0800116 void SetupFormFillEnvironment();
117
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800118 Delegate* delegate_;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800119 std::unique_ptr<Delegate> default_delegate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800120 FPDF_DOCUMENT document_;
Tom Sepezda8189e2015-01-30 14:41:50 -0800121 FPDF_FORMHANDLE form_handle_;
Tom Sepez96d13342015-01-16 14:59:26 -0800122 FPDF_AVAIL avail_;
123 FX_DOWNLOADHINTS hints_;
124 FPDF_FILEACCESS file_access_;
125 FX_FILEAVAIL file_avail_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700126#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700127 v8::Platform* platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700128#endif // PDF_ENABLE_V8
129 void* external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800130 TestLoader* loader_;
131 size_t file_length_;
Tom Sepez0aa35312016-01-06 10:16:32 -0800132 std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800133
134 private:
135 static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
137 FPDF_WIDESTRING message,
138 FPDF_WIDESTRING title,
139 int type,
140 int icon);
141 static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
142 int msecs,
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700143 TimerCallback fn);
144 static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
Tom Sepez396e8722015-09-09 10:16:08 -0700145 static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
146 FPDF_DOCUMENT document,
147 int page_index);
Tom Sepez96d13342015-01-16 14:59:26 -0800148};
149
150#endif // TESTING_EMBEDDER_TEST_H_