1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "test.h"
17
18 #include "utils/log.h"
19 #include "v8_native_engine.h"
20
21 static NativeEngine* g_nativeEngine = nullptr;
22
NativeEngineTest()23 NativeEngineTest::NativeEngineTest()
24 {
25 engine_ = g_nativeEngine;
26 }
27
~NativeEngineTest()28 NativeEngineTest::~NativeEngineTest() {}
29
30 extern const char _binary_strip_native_min_js_bin_start[];
31 extern const char _binary_strip_native_min_js_bin_end[];
32
main(int argc,char ** argv)33 int main(int argc, char** argv)
34 {
35 int retCode = 0;
36
37 testing::GTEST_FLAG(output) = "xml:./";
38 testing::InitGoogleTest(&argc, argv);
39
40 static v8::StartupData snapshotBlob = {
41 .data = _binary_strip_native_min_js_bin_start,
42 .raw_size = _binary_strip_native_min_js_bin_end - _binary_strip_native_min_js_bin_start,
43 };
44 v8::V8::SetSnapshotDataBlob(&snapshotBlob);
45
46 std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
47 v8::V8::InitializePlatform(platform.get());
48 if (!v8::V8::Initialize()) {
49 return retCode;
50 }
51 v8::Isolate::CreateParams createParams;
52 createParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
53
54 if (createParams.array_buffer_allocator == nullptr) {
55 return retCode;
56 }
57
58 v8::Isolate* isolate = v8::Isolate::New(createParams);
59 {
60 v8::Isolate::Scope isolateScope(isolate);
61 v8::HandleScope handleScope(isolate);
62 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
63 v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, global);
64
65 v8::Persistent<v8::Context> persistContext;
66 persistContext.Reset(isolate, context);
67
68 if (context.IsEmpty()) {
69 return retCode;
70 }
71
72 v8::Context::Scope contextScope(context);
73 {
74 g_nativeEngine = new V8NativeEngine(platform.get(), isolate, persistContext, 0); // default instance id 0
75
76 retCode = testing::UnitTest::GetInstance()->Run();
77
78 g_nativeEngine->Loop(LOOP_DEFAULT);
79 delete g_nativeEngine;
80 g_nativeEngine = nullptr;
81 }
82 }
83 isolate->Dispose();
84 v8::V8::Dispose();
85 v8::V8::ShutdownPlatform();
86 delete createParams.array_buffer_allocator;
87
88 return retCode;
89 }
90