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 "ecmascript/ecma_module.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_locale.h"
19 #include "ecmascript/tagged_dictionary.h"
20 #include "ecmascript/tests/test_helper.h"
21
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class EcmaModuleTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46 PandaVM *instance {nullptr};
47 ecmascript::EcmaHandleScope *scope {nullptr};
48 JSThread *thread {nullptr};
49 };
50
EcmaModuleCreate(JSThread * thread)51 EcmaModule *EcmaModuleCreate(JSThread *thread)
52 {
53 ObjectFactory *objectFactory = thread->GetEcmaVM()->GetFactory();
54 JSHandle<EcmaModule> handleEcmaModule = objectFactory->NewEmptyEcmaModule();
55 return *handleEcmaModule;
56 }
57
58 /*
59 * Feature: EcmaModule
60 * Function: AddItem
61 * SubFunction: GetItem
62 * FunctionPoints: Add Item To EcmaModule
63 * CaseDescription: Add an item for a EcmaModule that has called "SetNameDictionary" function in the HWTEST_F_L0, and
64 * check its value through 'GetItem' function.
65 */
HWTEST_F_L0(EcmaModuleTest,AddItem_001)66 HWTEST_F_L0(EcmaModuleTest, AddItem_001)
67 {
68 int numOfElementsDict = 4;
69 CString cStrItemName = "key1";
70 int intItemValue = 1;
71 JSHandle<EcmaModule> handleEcmaModule(thread, EcmaModuleCreate(thread));
72 JSHandle<NameDictionary> handleNameDict(NameDictionary::Create(thread, numOfElementsDict));
73 JSHandle<JSTaggedValue> handleTagValItemName(
74 thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName));
75 JSHandle<JSTaggedValue> handleTagValItemValue(thread, JSTaggedValue(intItemValue));
76
77 handleEcmaModule->SetNameDictionary(thread, handleNameDict); // Call SetNameDictionary in HWTEST_F_L0
78 EcmaModule::AddItem(thread, handleEcmaModule, handleTagValItemName, handleTagValItemValue);
79 EXPECT_EQ(handleEcmaModule->GetItem(thread, handleTagValItemName)->GetNumber(), intItemValue);
80 }
81
82 /*
83 * Feature: EcmaModule
84 * Function: AddItem
85 * SubFunction: GetItem
86 * FunctionPoints: Add Item To EcmaModule
87 * CaseDescription: Add an item for a EcmaModule that has not called "SetNameDictionary" function in the HWTEST_F_L0,
88 * and check its value through 'GetItem' function.
89 */
HWTEST_F_L0(EcmaModuleTest,AddItem_002)90 HWTEST_F_L0(EcmaModuleTest, AddItem_002)
91 {
92 CString cStrItemName = "cStrItemName";
93 int intItemValue = 1;
94 JSHandle<EcmaModule> handleEcmaModule(thread, EcmaModuleCreate(thread));
95 JSHandle<JSTaggedValue> handleTagValItemName(
96 thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName));
97 JSHandle<JSTaggedValue> handleTagValItemValue(thread, JSTaggedValue(intItemValue));
98
99 // This EcmaModule calls 'SetNameDictionary' function through 'AddItem' function of "ecma_module.cpp".
100 EcmaModule::AddItem(thread, handleEcmaModule, handleTagValItemName, handleTagValItemValue);
101 EXPECT_EQ(handleEcmaModule->GetItem(thread, handleTagValItemName)->GetNumber(), intItemValue);
102 }
103
104 /*
105 * Feature: EcmaModule
106 * Function: RemoveItem
107 * SubFunction: AddItem/GetItem
108 * FunctionPoints: Remove Item From EcmaModule
109 * CaseDescription: Add an item for an empty EcmaModule through 'AddItem' function, then remove the item from the
110 * EcmaModule through 'RemoveItem' function, finally check whether the item obtained from the
111 * EcmaModule through 'GetItem' function is undefined.
112 */
HWTEST_F_L0(EcmaModuleTest,RemoveItem)113 HWTEST_F_L0(EcmaModuleTest, RemoveItem)
114 {
115 ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory();
116
117 CString cStrItemName = "cStrItemName";
118 int intItemValue = 1;
119 JSHandle<EcmaModule> handleEcmaModule(thread, EcmaModuleCreate(thread));
120 JSHandle<JSTaggedValue> handleTagValItemName(objFactory->NewFromCanBeCompressString(cStrItemName));
121 JSHandle<JSTaggedValue> handleTagValItemValue(thread, JSTaggedValue(intItemValue));
122
123 EcmaModule::AddItem(thread, handleEcmaModule, handleTagValItemName, handleTagValItemValue);
124 EcmaModule::RemoveItem(thread, handleEcmaModule, handleTagValItemName);
125 EXPECT_TRUE(handleEcmaModule->GetItem(thread, handleTagValItemName)->IsUndefined());
126 }
127
128 /*
129 * Feature: EcmaModule
130 * Function: SetNameDictionary
131 * SubFunction: NameDictionary::Put/GetNameDictionary
132 * FunctionPoints: Set NameDictionary For EcmaModule
133 * CaseDescription: Create a source key, a source value, a source NameDictionary and a target EcmaModule, change the
134 * NameDictionary through 'NameDictionary::Put' function, set the changed source NameDictionary as
135 * this target EcmaModule's NameDictionary through 'SetNameDictionary' function, check whether the
136 * result returned through 'GetNameDictionary' function from the target EcmaModule are within
137 * expectations.
138 */
HWTEST_F_L0(EcmaModuleTest,SetNameDictionary)139 HWTEST_F_L0(EcmaModuleTest, SetNameDictionary)
140 {
141 ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory();
142
143 int numOfElementsDict = 4;
144 JSHandle<NameDictionary> handleNameDict(NameDictionary::Create(thread, numOfElementsDict));
145 JSHandle<JSTaggedValue> handleObjFunc = thread->GetEcmaVM()->GetGlobalEnv()->GetObjectFunction();
146 CString keyArray1 = "hello1";
147 JSHandle<EcmaString> stringKey1 = objFactory->NewFromCanBeCompressString(keyArray1);
148 JSHandle<JSTaggedValue> key1(stringKey1);
149 JSHandle<JSTaggedValue> value1(objFactory
150 ->NewJSObjectByConstructor(JSHandle<JSFunction>(handleObjFunc), handleObjFunc));
151 JSHandle<NameDictionary> handleNameDictionaryFrom(
152 NameDictionary::Put(thread, handleNameDict, key1, value1, PropertyAttributes::Default()));
153 JSHandle<EcmaModule> handleEcmaModule(thread, EcmaModuleCreate(thread));
154
155 handleEcmaModule->SetNameDictionary(thread, handleNameDictionaryFrom);
156 JSHandle<NameDictionary> handleNameDictionaryTo(thread,
157 NameDictionary::Cast(handleEcmaModule->GetNameDictionary().GetTaggedObject()));
158 EXPECT_EQ(handleNameDictionaryTo->EntriesCount(), 1);
159 int entry1 = handleNameDictionaryTo->FindEntry(key1.GetTaggedValue());
160 EXPECT_TRUE(key1.GetTaggedValue() == handleNameDictionaryTo->GetKey(entry1));
161 EXPECT_TRUE(value1.GetTaggedValue() == handleNameDictionaryTo->GetValue(entry1));
162 }
163
164 /*
165 * Feature: ModuleManager
166 * Function: AddModule
167 * SubFunction: AddItem/GetModule/GetItem
168 * FunctionPoints: Add EcmaModule To ModuleManager
169 * CaseDescription: Create 2 source EcmaModules that both add an item, create a ModuleManager that add the 2 source
170 * EcmaModule, check whether the items of EcmaModules obtained from the ModuleManager through
171 * 'GetModule' function and 'GetItem' function are within expectations.
172 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_AddModule)173 HWTEST_F_L0(EcmaModuleTest, ModuleManager_AddModule)
174 {
175 ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory();
176 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
177
178 int numOfElementsDict1 = 4;
179 int numOfElementsDict2 = 4;
180 CString cStrItemName1 = "cStrItemName1";
181 CString cStrItemName2 = "cStrItemName2";
182 int intItemValue1 = 1;
183 int intItemValue2 = 2;
184 JSHandle<NameDictionary> handleNameDict1(NameDictionary::Create(thread, numOfElementsDict1));
185 JSHandle<NameDictionary> handleNameDict2(NameDictionary::Create(thread, numOfElementsDict2));
186 JSHandle<JSTaggedValue> handleItemName1(
187 thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName1));
188 JSHandle<JSTaggedValue> handleItemName2(
189 thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString(cStrItemName2));
190 JSHandle<JSTaggedValue> handleItemValue1(thread, JSTaggedValue(intItemValue1));
191 JSHandle<JSTaggedValue> handleItemValue2(thread, JSTaggedValue(intItemValue2));
192 JSHandle<EcmaModule> handleEcmaModuleAddFrom1(thread, EcmaModuleCreate(thread));
193 JSHandle<EcmaModule> handleEcmaModuleAddFrom2(thread, EcmaModuleCreate(thread));
194 handleEcmaModuleAddFrom1->SetNameDictionary(thread, handleNameDict1);
195 handleEcmaModuleAddFrom2->SetNameDictionary(thread, handleNameDict2);
196
197 EcmaModule::AddItem(thread, handleEcmaModuleAddFrom1, handleItemName1, handleItemValue1);
198 JSHandle<JSTaggedValue> handleTagValEcmaModuleAddFrom1(thread, handleEcmaModuleAddFrom1.GetTaggedValue());
199 std::string stdStrNameEcmaModuleAdd1 = "NameEcmaModule1";
200 JSHandle<JSTaggedValue> handleTagValNameEcmaModuleAdd1(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd1));
201 EcmaModule::AddItem(thread, handleEcmaModuleAddFrom2, handleItemName2, handleItemValue2);
202 JSHandle<JSTaggedValue> handleTagValEcmaModuleAddFrom2(thread, handleEcmaModuleAddFrom2.GetTaggedValue());
203 std::string stdStrNameEcmaModuleAdd2 = "NameEcmaModule2";
204 JSHandle<JSTaggedValue> handleTagValNameEcmaModuleAdd2(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd2));
205
206 moduleManager->AddModule(handleTagValNameEcmaModuleAdd1, handleTagValEcmaModuleAddFrom1);
207 moduleManager->AddModule(handleTagValNameEcmaModuleAdd2, handleTagValEcmaModuleAddFrom2);
208 JSHandle<JSTaggedValue> handleTagValEcmaModuleGet1 = moduleManager->GetModule(thread,
209 handleTagValNameEcmaModuleAdd1);
210 JSHandle<JSTaggedValue> handleTagValEcmaModuleGet2 = moduleManager->GetModule(thread,
211 handleTagValNameEcmaModuleAdd2);
212 EXPECT_EQ(JSHandle<EcmaModule>::Cast(handleTagValEcmaModuleGet1)->GetItem(thread, handleItemName1)->GetNumber(),
213 intItemValue1);
214 EXPECT_EQ(JSHandle<EcmaModule>::Cast(handleTagValEcmaModuleGet2)->GetItem(thread, handleItemName2)->GetNumber(),
215 intItemValue2);
216 }
217
218 /*
219 * Feature: ModuleManager
220 * Function: RemoveModule
221 * SubFunction: AddItem/GetModule/AddModule/GetItem
222 * FunctionPoints: Remove EcmaModule From ModuleManager
223 * CaseDescription: Create two source EcmaModules that add different items, create a ModuleManager that add the two
224 * source EcmaModules, check whether the properties of the EcmaModules obtained from the ModuleManager
225 * through 'GetModule' function are within expectations while removing EcmaModules from the
226 * ModuleManager through 'RemoveModule' function one by one.
227 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_RemoveModule)228 HWTEST_F_L0(EcmaModuleTest, ModuleManager_RemoveModule)
229 {
230 ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory();
231 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
232
233 std::string stdStrNameEcmaModuleAdd1 = "NameEcmaModule1";
234 std::string stdStrNameEcmaModuleAdd2 = "NameEcmaModule2";
235 int intItemValue1 = 1;
236 int intItemValue2 = 2;
237 int numOfElementsDict1 = 4;
238 int numOfElementsDict2 = 4;
239 JSHandle<JSTaggedValue> handleTagValNameEcmaModuleAdd1(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd1));
240 JSHandle<JSTaggedValue> handleTagValNameEcmaModuleAdd2(objFactory->NewFromStdString(stdStrNameEcmaModuleAdd2));
241 JSHandle<JSTaggedValue> handleTagValItemName1(objFactory->NewFromCanBeCompressString("name1"));
242 JSHandle<JSTaggedValue> handleTagValItemName2(objFactory->NewFromCanBeCompressString("name2"));
243 JSHandle<JSTaggedValue> handleTagValItemValue1(thread, JSTaggedValue(intItemValue1));
244 JSHandle<JSTaggedValue> handleTagValItemValue2(thread, JSTaggedValue(intItemValue2));
245 JSHandle<EcmaModule> handleEcmaModuleAddFrom1(thread, EcmaModuleCreate(thread));
246 JSHandle<EcmaModule> handleEcmaModuleAddFrom2(thread, EcmaModuleCreate(thread));
247 JSHandle<NameDictionary> handleNameDict1(NameDictionary::Create(thread, numOfElementsDict1));
248 JSHandle<NameDictionary> handleNameDict2(NameDictionary::Create(thread, numOfElementsDict2));
249 handleEcmaModuleAddFrom1->SetNameDictionary(thread, handleNameDict1);
250 handleEcmaModuleAddFrom2->SetNameDictionary(thread, handleNameDict2);
251 EcmaModule::AddItem(thread, handleEcmaModuleAddFrom1, handleTagValItemName1, handleTagValItemValue1);
252 EcmaModule::AddItem(thread, handleEcmaModuleAddFrom2, handleTagValItemName2, handleTagValItemValue2);
253 JSHandle<JSTaggedValue> handleTaggedValueEcmaModuleAddFrom1(thread, handleEcmaModuleAddFrom1.GetTaggedValue());
254 JSHandle<JSTaggedValue> handleTaggedValueEcmaModuleAddFrom2(thread, handleEcmaModuleAddFrom2.GetTaggedValue());
255
256 moduleManager->AddModule(handleTagValNameEcmaModuleAdd1, handleTaggedValueEcmaModuleAddFrom1);
257 moduleManager->AddModule(handleTagValNameEcmaModuleAdd2, handleTaggedValueEcmaModuleAddFrom2);
258 EXPECT_EQ(JSHandle<EcmaModule>::Cast(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd1))
259 ->GetItem(thread, handleTagValItemName1)->GetNumber(), intItemValue1);
260 EXPECT_EQ(JSHandle<EcmaModule>::Cast(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd2))
261 ->GetItem(thread, handleTagValItemName2)->GetNumber(), intItemValue2);
262
263 moduleManager->RemoveModule(handleTagValNameEcmaModuleAdd1);
264 EXPECT_TRUE(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd1)->IsUndefined());
265 EXPECT_EQ(JSHandle<EcmaModule>::Cast(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd2))
266 ->GetItem(thread, handleTagValItemName2)->GetNumber(), intItemValue2);
267
268 moduleManager->RemoveModule(handleTagValNameEcmaModuleAdd2);
269 EXPECT_TRUE(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd1)->IsUndefined());
270 EXPECT_TRUE(moduleManager->GetModule(thread, handleTagValNameEcmaModuleAdd2)->IsUndefined());
271 }
272
273 /*
274 * Feature: ModuleManager
275 * Function: SetCurrentExportModuleName
276 * SubFunction: GetCurrentExportModuleName
277 * FunctionPoints: Get Current ExportModuleName Of ModuleManager
278 * CaseDescription: Create a ModuleManager, check whether the ExportModuleName obtained from the ModuleManager through
279 * 'GetCurrentExportModuleName' function is within expectations while changing the Current
280 * ExportModuleName of the ModuleManager through 'SetCurrentExportModuleName' function.
281 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_SetCurrentExportModuleName)282 HWTEST_F_L0(EcmaModuleTest, ModuleManager_SetCurrentExportModuleName)
283 {
284 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
285
286 std::string_view strViewNameEcmaModule1 = "NameEcmaModule1";
287 std::string_view strViewNameEcmaModule2 = "NameEcmaModule2";
288 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule1);
289 EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule1).c_str());
290 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule2);
291 EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule2).c_str());
292 }
293
294 /*
295 * Feature: ModuleManager
296 * Function: GetPrevExportModuleName
297 * SubFunction: SetCurrentExportModuleName
298 * FunctionPoints: Get Previous ExportModuleName Of ModuleManager
299 * CaseDescription: Create a ModuleManager, check whether the previous ExportModuleName obtained from the ModuleManager
300 * through 'GetPrevExportModuleName' function is within expectations while changing the Current
301 * ExportModuleName of the ModuleManager through 'SetCurrentExportModuleName' function.
302 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_GetPrevExportModuleName)303 HWTEST_F_L0(EcmaModuleTest, ModuleManager_GetPrevExportModuleName)
304 {
305 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
306
307 std::string_view strViewNameEcmaModule1 = "NameEcmaModule1";
308 std::string_view strViewNameEcmaModule2 = "NameEcmaModule2";
309 std::string_view strViewNameEcmaModule3 = "NameEcmaModule3";
310 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule1);
311 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule2);
312 EXPECT_STREQ(moduleManager->GetPrevExportModuleName().c_str(), CString(strViewNameEcmaModule1).c_str());
313 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule3);
314 EXPECT_STREQ(moduleManager->GetPrevExportModuleName().c_str(), CString(strViewNameEcmaModule2).c_str());
315 }
316
317 /*
318 * Feature: ModuleManager
319 * Function: RestoreCurrentExportModuleName
320 * SubFunction: SetCurrentExportModuleName/GetCurrentExportModuleName
321 * FunctionPoints: Restore Current ExportModuleName Of ModuleManager
322 * CaseDescription: Create a ModuleManager, check whether the current ExportModuleName obtained from the ModuleManager
323 * through 'GetCurrentExportModuleName' function is within expectations while changing the Current
324 * ExportModuleName of the ModuleManager through 'SetCurrentExportModuleName' function and
325 * 'RestoreCurrentExportModuleName' function.
326 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_RestoreCurrentExportModuleName)327 HWTEST_F_L0(EcmaModuleTest, ModuleManager_RestoreCurrentExportModuleName)
328 {
329 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
330
331 std::string_view strViewNameEcmaModule1 = "NameEcmaModule1";
332 std::string_view strViewNameEcmaModule2 = "NameEcmaModule2";
333 std::string_view strViewNameEcmaModule3 = "NameEcmaModule3";
334 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule1);
335 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule2);
336 moduleManager->SetCurrentExportModuleName(strViewNameEcmaModule3);
337 EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule3).c_str());
338 moduleManager->RestoreCurrentExportModuleName();
339 EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule2).c_str());
340 moduleManager->RestoreCurrentExportModuleName();
341 EXPECT_STREQ(moduleManager->GetCurrentExportModuleName().c_str(), CString(strViewNameEcmaModule1).c_str());
342 }
343
344 /*
345 * Feature: ModuleManager
346 * Function: AddModuleItem
347 * SubFunction: SetCurrentExportModuleName/GetModule/GetModuleItem
348 * FunctionPoints: Add ModuleItem For Current EcmaModule Of ModuleManager
349 * CaseDescription: Create a ModuleManager, set the current EcmaModule for the ModuleManager through
350 * 'SetCurrentExportModuleName' function, add source ModuleItems for the current EcmaModule Of the
351 * ModuleManager, check whether the ModuleItems obtained through 'GetModuleItem' function from the
352 * ModuleManager are within expectations.
353 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_AddModuleItem)354 HWTEST_F_L0(EcmaModuleTest, ModuleManager_AddModuleItem)
355 {
356 ObjectFactory *objFactory = thread->GetEcmaVM()->GetFactory();
357 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
358
359 int intItemValue11 = 11;
360 int intItemValue12 = 12;
361 int intItemValue21 = 21;
362 int intItemValue22 = 22;
363 JSHandle<JSTaggedValue> handleTagValItemName11(objFactory->NewFromCanBeCompressString("cStrItemName11"));
364 JSHandle<JSTaggedValue> handleTagValItemName12(objFactory->NewFromCanBeCompressString("cStrItemName12"));
365 JSHandle<JSTaggedValue> handleTagValItemName21(objFactory->NewFromCanBeCompressString("cStrItemName21"));
366 JSHandle<JSTaggedValue> handleTagValItemName22(objFactory->NewFromCanBeCompressString("cStrItemName22"));
367 JSHandle<JSTaggedValue> handleTagValItemValue11(thread, JSTaggedValue(intItemValue11));
368 JSHandle<JSTaggedValue> handleTagValItemValue12(thread, JSTaggedValue(intItemValue12));
369 JSHandle<JSTaggedValue> handleTagValItemValue21(thread, JSTaggedValue(intItemValue21));
370 JSHandle<JSTaggedValue> handleTagValItemValue22(thread, JSTaggedValue(intItemValue22));
371 JSHandle<EcmaString> handleEcmaStrNameEcmaModule1 = objFactory->NewFromString("cStrNameEcmaModule1");
372 JSHandle<EcmaString> handleEcmaStrNameEcmaModule2 = objFactory->NewFromString("cStrNameEcmaModule2");
373 std::string stdStrModuleFileName1 = JSLocale::ConvertToStdString(handleEcmaStrNameEcmaModule1);
374 std::string stdStrModuleFileName2 = JSLocale::ConvertToStdString(handleEcmaStrNameEcmaModule2);
375 JSHandle<JSTaggedValue> handleTagValEcmaModuleName1(handleEcmaStrNameEcmaModule1);
376 JSHandle<JSTaggedValue> handleTagValEcmaModuleName2(handleEcmaStrNameEcmaModule2);
377
378 // Test when the module is created through 'NewEmptyEcmaModule' function called at HWTEST_F_L0.
379 JSHandle<EcmaModule> handleEcmaModule1(thread, EcmaModuleCreate(thread));
380 JSHandle<JSTaggedValue> handleTagValEcmaModule1(thread, handleEcmaModule1.GetTaggedValue());
381 moduleManager->AddModule(handleTagValEcmaModuleName1, handleTagValEcmaModule1);
382 moduleManager->SetCurrentExportModuleName(stdStrModuleFileName1);
383 moduleManager->AddModuleItem(thread, handleTagValItemName11, handleTagValItemValue11);
384 moduleManager->AddModuleItem(thread, handleTagValItemName12, handleTagValItemValue12);
385
386 EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName11)->GetNumber(),
387 intItemValue11);
388 EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName12)->GetNumber(),
389 intItemValue12);
390
391 // Test when the module is created through 'NewEmptyEcmaModule' function called at "ecma_module.cpp".
392 moduleManager->SetCurrentExportModuleName(stdStrModuleFileName2);
393 moduleManager->AddModuleItem(thread, handleTagValItemName21, handleTagValItemValue21);
394 moduleManager->AddModuleItem(thread, handleTagValItemName22, handleTagValItemValue22);
395
396 JSHandle<JSTaggedValue> handleTagValEcmaModule2 = moduleManager->GetModule(thread, handleTagValEcmaModuleName2);
397 EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName11)->GetNumber(),
398 intItemValue11);
399 EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule1, handleTagValItemName12)->GetNumber(),
400 intItemValue12);
401 EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule2, handleTagValItemName21)->GetNumber(),
402 intItemValue21);
403 EXPECT_EQ(moduleManager->GetModuleItem(thread, handleTagValEcmaModule2, handleTagValItemName22)->GetNumber(),
404 intItemValue22);
405 }
406
407 /*
408 * Feature: ModuleManager
409 * Function: CopyModule
410 * SubFunction: AddItem/SetCurrentExportModuleName/GetModule/GetModuleItem
411 * FunctionPoints: Copy EcmaModule To ModuleManager
412 * CaseDescription: Create two source EcmaModules and one target ModuleManager, prepare the two source EcmaModules
413 * through 'AddItem' function, check whether the the ModuleItems obtained through 'GetModuleItem' from
414 * the target ModuleManager are within expectations while changing the target ModuleManager through
415 * 'SetCurrentExportModuleName' function and 'CopyModule' function.
416 */
HWTEST_F_L0(EcmaModuleTest,ModuleManager_CopyModule)417 HWTEST_F_L0(EcmaModuleTest, ModuleManager_CopyModule)
418 {
419 ObjectFactory *objFactory = thread->GetEcmaVM()->GetFactory();
420 ModuleManager *moduleManager = thread->GetEcmaVM()->GetModuleManager();
421
422 int intItemValue11 = 11;
423 int intItemValue12 = 12;
424 int intItemValue21 = 21;
425 int intItemValue22 = 22;
426 std::string_view fileNameEcmaModuleCopyTo1 = "fileNameEcmaModuleCopyTo1";
427 std::string_view fileNameEcmaModuleCopyTo2 = "fileNameEcmaModuleCopyTo2";
428 JSHandle<JSTaggedValue> handleTagValItemName11(objFactory->NewFromCanBeCompressString("ItemName11"));
429 JSHandle<JSTaggedValue> handleTagValItemName12(objFactory->NewFromCanBeCompressString("ItemName12"));
430 JSHandle<JSTaggedValue> handleTagValItemName21(objFactory->NewFromCanBeCompressString("ItemName21"));
431 JSHandle<JSTaggedValue> handleTagValItemName22(objFactory->NewFromCanBeCompressString("ItemName22"));
432 JSHandle<JSTaggedValue> handleTagValItemValue11(thread, JSTaggedValue(intItemValue11));
433 JSHandle<JSTaggedValue> handleTagValItemValue12(thread, JSTaggedValue(intItemValue12));
434 JSHandle<JSTaggedValue> handleTagValItemValue21(thread, JSTaggedValue(intItemValue21));
435 JSHandle<JSTaggedValue> handleTagValItemValue22(thread, JSTaggedValue(intItemValue22));
436 JSHandle<EcmaModule> handleEcmaModuleCopyFrom1(thread, EcmaModuleCreate(thread));
437 JSHandle<EcmaModule> handleEcmaModuleCopyFrom2(thread, EcmaModuleCreate(thread));
438 JSHandle<JSTaggedValue> handleTagValEcmaModuleCopyFrom1(thread, handleEcmaModuleCopyFrom1.GetTaggedValue());
439 JSHandle<JSTaggedValue> handleTagValEcmaModuleCopyFrom2(thread, handleEcmaModuleCopyFrom2.GetTaggedValue());
440 EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom1, handleTagValItemName11, handleTagValItemValue11);
441 EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom1, handleTagValItemName12, handleTagValItemValue12);
442 EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom2, handleTagValItemName21, handleTagValItemValue21);
443 EcmaModule::AddItem(thread, handleEcmaModuleCopyFrom2, handleTagValItemName22, handleTagValItemValue22);
444
445 moduleManager->SetCurrentExportModuleName(fileNameEcmaModuleCopyTo1);
446 moduleManager->CopyModule(thread, handleTagValEcmaModuleCopyFrom1);
447 JSHandle<JSTaggedValue> handleTagValEcmaModuleCopyTo1 = moduleManager->GetModule(thread,
448 JSHandle<JSTaggedValue>::Cast(objFactory->NewFromString(CString(fileNameEcmaModuleCopyTo1))));
449 EXPECT_EQ(intItemValue11,
450 moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName11)->GetNumber());
451 EXPECT_EQ(intItemValue12,
452 moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName12)->GetNumber());
453
454 moduleManager->SetCurrentExportModuleName(fileNameEcmaModuleCopyTo2);
455 moduleManager->CopyModule(thread, handleTagValEcmaModuleCopyFrom2);
456 JSHandle<JSTaggedValue> handleTagValEcmaModuleCopyTo2 = moduleManager->GetModule(thread,
457 JSHandle<JSTaggedValue>::Cast(objFactory->NewFromString(CString(fileNameEcmaModuleCopyTo2))));
458 EXPECT_EQ(intItemValue11,
459 moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName11)->GetNumber());
460 EXPECT_EQ(intItemValue12,
461 moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo1, handleTagValItemName12)->GetNumber());
462 EXPECT_EQ(intItemValue21,
463 moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo2, handleTagValItemName21)->GetNumber());
464 EXPECT_EQ(intItemValue22,
465 moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo2, handleTagValItemName22)->GetNumber());
466 }
467 } // namespace panda::ecmascript
468