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 <functional>
17 #include <gtest/gtest.h>
18 
19 #include "watchdog.h"
20 #include "work_policy_manager.h"
21 #include "work_scheduler_service.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace WorkScheduler {
27 
28 class WatchdogTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34     static std::shared_ptr<Watchdog> watchdog_;
35 };
36 
37 std::shared_ptr<Watchdog> WatchdogTest::watchdog_ = nullptr;
38 
SetUpTestCase()39 void WatchdogTest::SetUpTestCase()
40 {
41     std::shared_ptr<WorkSchedulerService> workSchedulerService = DelayedSingleton<WorkSchedulerService>::GetInstance();
42     std::shared_ptr<AppExecFwk::EventRunner> runner;
43     watchdog_ = std::make_shared<Watchdog>(workSchedulerService->GetWorkPolicyManager(), runner);
44 }
45 
46 /**
47  * @tc.name: watchdog_001
48  * @tc.desc: Test Watchdog AddWatchdog.
49  * @tc.type: FUNC
50  * @tc.require: I8JBRY
51  */
52 HWTEST_F(WatchdogTest, watchdog_001, TestSize.Level1)
53 {
54     bool result = watchdog_->AddWatchdog(1, 1);
55     EXPECT_EQ(result, false);
56 }
57 
58 /**
59  * @tc.name: watchdog_002
60  * @tc.desc: Test Watchdog RemoveWatchdog.
61  * @tc.type: FUNC
62  * @tc.require: I8JBRY
63  */
64 HWTEST_F(WatchdogTest, watchdog_002, TestSize.Level1)
65 {
66     bool result = watchdog_->AddWatchdog(1, 1);
67     EXPECT_EQ(result, false);
68     watchdog_->RemoveWatchdog(1);
69 }
70 
71 /**
72  * @tc.name: watchdog_003
73  * @tc.desc: Test Watchdog ProcessEvent.
74  * @tc.type: FUNC
75  * @tc.require: I8JBRY
76  */
77 HWTEST_F(WatchdogTest, watchdog_003, TestSize.Level1)
78 {
79     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(0);
80     event = nullptr;
81     watchdog_->ProcessEvent(event);
82     EXPECT_TRUE(event == nullptr);
83 }
84 
85 /**
86  * @tc.name: watchdog_004
87  * @tc.desc: Test Watchdog ProcessEvent.
88  * @tc.type: FUNC
89  * @tc.require: I8JBRY
90  */
91 HWTEST_F(WatchdogTest, watchdog_004, TestSize.Level1)
92 {
93     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(0);
94     watchdog_->ProcessEvent(event);
95     EXPECT_TRUE(event->GetInnerEventId() == 0);
96 }
97 
98 /**
99  * @tc.name: watchdog_005
100  * @tc.desc: Test Watchdog ProcessEvent.
101  * @tc.type: FUNC
102  * @tc.require: I8JBRY
103  */
104 HWTEST_F(WatchdogTest, watchdog_005, TestSize.Level1)
105 {
106     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(0);
107     watchdog_->service_ = nullptr;
108     watchdog_->ProcessEvent(event);
109     std::shared_ptr<WorkPolicyManager> service;
110     watchdog_->service_ = service;
111     EXPECT_TRUE(event->GetInnerEventId() == 0);
112 }
113 
114 }
115 }