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 "disable_task_test.h"
17
18 #include "ffrt.h"
19
20 #include "dh_utils_tool.h"
21 #include "distributed_hardware_errno.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 namespace {
29
30 const std::string DEV_ID_1 = "bb536a637105409e904d4da83790a4a8";
31 const std::string DEV_NETWORK_ID_1 = "nt36a637105409e904d4da83790a4a8";
32 const std::string DEV_DID_1 = "2144a637105409e904d4da83790a4a8";
33
34 const TaskParam TASK_PARAM_1 = {
35 .networkId = DEV_NETWORK_ID_1,
36 .uuid = DEV_ID_1,
37 .udid = DEV_DID_1,
38 .dhId = "",
39 .dhType = DHType::UNKNOWN
40 };
41
42 }
43
SetUpTestCase()44 void DisableTaskTest::SetUpTestCase()
45 {}
46
TearDownTestCase()47 void DisableTaskTest::TearDownTestCase()
48 {}
49
SetUp()50 void DisableTaskTest::SetUp()
51 {
52 auto componentManager = IComponentManager::GetOrCreateInstance();
53 componentManager_ = std::static_pointer_cast<MockComponentManager>(componentManager);
54 auto dhContext = IDHContext::GetOrCreateInstance();
55 dhContext_ = std::static_pointer_cast<MockDHContext>(dhContext);
56 auto utilTool = IDhUtilTool::GetOrCreateInstance();
57 utilTool_ = std::static_pointer_cast<MockDhUtilTool>(utilTool);
58 }
59
TearDown()60 void DisableTaskTest::TearDown()
61 {
62 IComponentManager::ReleaseInstance();
63 componentManager_ = nullptr;
64 IDHContext::ReleaseInstance();
65 dhContext_ = nullptr;
66 IDhUtilTool::ReleaseInstance();
67 utilTool_ = nullptr;
68 }
69
70 HWTEST_F(DisableTaskTest, UnRegisterHardware_001, TestSize.Level0)
71 {
72 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
73 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
74 disableTask->SetCallingUid(1);
75 auto ret = disableTask->UnRegisterHardware();
76 EXPECT_EQ(DH_FWK_SUCCESS, ret);
77 }
78
79 HWTEST_F(DisableTaskTest, UnRegisterHardware_002, TestSize.Level0)
80 {
81 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
82 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
83 disableTask->SetCallingPid(1);
84 auto ret = disableTask->UnRegisterHardware();
85 EXPECT_EQ(DH_FWK_SUCCESS, ret);
86 }
87
88 HWTEST_F(DisableTaskTest, UnRegisterHardware_003, TestSize.Level0)
89 {
90 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
91 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
92 disableTask->SetCallingUid(1);
93 disableTask->SetCallingPid(1);
94 auto ret = disableTask->UnRegisterHardware();
95 EXPECT_EQ(DH_FWK_SUCCESS, ret);
96 }
97
98 HWTEST_F(DisableTaskTest, UnRegisterHardware_004, TestSize.Level0)
99 {
100 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
101 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
102 EXPECT_CALL(*utilTool_, GetLocalUdid()).WillRepeatedly(Return("udid_test"));
103 EXPECT_CALL(*componentManager_, ForceDisableSource(_, _)).Times(1).WillRepeatedly(Return(0));
104 auto ret = disableTask->UnRegisterHardware();
105 EXPECT_EQ(DH_FWK_SUCCESS, ret);
106 }
107
108 HWTEST_F(DisableTaskTest, UnRegisterHardware_005, TestSize.Level0)
109 {
110 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
111 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
112 EXPECT_CALL(*utilTool_, GetLocalUdid()).WillRepeatedly(Return("udid_test"));
113 EXPECT_CALL(*componentManager_, ForceDisableSource(_, _)).Times(1).WillRepeatedly(Return(-1));
114 auto ret = disableTask->UnRegisterHardware();
115 EXPECT_EQ(-1, ret);
116 }
117
118 HWTEST_F(DisableTaskTest, DoAutoDisable_001, TestSize.Level0)
119 {
120 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
121 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
122 EXPECT_CALL(*utilTool_, GetLocalUdid()).WillRepeatedly(Return(DEV_DID_1));
123 EXPECT_CALL(*dhContext_, GetRealTimeOnlineDeviceCount()).Times(1).WillRepeatedly(Return(0));
124 EXPECT_CALL(*dhContext_, GetIsomerismConnectCount()).Times(1).WillRepeatedly(Return(0));
125 EXPECT_CALL(*componentManager_, ForceDisableSink(_)).Times(1).WillRepeatedly(Return(0));
126 auto ret = disableTask->DoAutoDisable();
127 EXPECT_EQ(DH_FWK_SUCCESS, ret);
128 }
129
130 HWTEST_F(DisableTaskTest, DoAutoDisable_002, TestSize.Level0)
131 {
132 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
133 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
134 EXPECT_CALL(*utilTool_, GetLocalUdid()).WillRepeatedly(Return(DEV_DID_1));
135 EXPECT_CALL(*dhContext_, GetRealTimeOnlineDeviceCount()).Times(1).WillRepeatedly(Return(0));
136 EXPECT_CALL(*dhContext_, GetIsomerismConnectCount()).Times(1).WillRepeatedly(Return(0));
137 EXPECT_CALL(*componentManager_, ForceDisableSink(_)).Times(1).WillRepeatedly(Return(-1));
138 auto ret = disableTask->DoAutoDisable();
139 EXPECT_EQ(-1, ret);
140 }
141
142 HWTEST_F(DisableTaskTest, DoActiveDisable_001, TestSize.Level0)
143 {
144 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
145 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
146 disableTask->SetEffectSink(true);
147 EXPECT_CALL(*componentManager_, DisableSink(_, _, _)).Times(1).WillRepeatedly(Return(0));
148 auto ret = disableTask->DoActiveDisable();
149 EXPECT_EQ(DH_FWK_SUCCESS, ret);
150 }
151
152 HWTEST_F(DisableTaskTest, DoActiveDisable_002, TestSize.Level0)
153 {
154 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
155 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
156 disableTask->SetEffectSink(true);
157 EXPECT_CALL(*componentManager_, DisableSink(_, _, _)).Times(1).WillRepeatedly(Return(-1));
158 auto ret = disableTask->DoActiveDisable();
159 EXPECT_EQ(-1, ret);
160 }
161
162 HWTEST_F(DisableTaskTest, DoActiveDisable_003, TestSize.Level0)
163 {
164 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
165 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
166 disableTask->SetEffectSource(true);
167 EXPECT_CALL(*componentManager_, DisableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(0));
168 auto ret = disableTask->DoActiveDisable();
169 EXPECT_EQ(DH_FWK_SUCCESS, ret);
170 }
171
172 HWTEST_F(DisableTaskTest, DoActiveDisable_004, TestSize.Level0)
173 {
174 auto disableTask = std::make_shared<DisableTask>(TASK_PARAM_1.networkId, TASK_PARAM_1.uuid, TASK_PARAM_1.udid,
175 TASK_PARAM_1.dhId, TASK_PARAM_1.dhType);
176 disableTask->SetEffectSource(true);
177 EXPECT_CALL(*componentManager_, DisableSource(_, _, _, _)).Times(1).WillRepeatedly(Return(-1));
178 auto ret = disableTask->DoActiveDisable();
179 EXPECT_EQ(-1, ret);
180 }
181 } // namespace DistributedHardware
182 } // namespace OHOS