blob: a877a36e56ba8c40af7e9b406da17d53c0e27dd7 [file] [log] [blame]
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07001// 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
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07005#include "testing/gtest/include/gtest/gtest.h"
Dan Sinclair61046b92016-02-18 14:48:48 -05006#include "testing/js_embedder_test.h"
Tom Sepez7d0fcbf2015-09-15 15:30:34 -07007
8namespace {
9
Tom Sepez9967cc52016-03-24 11:45:37 -070010const double kExpected0 = 6.0;
11const double kExpected1 = 7.0;
12const double kExpected2 = 8.0;
13
14const wchar_t kScript0[] = L"fred = 6";
15const wchar_t kScript1[] = L"fred = 7";
16const wchar_t kScript2[] = L"fred = 8";
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070017
18} // namespace
19
Tom Sepez9967cc52016-03-24 11:45:37 -070020class FXJSV8EmbedderTest : public JSEmbedderTest {
21 public:
dsinclair884b4f92016-06-06 09:49:32 -070022 void ExecuteInCurrentContext(const CFX_WideString& script) {
Tom Sepez9967cc52016-03-24 11:45:37 -070023 FXJSErr error;
tsepezb4694242016-08-15 16:44:55 -070024 int sts = engine()->Execute(script, &error);
Tom Sepez9967cc52016-03-24 11:45:37 -070025 EXPECT_EQ(0, sts);
26 }
27 void CheckAssignmentInCurrentContext(double expected) {
tsepezb4694242016-08-15 16:44:55 -070028 v8::Local<v8::Object> This = engine()->GetThisObj();
29 v8::Local<v8::Value> fred = engine()->GetObjectProperty(This, L"fred");
Tom Sepez9967cc52016-03-24 11:45:37 -070030 EXPECT_TRUE(fred->IsNumber());
tsepezb4694242016-08-15 16:44:55 -070031 EXPECT_EQ(expected, engine()->ToNumber(fred));
Tom Sepez9967cc52016-03-24 11:45:37 -070032 }
33};
Tom Sepeza72e8e22015-10-07 10:17:53 -070034
Lei Zhang1ac47eb2015-12-21 11:04:44 -080035TEST_F(FXJSV8EmbedderTest, Getters) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070036 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070037 v8::HandleScope handle_scope(isolate());
38 v8::Context::Scope context_scope(GetV8Context());
39
dsinclair884b4f92016-06-06 09:49:32 -070040 ExecuteInCurrentContext(CFX_WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070041 CheckAssignmentInCurrentContext(kExpected1);
42}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070043
tsepeza4941912016-08-15 11:40:12 -070044TEST_F(FXJSV8EmbedderTest, MultipleEngines) {
Tom Sepez9967cc52016-03-24 11:45:37 -070045 v8::Isolate::Scope isolate_scope(isolate());
Tom Sepez9967cc52016-03-24 11:45:37 -070046 v8::HandleScope handle_scope(isolate());
47
weili0b2a9872016-09-21 11:50:43 -070048 CFXJS_Engine engine1(isolate());
tsepezb4694242016-08-15 16:44:55 -070049 engine1.InitializeEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070050
weili0b2a9872016-09-21 11:50:43 -070051 CFXJS_Engine engine2(isolate());
tsepezb4694242016-08-15 16:44:55 -070052 engine2.InitializeEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070053
54 v8::Context::Scope context_scope(GetV8Context());
dsinclair884b4f92016-06-06 09:49:32 -070055 ExecuteInCurrentContext(CFX_WideString(kScript0));
Tom Sepez9967cc52016-03-24 11:45:37 -070056 CheckAssignmentInCurrentContext(kExpected0);
57
58 {
tsepezb4694242016-08-15 16:44:55 -070059 v8::Local<v8::Context> context1 = engine1.NewLocalContext();
weilidb444d22016-06-02 15:48:15 -070060 v8::Context::Scope context_scope1(context1);
dsinclair884b4f92016-06-06 09:49:32 -070061 ExecuteInCurrentContext(CFX_WideString(kScript1));
Tom Sepez9967cc52016-03-24 11:45:37 -070062 CheckAssignmentInCurrentContext(kExpected1);
63 }
tsepezb4694242016-08-15 16:44:55 -070064
65 engine1.ReleaseEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070066
67 {
tsepezb4694242016-08-15 16:44:55 -070068 v8::Local<v8::Context> context2 = engine2.NewLocalContext();
weilidb444d22016-06-02 15:48:15 -070069 v8::Context::Scope context_scope2(context2);
dsinclair884b4f92016-06-06 09:49:32 -070070 ExecuteInCurrentContext(CFX_WideString(kScript2));
Tom Sepez9967cc52016-03-24 11:45:37 -070071 CheckAssignmentInCurrentContext(kExpected2);
72 }
Tom Sepez9967cc52016-03-24 11:45:37 -070073
tsepezb4694242016-08-15 16:44:55 -070074 engine2.ReleaseEngine();
Tom Sepez9967cc52016-03-24 11:45:37 -070075 CheckAssignmentInCurrentContext(kExpected0);
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070076}