1 // 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 
9 #include "fpdfsdk/cpdfsdk_helpers.h"
10 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
11 #include "fxjs/xfa/cfxjse_engine.h"
12 #include "fxjs/xfa/cfxjse_value.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/base/ptr_util.h"
15 #include "xfa/fxfa/cxfa_ffapp.h"
16 
XFAJSEmbedderTest()17 XFAJSEmbedderTest::XFAJSEmbedderTest()
18     : array_buffer_allocator_(
19           pdfium::MakeUnique<CFX_V8ArrayBufferAllocator>()) {}
20 
~XFAJSEmbedderTest()21 XFAJSEmbedderTest::~XFAJSEmbedderTest() {}
22 
SetUp()23 void 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();
31 
32   CXFA_FFApp::SkipFontLoadForTesting(true);
33 }
34 
TearDown()35 void XFAJSEmbedderTest::TearDown() {
36   CXFA_FFApp::SkipFontLoadForTesting(false);
37 
38   value_.reset();
39   script_context_ = nullptr;
40 
41   EmbedderTest::TearDown();
42 
43   isolate_->Dispose();
44   isolate_ = nullptr;
45 }
46 
GetXFADocument() const47 CXFA_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();
57 }
58 
OpenDocumentWithOptions(const std::string & filename,const char * password,LinearizeOption linearize_option,JavaScriptOption javascript_option)59 bool 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)) {
68     return false;
69   }
70   script_context_ = GetXFADocument()->GetScriptContext();
71   return true;
72 }
73 
Execute(ByteStringView input)74 bool XFAJSEmbedderTest::Execute(ByteStringView input) {
75   if (ExecuteHelper(input))
76     return true;
77 
78   CFXJSE_Value msg(GetIsolate());
79   value_->GetObjectPropertyByIdx(1, &msg);
80   fprintf(stderr, "FormCalc: %.*s\n", static_cast<int>(input.GetLength()),
81           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 
ExecuteSilenceFailure(ByteStringView input)89 bool XFAJSEmbedderTest::ExecuteSilenceFailure(ByteStringView input) {
90   return ExecuteHelper(input);
91 }
92 
ExecuteHelper(ByteStringView input)93 bool XFAJSEmbedderTest::ExecuteHelper(ByteStringView input) {
94   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 }
99