1 /*
2  * Copyright (c) 2024 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 "mmi_log.h"
19 #include "watchdog_task.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "WatchdogTaskTest"
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 using namespace testing::ext;
27 } // namespace
28 
29 class WatchdogTaskTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
33 };
34 
35 /**
36  * @tc.name: WatchdogTaskTest_GetFirstLine_001
37  * @tc.desc: Test the function GetFirstLine
38  * @tc.type: FUNC
39  * @tc.require:
40  */
41 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_GetFirstLine_001, TestSize.Level1)
42 {
43     CALL_TEST_DEBUG;
44     WatchdogTask watchdogtask;
45     std::string path;
46     auto ret = watchdogtask.GetFirstLine(path);
47     EXPECT_EQ(ret, path);
48 }
49 
50 /**
51  * @tc.name: WatchdogTaskTest_GetProcessNameFromProcCmdline_001
52  * @tc.desc: Test the function GetProcessNameFromProcCmdline
53  * @tc.type: FUNC
54  * @tc.require:
55  */
56 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_GetProcessNameFromProcCmdline_001, TestSize.Level1)
57 {
58     CALL_TEST_DEBUG;
59     WatchdogTask watchdogtask;
60     std::string expectedDescription;
61     int32_t pid = -1;
62     auto ret = watchdogtask.GetProcessNameFromProcCmdline(pid);
63     EXPECT_EQ(ret, expectedDescription);
64 }
65 
66 /**
67  * @tc.name: WatchdogTaskTest_IsNumberic_001
68  * @tc.desc: Test the function IsNumberic
69  * @tc.type: FUNC
70  * @tc.require:
71  */
72 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_IsNumberic_001, TestSize.Level1)
73 {
74     CALL_TEST_DEBUG;
75     WatchdogTask watchdogtask;
76     std::string testString = "12345";
77     bool ret = watchdogtask.IsNumberic(testString);
78     EXPECT_TRUE(ret);
79 }
80 
81 /**
82  * @tc.name: WatchdogTaskTest_IsProcessDebug_001
83  * @tc.desc: Test the function IsProcessDebug
84  * @tc.type: FUNC
85  * @tc.require:
86  */
87 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_IsProcessDebug_001, TestSize.Level1)
88 {
89     CALL_TEST_DEBUG;
90     WatchdogTask watchdogtask;
91     int32_t pid = 1;
92     bool ret = watchdogtask.IsProcessDebug(pid);
93     EXPECT_FALSE(ret);
94 }
95 
96 /**
97  * @tc.name: WatchdogTaskTest_GetBlockDescription_001
98  * @tc.desc: Test the function GetBlockDescription
99  * @tc.type: FUNC
100  * @tc.require:
101  */
102 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_GetBlockDescription_001, TestSize.Level1)
103 {
104     CALL_TEST_DEBUG;
105     WatchdogTask watchdogtask;
106     std::string desc = "Watchdog: thread(mmi_service) blocked 5000s";
107     uint64_t timeInterval = 5000;
108     auto ret = watchdogtask.GetBlockDescription(timeInterval);
109     EXPECT_EQ(ret, desc);
110 }
111 
112 /**
113  * @tc.name: WatchdogTaskTest_GetSelfProcName_001
114  * @tc.desc: Test the function GetSelfProcName
115  * @tc.type: FUNC
116  * @tc.require:
117  */
118 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_GetSelfProcName_001, TestSize.Level1)
119 {
120     CALL_TEST_DEBUG;
121     WatchdogTask watchdogtask;
122     std::string desc = "ut-mmi-service-";
123     auto ret = watchdogtask.GetSelfProcName();
124     EXPECT_NE(ret, desc);
125 }
126 
127 /**
128  * @tc.name: WatchdogTaskTest_SendEvent_001
129  * @tc.desc: Test the function SendEvent
130  * @tc.type: FUNC
131  * @tc.require:
132  */
133 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_SendEvent_001, TestSize.Level1)
134 {
135     CALL_TEST_DEBUG;
136     WatchdogTask watchdogtask;
137     std::string message = "This is a test message";
138     std::string event = "TestEvent";
139     ASSERT_NO_FATAL_FAILURE(watchdogtask.SendEvent(message, event));
140 }
141 } // namespace MMI
142 } // namespace OHOS