1 /*
2  * Copyright (c) 2022-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 <gtest/gtest.h>
17 #include <thread>
18 
19 #define private public
20 #define protected public
21 #include "watchdog.h"
22 #undef private
23 #undef protected
24 
25 #include "main_thread.h"
26 #include "mock_app_thread.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 constexpr int64_t TEST_INTERVAL_TIME = 5000;
35 class WatchdogTest : public testing::Test {
36 public:
WatchdogTest()37     WatchdogTest()
38     {}
~WatchdogTest()39     ~WatchdogTest()
40     {}
41     std::shared_ptr<MockHandler> mockHandler_ = nullptr;
42     std::shared_ptr<EventRunner> runner_ = nullptr;
43     std::shared_ptr<Watchdog> watchdog_ = nullptr;
44     static void SetUpTestCase(void);
45     static void TearDownTestCase(void);
46     void SetUp();
47     void TearDown();
48 };
49 
SetUpTestCase(void)50 void WatchdogTest::SetUpTestCase(void)
51 {}
52 
TearDownTestCase(void)53 void WatchdogTest::TearDownTestCase(void)
54 {}
55 
SetUp(void)56 void WatchdogTest::SetUp(void)
57 {
58     runner_ = EventRunner::Create("");
59     mockHandler_ = std::make_shared<MockHandler>(runner_);
60 
61     watchdog_ = std::make_shared<Watchdog>();
62     watchdog_->Init(mockHandler_);
63 }
64 
TearDown(void)65 void WatchdogTest::TearDown(void)
66 {
67     watchdog_->Stop();
68 }
69 
70 /**
71  * @tc.number: AppExecFwk_watchdog_IsReportEvent_0001
72  * @tc.name: IsReportEvent
73  * @tc.desc: Test the abnormal state of IsReportEvent.
74  * @tc.require: issueI5MGFU
75  */
76 HWTEST_F(WatchdogTest, AppExecFwk_watchdog_IsReportEvent_0001, Function | MediumTest | Level3)
77 {
78     watchdog_->SetAppMainThreadState(false);
79     bool ret = watchdog_->IsReportEvent();
80     EXPECT_TRUE(ret);
81 }
82 
83 /**
84  * @tc.number: AppExecFwk_watchdog_IsReportEvent_0002
85  * @tc.name: IsReportEvent
86  * @tc.desc: Test the change state of IsReportEvent.
87  * @tc.require: issueI5MGFU
88  */
89 HWTEST_F(WatchdogTest, AppExecFwk_watchdog_IsReportEvent_0002, Function | MediumTest | Level3)
90 {
91     watchdog_->SetAppMainThreadState(true);
92     watchdog_->AllowReportEvent();
93     bool ret = watchdog_->IsReportEvent();
94     EXPECT_FALSE(ret);
95 }
96 
97 /**
98  * @tc.number: AppExecFwk_watchdog_ReportEvent_0001
99  * @tc.name: ReportEvent
100  * @tc.desc: Test ReportEvent.
101  * @tc.require: I5UL6H
102  */
103 HWTEST_F(WatchdogTest, AppExecFwk_watchdog_ReportEvent_0001, Function | MediumTest | Level3)
104 {
105     // be ready for ReportEvent
106     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::
107         steady_clock::now().time_since_epoch()).count() - TEST_INTERVAL_TIME;
108     watchdog_->needReport_ = true;
109 
110     watchdog_->isSixSecondEvent_.store(true);
111 
112     watchdog_->ReportEvent();
113     EXPECT_TRUE(1);
114 }
115 
116 /**
117  * @tc.number: AppExecFwk_watchdog_ReportEvent_0002
118  * @tc.name: ReportEvent
119  * @tc.desc: Test ReportEvent.
120  * @tc.require: I5UL6H
121  */
122 HWTEST_F(WatchdogTest, AppExecFwk_watchdog_ReportEvent_0002, Function | MediumTest | Level3)
123 {
124     // be ready for ReportEvent
125     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::
126         system_clock::now().time_since_epoch()).count() - TEST_INTERVAL_TIME;
127     watchdog_->needReport_ = true;
128 
129     watchdog_->isSixSecondEvent_.store(false);
130 
131     watchdog_->ReportEvent();
132     EXPECT_TRUE(1);
133 }
134 
135 /**
136  * @tc.number: WatchdogTest_Init_001
137  * @tc.name: Init
138  * @tc.desc: Verify that function Init.
139  */
140 HWTEST_F(WatchdogTest, WatchdogTest_Init_001, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "WatchdogTest_Init_001 start";
143     std::shared_ptr<EventHandler> eventHandler = std::make_shared<EventHandler>();
144     watchdog_->Init(eventHandler);
145     EXPECT_TRUE(watchdog_->appMainHandler_ != nullptr);
146     GTEST_LOG_(INFO) << "WatchdogTest_Init_001 end";
147 }
148 
149 /**
150  * @tc.number: WatchdogTest_Init_002
151  * @tc.name: Init
152  * @tc.desc: Verify that function Init.
153  */
154 HWTEST_F(WatchdogTest, WatchdogTest_Init_002, TestSize.Level1)
155 {
156     GTEST_LOG_(INFO) << "WatchdogTest_Init_002 start";
157     std::shared_ptr<EventHandler> eventHandler = nullptr;
158     watchdog_->lastWatchTime_ = 2;
159     watchdog_->Init(eventHandler);
160     EXPECT_EQ(watchdog_->lastWatchTime_, 0);
161     GTEST_LOG_(INFO) << "WatchdogTest_Init_002 end";
162 }
163 
164 /**
165  * @tc.number: WatchdogTest_Stop_001
166  * @tc.name: Stop
167  * @tc.desc: Verify that function Init.
168  */
169 HWTEST_F(WatchdogTest, WatchdogTest_Stop_001, TestSize.Level1)
170 {
171     GTEST_LOG_(INFO) << "WatchdogTest_Stop_001 start";
172     std::shared_ptr<EventHandler> eventHandler = std::make_shared<EventHandler>();
173     watchdog_->Init(eventHandler);
174     EXPECT_TRUE(watchdog_->appMainHandler_ != nullptr);
175     watchdog_->Stop();
176     EXPECT_TRUE(watchdog_->appMainHandler_ == nullptr);
177     GTEST_LOG_(INFO) << "WatchdogTest_Stop_001 end";
178 }
179 
180 /**
181  * @tc.number: WatchdogTest_Stop_002
182  * @tc.name: Stop
183  * @tc.desc: Verify that function Stop.
184  */
185 HWTEST_F(WatchdogTest, WatchdogTest_Stop_002, TestSize.Level1)
186 {
187     GTEST_LOG_(INFO) << "WatchdogTest_Stop_002 start";
188     std::shared_ptr<EventHandler> eventHandler = nullptr;
189     watchdog_->Stop();
190     EXPECT_TRUE(watchdog_->stopWatchdog_);
191     GTEST_LOG_(INFO) << "WatchdogTest_Stop_002 end";
192 }
193 
194 /**
195  * @tc.number: WatchdogTest_SetAppMainThreadState_001
196  * @tc.name: SetAppMainThreadState
197  * @tc.desc: Verify that function SetAppMainThreadState.
198  */
199 HWTEST_F(WatchdogTest, WatchdogTest_SetAppMainThreadState_001, TestSize.Level1)
200 {
201     GTEST_LOG_(INFO) << "WatchdogTest_SetAppMainThreadState_001 start";
202     bool appMainThreadState = true;
203     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
204     watchdog_->SetAppMainThreadState(appMainThreadState);
205     EXPECT_TRUE(watchdog_->appMainThreadIsAlive_);
206     GTEST_LOG_(INFO) << "WatchdogTest_SetAppMainThreadState_001 end";
207 }
208 
209 /**
210  * @tc.number: WatchdogTest_SetBackgroundStatus_001
211  * @tc.name: SetBackgroundStatus
212  * @tc.desc: Verify that function SetBackgroundStatus.
213  */
214 HWTEST_F(WatchdogTest, WatchdogTest_SetBackgroundStatus_001, TestSize.Level1)
215 {
216     GTEST_LOG_(INFO) << "WatchdogTest_SetBackgroundStatus_001 start";
217     bool isInBackground = false;
218     EXPECT_TRUE(watchdog_->isInBackground_);
219     watchdog_->SetBackgroundStatus(isInBackground);
220     EXPECT_FALSE(watchdog_->isInBackground_);
221     GTEST_LOG_(INFO) << "WatchdogTest_SetBackgroundStatus_001 end";
222 }
223 
224 /**
225  * @tc.number: WatchdogTest_AllowReportEvent_001
226  * @tc.name: AllowReportEvent
227  * @tc.desc: Verify that function AllowReportEvent.
228  */
229 HWTEST_F(WatchdogTest, WatchdogTest_AllowReportEvent_001, TestSize.Level1)
230 {
231     GTEST_LOG_(INFO) << "WatchdogTest_AllowReportEvent_001 start";
232     watchdog_->needReport_ = false;
233     watchdog_->AllowReportEvent();
234     EXPECT_TRUE(watchdog_->needReport_);
235     GTEST_LOG_(INFO) << "WatchdogTest_AllowReportEvent_001 end";
236 }
237 
238 /**
239  * @tc.number: WatchdogTest_IsReportEvent_001
240  * @tc.name: IsReportEvent
241  * @tc.desc: Verify that function IsReportEvent.
242  */
243 HWTEST_F(WatchdogTest, WatchdogTest_IsReportEvent_003, TestSize.Level1)
244 {
245     GTEST_LOG_(INFO) << "WatchdogTest_IsReportEvent_003 start";
246     watchdog_->SetAppMainThreadState(false);
247     auto result = watchdog_->IsReportEvent();
248     EXPECT_TRUE(result);
249     GTEST_LOG_(INFO) << "WatchdogTest_IsReportEvent_003 end";
250 }
251 
252 /**
253  * @tc.number: WatchdogTest_IsReportEvent_002
254  * @tc.name: IsReportEvent
255  * @tc.desc: Verify that function IsReportEvent.
256  */
257 HWTEST_F(WatchdogTest, WatchdogTest_IsReportEvent_004, TestSize.Level1)
258 {
259     GTEST_LOG_(INFO) << "WatchdogTest_IsReportEvent_004 start";
260     bool appMainThreadState = true;
261     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
262     watchdog_->SetAppMainThreadState(appMainThreadState);
263     auto result = watchdog_->IsReportEvent();
264     EXPECT_FALSE(result);
265     GTEST_LOG_(INFO) << "WatchdogTest_IsReportEvent_004 end";
266 }
267 
268 /**
269  * @tc.number: WatchdogTest_IsStopwatchdog_001
270  * @tc.name: IsStopWatchdog
271  * @tc.desc: Verify that function IsStopWatchdog.
272  */
273 HWTEST_F(WatchdogTest, WatchdogTest_IsStopwatchdog_001, TestSize.Level1)
274 {
275     GTEST_LOG_(INFO) << "WatchdogTest_IsStopwatchdog_001 start";
276     auto result = watchdog_->IsStopWatchdog();
277     EXPECT_FALSE(result);
278     GTEST_LOG_(INFO) << "WatchdogTest_IsStopwatchdog_001 end";
279 }
280 
281 /**
282  * @tc.number: WatchdogTest_Timer_001
283  * @tc.name: Timer
284  * @tc.desc: Verify that function Timer.
285  */
286 HWTEST_F(WatchdogTest, WatchdogTest_Timer_001, TestSize.Level1)
287 {
288     GTEST_LOG_(INFO) << "WatchdogTest_Timer_001 start";
289     watchdog_->needReport_ = false;
290     watchdog_->Timer();
291     EXPECT_TRUE(!watchdog_->needReport_);
292     GTEST_LOG_(INFO) << "WatchdogTest_Timer_001 end";
293 }
294 
295 /**
296  * @tc.number: WatchdogTest_Timer_002
297  * @tc.name: Timer
298  * @tc.desc: Verify that function Timer.
299  */
300 HWTEST_F(WatchdogTest, WatchdogTest_Timer_002, TestSize.Level1)
301 {
302     GTEST_LOG_(INFO) << "WatchdogTest_Timer_002 start";
303     bool isInBackground = true;
304     EXPECT_TRUE(watchdog_->isInBackground_);
305     watchdog_->SetBackgroundStatus(isInBackground);
306     watchdog_->SetAppMainThreadState(true);
307     watchdog_->Timer();
308     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
309     GTEST_LOG_(INFO) << "WatchdogTest_Timer_002 end";
310 }
311 
312 /**
313  * @tc.number: WatchdogTest_Timer_003
314  * @tc.name: Timer
315  * @tc.desc: Verify that function Timer.
316  */
317 HWTEST_F(WatchdogTest, WatchdogTest_Timer_003, TestSize.Level1)
318 {
319     GTEST_LOG_(INFO) << "WatchdogTest_Timer_003 start";
320     bool appMainThreadState = true;
321     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
322     watchdog_->SetAppMainThreadState(appMainThreadState);
323     watchdog_->Timer();
324     bool isInBackground = false;
325     watchdog_->SetBackgroundStatus(isInBackground);
326     watchdog_->Timer();
327     watchdog_->stopWatchdog_ = true;
328     watchdog_->Timer();
329     EXPECT_TRUE(watchdog_->needReport_);
330     EXPECT_FALSE(watchdog_->isInBackground_);
331     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
332     GTEST_LOG_(INFO) << "WatchdogTest_Timer_003 end";
333 }
334 
335 /**
336  * @tc.number: WatchdogTest_Timer_004
337  * @tc.name: Timer
338  * @tc.desc: Verify that function Timer.
339  */
340 HWTEST_F(WatchdogTest, WatchdogTest_Timer_004, TestSize.Level1)
341 {
342     GTEST_LOG_(INFO) << "WatchdogTest_Timer_004 start";
343     bool appMainThreadState = true;
344     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
345     watchdog_->SetAppMainThreadState(appMainThreadState);
346     bool isInBackground = false;
347     watchdog_->SetBackgroundStatus(isInBackground);
348     watchdog_->appMainHandler_ = std::make_shared<EventHandler>();
349     watchdog_->Timer();
350     EXPECT_TRUE(watchdog_->needReport_);
351     EXPECT_FALSE(watchdog_->isInBackground_);
352     EXPECT_FALSE(watchdog_->appMainThreadIsAlive_);
353     EXPECT_TRUE(watchdog_->appMainHandler_ != nullptr);
354     GTEST_LOG_(INFO) << "WatchdogTest_Timer_004 end";
355 }
356 
357 /**
358  * @tc.number: WatchdogTest_ReportEvent_003
359  * @tc.name: ReportEvent
360  * @tc.desc: Verify that function ReportEvent.
361  */
362 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_003, TestSize.Level1)
363 {
364     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_003 start";
365     watchdog_->ReportEvent();
366     EXPECT_FALSE(watchdog_->isSixSecondEvent_);
367     EXPECT_TRUE(watchdog_->needReport_);
368     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_003 end";
369 }
370 
371 /**
372  * @tc.number: WatchdogTest_ReportEvent_004
373  * @tc.name: ReportEvent
374  * @tc.desc: Verify that function ReportEvent.
375  */
376 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_004, TestSize.Level1)
377 {
378     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_002 start";
379     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::microseconds>(
380         std::chrono::steady_clock::now().time_since_epoch()).count();
381     watchdog_->ReportEvent();
382     EXPECT_TRUE(watchdog_->appMainHandler_ != nullptr);
383     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_002 end";
384 }
385 
386 /**
387  * @tc.number: WatchdogTest_ReportEvent_005
388  * @tc.name: ReportEvent
389  * @tc.desc: Verify that function ReportEvent.
390  */
391 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_005, TestSize.Level1)
392 {
393     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_005 start";
394     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::microseconds>(
395         std::chrono::steady_clock::now().time_since_epoch()).count();
396     watchdog_->needReport_ = false;
397     watchdog_->ReportEvent();
398     EXPECT_TRUE(watchdog_->needReport_ == false);
399     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_005 end";
400 }
401 
402 /**
403  * @tc.number: WatchdogTest_ReportEvent_006
404  * @tc.name: ReportEvent
405  * @tc.desc: Verify that function ReportEvent.
406  */
407 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_006, TestSize.Level1)
408 {
409     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_006 start";
410     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::microseconds>(
411         std::chrono::steady_clock::now().time_since_epoch()).count();
412     watchdog_->isSixSecondEvent_ = true;
413     watchdog_->ReportEvent();
414     EXPECT_TRUE(watchdog_->needReport_);
415     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_006 end";
416 }
417 
418 /**
419  * @tc.number: WatchdogTest_ReportEvent_007
420  * @tc.name: ReportEvent
421  * @tc.desc: Verify that function ReportEvent.
422  */
423 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_007, TestSize.Level1)
424 {
425     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_007 start";
426     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::microseconds>(
427         std::chrono::steady_clock::now().time_since_epoch()).count();
428     watchdog_->ReportEvent();
429     EXPECT_FALSE(watchdog_->isSixSecondEvent_);
430     GTEST_LOG_(INFO) << "WatchdogTest_ReportEvent_007 end";
431 }
432 
433 /**
434  * @tc.number: WatchdogTest_ReportEvent_008
435  * @tc.name: ReportEvent
436  * @tc.desc: Verify that function ReportEvent.
437  */
438 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_008, TestSize.Level1)
439 {
440     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::
441         system_clock::now().time_since_epoch()).count() - TEST_INTERVAL_TIME;
442     std::shared_ptr<EventHandler> eventHandler = std::make_shared<EventHandler>();
443     watchdog_->needReport_ = true;
444     watchdog_->isSixSecondEvent_.store(true);
445     EXPECT_TRUE(watchdog_->needReport_);
446     watchdog_->ReportEvent();
447 }
448 
449 /**
450  * @tc.number: WatchdogTest_ReportEvent_009
451  * @tc.name: ReportEvent
452  * @tc.desc: Verify that function ReportEvent.
453  */
454 HWTEST_F(WatchdogTest, WatchdogTest_ReportEvent_009, TestSize.Level1)
455 {
456     watchdog_->lastWatchTime_ = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::
457         system_clock::now().time_since_epoch()).count() - TEST_INTERVAL_TIME;
458     watchdog_->isInBackground_ = true;
459     watchdog_->backgroundReportCount_ = 0;
460     watchdog_->ReportEvent();
461     EXPECT_EQ(watchdog_->backgroundReportCount_, 1);
462     watchdog_->SetBundleInfo("test", "1.1.0");
463     watchdog_->SetBgWorkingThreadStatus(false);
464 }
465 }  // namespace AppExecFwk
466 }  // namespace OHOS
467