• 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 "ark_native_engine.h"
17 #include "native_engine/native_value.h"
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20 #include "test.h"
21 #include "test_timer.h"
22 #include "js_sys_module/timer/sys_timer.h"
23 #include "tools/log.h"
24 
25 using namespace Commonlibrary::Concurrent::Common;
26 using namespace OHOS::JsSysModule;
27 using panda::RuntimeOption;
28 
29 #define ASSERT_CHECK_CALL(call)   \
30     {                             \
31         ASSERT_EQ(call, napi_ok); \
32     }
33 
34 #define ASSERT_CHECK_VALUE_TYPE(env, value, type)               \
35     {                                                           \
36         napi_valuetype valueType = napi_undefined;              \
37         ASSERT_TRUE((value) != nullptr);                        \
38         ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
39         ASSERT_EQ(valueType, type);                             \
40     }
41 
TimerCallback(napi_env env,napi_callback_info info)42 napi_value TimerCallback(napi_env env, napi_callback_info info)
43 {
44     if (info == nullptr) {
45         HILOG_ERROR("TimerCallback, Invalid input info.");
46     }
47     return nullptr;
48 }
49 
50 class NativeEngineProxy {
51 public:
NativeEngineProxy()52     NativeEngineProxy()
53     {
54         //Setup
55         RuntimeOption option;
56         option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC);
57         const int64_t poolSize = 0x1000000;
58         option.SetGcPoolSize(poolSize);
59         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
60         option.SetDebuggerLibraryPath("");
61         vm_ = panda::JSNApi::CreateJSVM(option);
62         if (vm_ == nullptr) {
63             return;
64         }
65 
66         engine_ = new ArkNativeEngine(vm_, nullptr);
67         Timer::RegisterTime(reinterpret_cast<napi_env>(engine_));
68     }
69 
~NativeEngineProxy()70     ~NativeEngineProxy()
71     {
72         delete engine_;
73         panda::JSNApi::DestroyJSVM(vm_);
74     }
75 
operator -() const76     inline ArkNativeEngine* operator-() const
77     {
78         return engine_;
79     }
80 
operator napi_env() const81     inline operator napi_env() const
82     {
83         return reinterpret_cast<napi_env>(engine_);
84     }
85 
86 private:
87     EcmaVM* vm_ {nullptr};
88     ArkNativeEngine* engine_ {nullptr};
89 };
90 
91 struct TestData {
92     napi_env env_;
93 };
94 
AssertFalse()95 void AssertFalse()
96 {
97     ASSERT_TRUE(false);
98 }
99 
TimerAbort(napi_env env,napi_callback_info info)100 napi_value TimerAbort(napi_env env, napi_callback_info info)
101 {
102     AssertFalse();
103     return nullptr;
104 }
105 
GetGlobalProperty(napi_env env,const char * name)106 napi_value GetGlobalProperty(napi_env env, const char *name)
107 {
108     napi_value value = nullptr;
109     napi_value global;
110     napi_get_global(env, &global);
111     napi_get_named_property(env, global, name, &value);
112     return value;
113 }
114 
ThrowingCallback(napi_env env,napi_callback_info info)115 napi_value ThrowingCallback(napi_env env, napi_callback_info info)
116 {
117     napi_value errorMessage;
118     napi_value error;
119     napi_create_string_utf8(env, "An error occurred!", NAPI_AUTO_LENGTH, &errorMessage);
120     napi_create_error(env, nullptr, errorMessage, &error);
121     napi_throw(env, error);
122     return nullptr;
123 }
124 
125 /* @tc.name: Init
126  * @tc.desc: Test.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(NativeEngineTest, TimerTest001, testing::ext::TestSize.Level0)
130 {
131     napi_env env = (napi_env)engine_;
132     bool res0 = Timer::RegisterTime(env);
133     ASSERT_TRUE(res0);
134     bool res1 = Timer::RegisterTime(nullptr);
135     ASSERT_TRUE(!res1);
136 }
137 
138 /* @tc.name: settimeout
139  * @tc.desc: Test.
140  * @tc.type: FUNC
141  */
142 HWTEST_F(NativeEngineTest, TimerTest002, testing::ext::TestSize.Level0)
143 {
144     napi_env env = (napi_env)engine_;
145     size_t argc = 0;
146     napi_value argv0[] = {nullptr}; // no args has exception
147     napi_value cb = GetGlobalProperty(env, "setInterval");
148     napi_value res = nullptr;
149     napi_call_function(env, nullptr, cb, argc, argv0, &res);
150     ASSERT_TRUE(res == nullptr);
151     bool res0 = 0;
152     napi_is_exception_pending(env, &res0);
153     ASSERT_TRUE(res0);
154     napi_value exception = nullptr;
155     napi_get_and_clear_last_exception(env, &exception);
156 }
157 
158 /* @tc.name: settimeout
159  * @tc.desc: Test.
160  * @tc.type: FUNC
161  */
162 HWTEST_F(NativeEngineTest, TimerTest003, testing::ext::TestSize.Level0)
163 {
164     napi_env env = (napi_env)engine_;
165     size_t argc = 2;
166     napi_value nativeMessage0 = nullptr;
167     napi_create_uint32(env, 5, &nativeMessage0); // Random number
168     napi_value nativeMessage1 = nullptr;
169     napi_create_uint32(env, 50, &nativeMessage1); // Random number
170     napi_value argv[] = {nativeMessage0, nativeMessage1};
171     napi_value cb = GetGlobalProperty(env, "setTimeout");
172     napi_value tId = nullptr;
173     napi_call_function(env, nullptr, cb, argc, argv, &tId);
174     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_undefined);
175 }
176 
177 /* @tc.name: settimeout/ClearTimer
178  * @tc.desc: Test.
179  * @tc.type: FUNC
180  */
181 HWTEST_F(NativeEngineTest, TimerTest004, testing::ext::TestSize.Level0)
182 {
183     napi_env env = (napi_env)engine_;
184     size_t argc = 2;
185     napi_value nativeMessage0 = nullptr;
186     napi_create_uint32(env, 50, &nativeMessage0); // Random number
187     napi_value nativeMessage1 = nullptr;
188     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
189     napi_value argv[] = {nativeMessage1, nativeMessage0};
190     napi_value setTimeoutCB = GetGlobalProperty(env, "setTimeout");
191     napi_value tId = nullptr;
192     napi_call_function(env, nullptr, setTimeoutCB, argc, argv, &tId);
193     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
194     napi_value argv1[] = {tId};
195     napi_value clearTimerCB = GetGlobalProperty(env, "clearTimeout");
196     napi_value res = nullptr;
197     napi_call_function(env, nullptr, clearTimerCB, 1, argv1, &res);
198     ASSERT_CHECK_VALUE_TYPE(env, res, napi_undefined);
199 }
200 
201 /* @tc.name: settimeout
202  * @tc.desc: Test.
203  * @tc.type: FUNC
204  */
205 HWTEST_F(NativeEngineTest, TimerTest005, testing::ext::TestSize.Level0)
206 {
207     napi_env env = (napi_env)engine_;
208     size_t argc = 2;
209     int32_t number = -50.0; // Random number
210     napi_value nativeMessage0 = nullptr;
211     napi_create_int32(env, number, &nativeMessage0);
212     napi_value nativeMessage1 = nullptr;
213     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
214     napi_value argv[] = {nativeMessage1, nativeMessage0};
215     napi_value cb = GetGlobalProperty(env, "setTimeout");
216     napi_value tId = nullptr;
217     napi_call_function(env, nullptr, cb, argc, argv, &tId);
218     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
219 }
220 
221 /* @tc.name: settimeout
222  * @tc.desc: Test.
223  * @tc.type: FUNC
224  */
225 HWTEST_F(NativeEngineTest, TimerTest006, testing::ext::TestSize.Level0)
226 {
227     napi_env env = (napi_env)engine_;
228     size_t argc = 2;
229     napi_value nativeMessage0 = nullptr;
230     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage0);
231     std::string message = "50";
232     napi_value nativeMessage1 = nullptr;
233     napi_create_string_utf8(env, message.c_str(), message.length(), &nativeMessage1); // timeout is string
234     napi_value argv[] = {nativeMessage0, nativeMessage1};
235     napi_value cb = GetGlobalProperty(env, "setTimeout");
236     napi_value tId = nullptr;
237     napi_call_function(env, nullptr, cb, argc, argv, &tId);
238     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
239 }
240 
241 /* @tc.name: ClearTimer
242  * @tc.desc: Test.
243  * @tc.type: FUNC
244  */
245 HWTEST_F(NativeEngineTest, TimerTest007, testing::ext::TestSize.Level0)
246 {
247     napi_env env = (napi_env)engine_;
248     size_t argc = 0;
249     napi_value argv[] = {nullptr}; // no args
250     napi_value cb = GetGlobalProperty(env, "clearTimeout");
251     napi_value res = nullptr;
252     napi_call_function(env, nullptr, cb, argc, argv, &res);
253     ASSERT_CHECK_VALUE_TYPE(env, res, napi_undefined);
254 }
255 
256 /* @tc.name: ClearTimer
257  * @tc.desc: Test.
258  * @tc.type: FUNC
259  */
260 HWTEST_F(NativeEngineTest, TimerTest008, testing::ext::TestSize.Level0)
261 {
262     napi_env env = (napi_env)engine_;
263     size_t argc = 1;
264     napi_value nativeMessage = nullptr;
265     std::string message = "50"; // Random number
266     napi_create_string_utf8(env, message.c_str(), message.length(), &nativeMessage);
267     napi_value argv[] = {nativeMessage};
268     napi_value cb = GetGlobalProperty(env, "clearTimeout");
269     napi_value res = nullptr;
270     napi_call_function(env, nullptr, cb, argc, argv, &res);
271     ASSERT_CHECK_VALUE_TYPE(env, res, napi_undefined);
272 }
273 
274 /* @tc.name: ClearTimer
275  * @tc.desc: Test.
276  * @tc.type: FUNC
277  */
278 HWTEST_F(NativeEngineTest, TimerTest009, testing::ext::TestSize.Level0)
279 {
280     napi_env env = (napi_env)engine_;
281     size_t argc = 1;
282     napi_value cb = GetGlobalProperty(env, "clearTimeout");
283     napi_value inputId = nullptr;
284     napi_create_uint32(env, 50, &inputId); // Random number
285     napi_value argv[] = { inputId }; // timerId is inexistent
286     napi_value res = nullptr;
287     napi_call_function(env, nullptr, cb, argc, argv, &res);
288     ASSERT_CHECK_VALUE_TYPE(env, res, napi_undefined);
289 }
290 
291 /* @tc.name: setinteval
292  * @tc.desc: Test.
293  * @tc.type: FUNC
294  */
295 HWTEST_F(NativeEngineTest, TimerTest010, testing::ext::TestSize.Level0)
296 {
297     napi_env env = (napi_env)engine_;
298     size_t argc = 2;
299     napi_value nativeMessage0 = nullptr;
300     napi_create_uint32(env, 5, &nativeMessage0); // Random number
301     napi_value nativeMessage1 = nullptr;
302     napi_create_uint32(env, 50, &nativeMessage1); // Random number
303     napi_value argv1[] = {nativeMessage0, nativeMessage1};
304     napi_value cb1 = GetGlobalProperty(env, "setInterval");
305     napi_value tId1 = nullptr;
306     napi_call_function(env, nullptr, cb1, argc, argv1, &tId1);
307     ASSERT_CHECK_VALUE_TYPE(env, tId1, napi_undefined);
308     napi_value nativeMessage2 = nullptr;
309     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage2);
310     napi_value argv2[] = {nativeMessage2, nativeMessage1};
311     napi_value tId2 = nullptr;
312     napi_value cb2 = GetGlobalProperty(env, "setInterval");
313     napi_call_function(env, nullptr, cb2, argc, argv2, &tId2);
314     ASSERT_CHECK_VALUE_TYPE(env, tId2, napi_number);
315     int32_t number = -50.0; // Random number
316     napi_value nativeMessage3 = nullptr;
317     napi_create_int32(env, number, &nativeMessage3);
318     napi_value argv3[] = {nativeMessage2, nativeMessage3};
319     napi_value tId3 = nullptr;
320     napi_value cb3 = GetGlobalProperty(env, "setTimeout");
321     napi_call_function(env, nullptr, cb3, argc, argv3, &tId3);
322     ASSERT_CHECK_VALUE_TYPE(env, tId3, napi_number);
323     std::string message = "50"; // Random number
324     napi_value nativeMessage4 = nullptr;
325     napi_create_string_utf8(env, message.c_str(), message.length(), &nativeMessage4);
326     napi_value argv4[] = {nativeMessage2, nativeMessage4};
327     napi_value tId4 = nullptr;
328     napi_value cb4 = GetGlobalProperty(env, "setInterval");
329     napi_call_function(env, nullptr, cb4, argc, argv4, &tId4);
330     ASSERT_CHECK_VALUE_TYPE(env, tId4, napi_number);
331 
332     bool res0 = Timer::HasTimer(env);
333     ASSERT_TRUE(res0);
334     Timer::ClearEnvironmentTimer(env);
335     bool res1 = Timer::HasTimer(env);
336     ASSERT_TRUE(!res1);
337 }
338 
339 /* @tc.name: settimeout
340  * @tc.desc: Test.
341  * @tc.type: FUNC
342  */
343 HWTEST_F(NativeEngineTest, TimerTest011, testing::ext::TestSize.Level0)
344 {
345     napi_env env = (napi_env)engine_;
346     size_t argc = 1;  // argc = 1, timeout = 0
347 
348     napi_value tId = nullptr;
349     napi_value cb = GetGlobalProperty(env, "setTimeout");
350     napi_value nativeMessage1 = nullptr;
351     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
352     napi_value argv[] = {nativeMessage1};
353     napi_call_function(env, nullptr, cb, argc, argv, &tId);
354     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
355 }
356 
357 /* @tc.name: settimeout
358  * @tc.desc: Test: callbackArgc > 0
359  * @tc.type: FUNC
360  */
361 HWTEST_F(NativeEngineTest, TimerTest012, testing::ext::TestSize.Level0)
362 {
363     napi_env env = (napi_env)engine_;
364     size_t argc = 3;
365     napi_value tId = nullptr;
366     napi_value cb = GetGlobalProperty(env, "setTimeout");
367     napi_value nativeMessage0 = nullptr;
368     napi_create_uint32(env, 50, &nativeMessage0); // Random number
369     napi_value nativeMessage2 = nullptr;
370     napi_create_uint32(env, 50, &nativeMessage2); // Random number
371     napi_value nativeMessage1 = nullptr;
372     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
373     napi_value argv[] = {nativeMessage1, nativeMessage0, nativeMessage2};
374     napi_call_function(env, nullptr, cb, argc, argv, &tId);
375     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
376 }
377 
378 /* @tc.name: DeleteTimer
379  * @tc.desc: Test.
380  * @tc.type: FUNC
381  */
382 HWTEST_F(NativeEngineTest, TimerTest013, testing::ext::TestSize.Level0)
383 {
384     std::map<uint32_t, TimerCallbackInfo*>& table = TimerTest::create_timerTable();
385     napi_env env = (napi_env)engine_;
386     uint32_t tId = 1;
387     int32_t timeout = 1000;
388     napi_ref callback = nullptr;
389     bool repeat = false;
390     size_t argc = 0;
391     napi_ref* argv = nullptr;
392     TimerCallbackInfo* callbackInfo = new TimerCallbackInfo(env, tId, timeout, callback, repeat, argc, argv);
393     table[tId] = callbackInfo;
394     TimerTest::DeleteTimer(tId, callbackInfo);
395     ASSERT_TRUE(table.find(tId) == table.end());
396 }
397 
398 /* @tc.name: Settimeout
399  * @tc.desc: Test.
400  * @tc.type: FUNC
401  */
402 HWTEST_F(NativeEngineTest, TimerTest014, testing::ext::TestSize.Level0)
403 {
404     napi_env env = (napi_env)engine_;
405     napi_value nativeMessage0 = nullptr;
406     napi_create_uint32(env, 50, &nativeMessage0);
407     napi_value nativeMessage1 = nullptr;
408     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
409     napi_value argv[] = {nativeMessage1, nativeMessage0};
410     napi_value setTimeoutCB = GetGlobalProperty(env, "setTimeout");
411     napi_value tId = nullptr;
412     size_t argc = 2;
413     napi_call_function(env, nullptr, setTimeoutCB, argc, argv, &tId);
414     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
415     napi_env* env2 = new napi_env;
416     napi_create_runtime(env, env2);
417     napi_value argv1[] = {tId};
418     napi_value clearTimerCB = GetGlobalProperty(env, "clearTimeout");
419     napi_value res = nullptr;
420     napi_call_function(*env2, nullptr, clearTimerCB, 1, argv1, &res);
421     ASSERT_CHECK_VALUE_TYPE(*env2, res, napi_undefined);
422 }
423 
424 /* @tc.name: ClearEnvironmentTimer
425  * @tc.desc: Test.
426  * @tc.type: FUNC
427  */
428 HWTEST_F(NativeEngineTest, TimerTest015, testing::ext::TestSize.Level0)
429 {
430     uv_timer_t* handle = new uv_timer_t;
431     handle->data = nullptr;
432     TimerTest::TimerCallback(handle);
433 
434     napi_env env = (napi_env)engine_;
435     size_t argc = 2;
436     napi_value nativeMessage0 = nullptr;
437     napi_create_uint32(env, 50, &nativeMessage0); // Random number
438     napi_value nativeMessage1 = nullptr;
439     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
440     napi_value argv[] = {nativeMessage1, nativeMessage0};
441     napi_value setTimeoutCB = GetGlobalProperty(env, "setTimeout");
442     napi_value tId = nullptr;
443     napi_call_function(env, nullptr, setTimeoutCB, argc, argv, &tId);
444     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
445     napi_env* env2 = new napi_env;
446     napi_create_runtime(env, env2);
447     TimerTest::ClearEnvironmentTimer(*env2);
448     std::map<uint32_t, TimerCallbackInfo*>& table = TimerTest::create_timerTable();
449     ASSERT_TRUE(table.size() != 0);
450 }
451 
452 /* @tc.name: settimeout
453  * @tc.desc: Test.
454  * @tc.type: FUNC
455  */
456 HWTEST_F(NativeEngineTest, TimerTest016, testing::ext::TestSize.Level0)
457 {
458     napi_env env = (napi_env)engine_;
459     napi_env* env2 = new napi_env;
460     napi_create_runtime(env, env2);
461     size_t argc = 2;
462     napi_value nativeMessage0 = nullptr;
463     napi_create_uint32(*env2, 50, &nativeMessage0); // Random number
464     napi_value nativeMessage1 = nullptr;
465     napi_create_function(*env2, "callback", NAPI_AUTO_LENGTH, TimerCallback, nullptr, &nativeMessage1);
466     napi_value argv[] = {nativeMessage1, nativeMessage0};
467     napi_value setTimeoutCB = GetGlobalProperty(env, "setTimeout");
468     napi_value tId = nullptr;
469     napi_call_function(*env2, nullptr, setTimeoutCB, argc, argv, &tId);
470     ASSERT_CHECK_VALUE_TYPE(*env2, tId, napi_number);
471 }
472 
473 /* @tc.name: settimeout
474  * @tc.desc: Test.
475  * @tc.type: FUNC
476  */
477 HWTEST_F(NativeEngineTest, TimerTest017, testing::ext::TestSize.Level0)
478 {
479     napi_env env = (napi_env)engine_;
480     napi_env env2 = nullptr;
481     uv_timer_t* handle = new uv_timer_t;
482     uint32_t tId2 = 1;
483     int32_t timeout = 1000;
484     napi_ref callback = nullptr;
485     bool repeat = false;
486     size_t argc2 = 0;
487     napi_ref* argv2 = nullptr;
488     handle->data = new TimerCallbackInfo(env, tId2, timeout, callback, repeat, argc2, argv2);
489     TimerCallbackInfo* callbackInfo = static_cast<TimerCallbackInfo*>(handle->data);
490     callbackInfo->env_ = env2;
491     TimerTest::TimerCallback(handle);
492 
493     size_t argc = 2;
494     napi_value nativeMessage0 = nullptr;
495     napi_create_uint32(env, 50, &nativeMessage0); // Random number
496     napi_value nativeMessage1 = nullptr;
497     napi_create_function(env, "callback", NAPI_AUTO_LENGTH, ThrowingCallback, nullptr, &nativeMessage1);
498     napi_value argv[] = {nativeMessage1, nativeMessage0};
499     napi_value setTimeoutCB = GetGlobalProperty(env, "setTimeout");
500     napi_value tId = nullptr;
501     napi_call_function(env, nullptr, setTimeoutCB, argc, argv, &tId);
502     ASSERT_CHECK_VALUE_TYPE(env, tId, napi_number);
503 }
504 
505 /* @tc.name: settimeout
506  * @tc.desc: Test settimeout should not be change.
507  * @tc.type: FUNC
508  */
509 HWTEST_F(NativeEngineTest, TimerTest018, testing::ext::TestSize.Level0)
510 {
511     NativeEngineProxy env;
512     thread_local auto cleanUpData = new TestData();
513     cleanUpData->env_ = env;
__anonc25abc800102(void * data) 514     napi_add_env_cleanup_hook(env, [](void * data) {
515         auto that = reinterpret_cast<TestData*>(cleanUpData);
516         size_t argc = 2;
517         napi_value nativeMessage0 = nullptr;
518         napi_value nativeMessage1 = nullptr;
519         napi_create_function(that->env_, "callback", NAPI_AUTO_LENGTH, TimerAbort, nullptr, &nativeMessage1);
520         napi_create_uint32(that->env_, 0, &nativeMessage0);
521         napi_value argv[] = {nativeMessage1, nativeMessage0};
522         napi_value setTimeoutCB = GetGlobalProperty(that->env_, "setTimeout");
523         napi_value tId = nullptr;
524         napi_call_function(that->env_, nullptr, setTimeoutCB, argc, argv, &tId);
525         ASSERT_CHECK_VALUE_TYPE(that->env_, tId, napi_number);
526     }, cleanUpData);
527 }
528 
529 /* @tc.name: setInterval
530  * @tc.desc: Test setInterval should not be change.
531  * @tc.type: FUNC
532  */
533 HWTEST_F(NativeEngineTest, TimerTest019, testing::ext::TestSize.Level0)
534 {
535     NativeEngineProxy env;
536     thread_local auto cleanData = new TestData();
537     cleanData->env_ = env;
__anonc25abc800202(void * data) 538     napi_add_env_cleanup_hook(env, [](void * data) {
539         napi_value nativeMessage1 = nullptr;
540         auto that = reinterpret_cast<TestData*>(cleanData);
541         size_t argc = 2;
542         napi_create_function(that->env_, "callback", NAPI_AUTO_LENGTH, TimerAbort, nullptr, &nativeMessage1);
543         napi_value nativeMessage0 = nullptr;
544         napi_create_uint32(that->env_, 0, &nativeMessage0);
545         napi_value argv[] = {nativeMessage1, nativeMessage0};
546         napi_value setIntervalCB = GetGlobalProperty(that->env_, "setInterval");
547         napi_value tId = nullptr;
548         napi_call_function(that->env_, nullptr, setIntervalCB, argc, argv, &tId);
549         ASSERT_CHECK_VALUE_TYPE(that->env_, tId, napi_number);
550     }, cleanData);
551 }
552 
553 /* @tc.name: timer
554  * @tc.desc: Test timer can be write.
555  * @tc.type: FUNC
556  */
557 HWTEST_F(NativeEngineTest, TimerTest020, testing::ext::TestSize.Level0)
558 {
559     NativeEngineProxy env;
560     napi_value func1 = nullptr;
561     napi_status status = napi_create_function(env, "setTimeout", NAPI_AUTO_LENGTH,
562                                               TimerTest::SetTimeout, nullptr, &func1);
563     ASSERT_TRUE(status == napi_ok);
564     napi_value func2 = nullptr;
565     status = napi_create_function(env, "setInterval", NAPI_AUTO_LENGTH, TimerTest::SetInterval, nullptr, &func2);
566     ASSERT_TRUE(status == napi_ok);
567     napi_value func3 = nullptr;
568     status = napi_create_function(env, "clearTimeout", NAPI_AUTO_LENGTH, TimerTest::ClearTimer, nullptr, &func3);
569     ASSERT_TRUE(status == napi_ok);
570     napi_value func4 = nullptr;
571     status = napi_create_function(env, "clearInterval", NAPI_AUTO_LENGTH, TimerTest::ClearTimer, nullptr, &func4);
572     ASSERT_TRUE(status == napi_ok);
573     napi_property_descriptor properties[] = {
574         {"setTimeout", nullptr, nullptr, nullptr, nullptr, func1, napi_default_jsproperty, nullptr},
575         {"setInterval", nullptr, nullptr, nullptr, nullptr, func2, napi_default_jsproperty, nullptr},
576         {"clearTimeout", nullptr, nullptr, nullptr, nullptr, func3, napi_default_jsproperty, nullptr},
577         {"clearInterval", nullptr, nullptr, nullptr, nullptr, func4, napi_default_jsproperty, nullptr}
578     };
579     napi_value globalObj = Helper::NapiHelper::GetGlobalObject(env);
580     status = napi_define_properties(env, globalObj, sizeof(properties) / sizeof(properties[0]), properties);
581     ASSERT_TRUE(status == napi_ok);
582     napi_value setTimeoutCB = GetGlobalProperty(env, "setTimeout");
583     napi_value setIntervalCB = GetGlobalProperty(env, "setInterval");
584     napi_value clearTimeoutCB = GetGlobalProperty(env, "clearTimeout");
585     napi_value clearIntervalCB = GetGlobalProperty(env, "clearInterval");
586     bool isEqual = false;
587     napi_strict_equals(env, setTimeoutCB, func1, &isEqual);
588     ASSERT_TRUE(isEqual);
589     napi_strict_equals(env, setIntervalCB, func2, &isEqual);
590     ASSERT_TRUE(isEqual);
591     napi_strict_equals(env, clearTimeoutCB, func3, &isEqual);
592     ASSERT_TRUE(isEqual);
593     napi_strict_equals(env, clearIntervalCB, func4, &isEqual);
594     ASSERT_TRUE(isEqual);
595 }
596 
597 /* @tc.name: settimeout
598  * @tc.desc: Test settimeout can be write.
599  * @tc.type: FUNC
600  */
601 HWTEST_F(NativeEngineTest, TimerTest021, testing::ext::TestSize.Level0)
602 {
603     NativeEngineProxy env;
604     napi_value func = nullptr;
605     napi_status status = napi_create_function(env, "setTimeout", NAPI_AUTO_LENGTH,
606                                               TimerTest::SetTimeout, nullptr, &func);
607     ASSERT_TRUE(status == napi_ok);
608     napi_value globalObj = Helper::NapiHelper::GetGlobalObject(env);
609     ASSERT_TRUE(napi_set_named_property(env, globalObj, "setTimeout", func) == napi_ok);
610 }
611 
612 /* @tc.name: setInterval
613  * @tc.desc: Test setInterval can be write.
614  * @tc.type: FUNC
615  */
616 HWTEST_F(NativeEngineTest, TimerTest022, testing::ext::TestSize.Level0)
617 {
618     NativeEngineProxy env;
619     napi_value func = nullptr;
620     napi_status status = napi_create_function(env, "setInterval", NAPI_AUTO_LENGTH,
621                                               TimerTest::SetInterval, nullptr, &func);
622     ASSERT_TRUE(status == napi_ok);
623     napi_value globalObj = Helper::NapiHelper::GetGlobalObject(env);
624     ASSERT_TRUE(napi_set_named_property(env, globalObj, "setInterval", func) == napi_ok);
625 }
626 
627 /* @tc.name: clearTimeout
628  * @tc.desc: Test clearTimeout can be write.
629  * @tc.type: FUNC
630  */
631 HWTEST_F(NativeEngineTest, TimerTest023, testing::ext::TestSize.Level0)
632 {
633     NativeEngineProxy env;
634     napi_value func = nullptr;
635     napi_status status = napi_create_function(env, "clearTimeout", NAPI_AUTO_LENGTH,
636                                               TimerTest::ClearTimer, nullptr, &func);
637     ASSERT_TRUE(status == napi_ok);
638     napi_value globalObj = Helper::NapiHelper::GetGlobalObject(env);
639     ASSERT_TRUE(napi_set_named_property(env, globalObj, "clearTimeout", func) == napi_ok);
640 }
641 
642 /* @tc.name: clearInterval
643  * @tc.desc: Test clearInterval can be write.
644  * @tc.type: FUNC
645  */
646 HWTEST_F(NativeEngineTest, TimerTest024, testing::ext::TestSize.Level0)
647 {
648     NativeEngineProxy env;
649     napi_value func = nullptr;
650     napi_status status = napi_create_function(env, "clearInterval", NAPI_AUTO_LENGTH,
651                                               TimerTest::ClearTimer, nullptr, &func);
652     ASSERT_TRUE(status == napi_ok);
653     napi_value globalObj = Helper::NapiHelper::GetGlobalObject(env);
654     ASSERT_TRUE(napi_set_named_property(env, globalObj, "clearInterval", func) == napi_ok);
655 }