Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 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 | |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 5 | #include "testing/gtest/include/gtest/gtest.h" |
Dan Sinclair | 61046b9 | 2016-02-18 14:48:48 -0500 | [diff] [blame] | 6 | #include "testing/js_embedder_test.h" |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 7 | |
| 8 | namespace { |
| 9 | |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 10 | const double kExpected0 = 6.0; |
| 11 | const double kExpected1 = 7.0; |
| 12 | const double kExpected2 = 8.0; |
| 13 | |
| 14 | const wchar_t kScript0[] = L"fred = 6"; |
| 15 | const wchar_t kScript1[] = L"fred = 7"; |
| 16 | const wchar_t kScript2[] = L"fred = 8"; |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 17 | |
| 18 | } // namespace |
| 19 | |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 20 | class FXJSV8EmbedderTest : public JSEmbedderTest { |
| 21 | public: |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 22 | void ExecuteInCurrentContext(const CFX_WideString& script) { |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 23 | FXJSErr error; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 24 | int sts = engine()->Execute(script, &error); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 25 | EXPECT_EQ(0, sts); |
| 26 | } |
| 27 | void CheckAssignmentInCurrentContext(double expected) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 28 | v8::Local<v8::Object> This = engine()->GetThisObj(); |
| 29 | v8::Local<v8::Value> fred = engine()->GetObjectProperty(This, L"fred"); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 30 | EXPECT_TRUE(fred->IsNumber()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 31 | EXPECT_EQ(expected, engine()->ToNumber(fred)); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 32 | } |
| 33 | }; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 34 | |
Lei Zhang | 1ac47eb | 2015-12-21 11:04:44 -0800 | [diff] [blame] | 35 | TEST_F(FXJSV8EmbedderTest, Getters) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 36 | v8::Isolate::Scope isolate_scope(isolate()); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 37 | v8::HandleScope handle_scope(isolate()); |
| 38 | v8::Context::Scope context_scope(GetV8Context()); |
| 39 | |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 40 | ExecuteInCurrentContext(CFX_WideString(kScript1)); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 41 | CheckAssignmentInCurrentContext(kExpected1); |
| 42 | } |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 43 | |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 44 | TEST_F(FXJSV8EmbedderTest, MultipleEngines) { |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 45 | v8::Isolate::Scope isolate_scope(isolate()); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 46 | v8::HandleScope handle_scope(isolate()); |
| 47 | |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 48 | CFXJS_Engine engine1(isolate()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 49 | engine1.InitializeEngine(); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 50 | |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 51 | CFXJS_Engine engine2(isolate()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 52 | engine2.InitializeEngine(); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 53 | |
| 54 | v8::Context::Scope context_scope(GetV8Context()); |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 55 | ExecuteInCurrentContext(CFX_WideString(kScript0)); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 56 | CheckAssignmentInCurrentContext(kExpected0); |
| 57 | |
| 58 | { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 59 | v8::Local<v8::Context> context1 = engine1.NewLocalContext(); |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 60 | v8::Context::Scope context_scope1(context1); |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 61 | ExecuteInCurrentContext(CFX_WideString(kScript1)); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 62 | CheckAssignmentInCurrentContext(kExpected1); |
| 63 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 64 | |
| 65 | engine1.ReleaseEngine(); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 66 | |
| 67 | { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 68 | v8::Local<v8::Context> context2 = engine2.NewLocalContext(); |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 69 | v8::Context::Scope context_scope2(context2); |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 70 | ExecuteInCurrentContext(CFX_WideString(kScript2)); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 71 | CheckAssignmentInCurrentContext(kExpected2); |
| 72 | } |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 73 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 74 | engine2.ReleaseEngine(); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 75 | CheckAssignmentInCurrentContext(kExpected0); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 76 | } |