Clean up CPDF_AnnotList.
[pdfium.git] / fpdfsdk / src / jsapi / fxjs_v8_embeddertest.cpp
1 // Copyright 2015 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 "../../../third_party/base/nonstd_unique_ptr.h"
6 #include "../../../core/include/fpdfapi/fpdf_parser.h"
7 #include "../../../testing/embedder_test.h"
8 #include "../../include/jsapi/fxjs_v8.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 const wchar_t kScript[] = L"fred = 7";
14
15 }  // namespace
16
17 class FXJSV8Embeddertest : public EmbedderTest {
18  public:
19   FXJSV8Embeddertest()
20       : m_pArrayBufferAllocator(new FXJS_ArrayBufferAllocator) {
21     v8::Isolate::CreateParams params;
22     params.array_buffer_allocator = m_pArrayBufferAllocator.get();
23     m_pIsolate = v8::Isolate::New(params);
24   }
25
26   ~FXJSV8Embeddertest() override { m_pIsolate->Dispose(); }
27
28   void SetUp() override {
29     EmbedderTest::SetExternalIsolate(m_pIsolate);
30     EmbedderTest::SetUp();
31
32     v8::Isolate::Scope isolate_scope(m_pIsolate);
33     v8::HandleScope handle_scope(m_pIsolate);
34     FXJS_PerIsolateData::SetUp(m_pIsolate);
35     FXJS_InitializeRuntime(m_pIsolate, nullptr, m_pPersistentContext);
36   }
37
38   void TearDown() override {
39     FXJS_ReleaseRuntime(m_pIsolate, m_pPersistentContext);
40     m_pPersistentContext.Reset();
41     FXJS_Release();
42     EmbedderTest::TearDown();
43   }
44
45   v8::Isolate* isolate() { return m_pIsolate; }
46   v8::Local<v8::Context> GetV8Context() {
47     return m_pPersistentContext.Get(m_pIsolate);
48   }
49
50  private:
51   nonstd::unique_ptr<FXJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
52   v8::Isolate* m_pIsolate;
53   v8::Global<v8::Context> m_pPersistentContext;
54 };
55
56 TEST_F(FXJSV8Embeddertest, Getters) {
57   v8::Isolate::Scope isolate_scope(isolate());
58   v8::HandleScope handle_scope(isolate());
59   v8::Context::Scope context_scope(GetV8Context());
60
61   FXJSErr error;
62   CFX_WideString wsInfo;
63   CFX_WideString wsScript(kScript);
64   int sts = FXJS_Execute(isolate(), nullptr, kScript, &error);
65   EXPECT_EQ(0, sts);
66
67   v8::Local<v8::Object> This = FXJS_GetThisObj(isolate());
68   v8::Local<v8::Value> fred = FXJS_GetObjectElement(isolate(), This, L"fred");
69   EXPECT_TRUE(fred->IsNumber());
70 }