blob: 4a29872eab7fde0fd93d512b465f33ef365f5f4f [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) {
56 value_ = pdfium::MakeUnique<CFXJSE_Value>(GetIsolate());
57 if (script_context_->RunScript(XFA_SCRIPTLANGTYPE_Formcalc,
58 CFX_WideString::FromUTF8(input).AsStringC(),
Dan Sinclair129b0132017-05-24 09:33:20 -040059 value_.get(), GetXFADocument()->GetRoot())) {
Dan Sinclair14aacd52017-05-18 14:11:29 -040060 return true;
61 }
62
63 CFXJSE_Value msg(GetIsolate());
64 value_->GetObjectPropertyByIdx(1, &msg);
65 EXPECT_TRUE(msg.IsString());
66
67 fprintf(stderr, "JS: %.*s\n", input.GetLength(), input.c_str());
68 fprintf(stderr, "JS ERROR: %ls\n", msg.ToWideString().c_str());
69 return false;
70}