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 | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 5 | #include "fxjs/cfxjs_engine.h" |
| 6 | |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
Dan Sinclair | 61046b9 | 2016-02-18 14:48:48 -0500 | [diff] [blame] | 8 | #include "testing/js_embedder_test.h" |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 9 | |
| 10 | namespace { |
| 11 | |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 12 | const double kExpected0 = 6.0; |
| 13 | const double kExpected1 = 7.0; |
| 14 | const double kExpected2 = 8.0; |
| 15 | |
| 16 | const wchar_t kScript0[] = L"fred = 6"; |
| 17 | const wchar_t kScript1[] = L"fred = 7"; |
| 18 | const wchar_t kScript2[] = L"fred = 8"; |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 19 | |
| 20 | } // namespace |
| 21 | |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 22 | using CFXJSEngineEmbedderTest = JSEmbedderTest; |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 23 | |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 24 | void CheckAssignmentInEngineContext(CFXJS_Engine* current_engine, |
| 25 | double expected) { |
| 26 | v8::Context::Scope context_scope(current_engine->GetV8Context()); |
| 27 | v8::Local<v8::Object> This = current_engine->GetThisObj(); |
| 28 | v8::Local<v8::Value> fred = current_engine->GetObjectProperty(This, L"fred"); |
| 29 | EXPECT_TRUE(fred->IsNumber()); |
| 30 | EXPECT_EQ(expected, current_engine->ToDouble(fred)); |
| 31 | } |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 32 | |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 33 | TEST_F(CFXJSEngineEmbedderTest, Getters) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 34 | v8::Isolate::Scope isolate_scope(isolate()); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 35 | v8::HandleScope handle_scope(isolate()); |
| 36 | v8::Context::Scope context_scope(GetV8Context()); |
| 37 | |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 38 | Optional<IJS_Runtime::JS_Error> err = engine()->Execute(WideString(kScript1)); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 39 | EXPECT_FALSE(err); |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 40 | CheckAssignmentInEngineContext(engine(), kExpected1); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 41 | } |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 42 | |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 43 | TEST_F(CFXJSEngineEmbedderTest, MultipleEngines) { |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 44 | v8::Isolate::Scope isolate_scope(isolate()); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 45 | v8::HandleScope handle_scope(isolate()); |
| 46 | |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 47 | CFXJS_Engine engine1(isolate()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 48 | engine1.InitializeEngine(); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 49 | |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 50 | CFXJS_Engine engine2(isolate()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 51 | engine2.InitializeEngine(); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 52 | |
| 53 | v8::Context::Scope context_scope(GetV8Context()); |
Ryan Harrison | 203339a | 2018-08-23 20:58:14 +0000 | [diff] [blame] | 54 | { |
| 55 | Optional<IJS_Runtime::JS_Error> err = |
| 56 | engine()->Execute(WideString(kScript0)); |
| 57 | EXPECT_FALSE(err); |
| 58 | CheckAssignmentInEngineContext(engine(), kExpected0); |
| 59 | } |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 60 | { |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 61 | // engine1 executing in engine1's context doesn't affect main. |
| 62 | v8::Context::Scope context_scope1(engine1.GetV8Context()); |
| 63 | Optional<IJS_Runtime::JS_Error> err = engine1.Execute(WideString(kScript1)); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 64 | EXPECT_FALSE(err); |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 65 | CheckAssignmentInEngineContext(engine(), kExpected0); |
| 66 | CheckAssignmentInEngineContext(&engine1, kExpected1); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 67 | } |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 68 | { |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 69 | // engine1 executing in engine2's context doesn't affect engine1. |
| 70 | v8::Context::Scope context_scope2(engine2.GetV8Context()); |
| 71 | Optional<IJS_Runtime::JS_Error> err = engine1.Execute(WideString(kScript2)); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 72 | EXPECT_FALSE(err); |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 73 | CheckAssignmentInEngineContext(engine(), kExpected0); |
| 74 | CheckAssignmentInEngineContext(&engine1, kExpected1); |
| 75 | CheckAssignmentInEngineContext(&engine2, kExpected2); |
Tom Sepez | 9967cc5 | 2016-03-24 11:45:37 -0700 | [diff] [blame] | 76 | } |
Tom Sepez | 33c7ade | 2018-02-01 02:11:44 +0000 | [diff] [blame] | 77 | engine1.ReleaseEngine(); |
| 78 | engine2.ReleaseEngine(); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 79 | } |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 80 | |
| 81 | TEST_F(CFXJSEngineEmbedderTest, JSCompileError) { |
| 82 | v8::Isolate::Scope isolate_scope(isolate()); |
| 83 | v8::HandleScope handle_scope(isolate()); |
| 84 | v8::Context::Scope context_scope(GetV8Context()); |
| 85 | |
| 86 | Optional<IJS_Runtime::JS_Error> err = |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 87 | engine()->Execute(L"functoon(x) { return x+1; }"); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 88 | EXPECT_TRUE(err); |
| 89 | EXPECT_EQ(L"SyntaxError: Unexpected token {", err->exception); |
| 90 | EXPECT_EQ(1, err->line); |
| 91 | EXPECT_EQ(12, err->column); |
| 92 | } |
| 93 | |
| 94 | TEST_F(CFXJSEngineEmbedderTest, JSRuntimeError) { |
| 95 | v8::Isolate::Scope isolate_scope(isolate()); |
| 96 | v8::HandleScope handle_scope(isolate()); |
| 97 | v8::Context::Scope context_scope(GetV8Context()); |
| 98 | |
| 99 | Optional<IJS_Runtime::JS_Error> err = |
Tom Sepez | 10569fc | 2018-06-07 00:42:46 +0000 | [diff] [blame] | 100 | engine()->Execute(L"let a = 3;\nundefined.colour"); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 101 | EXPECT_TRUE(err); |
| 102 | EXPECT_EQ(L"TypeError: Cannot read property 'colour' of undefined", |
| 103 | err->exception); |
| 104 | EXPECT_EQ(2, err->line); |
| 105 | EXPECT_EQ(10, err->column); |
| 106 | } |