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 "message_parcel_mock.h"
20 #include "mmi_log.h"
21 #include "multimodal_input_connect_proxy.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "MultimodalInputConnectProxyTest"
25 
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 using namespace testing::ext;
30 using namespace testing;
31 constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
32 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
33 constexpr int32_t MIDDLE_PIXEL_MAP_WIDTH { 400 };
34 constexpr int32_t MIDDLE_PIXEL_MAP_HEIGHT { 400 };
35 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
36 constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
37 constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
38 constexpr int32_t INT32_BYTE { 4 };
39 
40 class RemoteObjectTest : public IRemoteObject {
41 public:
RemoteObjectTest(std::u16string descriptor)42     explicit RemoteObjectTest(std::u16string descriptor) : IRemoteObject(descriptor) {}
~RemoteObjectTest()43     ~RemoteObjectTest() {}
44 
GetObjectRefCount()45     int32_t GetObjectRefCount() { return 0; }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)46     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { return 0; }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)47     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)48     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
Dump(int fd,const std::vector<std::u16string> & args)49     int Dump(int fd, const std::vector<std::u16string> &args) { return 0; }
50 };
51 } // namespace
52 
53 class MultimodalInputConnectProxyTest : public testing::Test {
54 public:
55     static void SetUpTestCase(void);
56     static void TearDownTestCase();
57     static std::shared_ptr<Media::PixelMap> CreatePixelMap(int32_t width, int32_t height);
SetUp()58     void SetUp() {}
TearDown()59     void TearDown() {}
60 
61     static inline std::shared_ptr<MessageParcelMock> messageParcelMock_ = nullptr;
62 };
63 
SetUpTestCase(void)64 void MultimodalInputConnectProxyTest::SetUpTestCase(void)
65 {
66     messageParcelMock_ = std::make_shared<MessageParcelMock>();
67     MessageParcelMock::messageParcel = messageParcelMock_;
68 }
TearDownTestCase()69 void MultimodalInputConnectProxyTest::TearDownTestCase()
70 {
71     MessageParcelMock::messageParcel = nullptr;
72     messageParcelMock_ = nullptr;
73 }
74 
CreatePixelMap(int32_t width,int32_t height)75 std::shared_ptr<Media::PixelMap> MultimodalInputConnectProxyTest::CreatePixelMap(int32_t width, int32_t height)
76 {
77     CALL_DEBUG_ENTER;
78     if (width <= 0 || width > MAX_PIXEL_MAP_WIDTH || height <= 0 || height > MAX_PIXEL_MAP_HEIGHT) {
79         return nullptr;
80     }
81     Media::InitializationOptions opts;
82     opts.size.height = height;
83     opts.size.width = width;
84     opts.pixelFormat = Media::PixelFormat::BGRA_8888;
85     opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
86     opts.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
87 
88     int32_t colorLen = width * height;
89     uint32_t *pixelColors = new (std::nothrow) uint32_t[colorLen];
90     CHKPP(pixelColors);
91     int32_t colorByteCount = colorLen * INT32_BYTE;
92     errno_t ret = memset_s(pixelColors, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount);
93     if (ret != EOK) {
94         delete[] pixelColors;
95         return nullptr;
96     }
97     std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(pixelColors, colorLen, opts);
98     if (pixelMap == nullptr) {
99         delete[] pixelColors;
100         return nullptr;
101     }
102     delete[] pixelColors;
103     return pixelMap;
104 }
105 
106 /**
107  * @tc.name: MultimodalInputConnectProxyTest_SubscribeSwitchEvent_001
108  * @tc.desc: Cover if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor())) branch
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SubscribeSwitchEvent_001, TestSize.Level1)
113 {
114     CALL_TEST_DEBUG;
115     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
116     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
117     MultimodalInputConnectProxy proxy(remote);
118     int32_t subscribeId = 10;
119     int32_t switchType = 1;
120     EXPECT_EQ(proxy.SubscribeSwitchEvent(subscribeId, switchType), ERR_INVALID_VALUE);
121 }
122 
123 /**
124  * @tc.name: MultimodalInputConnectProxyTest_SubscribeSwitchEvent_002
125  * @tc.desc: Cover the else branch of if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor()))
126  * @tc.type: FUNC
127  * @tc.require:
128  */
129 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SubscribeSwitchEvent_002, TestSize.Level1)
130 {
131     CALL_TEST_DEBUG;
132     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
133     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillRepeatedly(Return(true));
134     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
135     MultimodalInputConnectProxy proxy(remote);
136     int32_t subscribeId = 10;
137     int32_t switchType = 1;
138     EXPECT_EQ(proxy.SubscribeSwitchEvent(subscribeId, switchType), RET_OK);
139 }
140 
141 /**
142  * @tc.name: MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_001
143  * @tc.desc: Cover if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor())) branch
144  * @tc.type: FUNC
145  * @tc.require:
146  */
147 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_001, TestSize.Level1)
148 {
149     CALL_TEST_DEBUG;
150     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
151     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
152     MultimodalInputConnectProxy proxy(remote);
153     int32_t subscribeId = 10;
154     EXPECT_EQ(proxy.UnsubscribeSwitchEvent(subscribeId), ERR_INVALID_VALUE);
155 }
156 
157 /**
158  * @tc.name: MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_002
159  * @tc.desc: Cover the else branch of if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor()))
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_UnsubscribeSwitchEvent_002, TestSize.Level1)
164 {
165     CALL_TEST_DEBUG;
166     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
167     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillRepeatedly(Return(true));
168     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
169     MultimodalInputConnectProxy proxy(remote);
170     int32_t subscribeId = 10;
171     EXPECT_EQ(proxy.UnsubscribeSwitchEvent(subscribeId), RET_OK);
172 }
173 
174 /**
175  * @tc.name: MultimodalInputConnectProxyTest_SetMouseHotSpot_001
176  * @tc.desc: Cover if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor())) branch
177  * @tc.type: FUNC
178  * @tc.require:
179  */
180 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SetMouseHotSpot_001, TestSize.Level1)
181 {
182     CALL_TEST_DEBUG;
183     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
184     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
185     MultimodalInputConnectProxy proxy(remote);
186     int32_t pid = 1000;
187     int32_t windowId = 50;
188     int32_t hotSpotX = 300;
189     int32_t hotSpotY = 300;
190     EXPECT_EQ(proxy.SetMouseHotSpot(pid, windowId, hotSpotX, hotSpotY), ERR_INVALID_VALUE);
191 }
192 
193 /**
194  * @tc.name: MultimodalInputConnectProxyTest_SetMouseHotSpot_002
195  * @tc.desc: Cover the else branch of if (!data.WriteInterfaceToken(MultimodalInputConnectProxy::GetDescriptor()))
196  * @tc.type: FUNC
197  * @tc.require:
198  */
199 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_SetMouseHotSpot_002, TestSize.Level1)
200 {
201     CALL_TEST_DEBUG;
202     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
203     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillRepeatedly(Return(true));
204     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
205     MultimodalInputConnectProxy proxy(remote);
206     int32_t pid = 1000;
207     int32_t windowId = 50;
208     int32_t hotSpotX = 300;
209     int32_t hotSpotY = 300;
210     EXPECT_EQ(proxy.SetMouseHotSpot(pid, windowId, hotSpotX, hotSpotY), RET_OK);
211 }
212 
213 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
214 /**
215  * @tc.name: MultimodalInputConnectProxyTest_GetPointerSnapshot
216  * @tc.desc: Test the function GetPointerSnapshot
217  * @tc.type: FUNC
218  * @tc.require:
219  */
220 HWTEST_F(MultimodalInputConnectProxyTest, MultimodalInputConnectProxyTest_GetPointerSnapshot, TestSize.Level1)
221 {
222     CALL_TEST_DEBUG;
223     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillRepeatedly(Return(false));
224     sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
225     MultimodalInputConnectProxy proxy(remote);
226     std::shared_ptr<Media::PixelMap> pixelMapPtr = CreatePixelMap(MIDDLE_PIXEL_MAP_WIDTH, MIDDLE_PIXEL_MAP_HEIGHT);
227     EXPECT_EQ(proxy.GetPointerSnapshot((void *)pixelMapPtr.get()), ERR_INVALID_VALUE);
228 }
229 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
230 } // namespace MMI
231 } // namespace OHOS