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, TestSize.Level1) 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, TestSize.Level1) 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, TestSize.Level1) 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, TestSize.Level1) 98 { 99 MockDataHandler handler; 100 auto callback = __anon59e799dd0102(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 101 [](SubSystemId id, uint32_t customId, AAFwk::Want&& data, 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, TestSize.Level1) 115 { 116 MockDataHandler handler; 117 auto callback = __anon59e799dd0202(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 118 [](SubSystemId id, uint32_t customId, AAFwk::Want&& data, 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, TestSize.Level1) 135 { 136 MockDataHandler handler; 137 auto callback = __anon59e799dd0302(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 138 [](SubSystemId id, uint32_t customId, AAFwk::Want&& data, 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, TestSize.Level1) 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, TestSize.Level1) 174 { 175 MockDataHandler handler; 176 bool callbackCalled = false; 177 178 auto callback = [&callbackCalled](SubSystemId id, 179 uint32_t customId, 180 AAFwk::Want&& data, __anon59e799dd0402(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 181 std::optional<AAFwk::Want>& reply) -> int32_t { 182 callbackCalled = true; 183 EXPECT_EQ(SubSystemId::WM_UIEXT, id); 184 EXPECT_EQ(123u, customId); 185 if (reply.has_value()) { 186 reply->SetParam("test", 1); 187 } 188 return 0; 189 }; 190 191 // Register consumer 192 auto ret = handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback)); 193 ASSERT_EQ(DataHandlerErr::OK, ret); 194 195 // Prepare test data 196 AAFwk::Want data; 197 std::optional<AAFwk::Want> reply = std::make_optional<AAFwk::Want>(); 198 DataTransferConfig config; 199 config.needSyncSend = true; 200 config.needReply = true; 201 config.subSystemId = SubSystemId::WM_UIEXT; 202 config.customId = 123; 203 204 // Test NotifyDataConsumer 205 ret = handler.NotifyDataConsumer(std::move(data), reply, config); 206 ASSERT_EQ(DataHandlerErr::OK, ret); 207 ASSERT_TRUE(callbackCalled); 208 ASSERT_TRUE(reply.has_value()); 209 ASSERT_EQ(1, reply->GetIntParam("test", 0)); 210 } 211 212 /** 213 * @tc.name: NotifyDataConsumer02 214 * @tc.desc: Test NotifyDataConsumer with async mode 215 * @tc.type: FUNC 216 */ 217 HWTEST_F(ExtensionDataHandlerTest, NotifyDataConsumer02, TestSize.Level1) 218 { 219 MockDataHandler handler; 220 bool callbackCalled = false; 221 222 auto callback = [&callbackCalled](SubSystemId id, 223 uint32_t customId, 224 AAFwk::Want&& data, __anon59e799dd0502(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply) 225 std::optional<AAFwk::Want>& reply) -> int32_t { 226 callbackCalled = true; 227 EXPECT_EQ(SubSystemId::WM_UIEXT, id); 228 EXPECT_EQ(123u, customId); 229 return 0; 230 }; 231 232 // Register consumer 233 ASSERT_EQ(DataHandlerErr::OK, handler.RegisterDataConsumer(SubSystemId::WM_UIEXT, std::move(callback))); 234 235 // Prepare test data 236 AAFwk::Want data; 237 std::optional<AAFwk::Want> reply; 238 DataTransferConfig config; 239 config.needSyncSend = false; 240 config.subSystemId = SubSystemId::WM_UIEXT; 241 config.customId = 123; 242 243 // Test NotifyDataConsumer 244 auto ret = handler.NotifyDataConsumer(std::move(data), reply, config); 245 ASSERT_EQ(DataHandlerErr::OK, ret); 246 } 247 248 /** 249 * @tc.name: NotifyDataConsumer03 250 * @tc.desc: Test NotifyDataConsumer with unregistered consumer 251 * @tc.type: FUNC 252 */ 253 HWTEST_F(ExtensionDataHandlerTest, NotifyDataConsumer03, TestSize.Level1) 254 { 255 MockDataHandler handler; 256 AAFwk::Want data; 257 std::optional<AAFwk::Want> reply; 258 DataTransferConfig config; 259 config.subSystemId = SubSystemId::WM_UIEXT; 260 config.customId = 123; 261 262 // Test NotifyDataConsumer without registering consumer 263 auto ret = handler.NotifyDataConsumer(std::move(data), reply, config); 264 ASSERT_EQ(DataHandlerErr::NO_CONSUME_CALLBACK, ret); 265 } 266 } // namespace OHOS::Rosen::Extension 267