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 "ui_extension/provider_data_handler.h"
17
18 #include <memory>
19
20 #include <message_parcel.h>
21 #include <want.h>
22
23 #include "iremote_object_mocker.h"
24 #include "session/host/include/zidl/session_proxy.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS::Rosen::Extension {
30 class ProviderDataHandlerTest : public testing::Test {
31 public:
SetUpTestCase()32 static void SetUpTestCase() {}
TearDownTestCase()33 static void TearDownTestCase() {}
SetUp()34 void SetUp() override {}
TearDown()35 void TearDown() override {}
36 };
37
38 /**
39 * @tc.name: ProviderDataHandlerSendData01
40 * @tc.desc: Test send data
41 * @tc.type: FUNC
42 */
TEST_F(ProviderDataHandlerTest,ProviderDataHandlerSendData01)43 TEST_F(ProviderDataHandlerTest, ProviderDataHandlerSendData01)
44 {
45 auto remoteObj = sptr<RemoteObjectMocker>::MakeSptr();
46 auto sessionProxy = sptr<SessionProxy>::MakeSptr(remoteObj);
47 // Mock the SendRequest behavior
48 EXPECT_CALL(*remoteObj, IsProxyObject()).WillRepeatedly(Return(true));
49 EXPECT_CALL(*remoteObj, SendRequest(_, _, _, _))
50 .WillOnce(Invoke([](uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) -> int {
51 // Verify the message option is sync
52 EXPECT_EQ(option.GetFlags(), MessageOption::TF_SYNC);
53
54 // Write mock response
55 reply.WriteUint32(static_cast<uint32_t>(DataHandlerErr::OK)); // Reply code
56
57 // Create and write mock reply Want
58 AAFwk::Want replyWant;
59 replyWant.SetParam("test_key", std::string("test_value"));
60 reply.WriteParcelable(&replyWant);
61
62 return 0;
63 }));
64
65 // Create test data
66 std::unique_ptr<ProviderDataHandler> handler = std::make_unique<ProviderDataHandler>();
67 handler->SetRemoteProxyObject(sessionProxy->AsObject());
68
69 AAFwk::Want sendWant;
70 sendWant.SetParam("send_key", std::string("send_value"));
71
72 // Test sync send with reply
73 AAFwk::Want reply;
74 auto result = handler->SendDataSync(SubSystemId::WM_UIEXT, 1, sendWant, reply);
75
76 // Verify results
77 EXPECT_EQ(result, DataHandlerErr::OK);
78 EXPECT_EQ(reply.GetStringParam("test_key"), "test_value");
79 }
80 } // namespace OHOS::Rosen::Extension
81