1 /*
2 * Copyright (c) 2023 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 <sys/statvfs.h>
17 #include "gtest/gtest.h"
18 #include "message_parcel.h"
19 #include "i_sync_completed_callback.h"
20 #include "ipc_object_stub.h"
21 #include "iremote_stub.h"
22 #include "refbase.h"
23
24 #define private public
25 #define protected public
26 #include "sync_completed_callback_proxy.h"
27 #include "sync_completed_callback_stub.h"
28 #undef private
29 #undef protected
30
31 namespace OHOS {
32 namespace DistributedDeviceProfile {
33 using namespace testing;
34 using namespace testing::ext;
35
36 class SyncCompletedCallbackStubTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void SyncCompletedCallbackStubTest::SetUpTestCase()
45 {
46 }
47
TearDownTestCase()48 void SyncCompletedCallbackStubTest::TearDownTestCase()
49 {
50 }
51
SetUp()52 void SyncCompletedCallbackStubTest::SetUp()
53 {
54 }
55
TearDown()56 void SyncCompletedCallbackStubTest::TearDown()
57 {
58 }
59
60 class SyncCompletedListener : public SyncCompletedCallbackStub {
61 public:
SyncCompletedListener()62 SyncCompletedListener()
63 {
64 }
~SyncCompletedListener()65 ~SyncCompletedListener()
66 {
67 }
OnSyncCompleted(const SyncResult & syncResults)68 void OnSyncCompleted(const SyncResult& syncResults) override
69 {
70 return;
71 }
72 };
73
74 /**
75 * @tc.name: OnRemoteRequest_001
76 * @tc.desc: OnRemoteRequest
77 * @tc.type: FUNC
78 * @tc.require: I4NY1T
79 */
80 HWTEST_F(SyncCompletedCallbackStubTest, OnRemoteRequest_001, TestSize.Level2)
81 {
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option { MessageOption::TF_ASYNC };
85 SyncResult syncResults;
86 syncResults.emplace("testdeviceid", SUCCEEDED);
87 if (!data.WriteInterfaceToken(SyncCompletedCallbackStub::GetDescriptor())) {
88 return;
89 }
90 if (!data.WriteInt32(static_cast<int32_t>(syncResults.size()))) {
91 return;
92 }
93
94 for (const auto& [deviceId, syncResult] : syncResults) {
95 if (!data.WriteString(deviceId) ||
96 !data.WriteInt32(static_cast<int32_t>(syncResult))) {
97 return;
98 }
99 }
100 uint32_t code = 27;
101 SyncCompletedListener syncCallBackStub;
102 int32_t errCode = syncCallBackStub.OnRemoteRequest(code, data, reply, option);
103 EXPECT_EQ(errCode, DP_SUCCESS);
104 }
105
106 /**
107 * @tc.name: OnRemoteRequest_002
108 * @tc.desc: OnRemoteRequest of profile event notification
109 * @tc.type: FUNC
110 * @tc.require: I4NY1T
111 */
112 HWTEST_F(SyncCompletedCallbackStubTest, OnRemoteRequest_002, TestSize.Level3)
113 {
114 MessageParcel data;
115 MessageParcel reply;
116 MessageOption option { MessageOption::TF_ASYNC };
117 if (!data.WriteInterfaceToken(SyncCompletedCallbackStub::GetDescriptor())) {
118 return;
119 }
120 uint32_t code = 1;
121 SyncCompletedListener syncCallBackStub;
122 int32_t errCode = syncCallBackStub.OnRemoteRequest(code, data, reply, option);
123 EXPECT_EQ(305, errCode);
124 }
125
126 /**
127 * @tc.name: OnRemoteRequest_003
128 * @tc.desc: OnRemoteRequest of profile event notification
129 * @tc.type: FUNC
130 * @tc.require: I4NY1T
131 */
132 HWTEST_F(SyncCompletedCallbackStubTest, OnRemoteRequest_003, TestSize.Level3)
133 {
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option { MessageOption::TF_ASYNC };
137 SyncResult syncResults;
138 uint32_t code = 1;
139 SyncCompletedListener syncCallBackStub;
140 int32_t errCode = syncCallBackStub.OnRemoteRequest(code, data, reply, option);
141 EXPECT_EQ(DP_INTERFACE_CHECK_FAILED, errCode);
142
143 sptr<SyncCompletedListener> stub(new SyncCompletedListener());
144 sptr<SyncCompletedCallbackProxy> proxy(new SyncCompletedCallbackProxy(stub));
145 ASSERT_NE(proxy, nullptr);
146 syncResults.emplace("testdeviceid", SUCCEEDED);
147 proxy->OnSyncCompleted(syncResults);
148 }
149 }
150 }