| OLD | NEW |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "testing/js_embedder_test.h" | 6 #include "testing/js_embedder_test.h" |
| 7 | 7 |
| 8 namespace { | 8 namespace { |
| 9 | 9 |
| 10 const double kExpected0 = 6.0; | 10 const double kExpected0 = 6.0; |
| 11 const double kExpected1 = 7.0; | 11 const double kExpected1 = 7.0; |
| 12 const double kExpected2 = 8.0; | 12 const double kExpected2 = 8.0; |
| 13 | 13 |
| 14 const wchar_t kScript0[] = L"fred = 6"; | 14 const wchar_t kScript0[] = L"fred = 6"; |
| 15 const wchar_t kScript1[] = L"fred = 7"; | 15 const wchar_t kScript1[] = L"fred = 7"; |
| 16 const wchar_t kScript2[] = L"fred = 8"; | 16 const wchar_t kScript2[] = L"fred = 8"; |
| 17 | 17 |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 class FXJSV8EmbedderTest : public JSEmbedderTest { | 20 class FXJSV8EmbedderTest : public JSEmbedderTest { |
| 21 public: | 21 public: |
| 22 void ExecuteInCurrentContext(const CFX_WideString& script) { | 22 void ExecuteInCurrentContext(const CFX_WideString& script) { |
| 23 FXJSErr error; | 23 FXJSErr error; |
| 24 int sts = FXJS_Execute(isolate(), script, &error); | 24 int sts = engine()->Execute(script, &error); |
| 25 EXPECT_EQ(0, sts); | 25 EXPECT_EQ(0, sts); |
| 26 } | 26 } |
| 27 void CheckAssignmentInCurrentContext(double expected) { | 27 void CheckAssignmentInCurrentContext(double expected) { |
| 28 v8::Local<v8::Object> This = FXJS_GetThisObj(isolate()); | 28 v8::Local<v8::Object> This = engine()->GetThisObj(); |
| 29 v8::Local<v8::Value> fred = | 29 v8::Local<v8::Value> fred = engine()->GetObjectProperty(This, L"fred"); |
| 30 FXJS_GetObjectProperty(isolate(), This, L"fred"); | |
| 31 EXPECT_TRUE(fred->IsNumber()); | 30 EXPECT_TRUE(fred->IsNumber()); |
| 32 EXPECT_EQ(expected, fred->ToNumber(isolate()->GetCurrentContext()) | 31 EXPECT_EQ(expected, engine()->ToNumber(fred)); |
| 33 .ToLocalChecked() | |
| 34 ->Value()); | |
| 35 } | 32 } |
| 36 }; | 33 }; |
| 37 | 34 |
| 38 TEST_F(FXJSV8EmbedderTest, Getters) { | 35 TEST_F(FXJSV8EmbedderTest, Getters) { |
| 39 v8::Isolate::Scope isolate_scope(isolate()); | 36 v8::Isolate::Scope isolate_scope(isolate()); |
| 40 v8::HandleScope handle_scope(isolate()); | 37 v8::HandleScope handle_scope(isolate()); |
| 41 v8::Context::Scope context_scope(GetV8Context()); | 38 v8::Context::Scope context_scope(GetV8Context()); |
| 42 | 39 |
| 43 ExecuteInCurrentContext(CFX_WideString(kScript1)); | 40 ExecuteInCurrentContext(CFX_WideString(kScript1)); |
| 44 CheckAssignmentInCurrentContext(kExpected1); | 41 CheckAssignmentInCurrentContext(kExpected1); |
| 45 } | 42 } |
| 46 | 43 |
| 47 TEST_F(FXJSV8EmbedderTest, MultipleEngines) { | 44 TEST_F(FXJSV8EmbedderTest, MultipleEngines) { |
| 48 v8::Isolate::Scope isolate_scope(isolate()); | 45 v8::Isolate::Scope isolate_scope(isolate()); |
| 49 v8::HandleScope handle_scope(isolate()); | 46 v8::HandleScope handle_scope(isolate()); |
| 50 | 47 |
| 51 v8::Global<v8::Context> global_context1; | 48 CFXJS_Engine engine1; |
| 52 std::vector<v8::Global<v8::Object>*> static_objects1; | 49 engine1.SetIsolate(isolate()); |
| 53 FXJS_InitializeEngine(isolate(), nullptr, &global_context1, &static_objects1); | 50 engine1.InitializeEngine(); |
| 54 | 51 |
| 55 v8::Global<v8::Context> global_context2; | 52 CFXJS_Engine engine2; |
| 56 std::vector<v8::Global<v8::Object>*> static_objects2; | 53 engine2.SetIsolate(isolate()); |
| 57 FXJS_InitializeEngine(isolate(), nullptr, &global_context2, &static_objects2); | 54 engine2.InitializeEngine(); |
| 58 | 55 |
| 59 v8::Context::Scope context_scope(GetV8Context()); | 56 v8::Context::Scope context_scope(GetV8Context()); |
| 60 ExecuteInCurrentContext(CFX_WideString(kScript0)); | 57 ExecuteInCurrentContext(CFX_WideString(kScript0)); |
| 61 CheckAssignmentInCurrentContext(kExpected0); | 58 CheckAssignmentInCurrentContext(kExpected0); |
| 62 | 59 |
| 63 { | 60 { |
| 64 v8::Local<v8::Context> context1 = | 61 v8::Local<v8::Context> context1 = engine1.NewLocalContext(); |
| 65 v8::Local<v8::Context>::New(isolate(), global_context1); | |
| 66 v8::Context::Scope context_scope1(context1); | 62 v8::Context::Scope context_scope1(context1); |
| 67 ExecuteInCurrentContext(CFX_WideString(kScript1)); | 63 ExecuteInCurrentContext(CFX_WideString(kScript1)); |
| 68 CheckAssignmentInCurrentContext(kExpected1); | 64 CheckAssignmentInCurrentContext(kExpected1); |
| 69 } | 65 } |
| 70 FXJS_ReleaseEngine(isolate(), &global_context1, &static_objects1); | 66 |
| 67 engine1.ReleaseEngine(); |
| 71 | 68 |
| 72 { | 69 { |
| 73 v8::Local<v8::Context> context2 = | 70 v8::Local<v8::Context> context2 = engine2.NewLocalContext(); |
| 74 v8::Local<v8::Context>::New(isolate(), global_context2); | |
| 75 v8::Context::Scope context_scope2(context2); | 71 v8::Context::Scope context_scope2(context2); |
| 76 ExecuteInCurrentContext(CFX_WideString(kScript2)); | 72 ExecuteInCurrentContext(CFX_WideString(kScript2)); |
| 77 CheckAssignmentInCurrentContext(kExpected2); | 73 CheckAssignmentInCurrentContext(kExpected2); |
| 78 } | 74 } |
| 79 FXJS_ReleaseEngine(isolate(), &global_context2, &static_objects2); | |
| 80 | 75 |
| 76 engine2.ReleaseEngine(); |
| 81 CheckAssignmentInCurrentContext(kExpected0); | 77 CheckAssignmentInCurrentContext(kExpected0); |
| 82 } | 78 } |
| OLD | NEW |