1 /*
2 * Copyright (C) 2025 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 "monitor_server_unittest.h"
17
18 #include <sys/time.h>
19 #include <string>
20 #include <unistd.h>
21 #include "monitor_server_object.h"
22 #include "media_log.h"
23 #include "media_errors.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::Media;
27 namespace OHOS {
28 namespace Media {
29 static const int32_t PID_TEST = 10;
SetUpTestCase(void)30 void PlayerMonitorServerTest::SetUpTestCase(void) {}
31
TearDownTestCase(void)32 void PlayerMonitorServerTest::TearDownTestCase(void) {}
33
SetUp(void)34 void PlayerMonitorServerTest::SetUp(void)
35 {
36 testPtr = std::make_shared<MonitorServer>();
37 }
38
TearDown(void)39 void PlayerMonitorServerTest::TearDown(void)
40 {
41 testPtr = nullptr;
42 }
43
44 /**
45 * @tc.name: EnableMonitorUnitTest_001
46 * @tc.desc: EnableMonitorUnitTest_001
47 * @tc.type: Test EnableMonitor interface
48 */
49 HWTEST_F(PlayerMonitorServerTest, EnableMonitorUnitTest_001, TestSize.Level0)
50 {
51 ASSERT_NE(testPtr, nullptr);
52 int32_t pid = PID_TEST;
53 MonitorServer::TimeInfo timeInfo(PID_TEST, true);
54 testPtr->timesMap_.insert(std::pair<int32_t, MonitorServer::TimeInfo>(pid, timeInfo));
55 testPtr->threadRunning_ = true;
56 int32_t ret = testPtr->EnableMonitor(pid);
57 EXPECT_EQ(ret, 0);
58 }
59
60 /**
61 * @tc.name: ObjCtrlUnitTest_001
62 * @tc.desc: ObjCtrlUnitTest_001
63 * @tc.type: Test ObjCtrl interface
64 */
65 HWTEST_F(PlayerMonitorServerTest, ObjCtrlUnitTest_001, TestSize.Level0)
66 {
67 ASSERT_NE(testPtr, nullptr);
68 OHOS::wptr<MonitorServerObject> weakObj1;
69 OHOS::wptr<MonitorServerObject> weakObj2;
70 std::list<wptr<MonitorServerObject>> recoveryList;
71 recoveryList.push_back(weakObj1);
72 std::list<wptr<MonitorServerObject>> abnormalList;
73 abnormalList.push_back(weakObj2);
74 int32_t ret = testPtr->ObjCtrl(recoveryList, abnormalList);
75 EXPECT_EQ(ret, 0);
76 weakObj1 = nullptr;
77 weakObj2 = nullptr;
78 }
79
80 /**
81 * @tc.name: GetObjListByPidUnitTest_001
82 * @tc.desc: GetObjListByPidUnitTest_001
83 * @tc.type: Test GetObjListByPid interface
84 */
85 HWTEST_F(PlayerMonitorServerTest, GetObjListByPidUnitTest_001, TestSize.Level0)
86 {
87 ASSERT_NE(testPtr, nullptr);
88 int32_t pid = PID_TEST;
89 OHOS::wptr<OHOS::Media::MonitorServerObject> weakObj1;
90 std::list<wptr<MonitorServerObject>> recoveryList;
91 recoveryList.push_back(weakObj1);
92 testPtr->objListMap_.insert(std::pair<int32_t, std::list<wptr<MonitorServerObject>>>(pid, recoveryList));
93 int32_t ret = testPtr->GetObjListByPid(pid, recoveryList);
94 EXPECT_EQ(ret, 0);
95 weakObj1 = nullptr;
96 }
97
98 /**
99 * @tc.name: GetWaitTimeUnitTest_001
100 * @tc.desc: GetWaitTimeUnitTest_001
101 * @tc.type: Test GetWaitTime interface
102 */
103 HWTEST_F(PlayerMonitorServerTest, GetWaitTimeUnitTest_001, TestSize.Level0)
104 {
105 ASSERT_NE(testPtr, nullptr);
106 int32_t pid = PID_TEST;
107 MonitorServer::TimeInfo timeInfo(PID_TEST, true);
108 testPtr->timesMap_.insert(std::pair<int32_t, MonitorServer::TimeInfo>(pid, timeInfo));
109 int32_t ret = testPtr->GetWaitTime();
110 EXPECT_EQ(ret, PID_TEST);
111 }
112
113 /**
114 * @tc.name: ObjectIpcAbnormalityUnitTest_001
115 * @tc.desc: ObjectIpcAbnormalityUnitTest_001
116 * @tc.type: Test IpcAbnormality interface
117 */
118 HWTEST_F(PlayerMonitorServerTest, ObjectIpcAbnormalityUnitTest_001, TestSize.Level0)
119 {
120 std::shared_ptr<MonitorServerObject> testPtr2 = std::make_shared<TestUser>();
121 ASSERT_NE(testPtr2, nullptr);
122 testPtr2->alarmed_ = true;
123 int32_t ret = -1;
124 ret = testPtr2->IpcAbnormality();
125 EXPECT_NE(ret, -1);
126 testPtr2 = nullptr;
127 }
128
129 /**
130 * @tc.name: ObjectIpcRecoveryUnitTest_001
131 * @tc.desc: ObjectIpcRecoveryUnitTest_001
132 * @tc.type: Test IpcRecovery interface
133 */
134 HWTEST_F(PlayerMonitorServerTest, ObjectIpcRecoveryUnitTest_001, TestSize.Level0)
135 {
136 std::shared_ptr<MonitorServerObject> testPtr2 = std::make_shared<TestUser>();
137 ASSERT_NE(testPtr2, nullptr);
138 testPtr2->alarmed_ = false;
139 bool fromMonitor = true;
140 int32_t ret = -1;
141 ret = testPtr2->IpcRecovery(fromMonitor);
142 EXPECT_NE(ret, -1);
143 testPtr2 = nullptr;
144 }
145
146 /**
147 * @tc.name: ObjectIpcAlarmedFlagUnitTest
148 * @tc.desc: ObjectIpcAlarmedFlagUnitTest
149 * @tc.type: Test SetIpcAlarmedFlag interface
150 */
151 HWTEST_F(PlayerMonitorServerTest, ObjectIpcAlarmedFlagUnitTest_001, TestSize.Level0)
152 {
153 std::shared_ptr<MonitorServerObject> testPtr2 = std::make_shared<TestUser>();
154 ASSERT_NE(testPtr2, nullptr);
155 testPtr2->SetIpcAlarmedFlag();
156 EXPECT_NE(testPtr2->alarmed_, false);
157 testPtr2->UnSetIpcAlarmedFlag();
158 EXPECT_EQ(testPtr2->alarmed_, false);
159 testPtr2 = nullptr;
160 }
161 } // Media
162 } // OHOS
163