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