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
17 #define private public
18 #define protected public
19
20 #include <future>
21 #include <optional>
22
23 #include <unistd.h>
24 #include <utility>
25
26 #include <gtest/gtest.h>
27
28 #include "cooperate_client.h"
29 #include "devicestatus_define.h"
30 #include "devicestatus_errors.h"
31
32
33 #undef LOG_TAG
34 #define LOG_TAG "CooperateClientTest"
35
36 namespace OHOS {
37 namespace Msdp {
38 namespace DeviceStatus {
39 using namespace testing::ext;
40 namespace {
41 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
42 const std::string SYSTEM_BASIC { "system_basic" };
43 } // namespace
44
45 class CooperateClientTest : public testing::Test {
46 public:
47 void SetUp();
48 void TearDown();
49 static void SetUpTestCase();
50 static void TearDownTestCase();
51 };
52
SetUpTestCase()53 void CooperateClientTest::SetUpTestCase() {}
54
TearDownTestCase()55 void CooperateClientTest::TearDownTestCase() {}
56
SetUp()57 void CooperateClientTest::SetUp() {}
58
TearDown()59 void CooperateClientTest::TearDown()
60 {
61 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
62 }
63
64 class CoordinationListenerTest : public ICoordinationListener {
65 public:
CoordinationListenerTest()66 CoordinationListenerTest() : ICoordinationListener() {}
OnCoordinationMessage(const std::string & networkId,CoordinationMessage msg)67 void OnCoordinationMessage(const std::string &networkId, CoordinationMessage msg) override
68 {
69 FI_HILOGD("Register coordination listener test");
70 (void) networkId;
71 };
72 };
73
74 class TunnelClientTest : public ITunnelClient {
75 public:
TunnelClientTest()76 TunnelClientTest() : ITunnelClient() {}
Enable(Intention intention,ParamBase & data,ParamBase & reply)77 int32_t Enable(Intention intention, ParamBase &data, ParamBase &reply)
78 {
79 return RET_ERR;
80 }
Disable(Intention intention,ParamBase & data,ParamBase & reply)81 int32_t Disable(Intention intention, ParamBase &data, ParamBase &reply)
82 {
83 return RET_ERR;
84 }
Start(Intention intention,ParamBase & data,ParamBase & reply)85 int32_t Start(Intention intention, ParamBase &data, ParamBase &reply)
86 {
87 return RET_ERR;
88 }
Stop(Intention intention,ParamBase & data,ParamBase & reply)89 int32_t Stop(Intention intention, ParamBase &data, ParamBase &reply)
90 {
91 return RET_ERR;
92 }
AddWatch(Intention intention,uint32_t id,ParamBase & data,ParamBase & reply)93 int32_t AddWatch(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply)
94 {
95 return RET_OK;
96 }
RemoveWatch(Intention intention,uint32_t id,ParamBase & data,ParamBase & reply)97 int32_t RemoveWatch(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply)
98 {
99 return RET_ERR;
100 }
SetParam(Intention intention,uint32_t id,ParamBase & data,ParamBase & reply)101 int32_t SetParam(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply)
102 {
103 return RET_ERR;
104 }
GetParam(Intention intention,uint32_t id,ParamBase & data,ParamBase & reply)105 int32_t GetParam(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply)
106 {
107 return RET_OK;
108 }
Control(Intention intention,uint32_t id,ParamBase & data,ParamBase & reply)109 int32_t Control(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply)
110 {
111 return RET_ERR;
112 }
113 };
114
115 class StreamClientTest : public StreamClient {
116 public:
117 StreamClientTest() = default;
Stop()118 void Stop() override
119 {}
Socket()120 int32_t Socket() override
121 {
122 return RET_ERR;
123 }
124 };
125
126 /**
127 * @tc.name: CooperateClientTest_RegisterListener_001
128 * @tc.desc: On Coordination Listener
129 * @tc.type: FUNC
130 * @tc.require:
131 */
132 HWTEST_F(CooperateClientTest, CooperateClientTest_RegisterListener_001, TestSize.Level1)
133 {
134 CALL_TEST_DEBUG;
135 std::shared_ptr<CoordinationListenerTest> consumer =
136 std::make_shared<CoordinationListenerTest>();
137 bool isCompatible = true;
138 TunnelClientTest tunnel;
139 CooperateClient cooperateClient;
140 int32_t ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
141 ASSERT_EQ(ret, RET_OK);
142 ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
143 ASSERT_EQ(ret, RET_ERR);
144 }
145
146 /**
147 * @tc.name: CooperateClientTest_RegisterListener_002
148 * @tc.desc: On Coordination Listener
149 * @tc.type: FUNC
150 * @tc.require:
151 */
152 HWTEST_F(CooperateClientTest, CooperateClientTest_RegisterListener_002, TestSize.Level1)
153 {
154 CALL_TEST_DEBUG;
155 std::shared_ptr<CoordinationListenerTest> consumer =
156 std::make_shared<CoordinationListenerTest>();
157 bool isCompatible = true;
158 TunnelClientTest tunnel;
159 CooperateClient cooperateClient;
160 int32_t ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
161 ASSERT_EQ(ret, RET_OK);
162 }
163
164 /**
165 * @tc.name: CooperateClientTest_OnCoordinationListener_001
166 * @tc.desc: On Coordination Listener
167 * @tc.type: FUNC
168 * @tc.require:
169 */
170 HWTEST_F(CooperateClientTest, CooperateClientTest_OnCoordinationListener_001, TestSize.Level1)
171 {
172 CALL_TEST_DEBUG;
173 std::shared_ptr<CoordinationListenerTest> consumer =
174 std::make_shared<CoordinationListenerTest>();
175 bool isCompatible = true;
176 TunnelClientTest tunnel;
177 CooperateClient cooperateClient;
178 int32_t ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
179 ASSERT_EQ(ret, RET_OK);
180 StreamClientTest client;
181 int32_t userData = 0;
182 std::string networkId = "networkId";
183 CoordinationMessage msg = CoordinationMessage::ACTIVATE_SUCCESS;
184 MessageId msgId = MessageId::COORDINATION_ADD_LISTENER;
185 NetPacket pkt(msgId);
186 pkt << userData << networkId << static_cast<int32_t>(msg);
187 ret = cooperateClient.OnCoordinationListener(client, pkt);
188 ASSERT_EQ(ret, RET_OK);
189 }
190
191 /**
192 * @tc.name: CooperateClientTest_OnCoordinationListener_002
193 * @tc.desc: On Coordination Listener
194 * @tc.type: FUNC
195 * @tc.require:
196 */
197 HWTEST_F(CooperateClientTest, CooperateClientTest_OnCoordinationListener_002, TestSize.Level1)
198 {
199 CALL_TEST_DEBUG;
200 std::shared_ptr<CoordinationListenerTest> consumer =
201 std::make_shared<CoordinationListenerTest>();
202 bool isCompatible = true;
203 TunnelClientTest tunnel;
204 CooperateClient cooperateClient;
205 int32_t ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
206 ASSERT_EQ(ret, RET_OK);
207 StreamClientTest client;
208 CoordinationMessage msg = CoordinationMessage::ACTIVATE_SUCCESS;
209 MessageId msgId = MessageId::COORDINATION_ADD_LISTENER;
210 NetPacket pkt(msgId);
211 pkt << static_cast<int32_t>(msg);
212 ret = cooperateClient.OnCoordinationListener(client, pkt);
213 ASSERT_EQ(ret, RET_ERR);
214 }
215
216 /**
217 * @tc.name: CooperateClientTest_OnMouseLocationListener_001
218 * @tc.desc: On Hot Area Listener
219 * @tc.type: FUNC
220 * @tc.require:
221 */
222 HWTEST_F(CooperateClientTest, CooperateClientTest_OnMouseLocationListener_001, TestSize.Level1)
223 {
224 CALL_TEST_DEBUG;
225 std::shared_ptr<CoordinationListenerTest> consumer =
226 std::make_shared<CoordinationListenerTest>();
227 bool isCompatible = true;
228 TunnelClientTest tunnel;
229 CooperateClient cooperateClient;
230 int32_t ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
231 ASSERT_EQ(ret, RET_OK);
232 Event event;
233 std::string networkId = "networkId";
234 MessageId msgId = MessageId::COORDINATION_ADD_LISTENER;
235 NetPacket pkt(msgId);
236 pkt << networkId << event.displayX << event.displayY << event.displayWidth << event.displayHeight;
237 StreamClientTest client;
238 ret = cooperateClient.OnMouseLocationListener(client, pkt);
239 ASSERT_EQ(ret, RET_OK);
240 }
241
242 /**
243 * @tc.name: CooperateClientTest_OnMouseLocationListener_002
244 * @tc.desc: On Hot Area Listener
245 * @tc.type: FUNC
246 * @tc.require:
247 */
248 HWTEST_F(CooperateClientTest, CooperateClientTest_OnMouseLocationListener_002, TestSize.Level1)
249 {
250 CALL_TEST_DEBUG;
251 std::shared_ptr<CoordinationListenerTest> consumer =
252 std::make_shared<CoordinationListenerTest>();
253 bool isCompatible = true;
254 TunnelClientTest tunnel;
255 CooperateClient cooperateClient;
256 int32_t ret = cooperateClient.RegisterListener(tunnel, consumer, isCompatible);
257 ASSERT_EQ(ret, RET_OK);
258 std::string networkId = "networkId";
259 MessageId msgId = MessageId::COORDINATION_ADD_LISTENER;
260 NetPacket pkt(msgId);
261 StreamClientTest client;
262 ret = cooperateClient.OnMouseLocationListener(client, pkt);
263 ASSERT_EQ(ret, RET_ERR);
264 }
265 } // namespace DeviceStatus
266 } // namespace Msdp
267 } // namespace OHOS