1 /*
2 * Copyright (c) 2024 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 <gtest/gtest.h>
17
18 #include "hilog_tag_wrapper.h"
19 #include "js_runtime.h"
20 #include "js_environment.h"
21 #include "js_runtime_lite.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace AbilityRuntime {
28
29 class JsRuntimeLiteTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 };
36
SetUpTestCase()37 void JsRuntimeLiteTest::SetUpTestCase() {}
38
TearDownTestCase()39 void JsRuntimeLiteTest::TearDownTestCase() {}
40
SetUp()41 void JsRuntimeLiteTest::SetUp() {}
42
TearDown()43 void JsRuntimeLiteTest::TearDown() {}
44
45 /**
46 * @tc.name: JsRuntimeLiteTest_001
47 * @tc.desc: basic function test.
48 * @tc.type: FUNC
49 */
50 HWTEST_F(JsRuntimeLiteTest, JsRuntimeLiteTest_001, TestSize.Level1)
51 {
52 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_001 start");
53 Options options;
54 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
55 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
56 EXPECT_EQ(err, napi_status::napi_ok);
57
58 err = JsRuntimeLite::GetInstance().RemoveJsEnv(reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()));
59 EXPECT_EQ(err, napi_status::napi_ok);
60 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_001 end");
61 }
62
63 /**
64 * @tc.name: JsRuntimeLiteTest_002
65 * @tc.desc: basic function test.
66 * @tc.type: FUNC
67 */
68 HWTEST_F(JsRuntimeLiteTest, JsRuntimeLiteTest_002, TestSize.Level1)
69 {
70 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_002 start");
71 Options options;
72 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
73 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
74 EXPECT_EQ(err, napi_status::napi_ok);
75
76 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv2 = nullptr;
77 err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv2);
78 EXPECT_EQ(err, napi_status::napi_create_ark_runtime_only_one_env_per_thread);
79
80 err = JsRuntimeLite::GetInstance().RemoveJsEnv(reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()));
81 EXPECT_EQ(err, napi_status::napi_ok);
82
83 err = JsRuntimeLite::GetInstance().RemoveJsEnv(reinterpret_cast<napi_env>(jsEnv2->GetNativeEngine()));
84 EXPECT_EQ(err, napi_status::napi_destroy_ark_runtime_env_not_exist);
85 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_002 end");
86 }
87
88 /**
89 * @tc.name: JsRuntimeLiteTest_003
90 * @tc.desc: basic function test.
91 * @tc.type: FUNC
92 */
93 HWTEST_F(JsRuntimeLiteTest, JsRuntimeLiteTest_003, TestSize.Level1)
94 {
95 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_003 start");
96 Options options;
97 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
98 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
99 EXPECT_EQ(err, napi_status::napi_ok);
100
101 napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine());
102 err = JsRuntimeLite::GetInstance().Init(options, env);
103 EXPECT_EQ(err, napi_status::napi_ok);
104
105 err = JsRuntimeLite::GetInstance().RemoveJsEnv(env);
106 EXPECT_EQ(err, napi_status::napi_ok);
107 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_003 end");
108 }
109
110 /**
111 * @tc.name: GetChildOptions_0100
112 * @tc.desc: JsRuntime test for GetChildOptions.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(JsRuntimeLiteTest, GetChildOptions_0100, TestSize.Level1)
116 {
117 auto child = JsRuntimeLite::GetInstance().GetChildOptions();
118 EXPECT_TRUE(child == nullptr);
119 }
120
121 /**
122 * @tc.name: GetPkgContextInfoListMap_0100
123 * @tc.desc: JsRuntimeLiteTest test for GetPkgContextInfoListMap.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(JsRuntimeLiteTest, GetPkgContextInfoListMap_0100, TestSize.Level0)
127 {
128 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0100 start");
129
130 std::map<std::string, std::string> modulePkgContentMap;
131 std::string pkgContentJsonString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
132 "library","version":"1.0.0","entryPath":"","isSO":false}})";
133 modulePkgContentMap["entry"] = pkgContentJsonString;
134
135 AbilityRuntime::Runtime::Options options;
136 options.preload = true;
137 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
138 std::map<std::string, std::vector<std::vector<std::string>>> ret;
139 std::map<std::string, std::string> pkgAliasMap;
140 JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(modulePkgContentMap, ret, pkgAliasMap);
141 std::string expectString = "library:packageName:library:bundleName:";
142 expectString += "com.xxx.xxxx:moduleName:library:version:1.0.0:entryPath::isSO:false:";
143 auto it = ret.find("entry");
144 ASSERT_EQ(it, ret.end());
145 std::string pkgRetString;
146 for (const auto& vec : it->second) {
147 for (const auto& str : vec) {
148 pkgRetString += str + ":";
149 }
150 }
151 ASSERT_EQ(pkgRetString, "");
152 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0100 end");
153 }
154
155 /**
156 * @tc.name: GetPkgContextInfoListMap_0200
157 * @tc.desc: JsRuntimeLiteTest test for GetPkgContextInfoListMap.
158 * @tc.type: FUNC
159 */
160 HWTEST_F(JsRuntimeLiteTest, GetPkgContextInfoListMap_0200, TestSize.Level0)
161 {
162 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0200 start");
163
164 std::map<std::string, std::string> modulePkgContentMap;
165 std::string pkgContentJsonString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
166 "library","version":"1.0.0","entryPath":"","isSO":false}})";
167 modulePkgContentMap["entry"] = pkgContentJsonString;
168
169 std::string libraryString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
170 "library","version":"1.0.0","entryPath":"","isSO":false}})";
171 modulePkgContentMap["library"] = libraryString;
172
173 AbilityRuntime::Runtime::Options options;
174 options.preload = true;
175 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
176 std::map<std::string, std::vector<std::vector<std::string>>> ret;
177 std::map<std::string, std::string> pkgAliasMap;
178 JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(modulePkgContentMap, ret, pkgAliasMap);
179 std::string expectString = "library:packageName:library:bundleName:";
180 expectString += "com.xxx.xxxx:moduleName:library:version:1.0.0:entryPath::isSO:false:";
181 auto it = ret.find("entry");
182 ASSERT_EQ(it, ret.end());
183 auto libraryIt = ret.find("library");
184 ASSERT_EQ(libraryIt, ret.end());
185 std::string pkgRetString;
186 for (const auto& vec : it->second) {
187 for (const auto& str : vec) {
188 pkgRetString += str + ":";
189 }
190 }
191 ASSERT_EQ(pkgRetString, "");
192 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0200 end");
193 }
194 } // namespace AbilityRuntime
195 } // namespace OHOS