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 "js_async_work_tdd_test.h"
17 #include "js_async_work.h"
18
19 namespace OHOS {
20 namespace ACELite {
21 static int8_t g_statusCode = 0;
22
SetUp()23 void JsAsyncWorkTddTest::SetUp()
24 {
25 g_statusCode = -1;
26 }
27
Execute(void * data)28 static void Execute(void *data)
29 {
30 (void)data;
31 }
32
ExecuteWithCode(void * data,int8_t statusCode)33 static void ExecuteWithCode(void *data, int8_t statusCode)
34 {
35 (void)data;
36 g_statusCode = statusCode;
37 }
38
JsAsyncWorkTest001()39 void JsAsyncWorkTddTest::JsAsyncWorkTest001()
40 {
41 TDD_CASE_BEGIN();
42 /**
43 * @tc.steps: step1. call DispatchAsyncWork with valid parameters
44 */
45 bool res = JsAsyncWork::DispatchAsyncWork(Execute, nullptr);
46 /**
47 * @tc.expected: step1. res is false due to null AceAbility instance
48 */
49 EXPECT_FALSE(res);
50 /**
51 * @tc.steps: step2. call overloaded DispatchAsyncWork with valid parameters
52 */
53 res = JsAsyncWork::DispatchAsyncWork(ExecuteWithCode, nullptr);
54 /**
55 * @tc.expected: step2. res is false due to null AceAbility instance
56 */
57 EXPECT_FALSE(res);
58 TDD_CASE_END();
59 }
60
JsAsyncWorkTest002()61 void JsAsyncWorkTddTest::JsAsyncWorkTest002()
62 {
63 TDD_CASE_BEGIN();
64 /**
65 * @tc.steps: step1. call DispatchAsyncWork with invalid parameters
66 */
67 AsyncWorkHandler handler1 = nullptr;
68 bool res = JsAsyncWork::DispatchAsyncWork(handler1, nullptr);
69 /**
70 * @tc.expected: step1. res is false due to invalid parameters, and error log can be checked
71 */
72 EXPECT_FALSE(res);
73 /**
74 * @tc.steps: step2. call overloaded DispatchAsyncWork with invalid parameters
75 */
76 AsyncHandler handler2 = nullptr;
77 res = JsAsyncWork::DispatchAsyncWork(handler2, nullptr);
78 /**
79 * @tc.expected: step2. res is false due to invalid parameters, and error log can be checked
80 */
81 EXPECT_FALSE(res);
82 TDD_CASE_END();
83 }
84
JsAsyncWorkTest003()85 void JsAsyncWorkTddTest::JsAsyncWorkTest003()
86 {
87 TDD_CASE_BEGIN();
88 /**
89 * @tc.steps: step1. call ExecuteAsyncWork with valid parameters
90 */
91 AsyncWork *asyncWork = new AsyncWork();
92 EXPECT_TRUE(asyncWork != nullptr);
93
94 asyncWork->workHandler = Execute;
95 asyncWork->handler = ExecuteWithCode;
96 JsAsyncWork::ExecuteAsyncWork(asyncWork);
97 JsAsyncWork::ExecuteAsyncWork(asyncWork, JsAsyncWork::ERR_OK);
98 /**
99 * @tc.expected: step1. log can be checked and g_statusCode = JsAsyncWork::ERR_OK
100 */
101 EXPECT_TRUE(g_statusCode == JsAsyncWork::ERR_OK);
102 if (asyncWork != nullptr) {
103 delete asyncWork;
104 asyncWork = nullptr;
105 }
106 TDD_CASE_END();
107 }
108
JsAsyncWorkTest004()109 void JsAsyncWorkTddTest::JsAsyncWorkTest004()
110 {
111 TDD_CASE_BEGIN();
112 /**
113 * @tc.steps: step1. call ExecuteAsyncWork with null asyncWork
114 */
115 AsyncWork *asyncWork = nullptr;
116 JsAsyncWork::ExecuteAsyncWork(asyncWork);
117 JsAsyncWork::ExecuteAsyncWork(asyncWork, JsAsyncWork::ERR_OK);
118 /**
119 * @tc.expected: step1. error log can be checked and g_statusCode != JsAsyncWork::ERR_OK
120 */
121 EXPECT_TRUE(g_statusCode != JsAsyncWork::ERR_OK);
122 TDD_CASE_END();
123 }
124
JsAsyncWorkTest005()125 void JsAsyncWorkTddTest::JsAsyncWorkTest005()
126 {
127 TDD_CASE_BEGIN();
128 /**
129 * @tc.steps: step1. call ExecuteAsyncWork with null workHandler
130 */
131 AsyncWork *asyncWork = new AsyncWork();
132 EXPECT_TRUE(asyncWork != nullptr);
133 JsAsyncWork::ExecuteAsyncWork(asyncWork);
134 JsAsyncWork::ExecuteAsyncWork(asyncWork, JsAsyncWork::ERR_OK);
135 /**
136 * @tc.expected: step1. error log can be checked and g_statusCode != JsAsyncWork::ERR_OK
137 */
138 EXPECT_TRUE(g_statusCode != JsAsyncWork::ERR_OK);
139 if (asyncWork != nullptr) {
140 delete asyncWork;
141 asyncWork = nullptr;
142 }
143 TDD_CASE_END();
144 }
145
RunTests()146 void JsAsyncWorkTddTest::RunTests()
147 {
148 JsAsyncWorkTest001();
149 JsAsyncWorkTest002();
150 JsAsyncWorkTest003();
151 JsAsyncWorkTest004();
152 JsAsyncWorkTest005();
153 }
154
155 #ifdef TDD_ASSERTIONS
156 /**
157 * @tc.name: JsAsyncWorkTest001
158 * @tc.desc: Verify DispatchAsyncWork with valid parameters.
159 */
160 HWTEST_F(JsAsyncWorkTddTest, test001, TestSize.Level1)
161 {
162 JsAsyncWorkTddTest::JsAsyncWorkTest001();
163 }
164
165 /**
166 * @tc.name: JsAsyncWorkTest002
167 * @tc.desc: Verify DispatchAsyncWork with invalid parameters.
168 */
169 HWTEST_F(JsAsyncWorkTddTest, test002, TestSize.Level1)
170 {
171 JsAsyncWorkTddTest::JsAsyncWorkTest002();
172 }
173
174 /**
175 * @tc.name: JsAsyncWorkTest003
176 * @tc.desc: Verify ExcuteAsyncWork with valid parameters.
177 */
178 HWTEST_F(JsAsyncWorkTddTest, test003, TestSize.Level1)
179 {
180 JsAsyncWorkTddTest::JsAsyncWorkTest003();
181 }
182
183 /**
184 * @tc.name: JsAsyncWorkTest004
185 * @tc.desc: Verify ExcuteAsyncWork with invalid parameters.
186 */
187 HWTEST_F(JsAsyncWorkTddTest, test004, TestSize.Level1)
188 {
189 JsAsyncWorkTddTest::JsAsyncWorkTest004();
190 }
191
192 /**
193 * @tc.name: JsAsyncWorkTest005
194 * @tc.desc: Verify ExcuteAsyncWork with invalid parameters.
195 */
196 HWTEST_F(JsAsyncWorkTddTest, test005, TestSize.Level1)
197 {
198 JsAsyncWorkTddTest::JsAsyncWorkTest005();
199 }
200 #endif
201 } // namespace ACELite
202 } // namespace OHOS
203