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