• 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 "common/include/extension_data_handler.h"
17 
18 #include <gtest/gtest.h>
19 #include <message_parcel.h>
20 #include <want.h>
21 
22 #include "extension_data_handler_mock.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Rosen::Extension {
28 class ExtensionDataHandlerTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() override {}
TearDown()33     void TearDown() override {}
34 };
35 
36 /**
37  * @tc.name: DataTransferConfigMarshalling01
38  * @tc.desc: Test DataTransferConfig Marshalling with valid data
39  * @tc.type: FUNC
40  */
41 HWTEST_F(ExtensionDataHandlerTest, DataTransferConfigMarshalling01, Function | SmallTest | Level2)
42 {
43     DataTransferConfig config;
44     config.subSystemId = SubSystemId::WM_UIEXT;
45     config.customId = 123;
46     config.needReply = true;
47     config.needSyncSend = false;
48 
49     MessageParcel parcel;
50     ASSERT_TRUE(config.Marshalling(parcel));
51 }
52 
53 /**
54  * @tc.name: DataTransferConfigUnmarshalling01
55  * @tc.desc: Test DataTransferConfig Unmarshalling with valid data
56  * @tc.type: FUNC
57  */
58 HWTEST_F(ExtensionDataHandlerTest, DataTransferConfigUnmarshalling01, Function | SmallTest | Level2)
59 {
60     MessageParcel parcel;
61     parcel.WriteUint8(static_cast<uint8_t>(SubSystemId::WM_UIEXT));
62     parcel.WriteUint32(123);
63     parcel.WriteBool(true);
64     parcel.WriteBool(false);
65 
66     auto config = DataTransferConfig::Unmarshalling(parcel);
67     ASSERT_NE(nullptr, config);
68     ASSERT_EQ(SubSystemId::WM_UIEXT, config->subSystemId);
69     ASSERT_EQ(123u, config->customId);
70     ASSERT_TRUE(config->needReply);
71     ASSERT_FALSE(config->needSyncSend);
72     delete config;
73 }
74 
75 /**
76  * @tc.name: DataTransferConfigUnmarshalling02
77  * @tc.desc: Test DataTransferConfig Unmarshalling with invalid subSystemId
78  * @tc.type: FUNC
79  */
80 HWTEST_F(ExtensionDataHandlerTest, DataTransferConfigUnmarshalling02, Function | SmallTest | Level2)
81 {
82     MessageParcel parcel;
83     parcel.WriteUint8(static_cast<uint8_t>(SubSystemId::INVALID));
84     parcel.WriteUint32(123);
85     parcel.WriteBool(true);
86     parcel.WriteBool(false);
87 
88     auto config = DataTransferConfig::Unmarshalling(parcel);
89     ASSERT_EQ(nullptr, config);
90 }
91 
92 /**
93  * @tc.name: RegisterDataConsumer01
94  * @tc.desc: Test RegisterDataConsumer with valid callback
95  * @tc.type: FUNC
96  */
97 HWTEST_F(ExtensionDataHandlerTest, RegisterDataConsumer01, Function | SmallTest | Level2)
98 {
99     MockDataHandler handler;
100     auto callback = [](SubSystemId id, uint32_t customId, AAFwk::Want&& data,
__anondd56bfbb0102(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 101                        std::optional<AAFwk::Want>& reply) -> int32_t {
102         return 0;
103     };
104 
105     auto ret = handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback));
106     ASSERT_EQ(DataHandlerErr::OK, ret);
107 }
108 
109 /**
110  * @tc.name: RegisterDataConsumer02
111  * @tc.desc: Test RegisterDataConsumer with duplicate registration
112  * @tc.type: FUNC
113  */
114 HWTEST_F(ExtensionDataHandlerTest, RegisterDataConsumer02, Function | SmallTest | Level2)
115 {
116     MockDataHandler handler;
117     auto callback = [](SubSystemId id, uint32_t customId, AAFwk::Want&& data,
__anondd56bfbb0202(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 118                        std::optional<AAFwk::Want>& reply) -> int32_t {
119         return 0;
120     };
121     auto callback1 = callback;
122     auto ret = handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback));
123     ASSERT_EQ(ret, DataHandlerErr::OK);
124 
125     ret = handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback1));
126     ASSERT_EQ(ret, DataHandlerErr::DUPLICATE_REGISTRATION);
127 }
128 
129 /**
130  * @tc.name: UnregisterDataConsumer01
131  * @tc.desc: Test UnregisterDataConsumer with registered consumer
132  * @tc.type: FUNC
133  */
134 HWTEST_F(ExtensionDataHandlerTest, UnregisterDataConsumer01, Function | SmallTest | Level2)
135 {
136     MockDataHandler handler;
137     auto callback = [](SubSystemId id, uint32_t customId, AAFwk::Want&& data,
__anondd56bfbb0302(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 138                        std::optional<AAFwk::Want>& reply) -> int32_t {
139         return 0;
140     };
141     auto callback1 = callback;
142 
143     ASSERT_EQ(DataHandlerErr::OK, handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback)));
144     handler.UnregisterDataConsumer(SubSystemId::WM_UIEXT);
145 
146     // Verify unregistration by trying to register again
147     ASSERT_EQ(DataHandlerErr::OK, handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback1)));
148 }
149 
150 /**
151  * @tc.name: SendDataTest01
152  * @tc.desc: Test SendDataSync with mocked behavior
153  * @tc.type: FUNC
154  */
155 HWTEST_F(ExtensionDataHandlerTest, SendDataTest01, Function | SmallTest | Level2)
156 {
157     MockDataHandler handler;
158     AAFwk::Want data;
159     AAFwk::Want reply;
160 
161     // Set up expectations
162     EXPECT_CALL(handler, SendData(testing::_, testing::_, testing::_)).WillOnce(testing::Return(DataHandlerErr::OK));
163 
164     // Test SendDataSync
165     ASSERT_EQ(DataHandlerErr::OK, handler.SendDataSync(SubSystemId::WM_UIEXT, 123, data, reply));
166 }
167 
168 /**
169  * @tc.name: NotifyDataConsumer01
170  * @tc.desc: Test NotifyDataConsumer with sync mode and valid callback
171  * @tc.type: FUNC
172  */
173 HWTEST_F(ExtensionDataHandlerTest, NotifyDataConsumer01, Function | SmallTest | Level2)
174 {
175     MockDataHandler handler;
176     bool callbackCalled = false;
177 
178     auto callback = [&callbackCalled](SubSystemId id, uint32_t customId, AAFwk::Want&& data,
__anondd56bfbb0402(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 179                                       std::optional<AAFwk::Want>& reply) -> int32_t {
180         callbackCalled = true;
181         EXPECT_EQ(SubSystemId::WM_UIEXT, id);
182         EXPECT_EQ(123u, customId);
183         if (reply.has_value()) {
184             reply->SetParam("test", 1);
185         }
186         return 0;
187     };
188 
189     // Register consumer
190     auto ret = handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback));
191     ASSERT_EQ(DataHandlerErr::OK, ret);
192 
193     // Prepare test data
194     AAFwk::Want data;
195     std::optional<AAFwk::Want> reply = std::make_optional<AAFwk::Want>();
196     DataTransferConfig config;
197     config.needSyncSend = true;
198     config.needReply = true;
199     config.subSystemId = SubSystemId::WM_UIEXT;
200     config.customId = 123;
201 
202     // Test NotifyDataConsumer
203     ret = handler.NotifyDataConsumer(std::move(data), reply, config);
204     ASSERT_EQ(DataHandlerErr::OK, ret);
205     ASSERT_TRUE(callbackCalled);
206     ASSERT_TRUE(reply.has_value());
207     ASSERT_EQ(1, reply->GetIntParam("test", 0));
208 }
209 
210 /**
211  * @tc.name: NotifyDataConsumer02
212  * @tc.desc: Test NotifyDataConsumer with async mode
213  * @tc.type: FUNC
214  */
215 HWTEST_F(ExtensionDataHandlerTest, NotifyDataConsumer02, Function | SmallTest | Level2)
216 {
217     MockDataHandler handler;
218     bool callbackCalled = false;
219 
220     auto callback = [&callbackCalled](SubSystemId id, uint32_t customId, AAFwk::Want&& data,
__anondd56bfbb0502(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 221                                       std::optional<AAFwk::Want>& reply) -> int32_t {
222         callbackCalled = true;
223         EXPECT_EQ(SubSystemId::WM_UIEXT, id);
224         EXPECT_EQ(123u, customId);
225         return 0;
226     };
227 
228     // Register consumer
229     ASSERT_EQ(DataHandlerErr::OK, handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback)));
230 
231     // Prepare test data
232     AAFwk::Want data;
233     std::optional<AAFwk::Want> reply;
234     DataTransferConfig config;
235     config.needSyncSend = false;
236     config.subSystemId = SubSystemId::WM_UIEXT;
237     config.customId = 123;
238 
239     // Test NotifyDataConsumer
240     auto ret = handler.NotifyDataConsumer(std::move(data), reply, config);
241     ASSERT_EQ(DataHandlerErr::OK, ret);
242 }
243 
244 /**
245  * @tc.name: NotifyDataConsumer03
246  * @tc.desc: Test NotifyDataConsumer with unregistered consumer
247  * @tc.type: FUNC
248  */
249 HWTEST_F(ExtensionDataHandlerTest, NotifyDataConsumer03, Function | SmallTest | Level2)
250 {
251     MockDataHandler handler;
252     AAFwk::Want data;
253     std::optional<AAFwk::Want> reply;
254     DataTransferConfig config;
255     config.subSystemId = SubSystemId::WM_UIEXT;
256     config.customId = 123;
257 
258     // Test NotifyDataConsumer without registering consumer
259     auto ret = handler.NotifyDataConsumer(std::move(data), reply, config);
260     ASSERT_EQ(DataHandlerErr::NO_CONSUME_CALLBACK, ret);
261 }
262 }  // namespace OHOS::Rosen::Extension
263