blob: a11806ffdae5cb56aa28a49983b648706cd3adee [file] [log] [blame]
Dan Sinclair14aacd52017-05-18 14:11:29 -04001// 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/fpdfxfa/cpdfxfa_context.h"
10#include "fpdfsdk/fsdk_define.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "third_party/base/ptr_util.h"
13#include "xfa/fxfa/parser/cxfa_scriptcontext.h"
14
15XFAJSEmbedderTest::XFAJSEmbedderTest()
16 : array_buffer_allocator_(pdfium::MakeUnique<FXJS_ArrayBufferAllocator>()) {
17}
18
19XFAJSEmbedderTest::~XFAJSEmbedderTest() {}
20
21void XFAJSEmbedderTest::SetUp() {
22 v8::Isolate::CreateParams params;
23 params.array_buffer_allocator = array_buffer_allocator_.get();
24 isolate_ = v8::Isolate::New(params);
25 ASSERT_TRUE(isolate_ != nullptr);
26
27 EmbedderTest::SetExternalIsolate(isolate_);
28 EmbedderTest::SetUp();
29}
30
31void XFAJSEmbedderTest::TearDown() {
32 value_ = nullptr;
33 script_context_ = nullptr;
34
35 EmbedderTest::TearDown();
36
37 isolate_->Dispose();
38 isolate_ = nullptr;
39}
40
Dan Sinclair129b0132017-05-24 09:33:20 -040041CXFA_Document* XFAJSEmbedderTest::GetXFADocument() {
42 return UnderlyingFromFPDFDocument(document())->GetXFADoc()->GetXFADoc();
43}
44
Dan Sinclair14aacd52017-05-18 14:11:29 -040045bool XFAJSEmbedderTest::OpenDocument(const std::string& filename,
46 const char* password,
47 bool must_linearize) {
48 if (!EmbedderTest::OpenDocument(filename, password, must_linearize))
49 return false;
50
Dan Sinclair129b0132017-05-24 09:33:20 -040051 script_context_ = GetXFADocument()->GetScriptContext();
Dan Sinclair14aacd52017-05-18 14:11:29 -040052 return true;
53}
54
55bool XFAJSEmbedderTest::Execute(const CFX_ByteStringC& input) {
Ryan Harrison580c1592017-06-29 10:43:53 -040056 if (ExecuteHelper(input)) {
Dan Sinclair14aacd52017-05-18 14:11:29 -040057 return true;
58 }
59
60 CFXJSE_Value msg(GetIsolate());
61 value_->GetObjectPropertyByIdx(1, &msg);
Ryan Harrison81f9eee2017-09-05 15:33:18 -040062 fprintf(stderr, "JS: %.*s\n", static_cast<int>(input.GetLength()),
63 input.unterminated_c_str());
Ryan Harrison580c1592017-06-29 10:43:53 -040064 // If the parsing of the input fails, then v8 will not run, so there will be
65 // no value here to print.
66 if (msg.IsString() && !msg.ToWideString().IsEmpty())
67 fprintf(stderr, "JS ERROR: %ls\n", msg.ToWideString().c_str());
Dan Sinclair14aacd52017-05-18 14:11:29 -040068 return false;
69}
Ryan Harrison580c1592017-06-29 10:43:53 -040070
71bool XFAJSEmbedderTest::ExecuteSilenceFailure(const CFX_ByteStringC& input) {
72 return ExecuteHelper(input);
73}
74
75bool XFAJSEmbedderTest::ExecuteHelper(const CFX_ByteStringC& input) {
76 value_ = pdfium::MakeUnique<CFXJSE_Value>(GetIsolate());
77 return script_context_->RunScript(XFA_SCRIPTLANGTYPE_Formcalc,
78 CFX_WideString::FromUTF8(input).AsStringC(),
79 value_.get(), GetXFADocument()->GetRoot());
80}