1 /*
2  * Copyright (c) 2021-2022 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 "event_handler.h"
19 #include "event_queue.h"
20 #include "event_runner.h"
21 #include "inner_event.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AppExecFwk;
26 
27 namespace {
28 bool g_isDump = false;
29 const int64_t DELAY_TIME = 10;
30 const std::string GET_TAG_SYSTEM_TEST1 = "GetTagSystemTest1";
31 const std::string GET_TAG_SYSTEM_TEST2 = " ";
32 const std::string GET_TAG_SYSTEM_TEST3 = "@#¥#3243adsafdf_中文";
33 const std::string GET_TAG_SYSTEM_TEST4 = std::to_string(0);
34 const std::string GET_TAG_SYSTEM_TEST5 = std::to_string(0) + "@#¥#3243adsafdf_中文";
35 }  // namespace
36 
37 class DumpTest : public Dumper {
38 public:
39     /**
40      * Processes the content of a specified string.
41      * @param message the content of a specified string.
42      */
Dump(const std::string & message)43     void Dump(const std::string &message)
44     {
45         g_isDump = true;
46         GTEST_LOG_(INFO) << message;
47     }
48 
49     /**
50      * Obtains the tag information.
51      * which is a prefix added to each string before the string content is processed.
52      * @return the tag information.
53      */
GetTag()54     std::string GetTag()
55     {
56         return "DumpTest";
57     }
58 };
59 
60 class GetTagTest1 : public Dumper {
61 public:
62     /**
63      * Processes the content of a specified string.
64      * @param message the content of a specified string.
65      */
Dump(const std::string & message)66     void Dump(const std::string &message)
67     {}
68 
69     /**
70      * Obtains the tag information.
71      * which is a prefix added to each string before the string content is processed.
72      * @return the tag information.
73      */
GetTag()74     std::string GetTag()
75     {
76         return GET_TAG_SYSTEM_TEST1;
77     }
78 };
79 
80 class GetTagTest2 : public Dumper {
81 public:
82     /**
83      * Processes the content of a specified string.
84      * @param message the content of a specified string.
85      */
Dump(const std::string & message)86     void Dump(const std::string &message)
87     {}
88 
89     /**
90      * Obtains the tag information.
91      * which is a prefix added to each string before the string content is processed.
92      * @return the tag information.
93      */
GetTag()94     std::string GetTag()
95     {
96         return GET_TAG_SYSTEM_TEST2;
97     }
98 };
99 
100 class GetTagTest3 : public Dumper {
101 public:
102     /**
103      * Processes the content of a specified string.
104      * @param message the content of a specified string.
105      */
Dump(const std::string & message)106     void Dump(const std::string &message)
107     {}
108 
109     /**
110      * Obtains the tag information.
111      * which is a prefix added to each string before the string content is processed.
112      * @return the tag information.
113      */
GetTag()114     std::string GetTag()
115     {
116         return GET_TAG_SYSTEM_TEST3;
117     }
118 };
119 
120 class GetTagTest4 : public Dumper {
121 public:
122     /**
123      * Processes the content of a specified string.
124      * @param message the content of a specified string.
125      */
Dump(const std::string & message)126     void Dump(const std::string &message)
127     {}
128 
129     /**
130      * Obtains the tag information.
131      * which is a prefix added to each string before the string content is processed.
132      * @return the tag information.
133      */
GetTag()134     std::string GetTag()
135     {
136         return GET_TAG_SYSTEM_TEST4;
137     }
138 };
139 
140 class GetTagTest5 : public Dumper {
141 public:
142     /**
143      * Processes the content of a specified string.
144      * @param message the content of a specified string.
145      */
Dump(const std::string & message)146     void Dump(const std::string &message)
147     {}
148 
149     /**
150      * Obtains the tag information.
151      * which is a prefix added to each string before the string content is processed.
152      * @return the tag information.
153      */
GetTag()154     std::string GetTag()
155     {
156         return GET_TAG_SYSTEM_TEST5;
157     }
158 };
159 
160 class EmsDumperSystemTest : public testing::Test {
161 public:
162     static void SetUpTestCase(void);
163     static void TearDownTestCase(void);
164     void SetUp();
165     void TearDown();
166 };
167 
SetUpTestCase(void)168 void EmsDumperSystemTest::SetUpTestCase(void)
169 {}
170 
TearDownTestCase(void)171 void EmsDumperSystemTest::TearDownTestCase(void)
172 {}
173 
SetUp(void)174 void EmsDumperSystemTest::SetUp(void)
175 {}
176 
TearDown(void)177 void EmsDumperSystemTest::TearDown(void)
178 {}
179 
180 /*
181  * @tc.name: Dump001
182  * @tc.desc: Check Dump
183  * @tc.type: FUNC
184  */
185 HWTEST_F(EmsDumperSystemTest, Dump001, TestSize.Level1)
186 {
187     auto runner = EventRunner::Create(true);
188     auto handler = std::make_shared<EventHandler>(runner);
189     auto event = InnerEvent::Get();
190     DumpTest dump001;
191     usleep(100 * 1000);
192     handler->Dump(dump001);
193     EXPECT_TRUE(g_isDump);
194 }
195 
196 /*
197  * @tc.name: Dump002
198  * @tc.desc: Check Dump after post task
199  * @tc.type: FUNC
200  */
201 HWTEST_F(EmsDumperSystemTest, Dump002, TestSize.Level1)
202 {
203     g_isDump = false;
204     auto runner = EventRunner::Create(true);
205     auto handler = std::make_shared<EventHandler>(runner);
__anon6fb4869b0202() 206     auto task = []() {; };
207     DumpTest dump002;
208     handler->PostTask(task, DELAY_TIME, EventQueue::Priority::LOW);
209     usleep(100 * 1000);
210     handler->Dump(dump002);
211     EXPECT_TRUE(g_isDump);
212 }
213 
214 /*
215  * @tc.name: Dump003
216  * @tc.desc: Check Dump after send event with event id
217  * @tc.type: FUNC
218  */
219 HWTEST_F(EmsDumperSystemTest, Dump003, TestSize.Level1)
220 {
221     g_isDump = false;
222     auto runner = EventRunner::Create(true);
223     auto handler = std::make_shared<EventHandler>(runner);
224     auto event = InnerEvent::Get();
225     DumpTest dump003;
226     handler->SendEvent(event, DELAY_TIME, EventQueue::Priority::LOW);
227     usleep(100 * 1000);
228     handler->Dump(dump003);
229     EXPECT_TRUE(g_isDump);
230 }
231 
232 /*
233  * @tc.name: Dump004
234  * @tc.desc: Check Dump after send event with event id and param
235  * @tc.type: FUNC
236  */
237 HWTEST_F(EmsDumperSystemTest, Dump004, TestSize.Level1)
238 {
239     g_isDump = false;
240     auto runner = EventRunner::Create(true);
241     auto handler = std::make_shared<EventHandler>(runner);
242     auto event = InnerEvent::Get();
243     DumpTest dump004;
244     handler->SendEvent(event, DELAY_TIME, EventQueue::Priority::LOW);
245     usleep(100 * 1000);
246     handler->Dump(dump004);
247     EXPECT_TRUE(g_isDump);
248 }
249 
250 /*
251  * @tc.name: Dump005
252  * @tc.desc: check when send event and post task dump success
253  * @tc.type: FUNC
254  */
255 HWTEST_F(EmsDumperSystemTest, Dump005, TestSize.Level1)
256 {
257     g_isDump = false;
258     auto runner = EventRunner::Create(true);
259     auto handler = std::make_shared<EventHandler>(runner);
260     auto event = InnerEvent::Get();
__anon6fb4869b0302() 261     auto task = []() {; };
262     DumpTest dump005;
263     handler->SendEvent(event, DELAY_TIME, EventQueue::Priority::LOW);
264     handler->PostTask(task, DELAY_TIME * 2, EventQueue::Priority::LOW);
265     usleep(100 * 1000);
266     handler->Dump(dump005);
267     EXPECT_TRUE(g_isDump);
268 }
269 
270 /*
271  * @tc.name: GetTag001
272  * @tc.desc: Check GetTagTest1 process GetTag success
273  * @tc.type: FUNC
274  */
275 HWTEST_F(EmsDumperSystemTest, GetTag001, TestSize.Level1)
276 {
277     GetTagTest1 getTagTest;
278     std::string result = getTagTest.GetTag();
279     EXPECT_EQ(result, GET_TAG_SYSTEM_TEST1);
280 }
281 
282 /*
283  * @tc.name: GetTag002
284  * @tc.desc: Check GetTagTest2 process GetTag success
285  * @tc.type: FUNC
286  */
287 HWTEST_F(EmsDumperSystemTest, GetTag002, TestSize.Level1)
288 {
289     GetTagTest2 getTagTest;
290     std::string result = getTagTest.GetTag();
291     EXPECT_EQ(result, GET_TAG_SYSTEM_TEST2);
292 }
293 
294 /*
295  * @tc.name: GetTag003
296  * @tc.desc: Check GetTagTest3 process GetTag success
297  * @tc.type: FUNC
298  */
299 HWTEST_F(EmsDumperSystemTest, GetTag003, TestSize.Level1)
300 {
301     GetTagTest3 getTagTest;
302     std::string result = getTagTest.GetTag();
303     EXPECT_EQ(result, GET_TAG_SYSTEM_TEST3);
304 }
305 
306 /*
307  * @tc.name: GetTag004
308  * @tc.desc: Check GetTagTest4 process GetTag success
309  * @tc.type: FUNC
310  */
311 HWTEST_F(EmsDumperSystemTest, GetTag004, TestSize.Level1)
312 {
313     GetTagTest4 getTagTest;
314     std::string result = getTagTest.GetTag();
315     EXPECT_EQ(result, GET_TAG_SYSTEM_TEST4);
316 }
317 
318 /*
319  * @tc.name: GetTag005
320  * @tc.desc: Check GetTagTest5 process GetTag success
321  * @tc.type: FUNC
322  */
323 HWTEST_F(EmsDumperSystemTest, GetTag005, TestSize.Level1)
324 {
325     GetTagTest5 getTagTest;
326     std::string result = getTagTest.GetTag();
327     EXPECT_EQ(result, GET_TAG_SYSTEM_TEST5);
328 }