blob: 2f9cc677c7a54831999a12091ac983034cb9eb99 [file] [log] [blame]
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -07001// Copyright 2017 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#include "testing/xfa_js_embedder_test.h"
6
7#include <string>
8
Haibo Huang49cc9302020-04-27 16:14:24 -07009#include "fpdfsdk/cpdfsdk_helpers.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070010#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
Haibo Huang49cc9302020-04-27 16:14:24 -070011#include "fxjs/xfa/cfxjse_engine.h"
12#include "fxjs/xfa/cfxjse_value.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070013#include "testing/gtest/include/gtest/gtest.h"
14#include "third_party/base/ptr_util.h"
Haibo Huang49cc9302020-04-27 16:14:24 -070015#include "xfa/fxfa/cxfa_ffapp.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070016
17XFAJSEmbedderTest::XFAJSEmbedderTest()
Haibo Huang49cc9302020-04-27 16:14:24 -070018 : array_buffer_allocator_(
19 pdfium::MakeUnique<CFX_V8ArrayBufferAllocator>()) {}
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070020
21XFAJSEmbedderTest::~XFAJSEmbedderTest() {}
22
23void XFAJSEmbedderTest::SetUp() {
24 v8::Isolate::CreateParams params;
25 params.array_buffer_allocator = array_buffer_allocator_.get();
26 isolate_ = v8::Isolate::New(params);
27 ASSERT_TRUE(isolate_);
28
29 EmbedderTest::SetExternalIsolate(isolate_);
30 EmbedderTest::SetUp();
Haibo Huang49cc9302020-04-27 16:14:24 -070031
32 CXFA_FFApp::SkipFontLoadForTesting(true);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070033}
34
35void XFAJSEmbedderTest::TearDown() {
Haibo Huang49cc9302020-04-27 16:14:24 -070036 CXFA_FFApp::SkipFontLoadForTesting(false);
37
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070038 value_.reset();
39 script_context_ = nullptr;
40
41 EmbedderTest::TearDown();
42
43 isolate_->Dispose();
44 isolate_ = nullptr;
45}
46
Haibo Huang49cc9302020-04-27 16:14:24 -070047CXFA_Document* XFAJSEmbedderTest::GetXFADocument() const {
48 auto* pDoc = CPDFDocumentFromFPDFDocument(document());
49 if (!pDoc)
50 return nullptr;
51
52 auto* pContext = static_cast<CPDFXFA_Context*>(pDoc->GetExtension());
53 if (!pContext)
54 return nullptr;
55
56 return pContext->GetXFADoc()->GetXFADoc();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070057}
58
Haibo Huang49cc9302020-04-27 16:14:24 -070059bool XFAJSEmbedderTest::OpenDocumentWithOptions(
60 const std::string& filename,
61 const char* password,
62 LinearizeOption linearize_option,
63 JavaScriptOption javascript_option) {
64 // JS required for XFA.
65 ASSERT(javascript_option == JavaScriptOption::kEnableJavaScript);
66 if (!EmbedderTest::OpenDocumentWithOptions(
67 filename, password, linearize_option, javascript_option)) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070068 return false;
Haibo Huang49cc9302020-04-27 16:14:24 -070069 }
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070070 script_context_ = GetXFADocument()->GetScriptContext();
71 return true;
72}
73
Haibo Huang49cc9302020-04-27 16:14:24 -070074bool XFAJSEmbedderTest::Execute(ByteStringView input) {
75 if (ExecuteHelper(input))
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070076 return true;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070077
78 CFXJSE_Value msg(GetIsolate());
79 value_->GetObjectPropertyByIdx(1, &msg);
Haibo Huang49cc9302020-04-27 16:14:24 -070080 fprintf(stderr, "FormCalc: %.*s\n", static_cast<int>(input.GetLength()),
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070081 input.unterminated_c_str());
82 // If the parsing of the input fails, then v8 will not run, so there will be
83 // no value here to print.
84 if (msg.IsString() && !msg.ToWideString().IsEmpty())
85 fprintf(stderr, "JS ERROR: %ls\n", msg.ToWideString().c_str());
86 return false;
87}
88
Haibo Huang49cc9302020-04-27 16:14:24 -070089bool XFAJSEmbedderTest::ExecuteSilenceFailure(ByteStringView input) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070090 return ExecuteHelper(input);
91}
92
Haibo Huang49cc9302020-04-27 16:14:24 -070093bool XFAJSEmbedderTest::ExecuteHelper(ByteStringView input) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070094 value_ = pdfium::MakeUnique<CFXJSE_Value>(GetIsolate());
95 return script_context_->RunScript(CXFA_Script::Type::Formcalc,
96 WideString::FromUTF8(input).AsStringView(),
97 value_.get(), GetXFADocument()->GetRoot());
98}