1 /*
2 * Copyright (c) 2021-2022 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 "quickjs_native_engine.h"
19
20 static NativeEngine* g_nativeEngine = nullptr;
21 static NativeEngine* g_moduleNameNativeEngine = nullptr;
22
NativeEngineTest()23 NativeEngineTest::NativeEngineTest()
24 {
25 engine_ = g_nativeEngine;
26 moduleNameEngine_ = g_moduleNameNativeEngine;
27 }
28
~NativeEngineTest()29 NativeEngineTest::~NativeEngineTest()
30 {
31 printf("NativeEngineTest::~NativeEngineTest \n");
32 engine_->RunCleanup();
33 }
34
main(int argc,char ** argv)35 int main(int argc, char** argv)
36 {
37 testing::GTEST_FLAG(output) = "xml:./";
38 testing::InitGoogleTest(&argc, argv);
39
40 JSRuntime* rt = JS_NewRuntime();
41 if (rt == nullptr) {
42 return 0;
43 }
44
45 JSContext* ctx = JS_NewContext(rt);
46 if (ctx == nullptr) {
47 return 0;
48 }
49
50 js_std_add_helpers(ctx, 0, nullptr);
51 g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); // default instance id 0
52
53 g_moduleNameNativeEngine = new QuickJSNativeEngine(g_nativeEngine->GetNativeEngineImpl(), 0, false);
54
55 NativeModuleManager* moduleManager = g_nativeEngine->GetModuleManager();
56 const char* moduleName1 = "ability.featureAbility";
57 const char* moduleName2 = "window";
58 NativeModule* module1 = moduleManager->LoadNativeModule(moduleName1, nullptr, false);
59 if (module1 != nullptr) {
60 HILOG_INFO("moduleManager->LoadNativeModule featureability succcess");
61 std::string strModuleName1(moduleName1);
62 moduleManager->SetNativeEngine(strModuleName1, g_nativeEngine);
63 g_nativeEngine->SetModuleFileName(strModuleName1);
64 }
65 NativeModule* module2 = moduleManager->LoadNativeModule(moduleName2, nullptr, false);
66 if (module2 != nullptr) {
67 HILOG_INFO("moduleManager->LoadNativeModule window succcess");
68 std::string strModuleName2(moduleName2);
69 moduleManager->SetNativeEngine(strModuleName2, g_moduleNameNativeEngine);
70 g_moduleNameNativeEngine->SetModuleFileName(strModuleName2);
71 }
72
73 int ret = RUN_ALL_TESTS();
74
75 g_nativeEngine->DeleteEngine();
76 delete g_nativeEngine;
77 g_nativeEngine = nullptr;
78 delete g_moduleNameNativeEngine;
79 g_moduleNameNativeEngine = nullptr;
80
81 js_std_free_handlers(rt);
82 JS_FreeContext(ctx);
83 JS_FreeRuntime(rt);
84
85 return ret;
86 }
87