blob: d72e8762e8e4b15e4e272572249fff5cac5a039c [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
41bool XFAJSEmbedderTest::OpenDocument(const std::string& filename,
42 const char* password,
43 bool must_linearize) {
44 if (!EmbedderTest::OpenDocument(filename, password, must_linearize))
45 return false;
46
47 script_context_ = UnderlyingFromFPDFDocument(document())
48 ->GetXFADoc()
49 ->GetXFADoc()
50 ->GetScriptContext();
51 return true;
52}
53
54bool XFAJSEmbedderTest::Execute(const CFX_ByteStringC& input) {
55 value_ = pdfium::MakeUnique<CFXJSE_Value>(GetIsolate());
56 if (script_context_->RunScript(XFA_SCRIPTLANGTYPE_Formcalc,
57 CFX_WideString::FromUTF8(input).AsStringC(),
58 value_.get(), nullptr)) {
59 return true;
60 }
61
62 CFXJSE_Value msg(GetIsolate());
63 value_->GetObjectPropertyByIdx(1, &msg);
64 EXPECT_TRUE(msg.IsString());
65
66 fprintf(stderr, "JS: %.*s\n", input.GetLength(), input.c_str());
67 fprintf(stderr, "JS ERROR: %ls\n", msg.ToWideString().c_str());
68 return false;
69}