1 /*
2 * Copyright (c) 2025 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 <iostream>
17 #include <chrono>
18 #include <thread>
19 #include <vector>
20
21 #include <gtest/gtest.h>
22 #include "iservice_registry.h"
23 #include "if_system_ability_manager.h"
24 #include "listen_ability_client.h"
25 #include "my_rawdata.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Idl {
32
33 class IdlSaUnitTest : public testing::Test {
34 public:
IdlSaUnitTest()35 IdlSaUnitTest() {}
36
~IdlSaUnitTest()37 virtual ~IdlSaUnitTest() {}
38
39 static void SetUpTestCase();
40
41 static void TearDownTestCase();
42
43 void SetUp();
44
45 void TearDown();
46 };
47
SetUpTestCase()48 void IdlSaUnitTest::SetUpTestCase() {}
49
TearDownTestCase()50 void IdlSaUnitTest::TearDownTestCase() {}
51
SetUp()52 void IdlSaUnitTest::SetUp() {}
53
TearDown()54 void IdlSaUnitTest::TearDown() {}
55
56 /*
57 * @tc.name: IdlSaProxyTest001
58 * @tc.desc: sync func run
59 * @tc.type: FUNC
60 */
61 HWTEST_F(IdlSaUnitTest, IdlSaProxyTest001, TestSize.Level1)
62 {
63 std::cout << "IdlSaProxyTest001 start" << std::endl;
64
65 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
66 ASSERT_NE(client_, nullptr);
67
68 // sync func
69 double retDouble;
70 int32_t ret = client_->TestSaCallSa(100, retDouble);
71 std::cout << "TestSaCallSa client" << std::endl;
72 EXPECT_EQ(ret, ERR_OK);
73
74 int32_t times;
75 ret = client_->TestGetIpcSendRequestTimes(times);
76 std::cout << "TestGetIpcSendRequestTimes client" << std::endl;
77 EXPECT_EQ(ret, ERR_OK);
78 EXPECT_EQ(times, 1);
79
80 std::this_thread::sleep_for(std::chrono::seconds(2));
81 delete client_;
82 }
83
84 /*
85 * @tc.name: IdlSaProxyTest002
86 * @tc.desc: async func run
87 * @tc.type: FUNC
88 */
89 HWTEST_F(IdlSaUnitTest, IdlSaProxyTest002, TestSize.Level1)
90 {
91 std::cout << "IdlSaProxyTest002 start" << std::endl;
92
93 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
94 ASSERT_NE(client_, nullptr);
95
96 // async func
97 int32_t ret = client_->LoadSystemAbility(3);
98 client_->AddVolume(100);
99 EXPECT_EQ(ret, ERR_OK);
100
101 std::cout << "AddVolume client" << std::endl;
102 std::this_thread::sleep_for(std::chrono::seconds(2));
103 delete client_;
104 }
105
106 /*
107 * @tc.name: IdlSaRemoveDeathTest001
108 * @tc.desc: RemoveDeathRecipient test
109 * @tc.type: FUNC
110 */
111 HWTEST_F(IdlSaUnitTest, IdlSaRemoveDeathTest001, TestSize.Level1)
112 {
113 std::cout << "IdlSaRemoveDeathTest001 start" << std::endl;
114
115 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
116 ASSERT_NE(client_, nullptr);
117
__anon8a05f80c0102(bool status) 118 auto cb = [](bool status) {
119 std::cout << "LoadSystemAbility cb status: " << status << std::endl;
120 };
121
__anon8a05f80c0202() 122 auto cbDied = []() {
123 std::cout << "LoadSystemAbility cbDied." << std::endl;
124 };
125 client_->RegisterOnRemoteDiedCallback(cbDied);
126 int32_t ret = client_->LoadSystemAbility(cb);
127 EXPECT_EQ(ret, ERR_OK);
128
129 std::this_thread::sleep_for(std::chrono::minutes(2));
130 delete client_;
131 }
132
133 /*
134 * @tc.name: IdlSaLoadTest001
135 * @tc.desc: sync load sa
136 * @tc.type: FUNC
137 */
138 HWTEST_F(IdlSaUnitTest, IdlSaLoadTest001, TestSize.Level1)
139 {
140 std::cout << "IdlSaLoadTest001 start" << std::endl;
141
142 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
143 ASSERT_NE(client_, nullptr);
144
145 int32_t ret = client_->LoadSystemAbility(3);
146 EXPECT_EQ(ret, ERR_OK);
147
148 std::this_thread::sleep_for(std::chrono::seconds(2));
149 delete client_;
150 }
151
152 /*
153 * @tc.name: IdlSaLoadTest002
154 * @tc.desc: async load sa
155 * @tc.type: FUNC
156 */
157 HWTEST_F(IdlSaUnitTest, IdlSaLoadTest002, TestSize.Level1)
158 {
159 std::cout << "IdlSaLoadTest002 start" << std::endl;
160
161 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
162 ASSERT_NE(client_, nullptr);
163
__anon8a05f80c0302(bool status) 164 auto cb = [](bool status) {
165 std::cout << "LoadSystemAbility cb status: " << status << std::endl;
166 };
167
168 int32_t ret = client_->LoadSystemAbility(cb);
169 EXPECT_EQ(ret, ERR_OK);
170
171 std::this_thread::sleep_for(std::chrono::seconds(2));
172 delete client_;
173 }
174
175 /*
176 * @tc.name: IdlOverLoadTest001
177 * @tc.desc: async load sa
178 * @tc.type: FUNC
179 */
180 HWTEST_F(IdlSaUnitTest, IdlOverLoadTest001, TestSize.Level1)
181 {
182 std::cout << "IdlOverLoadTest001 start" << std::endl;
183
184 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
185 ASSERT_NE(client_, nullptr);
186
187 std::unordered_map<int32_t, int32_t> outApp;
188 int32_t outParam = 9;
189 outApp[4] = 11;
190 int32_t ret = client_->overloadfun(outParam);
191 #ifdef DEVICE
192 ret = client_->overloadfun(outApp);
193 #endif
194 std::cout << "TestOverLoad" << std::endl;
195 EXPECT_EQ(outParam, 10);
196 EXPECT_EQ(outApp[4], 7);
197 EXPECT_EQ(ret, ERR_OK);
198
199 std::this_thread::sleep_for(std::chrono::seconds(2));
200 delete client_;
201 }
202
203 /*
204 * @tc.name: IdlSaCustomTest001
205 * @tc.desc: async load sa
206 * @tc.type: FUNC
207 */
208 HWTEST_F(IdlSaUnitTest, IdlSaCustomTest001, TestSize.Level1)
209 {
210 std::cout << "IdlSaCustomTest001 start" << std::endl;
211
212 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
213 ASSERT_NE(client_, nullptr);
214
215 FooEnum in1 = FooEnum::ENUM_ONE, out1 = FooEnum::ENUM_ONE, inout1 = FooEnum::ENUM_ONE, result1 = FooEnum::ENUM_ONE;
216 int32_t ret = client_->enum_test_func(in1, out1, inout1, result1);
217 std::cout << "TestSaCallSa enum_test_func" << std::endl;
218 EXPECT_EQ(static_cast<int>(out1), 2);
219 EXPECT_EQ(ret, ERR_OK);
220
221 FooStruct in2 = { 1, "ExampleName", FooEnum::ENUM_ONE };
222 FooStruct inout2 = { 2, "AnotherName", FooEnum::ENUM_TWO };
223 FooStruct result2 = { 3, "ResultName", FooEnum::ENUM_NESTING};
224 RequestInfo out2 = {
225 {1, 2},
226 {{"key1", "value1"}, {"key2", "value2"}} // optionalData
227 };
228
229 ret = client_->struct_test_func(in2, out2, inout2, result2);
230 std::cout << "TestSaCallSa struct_test_func" << std::endl;
231 EXPECT_EQ(out2.initData[2], 3);
232 EXPECT_EQ(ret, ERR_OK);
233
234 FooUnion in3, out3, inout3, result3;
235 in3.enumType = FooEnum::ENUM_ONE;
236 ret = client_->union_test_func(in3, out3, inout3, result3);
237 std::cout << "TestSaCallSa union_test_func" << std::endl;
238 EXPECT_EQ(static_cast<int>(out3.enumType), 2);
239 EXPECT_EQ(ret, ERR_OK);
240
241 std::vector<std::string> quick;
242 bool isTrue = true;
243 ret = client_->ApplyQuickFix(quick, isTrue);
244 std::cout << "TestSaCallSa ApplyQuickFix" << std::endl;
245 EXPECT_EQ(ret, ERR_OK);
246
247 std::unordered_map<int32_t, FooStruct> inApp, outApp;
248 inApp[0].id = 11;
249 inApp[0].name = "shiyi";
250 ret = client_->GetAllAppSuspendState(inApp, outApp);
251 std::cout << "TestSaCallSa GetAllAppSuspendState" << std::endl;
252 EXPECT_EQ(outApp[1].id, 999);
253 EXPECT_EQ(outApp[1].name, "MapTest");
254 EXPECT_EQ(ret, ERR_OK);
255
256 std::this_thread::sleep_for(std::chrono::seconds(2));
257 delete client_;
258 }
259
260 /*
261 * @tc.name: IdlSaCustomTest001
262 * @tc.desc: async load sa
263 * @tc.type: FUNC
264 */
265 HWTEST_F(IdlSaUnitTest, IdlRawDataTest001, TestSize.Level1)
266 {
267 std::cout << "IdlRawDataTest001 start" << std::endl;
268
269 ListenAbilityClient* client_ = new ListenAbilityClient(1494);
270 ASSERT_NE(client_, nullptr);
271
272 MyRawdata in, out, inout, result;
273 const char sampleData[] = "Hello!";
274
275 in.size = sizeof(sampleData);
276 in.RawDataCpy(sampleData);
277 out.size = sizeof(sampleData);
278 out.RawDataCpy(sampleData);
279 inout.size = sizeof(sampleData);
280 inout.RawDataCpy(sampleData);
281 int32_t ret = client_->rawdata_test_func(in, out, inout, result);
282 std::cout << "TestRawData" << std::endl;
283
284 EXPECT_STREQ(static_cast<const char*>(out.data), "Hello, world!");
285 EXPECT_EQ(out.size, 14);
286 EXPECT_STREQ(static_cast<const char*>(inout.data), "world!");
287 EXPECT_EQ(inout.size, 7);
288 EXPECT_EQ(ret, ERR_OK);
289
290 std::this_thread::sleep_for(std::chrono::seconds(2));
291 delete client_;
292 }
293
294 } // namespace idl
295 } // namespace OHOS