Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [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 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 9 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 10 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 11 | #include "fxjs/xfa/cfxjse_engine.h" |
| 12 | #include "fxjs/xfa/cfxjse_value.h" |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "third_party/base/ptr_util.h" |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 15 | #include "xfa/fxfa/cxfa_ffapp.h" |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 16 | |
| 17 | XFAJSEmbedderTest::XFAJSEmbedderTest() |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 18 | : array_buffer_allocator_( |
| 19 | pdfium::MakeUnique<CFX_V8ArrayBufferAllocator>()) {} |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 20 | |
| 21 | XFAJSEmbedderTest::~XFAJSEmbedderTest() {} |
| 22 | |
| 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(); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 31 | |
| 32 | CXFA_FFApp::SkipFontLoadForTesting(true); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void XFAJSEmbedderTest::TearDown() { |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 36 | CXFA_FFApp::SkipFontLoadForTesting(false); |
| 37 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 38 | value_.reset(); |
| 39 | script_context_ = nullptr; |
| 40 | |
| 41 | EmbedderTest::TearDown(); |
| 42 | |
| 43 | isolate_->Dispose(); |
| 44 | isolate_ = nullptr; |
| 45 | } |
| 46 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 47 | 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(); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 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)) { |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 68 | return false; |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 69 | } |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 70 | script_context_ = GetXFADocument()->GetScriptContext(); |
| 71 | return true; |
| 72 | } |
| 73 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 74 | bool XFAJSEmbedderTest::Execute(ByteStringView input) { |
| 75 | if (ExecuteHelper(input)) |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 76 | return true; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 77 | |
| 78 | CFXJSE_Value msg(GetIsolate()); |
| 79 | value_->GetObjectPropertyByIdx(1, &msg); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 80 | fprintf(stderr, "FormCalc: %.*s\n", static_cast<int>(input.GetLength()), |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 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 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 89 | bool XFAJSEmbedderTest::ExecuteSilenceFailure(ByteStringView input) { |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 90 | return ExecuteHelper(input); |
| 91 | } |
| 92 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame^] | 93 | bool XFAJSEmbedderTest::ExecuteHelper(ByteStringView input) { |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 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 | } |