Dan Sinclair | 14aacd5 | 2017-05-18 14:11:29 -0400 | [diff] [blame] | 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/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 | |
| 15 | XFAJSEmbedderTest::XFAJSEmbedderTest() |
| 16 | : array_buffer_allocator_(pdfium::MakeUnique<FXJS_ArrayBufferAllocator>()) { |
| 17 | } |
| 18 | |
| 19 | XFAJSEmbedderTest::~XFAJSEmbedderTest() {} |
| 20 | |
| 21 | void 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 | |
| 31 | void XFAJSEmbedderTest::TearDown() { |
| 32 | value_ = nullptr; |
| 33 | script_context_ = nullptr; |
| 34 | |
| 35 | EmbedderTest::TearDown(); |
| 36 | |
| 37 | isolate_->Dispose(); |
| 38 | isolate_ = nullptr; |
| 39 | } |
| 40 | |
Dan Sinclair | 129b013 | 2017-05-24 09:33:20 -0400 | [diff] [blame] | 41 | CXFA_Document* XFAJSEmbedderTest::GetXFADocument() { |
| 42 | return UnderlyingFromFPDFDocument(document())->GetXFADoc()->GetXFADoc(); |
| 43 | } |
| 44 | |
Dan Sinclair | 14aacd5 | 2017-05-18 14:11:29 -0400 | [diff] [blame] | 45 | bool 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 Sinclair | 129b013 | 2017-05-24 09:33:20 -0400 | [diff] [blame] | 51 | script_context_ = GetXFADocument()->GetScriptContext(); |
Dan Sinclair | 14aacd5 | 2017-05-18 14:11:29 -0400 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool XFAJSEmbedderTest::Execute(const CFX_ByteStringC& input) { |
Ryan Harrison | 580c159 | 2017-06-29 10:43:53 -0400 | [diff] [blame] | 56 | if (ExecuteHelper(input)) { |
Dan Sinclair | 14aacd5 | 2017-05-18 14:11:29 -0400 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
| 60 | CFXJSE_Value msg(GetIsolate()); |
| 61 | value_->GetObjectPropertyByIdx(1, &msg); |
Ryan Harrison | 81f9eee | 2017-09-05 15:33:18 -0400 | [diff] [blame] | 62 | fprintf(stderr, "JS: %.*s\n", static_cast<int>(input.GetLength()), |
| 63 | input.unterminated_c_str()); |
Ryan Harrison | 580c159 | 2017-06-29 10:43:53 -0400 | [diff] [blame] | 64 | // 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 Sinclair | 14aacd5 | 2017-05-18 14:11:29 -0400 | [diff] [blame] | 68 | return false; |
| 69 | } |
Ryan Harrison | 580c159 | 2017-06-29 10:43:53 -0400 | [diff] [blame] | 70 | |
| 71 | bool XFAJSEmbedderTest::ExecuteSilenceFailure(const CFX_ByteStringC& input) { |
| 72 | return ExecuteHelper(input); |
| 73 | } |
| 74 | |
| 75 | bool 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 | } |