• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19 #include "utils/log.h"
20 #include "worker.h"
21 
22 #define ASSERT_CHECK_CALL(call)   \
23     {                             \
24         ASSERT_EQ(call, napi_ok); \
25     }
26 
27 #define ASSERT_CHECK_VALUE_TYPE(env, value, type)               \
28     {                                                           \
29         napi_valuetype valueType = napi_undefined;              \
30         ASSERT_TRUE(value != nullptr);                          \
31         ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
32         ASSERT_EQ(valueType, type);                             \
33     }
34 
35 using namespace Commonlibrary::Concurrent::WorkerModule;
36 
37 // worker constructor
Worker_Constructor(napi_env env,napi_value global)38 napi_value Worker_Constructor(napi_env env, napi_value global)
39 {
40     std::string funcName = "WorkerConstructor";
41     napi_value cb = nullptr;
42     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::WorkerConstructor, nullptr, &cb);
43 
44     napi_value result = nullptr;
45     napi_value argv[2] = { nullptr };
46 
47     std::string script = "entry/ets/workers/worker.ts";
48     napi_create_string_utf8(env, script.c_str(), script.length(), &argv[0]);
49     std::string type = "classic";
50     std::string name = "WorkerThread";
51     napi_value typeValue = nullptr;
52     napi_value nameValue = nullptr;
53     napi_create_string_utf8(env, name.c_str(), name.length(), &nameValue);
54     napi_create_string_utf8(env, type.c_str(), type.length(), &typeValue);
55 
56     napi_value object = nullptr;
57     napi_create_object(env, &object);
58 
59     napi_set_named_property(env, object, "name", nameValue);
60     napi_set_named_property(env, object, "type", typeValue);
61     argv[1]  = object;
62 
63     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
64     napi_env newEnv = nullptr;
65     napi_create_runtime(env, &newEnv);
66     return result;
67 }
68 
69 // worker terminate
Worker_Terminate(napi_env env,napi_value global)70 napi_value Worker_Terminate(napi_env env, napi_value global)
71 {
72     std::string funcName = "Terminate";
73     napi_value cb = nullptr;
74     napi_value result = nullptr;
75     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::Terminate, nullptr, &cb);
76     napi_call_function(env, global, cb, 0, nullptr, &result);
77     return result;
78 }
79 // worker WorkerConstructor
80 HWTEST_F(NativeEngineTest, WorkerConstructorTest001, testing::ext::TestSize.Level0)
81 {
82     napi_env env = (napi_env)engine_;
83     napi_value global;
84     napi_get_global(env, &global);
85 
86     napi_value result = nullptr;
87     result = Worker_Constructor(env, global);
88 
89     Worker* worker = nullptr;
90     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
91     std::string nameResult = worker->GetName();
92     ASSERT_EQ(nameResult, "WorkerThread");
93     std::string scriptResult = worker->GetScript();
94     ASSERT_EQ(scriptResult, "entry/ets/workers/worker.ts");
95 
96     result = Worker_Terminate(env, global);
97 
98     ASSERT_TRUE(result != nullptr);
99 }
100 //worker PostMessage
101 HWTEST_F(NativeEngineTest, PostMessageTest001, testing::ext::TestSize.Level0)
102 {
103     napi_env env = (napi_env)engine_;
104     napi_value global;
105     napi_get_global(env, &global);
106 
107     napi_value result = nullptr;
108     result = Worker_Constructor(env, global);
109 
110     Worker* worker = nullptr;
111     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
112     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
113 
114     napi_value argv[1] = { nullptr };
115     std::string message = "host";
116     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
117 
118     std::string funcName = "PostMessage";
119     napi_value cb = nullptr;
120 
121     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::PostMessage, worker, &cb);
122     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
123 
124     uv_async_t* req = new uv_async_t;
125     req->data = worker;
126     Worker::WorkerOnMessage(req);
127 
128     result = Worker_Terminate(env, global);
129     ASSERT_TRUE(result != nullptr);
130 }
131 //worker PostMessage
132 HWTEST_F(NativeEngineTest, PostMessageTest002, testing::ext::TestSize.Level0)
133 {
134     napi_env env = (napi_env)engine_;
135     napi_value global;
136     napi_get_global(env, &global);
137 
138     napi_value result = nullptr;
139     result = Worker_Constructor(env, global);
140 
141     napi_value argv[1] = { nullptr };
142     std::string message = "host";
143     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
144 
145     std::string funcName = "PostMessage";
146     napi_value cb = nullptr;
147 
148     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::PostMessage, nullptr, &cb);
149     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
150 
151     result = Worker_Terminate(env, global);
152     ASSERT_TRUE(result != nullptr);
153 }
154 //worker PostMessage
155 HWTEST_F(NativeEngineTest, PostMessageTest003, testing::ext::TestSize.Level0)
156 {
157     napi_env env = (napi_env)engine_;
158     napi_value global;
159     napi_get_global(env, &global);
160 
161     napi_value result = nullptr;
162     result = Worker_Constructor(env, global);
163 
164     Worker* worker = nullptr;
165     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
166     worker->UpdateWorkerState(Worker::RunnerState::TERMINATED);
167 
168     napi_value argv[1] = { nullptr };
169     std::string message = "host";
170     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
171 
172     std::string funcName = "PostMessage";
173     napi_value cb = nullptr;
174 
175     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::PostMessage, worker, &cb);
176     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
177 
178     uv_async_t* req = new uv_async_t;
179     req->data = worker;
180     Worker::WorkerOnMessage(req);
181 
182     result = Worker_Terminate(env, global);
183     ASSERT_TRUE(result != nullptr);
184 }
185 //worker PostMessage
186 HWTEST_F(NativeEngineTest, PostMessageToHostTest001, testing::ext::TestSize.Level0)
187 {
188     napi_env env = (napi_env)engine_;
189     napi_value global;
190     napi_get_global(env, &global);
191 
192     napi_value result = nullptr;
193     result = Worker_Constructor(env, global);
194 
195     Worker* worker = nullptr;
196     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
197     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
198 
199     napi_value argv[1] = { nullptr };
200     std::string message = "host";
201     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
202 
203     std::string funcName = "PostMessageToHost";
204     napi_value cb = nullptr;
205     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::PostMessageToHost, worker, &cb);
206     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
207 
208     uv_async_t* req = new uv_async_t;
209     req->data = worker;
210     Worker::HostOnMessage(req);
211 
212     result = Worker_Terminate(env, global);
213     ASSERT_TRUE(result != nullptr);
214 }
215 //worker PostMessageToHost
216 HWTEST_F(NativeEngineTest, PostMessageToHostTest002, testing::ext::TestSize.Level0)
217 {
218     napi_env env = (napi_env)engine_;
219     napi_value global;
220     napi_get_global(env, &global);
221 
222     napi_value result = nullptr;
223     result = Worker_Constructor(env, global);
224 
225     napi_value argv[1] = { nullptr };
226     std::string message = "host";
227     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
228 
229     std::string funcName = "PostMessageToHost";
230     napi_value cb = nullptr;
231     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::PostMessageToHost, nullptr, &cb);
232     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
233 
234     result = Worker_Terminate(env, global);
235     ASSERT_TRUE(result != nullptr);
236 }
237 //worker PostMessageToHost
238 HWTEST_F(NativeEngineTest, PostMessageToHostTest003, testing::ext::TestSize.Level0)
239 {
240     napi_env env = (napi_env)engine_;
241     napi_value global;
242     napi_get_global(env, &global);
243 
244     napi_value arrayresult = nullptr;
245     ASSERT_CHECK_CALL(napi_create_object(env, &arrayresult));
246     ASSERT_CHECK_VALUE_TYPE(env, arrayresult, napi_object);
247     const char testStr[] = "1234567";
248     napi_value strAttribute = nullptr;
249     ASSERT_CHECK_CALL(napi_create_string_utf8(env, testStr, strlen(testStr), &strAttribute));
250     ASSERT_CHECK_VALUE_TYPE(env, strAttribute, napi_string);
251     ASSERT_CHECK_CALL(napi_set_named_property(env, arrayresult, "strAttribute", strAttribute));
252 
253     napi_value retStrAttribute = nullptr;
254     ASSERT_CHECK_CALL(napi_get_named_property(env, arrayresult, "strAttribute", &retStrAttribute));
255     ASSERT_CHECK_VALUE_TYPE(env, retStrAttribute, napi_string);
256 
257     int32_t testNumber = 12345; // 12345 : indicates any number
258     napi_value numberAttribute = nullptr;
259     ASSERT_CHECK_CALL(napi_create_int32(env, testNumber, &numberAttribute));
260     ASSERT_CHECK_VALUE_TYPE(env, numberAttribute, napi_number);
261     ASSERT_CHECK_CALL(napi_set_named_property(env, arrayresult, "numberAttribute", numberAttribute));
262 
263     napi_value propNames = nullptr;
264     ASSERT_CHECK_CALL(napi_get_property_names(env, arrayresult, &propNames));
265     ASSERT_CHECK_VALUE_TYPE(env, propNames, napi_object);
266 
267     napi_value result = nullptr;
268     result = Worker_Constructor(env, global);
269 
270     Worker* worker = nullptr;
271     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
272     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
273 
274     napi_value argv[2] = { nullptr };
275     std::string message = "";
276     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
277     std::string funcName = "PostMessageToHost";
278     argv[1] = propNames;
279     napi_value cb = nullptr;
280     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::PostMessageToHost, worker, &cb);
281     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
282 
283     uv_async_t* req = new uv_async_t;
284     req->data = worker;
285     Worker::HostOnMessage(req);
286 
287     result = Worker_Terminate(env, global);
288     ASSERT_TRUE(result != nullptr);
289 }
290 //worker EventListener
291 HWTEST_F(NativeEngineTest, EventListenerTest001, testing::ext::TestSize.Level0)
292 {
293     napi_env env = (napi_env)engine_;
294     napi_value global;
295     napi_get_global(env, &global);
296     napi_value result = nullptr;
297     result = Worker_Constructor(env, global);
298     Worker* worker = nullptr;
299     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
300 
301     napi_value argv[2] = {nullptr};
302     std::string message = "host";
303     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
__anon926a01960102(napi_env env, napi_callback_info info) 304     auto func = [](napi_env env, napi_callback_info info) -> napi_value {
305         return nullptr;
306     };
307     napi_value funcValue = nullptr;
308     napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
309     argv[1] = funcValue;
310 
311     std::string funcName = "On";
312     napi_value cb = nullptr;
313     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::On, worker, &cb);
314     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
315 
316     funcName = "Once";
317     cb = nullptr;
318     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::Once, worker, &cb);
319     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
320 
321     funcName = "AddEventListener";
322     cb = nullptr;
323     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::AddEventListener, worker, &cb);
324     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
325 
326     funcName = "RemoveEventListener";
327     cb = nullptr;
328     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::RemoveEventListener, worker, &cb);
329     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
330 
331     funcName = "Off";
332     cb = nullptr;
333     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::Off, worker, &cb);
334     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
335 
336     result = Worker_Terminate(env, global);
337     ASSERT_TRUE(result != nullptr);
338 }
339 //worker EventListener
340 HWTEST_F(NativeEngineTest, EventListenerTest002, testing::ext::TestSize.Level0)
341 {
342     napi_env env = (napi_env)engine_;
343     napi_value global;
344     napi_get_global(env, &global);
345     napi_value result = nullptr;
346     result = Worker_Constructor(env, global);
347     Worker* worker = nullptr;
348     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
349 
350     napi_value argv[3] = {nullptr};
351     std::string message = "host";
352     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
__anon926a01960202(napi_env env, napi_callback_info info) 353     auto func = [](napi_env env, napi_callback_info info) -> napi_value {
354         return nullptr;
355     };
356     napi_value funcValue = nullptr;
357     napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
358     argv[1] = funcValue;
359     std::string funcName = "On";
360     napi_value cb = nullptr;
361     funcName = "Once";
362     cb = nullptr;
363     napi_value myobject = nullptr;
364     napi_create_object(env, &myobject);
365 
366     argv[2] = myobject;
367     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::Once, worker, &cb);
368     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
369     result = Worker_Terminate(env, global);
370     ASSERT_TRUE(result != nullptr);
371 }
372 //worker DispatchEvent
373 HWTEST_F(NativeEngineTest, DispatchEventTest001, testing::ext::TestSize.Level0)
374 {
375     napi_env env = (napi_env)engine_;
376     napi_value global;
377     napi_get_global(env, &global);
378     napi_value result = nullptr;
379     result = Worker_Constructor(env, global);
380     Worker* worker = nullptr;
381     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
382 
383 
384     napi_value argv1[2] = {nullptr};
385     std::string message = "host";
386     napi_create_string_utf8(env, message.c_str(), message.length(), &argv1[0]);
__anon926a01960302(napi_env env, napi_callback_info info) 387     auto func = [](napi_env env, napi_callback_info info) -> napi_value {
388         return nullptr;
389     };
390     napi_value funcValue = nullptr;
391     napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
392     argv1[1] = funcValue;
393 
394     std::string funcName = "Once";
395     napi_value cb = nullptr;
396     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::Once, worker, &cb);
397     napi_call_function(env, global, cb, sizeof(argv1) / sizeof(argv1[0]), argv1, &result);
398     napi_value argv[1] = {nullptr};
399 
400     napi_value typeValue = nullptr;
401     std::string type = "zhangsan";
402     napi_create_string_utf8(env, type.c_str(), type.length(), &typeValue);
403 
404     napi_value object = nullptr;
405     napi_create_object(env, &object);
406     napi_set_named_property(env, object, "type", typeValue);
407     argv[0] = object;
408 
409     funcName = "DispatchEvent";
410     cb = nullptr;
411     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::DispatchEvent, worker, &cb);
412     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
413 
414     result = Worker_Terminate(env, global);
415     ASSERT_TRUE(result != nullptr);
416 }
417 //worker ParentPortAddEventListener
418 HWTEST_F(NativeEngineTest, ParentPortAddEventListenerTest001, testing::ext::TestSize.Level0)
419 {
420     napi_env env = (napi_env)engine_;
421     napi_value global;
422     napi_get_global(env, &global);
423     napi_value result = nullptr;
424     result = Worker_Constructor(env, global);
425     Worker* worker = nullptr;
426     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
427     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
428     napi_value argv[3] = {nullptr};
429     std::string message = "host";
430     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
__anon926a01960402(napi_env env, napi_callback_info info) 431     auto func = [](napi_env env, napi_callback_info info) -> napi_value {
432         return nullptr;
433     };
434 
435     std::string funcName = "ParentPortAddEventListener";
436     napi_value cb = nullptr;
437     napi_value funcValue = nullptr;
438     napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
439     argv[1] = funcValue;
440     cb = nullptr;
441     napi_value myobject = nullptr;
442     napi_create_object(env, &myobject);
443 
444     argv[2] = myobject;
445     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::ParentPortAddEventListener, worker, &cb);
446     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
447     result = Worker_Terminate(env, global);
448     ASSERT_TRUE(result != nullptr);
449 }
450 //worker ParentPortRemoveAllListener
451 HWTEST_F(NativeEngineTest, ParentPortRemoveAllListenerTest001, testing::ext::TestSize.Level0)
452 {
453     napi_env env = (napi_env)engine_;
454     napi_value global;
455     napi_get_global(env, &global);
456     napi_value result = nullptr;
457     result = Worker_Constructor(env, global);
458     Worker* worker = nullptr;
459     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
460     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
461 
462     napi_value argv[1] = {nullptr};
463     std::string message = "host";
464     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
465     std::string funcName = "ParentPortRemoveAllListener";
466     napi_value cb = nullptr;
467     cb = nullptr;
468 
469     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::ParentPortRemoveAllListener, worker, &cb);
470     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
471     result = Worker_Terminate(env, global);
472     ASSERT_TRUE(result != nullptr);
473 }
474 //worker ParentPortDispatchEvent
475 HWTEST_F(NativeEngineTest, ParentPortDispatchEventTest001, testing::ext::TestSize.Level0)
476 {
477     napi_env env = (napi_env)engine_;
478     napi_value global;
479     napi_get_global(env, &global);
480     napi_value result = nullptr;
481     result = Worker_Constructor(env, global);
482     Worker* worker = nullptr;
483     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
484     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
485     napi_value argv[1] = {nullptr};
486     napi_value message;
487     napi_create_string_utf8(env, "message", NAPI_AUTO_LENGTH, &message);
488 
489     napi_value objresult = nullptr;
490     napi_create_object(env, &objresult);
491     napi_value cb = nullptr;
492     std::string funcName = "ParentPortDispatchEvent";
493     napi_value messageKey = nullptr;
494     const char* messageKeyStr = "type";
495     napi_create_string_latin1(env, messageKeyStr, strlen(messageKeyStr), &messageKey);
496     napi_value messageValue = nullptr;
497     const char* messageValueStr = "message";
498     napi_create_string_latin1(env, messageValueStr, strlen(messageValueStr), &messageValue);
499     napi_set_property(env, objresult, messageKey, messageValue);
500     argv[0] = objresult;
501     cb = nullptr;
502 
503     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::ParentPortDispatchEvent, worker, &cb);
504     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
505 
506     result = Worker_Terminate(env, global);
507     ASSERT_TRUE(result != nullptr);
508 }
509 //worker ParentPortRemoveEventListener
510 HWTEST_F(NativeEngineTest, ParentPortRemoveEventListenerTest001, testing::ext::TestSize.Level0)
511 {
512     napi_env env = (napi_env)engine_;
513     napi_value global;
514     napi_get_global(env, &global);
515     napi_value result = nullptr;
516     result = Worker_Constructor(env, global);
517     Worker* worker = nullptr;
518     napi_unwrap(env, result, reinterpret_cast<void**>(&worker));
519     worker->UpdateWorkerState(Worker::RunnerState::RUNNING);
520     napi_value argv[2] = {nullptr};
521     std::string message = "host";
522     napi_create_string_utf8(env, message.c_str(), message.length(), &argv[0]);
__anon926a01960502(napi_env env, napi_callback_info info) 523     auto func = [](napi_env env, napi_callback_info info) -> napi_value {
524         return nullptr;
525     };
526 
527     std::string funcName = "ParentPortRemoveEventListener";
528     napi_value cb = nullptr;
529     napi_value funcValue = nullptr;
530     napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
531     argv[1] = funcValue;
532     cb = nullptr;
533 
534     napi_create_function(env, funcName.c_str(), funcName.size(), Worker::ParentPortAddEventListener, worker, &cb);
535     napi_call_function(env, global, cb, sizeof(argv) / sizeof(argv[0]), argv, &result);
536 
537     result = Worker_Terminate(env, global);
538     ASSERT_TRUE(result != nullptr);
539 }