• 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "anco_channel_death_recipient.h"
20 #include "anco_channel_proxy.h"
21 #include "iremote_object.h"
22 #include "message_parcel_mock.h"
23 #include "mmi_log.h"
24 #include "multimodal_input_connect_proxy.h"
25 
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "MultimodalInputConnectProxyTest"
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 using namespace testing::ext;
33 using namespace testing;
34 constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
35 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
36 constexpr int32_t MIDDLE_PIXEL_MAP_WIDTH { 400 };
37 constexpr int32_t MIDDLE_PIXEL_MAP_HEIGHT { 400 };
38 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
39 constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
40 constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
41 constexpr int32_t INT32_BYTE { 4 };
42 
43 class RemoteObjectTest : public IRemoteObject {
44 public:
RemoteObjectTest(std::u16string descriptor)45     explicit RemoteObjectTest(std::u16string descriptor) : IRemoteObject(descriptor) {}
~RemoteObjectTest()46     ~RemoteObjectTest() {}
47 
GetObjectRefCount()48     int32_t GetObjectRefCount() { return 0; }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { return 0; }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)50     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)51     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
Dump(int fd,const std::vector<std::u16string> & args)52     int Dump(int fd, const std::vector<std::u16string> &args) { return 0; }
53 };
54 } // namespace
55 
56 class MultimodalInputConnectProxyTest : public testing::Test {
57 public:
58     static void SetUpTestCase(void);
59     static void TearDownTestCase();
60     static std::shared_ptr<Media::PixelMap> CreatePixelMap(int32_t width, int32_t height);
SetUp()61     void SetUp() {}
TearDown()62     void TearDown() {}
63 
64     static inline std::shared_ptr<MessageParcelMock> messageParcelMock_ = nullptr;
65 };
66 
SetUpTestCase(void)67 void MultimodalInputConnectProxyTest::SetUpTestCase(void)
68 {
69     messageParcelMock_ = std::make_shared<MessageParcelMock>();
70     MessageParcelMock::messageParcel = messageParcelMock_;
71 }
TearDownTestCase()72 void MultimodalInputConnectProxyTest::TearDownTestCase()
73 {
74     MessageParcelMock::messageParcel = nullptr;
75     messageParcelMock_ = nullptr;
76 }
77 
CreatePixelMap(int32_t width,int32_t height)78 std::shared_ptr<Media::PixelMap> MultimodalInputConnectProxyTest::CreatePixelMap(int32_t width, int32_t height)
79 {
80     CALL_DEBUG_ENTER;
81     if (width <= 0 || width > MAX_PIXEL_MAP_WIDTH || height <= 0 || height > MAX_PIXEL_MAP_HEIGHT) {
82         return nullptr;
83     }
84     Media::InitializationOptions opts;
85     opts.size.height = height;
86     opts.size.width = width;
87     opts.pixelFormat = Media::PixelFormat::BGRA_8888;
88     opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
89     opts.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
90 
91     int32_t colorLen = width * height;
92     uint32_t *pixelColors = new (std::nothrow) uint32_t[colorLen];
93     CHKPP(pixelColors);
94     int32_t colorByteCount = colorLen * INT32_BYTE;
95     errno_t ret = memset_s(pixelColors, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount);
96     if (ret != EOK) {
97         delete[] pixelColors;
98         return nullptr;
99     }
100     std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(pixelColors, colorLen, opts);
101     if (pixelMap == nullptr) {
102         delete[] pixelColors;
103         return nullptr;
104     }
105     delete[] pixelColors;
106     return pixelMap;
107 }
108 
109 class MockPointerEvent : public PointerEvent {
110 public:
111     int32_t eventType;
MockPointerEvent(int32_t eventType)112     explicit MockPointerEvent(int32_t eventType) : PointerEvent(eventType), eventType(eventType) {}
113     MOCK_METHOD(bool, WriteToParcel, (Parcel& data));
114 };
115 
116 class MockIRemoteObject : public IRemoteObject {
117 public:
118     MockIRemoteObject() = default;
119     ~MockIRemoteObject() override = default;
120 };
121 
122 /**
123  * @tc.name: MultimodalInputConnectProxyTest_SubscribeSwitchEvent_001
124  * @tc.desc: Cover if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor())) branch
125  * @tc.type: FUNC
126  * @tc.require:
127  */
128 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SubscribeSwitchEvent_001, TestSize.Level1)
129 {
130     CALL_TEST_DEBUG;
131     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
132     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
133     MultimodalInputConnectProxy proxy(remote);
134     int32_t subscribeId = 10;
135     int32_t switchType = 1;
136     EXPECT_EQ(proxy.SubscribeSwitchEvent(subscribeId, switchType), ERR_INVALID_VALUE);
137 }
138 
139 /**
140  * @tc.name: MultimodalInputConnectProxyTest_SubscribeSwitchEvent_002
141  * @tc.desc: Cover the else branch of if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor()))
142  * @tc.type: FUNC
143  * @tc.require:
144  */
145 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SubscribeSwitchEvent_002, TestSize.Level1)
146 {
147     CALL_TEST_DEBUG;
148     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
149     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillRepeatedly(Return(true));
150     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
151     MultimodalInputConnectProxy proxy(remote);
152     int32_t subscribeId = 10;
153     int32_t switchType = 1;
154     EXPECT_EQ(proxy.SubscribeSwitchEvent(subscribeId, switchType), RET_OK);
155 }
156 
157 /**
158  * @tc.name: MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_001
159  * @tc.desc: Cover if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor())) branch
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_001, TestSize.Level1)
164 {
165     CALL_TEST_DEBUG;
166     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
167     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
168     MultimodalInputConnectProxy proxy(remote);
169     int32_t subscribeId = 10;
170     EXPECT_EQ(proxy.UnsubscribeSwitchEvent(subscribeId), ERR_INVALID_VALUE);
171 }
172 
173 /**
174  * @tc.name: MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_002
175  * @tc.desc: Cover the else branch of if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor()))
176  * @tc.type: FUNC
177  * @tc.require:
178  */
179 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_002, TestSize.Level1)
180 {
181     CALL_TEST_DEBUG;
182     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
183     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillRepeatedly(Return(true));
184     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
185     MultimodalInputConnectProxy proxy(remote);
186     int32_t subscribeId = 10;
187     EXPECT_EQ(proxy.UnsubscribeSwitchEvent(subscribeId), RET_OK);
188 }
189 
190 /**
191  * @tc.name: MultimodalInputConnectProxyTest_SetMouseHotSpot_001
192  * @tc.desc: Cover if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor())) branch
193  * @tc.type: FUNC
194  * @tc.require:
195  */
196 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SetMouseHotSpot_001, TestSize.Level1)
197 {
198     CALL_TEST_DEBUG;
199     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
200     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
201     MultimodalInputConnectProxy proxy(remote);
202     int32_t pid = 1000;
203     int32_t windowId = 50;
204     int32_t hotSpotX = 300;
205     int32_t hotSpotY = 300;
206     EXPECT_EQ(proxy.SetMouseHotSpot(pid, windowId, hotSpotX, hotSpotY), ERR_INVALID_VALUE);
207 }
208 
209 /**
210  * @tc.name: MultimodalInputConnectProxyTest_SetMouseHotSpot_002
211  * @tc.desc: Cover the else branch of if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor()))
212  * @tc.type: FUNC
213  * @tc.require:
214  */
215 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SetMouseHotSpot_002, TestSize.Level1)
216 {
217     CALL_TEST_DEBUG;
218     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
219     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillRepeatedly(Return(true));
220     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
221     MultimodalInputConnectProxy proxy(remote);
222     int32_t pid = 1000;
223     int32_t windowId = 50;
224     int32_t hotSpotX = 300;
225     int32_t hotSpotY = 300;
226     EXPECT_EQ(proxy.SetMouseHotSpot(pid, windowId, hotSpotX, hotSpotY), RET_OK);
227 }
228 
229 /**
230  * @tc.name: MultimodalInputConnectProxyTest_SyncInputEvent_001
231  * @tc.desc: Test the function SyncInputEvent
232  * @tc.type: FUNC
233  * @tc.require:
234  */
235 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SyncInputEvent_001, TestSize.Level1)
236 {
237     CALL_TEST_DEBUG;
238     sptr<IRemoteObject> remoteObject;
239     OHOS::MMI::AncoChannelProxy ancoChannelProxy(remoteObject);
240     auto pointerEvent = PointerEvent::Create();
241     ASSERT_NE(pointerEvent, nullptr);
242     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(false));
243     int32_t ret = ancoChannelProxy.SyncInputEvent(pointerEvent);
244     EXPECT_EQ(ret, RET_ERR);
245     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(true));
246     ret = ancoChannelProxy.SyncInputEvent(pointerEvent);
247     EXPECT_EQ(ret, RET_ERR);
248 }
249 
250 /**
251  * @tc.name: MultimodalInputConnectProxyTest_SyncInputEvent_002
252  * @tc.desc: Test the function SyncInputEvent
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SyncInputEvent_002, TestSize.Level1)
257 {
258     CALL_TEST_DEBUG;
259     sptr<IRemoteObject> remoteObject;
260     OHOS::MMI::AncoChannelProxy ancoChannelProxy(remoteObject);
261     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(true));
262     int32_t eventType = 1;
263     auto pointerEvent = std::make_shared<MockPointerEvent>(eventType);
264     EXPECT_CALL(*pointerEvent, WriteToParcel(_)).WillRepeatedly(Return(true));
265     int32_t ret = ancoChannelProxy.SyncInputEvent(pointerEvent);
266     EXPECT_EQ(ret, RET_ERR);
267 }
268 
269 /**
270  * @tc.name: MultimodalInputConnectProxyTest_SyncInputEvent_003
271  * @tc.desc: Test the function SyncInputEvent
272  * @tc.type: FUNC
273  * @tc.require:
274  */
275 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SyncInputEvent_003, TestSize.Level1)
276 {
277     CALL_TEST_DEBUG;
278     sptr<IRemoteObject> remoteObject;
279     OHOS::MMI::AncoChannelProxy ancoChannelProxy(remoteObject);
280     auto keyEvent = KeyEvent::Create();
281     ASSERT_NE(keyEvent, nullptr);
282     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(false));
283     int32_t ret = ancoChannelProxy.SyncInputEvent(keyEvent);
284     EXPECT_EQ(ret, RET_ERR);
285     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(true));
286     ret = ancoChannelProxy.SyncInputEvent(keyEvent);
287     EXPECT_EQ(ret, RET_ERR);
288 }
289 
290 /**
291  * @tc.name: MultimodalInputConnectProxyTest_SyncInputEvent_004
292  * @tc.desc: Test the function SyncInputEvent
293  * @tc.type: FUNC
294  * @tc.require:
295  */
296 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SyncInputEvent_004, TestSize.Level1)
297 {
298     CALL_TEST_DEBUG;
299     sptr<IRemoteObject> remoteObject;
300     OHOS::MMI::AncoChannelProxy ancoChannelProxy(remoteObject);
301     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
302     ASSERT_NE(keyEvent, nullptr);
303     keyEvent->SetKeyCode(KeyEvent::KEYCODE_HOME);
304     keyEvent->SetActionTime(100);
305     keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
306     keyEvent->ActionToString(KeyEvent::KEY_ACTION_DOWN);
307     keyEvent->KeyCodeToString(KeyEvent::KEYCODE_HOME);
308     KeyEvent::KeyItem item;
309     item.SetKeyCode(KeyEvent::KEYCODE_HOME);
310     item.SetDownTime(100);
311     item.SetPressed(true);
312     keyEvent->AddKeyItem(item);
313     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(true));
314     int32_t ret = ancoChannelProxy.SyncInputEvent(keyEvent);
315     EXPECT_EQ(ret, RET_ERR);
316 }
317 
318 /**
319  * @tc.name: MultimodalInputConnectProxyTest_UpdateWindowInfo_001
320  * @tc.desc: Test the function UpdateWindowInfo
321  * @tc.type: FUNC
322  * @tc.require:
323  */
324 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_UpdateWindowInfo_001, TestSize.Level1)
325 {
326     CALL_TEST_DEBUG;
327     sptr<IRemoteObject> remoteObject;
328     OHOS::MMI::AncoChannelProxy ancoChannelProxy(remoteObject);
329     auto windows = std::make_shared<AncoWindows>();
330     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(false));
331     int32_t ret = ancoChannelProxy.UpdateWindowInfo(windows);
332     EXPECT_EQ(ret, RET_ERR);
333     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(true));
334     ret = ancoChannelProxy.UpdateWindowInfo(windows);
335     EXPECT_EQ(ret, RET_ERR);
336 }
337 
338 /**
339  * @tc.name: MultimodalInputConnectProxyTest_OnRemoteDied_001
340  * @tc.desc: Test the function OnRemoteDied
341  * @tc.type: FUNC
342  * @tc.require:
343  */
344 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_OnRemoteDied_001, TestSize.Level1)
345 {
346     CALL_TEST_DEBUG;
347     bool callbackCalled = false;
__anon63bb26a40202(const wptr<IRemoteObject>& object) 348     auto deathCallback = [&callbackCalled](const wptr<IRemoteObject>& object) {
349         callbackCalled = true;
350     };
351     OHOS::MMI::AncoChannelDeathRecipient recipient(deathCallback);
352     OHOS::sptr<OHOS::IRemoteObject> object;
353     auto remoteObjectWptr = wptr<OHOS::IRemoteObject>(object);
354     EXPECT_NO_FATAL_FAILURE(recipient.OnRemoteDied(remoteObjectWptr));
355 }
356 
357 /**
358  * @tc.name: MultimodalInputConnectProxyTest_OnRemoteDied_002
359  * @tc.desc: Test the function OnRemoteDied
360  * @tc.type: FUNC
361  * @tc.require:
362  */
363 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_OnRemoteDied_002, TestSize.Level1)
364 {
365     CALL_TEST_DEBUG;
366     OHOS::MMI::AncoChannelDeathRecipient recipient(nullptr);
367     OHOS::sptr<OHOS::IRemoteObject> object;
368     auto remoteObjectWptr = wptr<OHOS::IRemoteObject>(object);
369     EXPECT_NO_FATAL_FAILURE(recipient.OnRemoteDied(remoteObjectWptr));
370 }
371 
372 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
373 /**
374  * @tc.name: MultimodalInputConnectProxyTest_GetPointerSnapshot
375  * @tc.desc: Test the function GetPointerSnapshot
376  * @tc.type: FUNC
377  * @tc.require:
378  */
379 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_GetPointerSnapshot, TestSize.Level1)
380 {
381     CALL_TEST_DEBUG;
382     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(false));
383     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
384     MultimodalInputConnectProxy proxy(remote);
385     std::shared_ptr<Media::PixelMap> pixelMapPtr = CreatePixelMap(MIDDLE_PIXEL_MAP_WIDTH, MIDDLE_PIXEL_MAP_HEIGHT);
386     EXPECT_EQ(proxy.GetPointerSnapshot((void *)pixelMapPtr.get()), ERR_INVALID_VALUE);
387 }
388 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
389 } // namespace MMI
390 } // namespace OHOS