• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdlib>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <thread>
20 #include <unistd.h>
21 #include <chrono>
22 
23 #include "watchdog.h"
24 #include "hicollie.h"
25 
26 using namespace testing::ext;
27 
28 namespace {
29 class HiCollieTest : public ::testing::Test {
30 public:
SetUp()31     void SetUp() override {}
TearDown()32     void TearDown() override {}
33 };
34 
TaskTest()35 void TaskTest()
36 {
37     printf("TaskTest");
38 }
39 
InitBeginFunc(const char * eventName)40 void InitBeginFunc(const char* eventName)
41 {
42     std::string str(eventName);
43     printf("InitBeginFunc eventName: %s\n", str.c_str());
44 }
45 
InitEndFunc(const char * eventName)46 void InitEndFunc(const char* eventName)
47 {
48     std::string str(eventName);
49     printf("InitBeginFunc eventName: %s\n", str.c_str());
50 }
51 
52 /**
53  * @tc.name: OH_HiCollie_Init_StuckDetection
54  * @tc.desc: test OH_HiCollie_Init_StuckDetection
55  * @tc.type: FUNC
56  */
57 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Init_StuckDetection_1, TestSize.Level1)
58 {
59     int result = OH_HiCollie_Init_StuckDetection(&TaskTest);
60     EXPECT_EQ(result, HICOLLIE_SUCCESS);
61 }
62 
63 /**
64  * @tc.name: OH_HiCollie_Init_StuckDetection
65  * @tc.desc: test OH_HiCollie_Init_StuckDetection
66  * @tc.type: FUNC
67  */
68 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Init_StuckDetection_2, TestSize.Level1)
69 {
70     int result = OH_HiCollie_Init_StuckDetection(nullptr);
71     EXPECT_EQ(result, HICOLLIE_SUCCESS);
72 }
73 
74 /**
75  * @tc.name: OH_HiCollie_Init_StuckDetectionWithTimeout
76  * @tc.desc: test OH_HiCollie_Init_StuckDetectionWithTimeout
77  * @tc.type: FUNC
78  */
79 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Init_StuckDetectionWithTimeout_1, TestSize.Level0)
80 {
81     int result = OH_HiCollie_Init_StuckDetectionWithTimeout(&TaskTest, 5);
82     EXPECT_EQ(result, HICOLLIE_SUCCESS);
83     result = OH_HiCollie_Init_StuckDetection(&TaskTest);
84     EXPECT_EQ(result, HICOLLIE_SUCCESS);
85 }
86 
87 /**
88  * @tc.name: OH_HiCollie_Init_StuckDetectionWithTimeout
89  * @tc.desc: test OH_HiCollie_Init_StuckDetectionWithTimeout
90  * @tc.type: FUNC
91  */
92 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Init_StuckDetectionWithTimeout_2, TestSize.Level1)
93 {
94     int result = OH_HiCollie_Init_StuckDetectionWithTimeout(nullptr, 10);
95     EXPECT_EQ(result, HICOLLIE_SUCCESS);
96 }
97 
98 /**
99  * @tc.name: OH_HiCollie_Init_StuckDetectionWithTimeout
100  * @tc.desc: test OH_HiCollie_Init_StuckDetectionWithTimeout
101  * @tc.type: FUNC
102  */
103 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Init_StuckDetectionWithTimeout_3, TestSize.Level1)
104 {
105     int result = OH_HiCollie_Init_StuckDetectionWithTimeout(&TaskTest, 1);
106     EXPECT_EQ(result, HICOLLIE_INVALID_ARGUMENT);
107     result = OH_HiCollie_Init_StuckDetectionWithTimeout(&TaskTest, 16);
108     EXPECT_EQ(result, HICOLLIE_INVALID_ARGUMENT);
109 }
110 
111 /**
112  * @tc.name: OH_HiCollie_Init_JankDetection
113  * @tc.desc: test OH_HiCollie_Init_JankDetection
114  * @tc.type: FUNC
115  */
116 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Init_JankDetection_1, TestSize.Level1)
117 {
118     OH_HiCollie_BeginFunc begin_ = InitBeginFunc;
119     OH_HiCollie_EndFunc end_ = InitEndFunc;
120     HiCollie_DetectionParam param {
121         .sampleStackTriggerTime = 150,
122         .reserved = 0,
123     };
124     int result = OH_HiCollie_Init_JankDetection(&begin_, &end_, param);
125     EXPECT_EQ(result, HICOLLIE_SUCCESS);
126     result = OH_HiCollie_Init_JankDetection(nullptr, nullptr, param);
127     EXPECT_EQ(result, HICOLLIE_SUCCESS);
128 }
129 
130 /**
131  * @tc.name: OH_HiCollie_Report
132  * @tc.desc: test OH_HiCollie_Report
133  * @tc.type: FUNC
134  */
135 HWTEST_F(HiCollieTest, Test_OH_HiCollie_Report_1, TestSize.Level1)
136 {
137     bool isSixSecond = false;
138     OHOS::HiviewDFX::Watchdog::GetInstance().SetForeground(true);
139     int result = OH_HiCollie_Report(&isSixSecond);
140     printf("OH_HiCollie_Report result: %d\n", result);
141     EXPECT_TRUE(isSixSecond);
142     result = OH_HiCollie_Report(&isSixSecond);
143     printf("OH_HiCollie_Report result: %d\n", result);
144     EXPECT_FALSE(isSixSecond);
145     result = OH_HiCollie_Report(nullptr);
146     EXPECT_EQ(result, HICOLLIE_INVALID_ARGUMENT);
147     OHOS::HiviewDFX::Watchdog::GetInstance().SetAppDebug(true);
148     result = OH_HiCollie_Report(&isSixSecond);
149     EXPECT_EQ(result, HICOLLIE_SUCCESS);
150 }
151 
152 /**
153  * @tc.name: OH_HiCollie_SetTimer
154  * @tc.desc: test OH_HiCollie_SetTimer
155  * @tc.type: FUNC
156  */
157 HWTEST_F(HiCollieTest, Test_OH_HiCollie_SetTimer_1, TestSize.Level1)
158 {
159     int id;
160     HiCollie_SetTimerParam param = {nullptr, 1, nullptr, nullptr, HiCollie_Flag::HICOLLIE_FLAG_NOOP};
161     HiCollie_ErrorCode errCode = OH_HiCollie_SetTimer(param, &id);
162     EXPECT_EQ(errCode, HICOLLIE_INVALID_TIMER_NAME);
163     param = {"testSetTimer", 0, nullptr, nullptr, HiCollie_Flag::HICOLLIE_FLAG_NOOP};
164     errCode = OH_HiCollie_SetTimer(param, &id);
165     EXPECT_EQ(errCode, HICOLLIE_INVALID_TIMEOUT_VALUE);
166     param = {"testSetTimer", 1, nullptr, nullptr, HiCollie_Flag::HICOLLIE_FLAG_NOOP};
167     errCode = OH_HiCollie_SetTimer(param, nullptr);
168     EXPECT_EQ(errCode, HICOLLIE_WRONG_TIMER_ID_OUTPUT_PARAM);
169     param = {"testSetTimer", 1, nullptr, nullptr, HiCollie_Flag::HICOLLIE_FLAG_NOOP};
170     errCode = OH_HiCollie_SetTimer(param, &id);
171     EXPECT_FALSE(errCode == HICOLLIE_WRONG_PROCESS_CONTEXT);
172     EXPECT_EQ(errCode, HICOLLIE_SUCCESS);
173     printf("OH_HiCollie_SetTimer id: %d\n", id);
174     EXPECT_TRUE(id > 0);
175 }
176 
177 /**
178  * @tc.name: OH_HiCollie_CancelTimer
179  * @tc.desc: test OH_HiCollie_CancelTimer
180  * @tc.type: FUNC
181  */
182 HWTEST_F(HiCollieTest, Test_OH_HiCollie_CancelTimer_1, TestSize.Level1)
183 {
184     bool flag = true;
185     int id = 2025;
186     OH_HiCollie_CancelTimer(id);
187     EXPECT_TRUE(flag);
188 }
189 } // namespace
190