blob: f6842d23c3233701f51d94c4887303ebf0b7709f [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"
17#include "v8/include/v8.h"
18
19class TestLoader;
20
21// This class is used to load a PDF document, and then run programatic
22// API tests against it.
Tom Sepez4cb0fa72015-02-25 16:08:18 -080023class EmbedderTest : public ::testing::Test,
24 public UNSUPPORT_INFO,
25 public IPDF_JSPLATFORM,
26 public FPDF_FORMFILLINFO {
Tom Sepez96d13342015-01-16 14:59:26 -080027 public:
Tom Sepez4cb0fa72015-02-25 16:08:18 -080028 class Delegate {
29 public:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 virtual ~Delegate() {}
Tom Sepez96d13342015-01-16 14:59:26 -080031
Tom Sepez4cb0fa72015-02-25 16:08:18 -080032 // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 virtual void UnsupportedHandler(int type) {}
Tom Sepez4cb0fa72015-02-25 16:08:18 -080034
35 // Equivalent to IPDF_JSPLATFORM::app_alert().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 virtual int Alert(FPDF_WIDESTRING message,
37 FPDF_WIDESTRING title,
38 int type,
39 int icon) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080040 return 0;
41 }
Tom Sepez6efc0ad2015-06-02 17:11:18 -070042
43 // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
44 virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
45
46 // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 virtual void KillTimer(int id) {}
Tom Sepez396e8722015-09-09 10:16:08 -070048
49 // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage().
50 virtual FPDF_PAGE GetPage(FPDF_FORMHANDLE form_handle,
51 FPDF_DOCUMENT document,
52 int page_index);
53
54 private:
55 std::map<int, FPDF_PAGE> m_pageMap;
Tom Sepez4cb0fa72015-02-25 16:08:18 -080056 };
57
58 EmbedderTest();
59 virtual ~EmbedderTest();
Tom Sepez96d13342015-01-16 14:59:26 -080060
61 void SetUp() override;
62 void TearDown() override;
63
Tom Sepeza72e8e22015-10-07 10:17:53 -070064 // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
65 void SetExternalIsolate(v8::Isolate* isolate) { external_isolate_ = isolate; }
66
Tom Sepez4cb0fa72015-02-25 16:08:18 -080067 void SetDelegate(Delegate* delegate) {
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 delegate_ = delegate ? delegate : default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080069 }
70
Tom Sepez96d13342015-01-16 14:59:26 -080071 FPDF_DOCUMENT document() { return document_; }
Tom Sepezda8189e2015-01-30 14:41:50 -080072 FPDF_FORMHANDLE form_handle() { return form_handle_; }
Tom Sepez96d13342015-01-16 14:59:26 -080073
Tom Sepezda8189e2015-01-30 14:41:50 -080074 // Open the document specified by |filename|, and create its form fill
75 // environment, or return false on failure.
Tom Sepez96d13342015-01-16 14:59:26 -080076 virtual bool OpenDocument(const std::string& filename);
77
Tom Sepez96d13342015-01-16 14:59:26 -080078 // Perform JavaScript actions that are to run at document open time.
Tom Sepezda8189e2015-01-30 14:41:50 -080079 virtual void DoOpenActions();
Tom Sepez96d13342015-01-16 14:59:26 -080080
81 // Determine the page numbers present in the document.
82 virtual int GetFirstPageNum();
83 virtual int GetPageCount();
84
85 // Load a specific page of the open document.
Tom Sepezda8189e2015-01-30 14:41:50 -080086 virtual FPDF_PAGE LoadPage(int page_number);
Tom Sepez96d13342015-01-16 14:59:26 -080087
Tom Sepez396e8722015-09-09 10:16:08 -070088 // Load a specific page of the open document using delegate_->GetPage.
89 // delegate_->GetPage also caches loaded page.
90 virtual FPDF_PAGE LoadAndCachePage(int page_number);
91
Tom Sepez96d13342015-01-16 14:59:26 -080092 // Convert a loaded page into a bitmap.
Tom Sepezda8189e2015-01-30 14:41:50 -080093 virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -080094
95 // Relese the resources obtained from LoadPage(). Further use of |page|
96 // is prohibited after this call is made.
Tom Sepezda8189e2015-01-30 14:41:50 -080097 virtual void UnloadPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -080098
Tom Sepez2255a1b2015-01-23 15:33:44 -080099 protected:
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800100 Delegate* delegate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700101 nonstd::unique_ptr<Delegate> default_delegate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800102 FPDF_DOCUMENT document_;
Tom Sepezda8189e2015-01-30 14:41:50 -0800103 FPDF_FORMHANDLE form_handle_;
Tom Sepez96d13342015-01-16 14:59:26 -0800104 FPDF_AVAIL avail_;
105 FX_DOWNLOADHINTS hints_;
106 FPDF_FILEACCESS file_access_;
107 FX_FILEAVAIL file_avail_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700108 v8::Platform* platform_;
Tom Sepez96d13342015-01-16 14:59:26 -0800109 v8::StartupData natives_;
110 v8::StartupData snapshot_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700111 v8::Isolate* external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800112 TestLoader* loader_;
113 size_t file_length_;
114 char* file_contents_;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800115
116 private:
117 static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
119 FPDF_WIDESTRING message,
120 FPDF_WIDESTRING title,
121 int type,
122 int icon);
123 static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
124 int msecs,
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700125 TimerCallback fn);
126 static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
Tom Sepez396e8722015-09-09 10:16:08 -0700127 static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
128 FPDF_DOCUMENT document,
129 int page_index);
Tom Sepez96d13342015-01-16 14:59:26 -0800130};
131
132#endif // TESTING_EMBEDDER_TEST_H_