1 /*
2 * Copyright (C) 2021-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 #define private public
17 #define protected public
18 #include "state_registry_test.h"
19
20 #include "core_service_client.h"
21 #include "sim_state_type.h"
22 #include "telephony_log_wrapper.h"
23 #include "telephony_observer_client.h"
24 #include "telephony_observer_proxy.h"
25 #include "telephony_state_manager.h"
26 #include "telephony_state_registry_client.h"
27 #include "telephony_state_registry_service.h"
28
29 namespace OHOS {
30 namespace Telephony {
31 using namespace testing::ext;
32 sptr<StateRegistryObserver> StateRegistryTest::telephonyObserver0_ = nullptr;
33 sptr<StateRegistryObserver> StateRegistryTest::telephonyObserver1_ = nullptr;
34 static constexpr int32_t SIM_SLOT_ID_1 = DEFAULT_SIM_SLOT_ID + 1;
35 static constexpr int32_t CALL_STATUS_ACTIVE = 0;
36 static constexpr int32_t SINGLE_MODE_SIM_CARD = 10;
37 static constexpr int32_t SIM_STATE_NOT_PRESENT = 1;
38 static constexpr int32_t SIM_PIN = 1;
39 static constexpr int32_t DATA_STATE_CONNECTING = 1;
40 static constexpr int32_t NETWORK_TYPE_GSM = 1;
41 static constexpr int32_t DATA_FLOW_TYPE_DOWN = 1;
42
SetUpTestCase(void)43 void StateRegistryTest::SetUpTestCase(void)
44 {
45 ASSERT_TRUE(CoreServiceClient::GetInstance().GetProxy() != nullptr);
46 InitTelephonyObserver();
47 }
48
TearDownTestCase(void)49 void StateRegistryTest::TearDownTestCase(void)
50 {
51 DisableTelephonyObserver();
52 }
53
InitTelephonyObserver()54 void StateRegistryTest::InitTelephonyObserver()
55 {
56 TELEPHONY_LOGI("Init telephony observer");
57 AccessToken token;
58 if (!telephonyObserver0_) {
59 telephonyObserver0_ = std::make_unique<StateRegistryObserver>().release();
60 }
61 auto res =
62 Telephony::TelephonyObserverClient::GetInstance().AddStateObserver(telephonyObserver0_, DEFAULT_SIM_SLOT_ID,
63 Telephony::TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE |
64 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE |
65 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO |
66 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIGNAL_STRENGTHS |
67 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIM_STATE |
68 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE |
69 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW |
70 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CFU_INDICATOR |
71 Telephony::TelephonyObserverBroker::OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR,
72 true);
73 TELEPHONY_LOGI("StateRegistryTest init telephony observer0 ret:%{public}d", res);
74 ASSERT_TRUE(res == TELEPHONY_SUCCESS);
75 if (!telephonyObserver1_) {
76 telephonyObserver1_ = std::make_unique<StateRegistryObserver>().release();
77 }
78 res = Telephony::TelephonyObserverClient::GetInstance().AddStateObserver(telephonyObserver1_, SIM_SLOT_ID_1,
79 Telephony::TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE |
80 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE |
81 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO |
82 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIGNAL_STRENGTHS |
83 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIM_STATE |
84 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE |
85 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW |
86 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CFU_INDICATOR |
87 Telephony::TelephonyObserverBroker::OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR,
88 true);
89 TELEPHONY_LOGI("StateRegistryTest init telephony observer1 ret:%{public}d", res);
90 ASSERT_TRUE(res == TELEPHONY_SUCCESS);
91 }
92
DisableTelephonyObserver()93 void StateRegistryTest::DisableTelephonyObserver()
94 {
95 TELEPHONY_LOGI("Disable telephony observer");
96 ASSERT_TRUE(telephonyObserver0_ != nullptr);
97 Telephony::TelephonyObserverClient::GetInstance().RemoveStateObserver(
98 DEFAULT_SIM_SLOT_ID, Telephony::TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE |
99 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE |
100 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO |
101 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIGNAL_STRENGTHS |
102 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIM_STATE |
103 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE |
104 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW);
105 telephonyObserver0_ = nullptr;
106 ASSERT_TRUE(telephonyObserver1_ != nullptr);
107 Telephony::TelephonyObserverClient::GetInstance().RemoveStateObserver(
108 SIM_SLOT_ID_1, Telephony::TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE |
109 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE |
110 Telephony::TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO |
111 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIGNAL_STRENGTHS |
112 Telephony::TelephonyObserverBroker::OBSERVER_MASK_SIM_STATE |
113 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE |
114 Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW);
115 telephonyObserver1_ = nullptr;
116 }
117
SetUp(void)118 void StateRegistryTest::SetUp(void)
119 {
120 // step 3: input testcase setup step
121 }
122
TearDown(void)123 void StateRegistryTest::TearDown(void)
124 {
125 // step 3: input testcase teardown step
126 }
127
HasSimCard(int32_t slotId)128 bool StateRegistryTest::HasSimCard(int32_t slotId)
129 {
130 bool hasSimCard = false;
131 CoreServiceClient::GetInstance().HasSimCard(slotId, hasSimCard);
132 return hasSimCard;
133 }
134
UpdateCallState(int32_t slotId)135 void StateRegistryTest::UpdateCallState(int32_t slotId)
136 {
137 AccessToken token;
138 int32_t callState = 16;
139 std::string phoneNumber("137xxxxxxxx");
140 std::u16string number = Str8ToStr16(phoneNumber);
141 int32_t ret =
142 DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCallState(slotId, callState, number);
143 TELEPHONY_LOGI("StateRegistryTest::UpdateCallState ret = %{public}d", ret);
144 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
145 }
146
UpdateCallStateForSlotId(int32_t slotId)147 void StateRegistryTest::UpdateCallStateForSlotId(int32_t slotId)
148 {
149 AccessToken token;
150 int32_t callState = 16;
151 int32_t callId = 0;
152 std::string phoneNumber("137xxxxxxxx");
153 std::u16string number = Str8ToStr16(phoneNumber);
154 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCallStateForSlotId(
155 slotId, callId, callState, number);
156 TELEPHONY_LOGI("StateRegistryTest::UpdateCallStateForSlotId ret = %{public}d", ret);
157 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
158 }
159
UpdateSignalInfo(int32_t slotId)160 void StateRegistryTest::UpdateSignalInfo(int32_t slotId)
161 {
162 AccessToken token;
163 std::vector<sptr<SignalInformation>> vec;
164 std::unique_ptr<SignalInformation> signal = std::make_unique<GsmSignalInformation>();
165 ASSERT_TRUE(signal != nullptr);
166 vec.push_back(signal.release());
167 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateSignalInfo(slotId, vec);
168 TELEPHONY_LOGI("StateRegistryTest::UpdateSignalInfo result = %{public}d", ret);
169 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
170 }
171
UpdateCellularDataConnectState(int32_t slotId)172 void StateRegistryTest::UpdateCellularDataConnectState(int32_t slotId)
173 {
174 AccessToken token;
175 int32_t dataState = 1;
176 int32_t networkState = 1;
177 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCellularDataConnectState(
178 slotId, dataState, networkState);
179 TELEPHONY_LOGI("StateRegistryTest::UpdateCellularDataConnectState ret = %{public}d", ret);
180 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
181 }
182
UpdateCellularDataFlow(int32_t slotId)183 void StateRegistryTest::UpdateCellularDataFlow(int32_t slotId)
184 {
185 AccessToken token;
186 int32_t dataFlowType = 0;
187 int32_t ret =
188 DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCellularDataFlow(slotId, dataFlowType);
189 TELEPHONY_LOGI("StateRegistryTest::UpdateCellularDataFlow ret = %{public}d", ret);
190 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
191 }
192
UpdateSimState(int32_t slotId)193 void StateRegistryTest::UpdateSimState(int32_t slotId)
194 {
195 AccessToken token;
196 CardType type = CardType::UNKNOWN_CARD;
197 SimState state = SimState::SIM_STATE_UNKNOWN;
198 LockReason reason = LockReason::SIM_NONE;
199 int32_t ret =
200 DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateSimState(slotId, type, state, reason);
201 TELEPHONY_LOGI("StateRegistryTest::UpdateSimState ret = %{public}d", ret);
202 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
203 }
204
UpdateNetworkState(int32_t slotId)205 void StateRegistryTest::UpdateNetworkState(int32_t slotId)
206 {
207 AccessToken token;
208 std::unique_ptr<NetworkState> networkState = std::make_unique<NetworkState>();
209 ASSERT_TRUE(networkState != nullptr);
210 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateNetworkState(
211 slotId, networkState.release());
212 TELEPHONY_LOGI("StateRegistryTest::UpdateNetworkState ret = %{public}d", ret);
213 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
214 }
215
UpdateCfuIndicator(int32_t slotId)216 void StateRegistryTest::UpdateCfuIndicator(int32_t slotId)
217 {
218 AccessToken token;
219 bool cfuResult = true;
220 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCfuIndicator(
221 slotId, cfuResult);
222 TELEPHONY_LOGI("StateRegistryTest::UpdateCfuIndicator ret = %{public}d", ret);
223 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
224 }
225
UpdateVoiceMailMsgIndicator(int32_t slotId)226 void StateRegistryTest::UpdateVoiceMailMsgIndicator(int32_t slotId)
227 {
228 AccessToken token;
229 bool voiceMailMsgResult = true;
230 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateVoiceMailMsgIndicator(
231 slotId, voiceMailMsgResult);
232 TELEPHONY_LOGI("StateRegistryTest::UpdateVoiceMailMsgIndicator ret = %{public}d", ret);
233 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
234 }
235
UpdateIccAccount()236 void StateRegistryTest::UpdateIccAccount()
237 {
238 int32_t ret = DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateIccAccount();
239 TELEPHONY_LOGI("StateRegistryTest::UpdateIccAccount ret = %{public}d", ret);
240 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
241 }
242
243 #ifndef TEL_TEST_UNSUPPORT
244 /**
245 * @tc.number StateRegistry_001
246 * @tc.name Get System Services
247 * @tc.desc Function test
248 */
249 HWTEST_F(StateRegistryTest, StateRegistry_001, Function | MediumTest | Level0)
250 {
251 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
252 ASSERT_TRUE(systemAbilityMgr != nullptr);
253 auto remote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
254 ASSERT_TRUE(remote != nullptr);
255 auto stateRegistryService = iface_cast<ITelephonyStateNotify>(remote);
256 ASSERT_TRUE(stateRegistryService != nullptr);
257 TELEPHONY_LOGI("HWTEST_F StateRegistry_001");
258 }
259
260 /**
261 * @tc.number UpdateCallState_001
262 * @tc.name update call state
263 * @tc.desc Function test
264 */
265 HWTEST_F(StateRegistryTest, UpdateCallState_001, Function | MediumTest | Level1)
266 {
267 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
268 return;
269 }
270 UpdateCallState(DEFAULT_SIM_SLOT_ID);
271 }
272
273 /**
274 * @tc.number UpdateCallState_002
275 * @tc.name update call state
276 * @tc.desc Function test
277 */
278 HWTEST_F(StateRegistryTest, UpdateCallState_002, Function | MediumTest | Level1)
279 {
280 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
281 return;
282 }
283 UpdateCallState(SIM_SLOT_ID_1);
284 }
285
286 /**
287 * @tc.number UpdateCallStateForSlotId_001
288 * @tc.name update call state by slotId
289 * @tc.desc Function test
290 */
291 HWTEST_F(StateRegistryTest, UpdateCallStateForSlotId_001, Function | MediumTest | Level1)
292 {
293 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
294 return;
295 }
296 UpdateCallStateForSlotId(DEFAULT_SIM_SLOT_ID);
297 }
298
299 /**
300 * @tc.number UpdateCallStateForSlotId_002
301 * @tc.name update call state by slotId
302 * @tc.desc Function test
303 */
304 HWTEST_F(StateRegistryTest, UpdateCallStateForSlotId_002, Function | MediumTest | Level1)
305 {
306 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
307 return;
308 }
309 UpdateCallStateForSlotId(SIM_SLOT_ID_1);
310 }
311
312 /**
313 * @tc.number UpdateSignalInfo_001
314 * @tc.name update signal info
315 * @tc.desc Function test
316 */
317 HWTEST_F(StateRegistryTest, UpdateSignalInfo_001, Function | MediumTest | Level1)
318 {
319 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
320 return;
321 }
322 UpdateSignalInfo(DEFAULT_SIM_SLOT_ID);
323 }
324
325 /**
326 * @tc.number UpdateSignalInfo_002
327 * @tc.name update signal info
328 * @tc.desc Function test
329 */
330 HWTEST_F(StateRegistryTest, UpdateSignalInfo_002, Function | MediumTest | Level1)
331 {
332 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
333 return;
334 }
335 UpdateSignalInfo(SIM_SLOT_ID_1);
336 }
337
338 /**
339 * @tc.number UpdateCellularDataConnectState_001
340 * @tc.name update cellular data connect state
341 * @tc.desc Function test
342 */
343 HWTEST_F(StateRegistryTest, UpdateCellularDataConnectState_001, Function | MediumTest | Level1)
344 {
345 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
346 return;
347 }
348 UpdateCellularDataConnectState(DEFAULT_SIM_SLOT_ID);
349 }
350
351 /**
352 * @tc.number UpdateCellularDataConnectState_002
353 * @tc.name update cellular data connect state
354 * @tc.desc Function test
355 */
356 HWTEST_F(StateRegistryTest, UpdateCellularDataConnectState_002, Function | MediumTest | Level1)
357 {
358 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
359 return;
360 }
361 UpdateCellularDataConnectState(SIM_SLOT_ID_1);
362 }
363
364 /**
365 * @tc.number UpdateCellularDataFlow_001
366 * @tc.name update cellular flow data
367 * @tc.desc Function test
368 */
369 HWTEST_F(StateRegistryTest, UpdateCellularDataFlow_001, Function | MediumTest | Level1)
370 {
371 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
372 return;
373 }
374 UpdateCellularDataFlow(DEFAULT_SIM_SLOT_ID);
375 }
376
377 /**
378 * @tc.number UpdateCellularDataFlow_002
379 * @tc.name update cellular flow data
380 * @tc.desc Function test
381 */
382 HWTEST_F(StateRegistryTest, UpdateCellularDataFlow_002, Function | MediumTest | Level1)
383 {
384 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
385 return;
386 }
387 UpdateCellularDataFlow(SIM_SLOT_ID_1);
388 }
389
390 /**
391 * @tc.number UpdateSimState_001
392 * @tc.name update sim state
393 * @tc.desc Function test
394 */
395 HWTEST_F(StateRegistryTest, UpdateSimState_001, Function | MediumTest | Level1)
396 {
397 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
398 return;
399 }
400 UpdateSimState(DEFAULT_SIM_SLOT_ID);
401 }
402
403 /**
404 * @tc.number UpdateSimState_002
405 * @tc.name update sim state
406 * @tc.desc Function test
407 */
408 HWTEST_F(StateRegistryTest, UpdateSimState_002, Function | MediumTest | Level1)
409 {
410 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
411 return;
412 }
413 UpdateSimState(SIM_SLOT_ID_1);
414 }
415
416 /**
417 * @tc.number UpdateNetworkState_001
418 * @tc.name update network state
419 * @tc.desc Function test
420 */
421 HWTEST_F(StateRegistryTest, UpdateNetworkState_001, Function | MediumTest | Level1)
422 {
423 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
424 return;
425 }
426 UpdateNetworkState(DEFAULT_SIM_SLOT_ID);
427 }
428
429 /**
430 * @tc.number UpdateNetworkState_002
431 * @tc.name update network state
432 * @tc.desc Function test
433 */
434 HWTEST_F(StateRegistryTest, UpdateNetworkState_002, Function | MediumTest | Level1)
435 {
436 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
437 return;
438 }
439 UpdateNetworkState(SIM_SLOT_ID_1);
440 }
441
442 /**
443 * @tc.number UpdateCfuIndicator_001
444 * @tc.name update the result of call forwarding
445 * @tc.desc Function test
446 */
447 HWTEST_F(StateRegistryTest, UpdateCfuIndicator_001, Function | MediumTest | Level1)
448 {
449 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
450 return;
451 }
452 UpdateCfuIndicator(DEFAULT_SIM_SLOT_ID);
453 }
454
455 /**
456 * @tc.number UpdateCfuIndicator_002
457 * @tc.name update the result of call forwarding
458 * @tc.desc Function test
459 */
460 HWTEST_F(StateRegistryTest, UpdateCfuIndicator_002, Function | MediumTest | Level1)
461 {
462 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
463 return;
464 }
465 UpdateCfuIndicator(SIM_SLOT_ID_1);
466 }
467
468 /**
469 * @tc.number UpdateVoiceMailMsgIndicator_001
470 * @tc.name update voice mail message indicator
471 * @tc.desc Function test
472 */
473 HWTEST_F(StateRegistryTest, UpdateVoiceMailMsgIndicator_001, Function | MediumTest | Level1)
474 {
475 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
476 return;
477 }
478 UpdateVoiceMailMsgIndicator(DEFAULT_SIM_SLOT_ID);
479 }
480
481 /**
482 * @tc.number UpdateVoiceMailMsgIndicator_002
483 * @tc.name update voice mail message indicator
484 * @tc.desc Function test
485 */
486 HWTEST_F(StateRegistryTest, UpdateVoiceMailMsgIndicator_002, Function | MediumTest | Level1)
487 {
488 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
489 return;
490 }
491 UpdateVoiceMailMsgIndicator(SIM_SLOT_ID_1);
492 }
493
494 /**
495 * @tc.number UpdateIccAccount_001
496 * @tc.name Update Icc Account
497 * @tc.desc Function test
498 */
499 HWTEST_F(StateRegistryTest, UpdateIccAccount_001, Function | MediumTest | Level1)
500 {
501 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
502 return;
503 }
504 UpdateIccAccount();
505 }
506
507 /**
508 * @tc.number UpdateIccAccount_002
509 * @tc.name UpdateIccAccount
510 * @tc.desc Function test
511 */
512 HWTEST_F(StateRegistryTest, UpdateIccAccount_002, Function | MediumTest | Level1)
513 {
514 if (!StateRegistryTest::HasSimCard(SIM_SLOT_ID_1)) {
515 return;
516 }
517 UpdateIccAccount();
518 }
519
520 /**
521 * @tc.number TelephonyStateManagerTest_001
522 * @tc.name telephony state manager add/remove state observer test
523 * @tc.desc Function test
524 */
525 HWTEST_F(StateRegistryTest, TelephonyStateManagerTest_001, Function | MediumTest | Level1)
526 {
527 TELEPHONY_LOGI("TelephonyStateManagerTest_001 start!");
528 if (!StateRegistryTest::HasSimCard(DEFAULT_SIM_SLOT_ID)) {
529 return;
530 }
531 TelephonyStateManager::AddStateObserver(
532 telephonyObserver0_, DEFAULT_SIM_SLOT_ID, TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE, true);
533 wptr<IRemoteObject> wptrDeath = nullptr;
534 int32_t ret = TelephonyStateManager::RemoveStateObserver(
535 DEFAULT_SIM_SLOT_ID, Telephony::TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE);
536 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
537 }
538
539 /**
540 * @tc.number TelephonyObserverTest_001
541 * @tc.name telephony observer test
542 * @tc.desc Function test
543 */
544 HWTEST_F(StateRegistryTest, TelephonyObserverTest_001, Function | MediumTest | Level1)
545 {
546 TELEPHONY_LOGI("TelephonyObserverTest_001 start!");
547 MessageParcel dataParcel;
548 MessageParcel reply;
549 MessageOption option;
550 option.SetFlags(MessageOption::TF_ASYNC);
551 std::u16string testStr = u"test";
552 if (!dataParcel.WriteInterfaceToken(testStr)) {
553 TELEPHONY_LOGE("TelephonyObserverTest_001 WriteInterfaceToken failed!");
554 return;
555 }
556 int32_t ret = telephonyObserver.OnRemoteRequest(
557 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_CALL_STATE_UPDATED), dataParcel, reply,
558 option);
559 EXPECT_EQ(TELEPHONY_ERR_DESCRIPTOR_MISMATCH, ret);
560 }
561
562 /**
563 * @tc.number TelephonyObserverTest_002
564 * @tc.name telephony observer test
565 * @tc.desc Function test
566 */
567 HWTEST_F(StateRegistryTest, TelephonyObserverTest_002, Function | MediumTest | Level1)
568 {
569 TELEPHONY_LOGI("TelephonyObserverTest_002 start!");
570 MessageParcel dataParcel;
571 MessageParcel reply;
572 MessageOption option;
573 std::u16string phoneNumber = u"123456";
574 option.SetFlags(MessageOption::TF_ASYNC);
575 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
576 TELEPHONY_LOGE("TelephonyObserverTest_002 WriteInterfaceToken failed!");
577 return;
578 }
579 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
580 dataParcel.WriteInt32(CALL_STATUS_ACTIVE);
581 dataParcel.WriteString16(phoneNumber);
582 int32_t ret = telephonyObserver.OnRemoteRequest(
583 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_CALL_STATE_UPDATED), dataParcel, reply,
584 option);
585 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
586 }
587
588 /**
589 * @tc.number TelephonyObserverTest_003
590 * @tc.name telephony observer test
591 * @tc.desc Function test
592 */
593 HWTEST_F(StateRegistryTest, TelephonyObserverTest_003, Function | MediumTest | Level1)
594 {
595 TELEPHONY_LOGI("TelephonyObserverTest_003 start!");
596 MessageOption option;
597 MessageParcel dataParcel;
598 MessageParcel reply;
599 option.SetFlags(MessageOption::TF_ASYNC);
600 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
601 TELEPHONY_LOGE("TelephonyObserverTest_003 WriteInterfaceToken failed!");
602 return;
603 }
604 std::vector<sptr<SignalInformation>> vec;
605 std::unique_ptr<SignalInformation> gsmSignal = std::make_unique<GsmSignalInformation>();
606 ASSERT_TRUE(gsmSignal != nullptr);
607 vec.push_back(gsmSignal.release());
608 std::unique_ptr<WcdmaSignalInformation> wcdmaSignal = std::make_unique<WcdmaSignalInformation>();
609 ASSERT_TRUE(wcdmaSignal != nullptr);
610 vec.push_back(wcdmaSignal.release());
611 std::unique_ptr<TdScdmaSignalInformation> tdScdmaSignal = std::make_unique<TdScdmaSignalInformation>();
612 ASSERT_TRUE(tdScdmaSignal != nullptr);
613 vec.push_back(tdScdmaSignal.release());
614 std::unique_ptr<CdmaSignalInformation> cdmaSignal = std::make_unique<CdmaSignalInformation>();
615 ASSERT_TRUE(cdmaSignal != nullptr);
616 vec.push_back(cdmaSignal.release());
617 std::unique_ptr<LteSignalInformation> lteSignal = std::make_unique<LteSignalInformation>();
618 ASSERT_TRUE(lteSignal != nullptr);
619 vec.push_back(lteSignal.release());
620 std::unique_ptr<NrSignalInformation> nrSignal = std::make_unique<NrSignalInformation>();
621 ASSERT_TRUE(nrSignal != nullptr);
622 vec.push_back(nrSignal.release());
623 int32_t size = static_cast<int32_t>(vec.size());
624 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
625 dataParcel.WriteInt32(size);
626 for (const auto &v : vec) {
627 v->Marshalling(dataParcel);
628 }
629 int32_t ret = telephonyObserver.OnRemoteRequest(
630 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_SIGNAL_INFO_UPDATED), dataParcel, reply,
631 option);
632 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
633 }
634
635 /**
636 * @tc.number TelephonyObserverTest_004
637 * @tc.name telephony observer test
638 * @tc.desc Function test
639 */
640 HWTEST_F(StateRegistryTest, TelephonyObserverTest_004, Function | MediumTest | Level1)
641 {
642 TELEPHONY_LOGI("TelephonyObserverTest_004 start!");
643 MessageOption option;
644 MessageParcel dataParcel;
645 MessageParcel reply;
646 option.SetFlags(MessageOption::TF_ASYNC);
647 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
648 TELEPHONY_LOGE("TelephonyObserverTest_004 WriteInterfaceToken failed!");
649 return;
650 }
651 std::vector<sptr<CellInformation>> vec;
652 std::unique_ptr<GsmCellInformation> gsmCell = std::make_unique<GsmCellInformation>();
653 ASSERT_TRUE(gsmCell != nullptr);
654 vec.push_back(gsmCell.release());
655 std::unique_ptr<LteCellInformation> lteCell = std::make_unique<LteCellInformation>();
656 ASSERT_TRUE(lteCell != nullptr);
657 vec.push_back(lteCell.release());
658 std::unique_ptr<WcdmaCellInformation> wcdmaCell = std::make_unique<WcdmaCellInformation>();
659 ASSERT_TRUE(wcdmaCell != nullptr);
660 vec.push_back(wcdmaCell.release());
661 std::unique_ptr<NrCellInformation> nrCell = std::make_unique<NrCellInformation>();
662 ASSERT_TRUE(nrCell != nullptr);
663 vec.push_back(nrCell.release());
664 int32_t size = static_cast<int32_t>(vec.size());
665 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
666 if (!dataParcel.WriteInt32(size)) {
667 TELEPHONY_LOGE("Failed to write Cellinformation array size!");
668 return;
669 }
670 for (const auto &v : vec) {
671 v->Marshalling(dataParcel);
672 }
673 int32_t ret = telephonyObserver.OnRemoteRequest(
674 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_CELL_INFO_UPDATED), dataParcel, reply,
675 option);
676 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
677 }
678
679 /**
680 * @tc.number TelephonyObserverTest_005
681 * @tc.name telephony observer test
682 * @tc.desc Function test
683 */
684 HWTEST_F(StateRegistryTest, TelephonyObserverTest_005, Function | MediumTest | Level1)
685 {
686 TELEPHONY_LOGI("TelephonyObserverTest_005 start!");
687 MessageOption option;
688 MessageParcel dataParcel;
689 MessageParcel reply;
690 option.SetFlags(MessageOption::TF_ASYNC);
691 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
692 TELEPHONY_LOGE("TelephonyObserverTest_005 WriteInterfaceToken failed!");
693 return;
694 }
695 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
696 std::unique_ptr<NetworkState> networkState = std::make_unique<NetworkState>();
697 ASSERT_TRUE(networkState != nullptr);
698 (networkState.release())->Marshalling(dataParcel);
699 int32_t ret = telephonyObserver.OnRemoteRequest(
700 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_NETWORK_STATE_UPDATED), dataParcel, reply,
701 option);
702 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
703 }
704
705 /**
706 * @tc.number TelephonyObserverTest_006
707 * @tc.name telephony observer test
708 * @tc.desc Function test
709 */
710 HWTEST_F(StateRegistryTest, TelephonyObserverTest_006, Function | MediumTest | Level1)
711 {
712 TELEPHONY_LOGI("TelephonyObserverTest_006 start!");
713 MessageOption option;
714 MessageParcel dataParcel;
715 MessageParcel reply;
716 option.SetFlags(MessageOption::TF_ASYNC);
717 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
718 TELEPHONY_LOGE("TelephonyObserverTest_006 WriteInterfaceToken failed!");
719 return;
720 }
721 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
722 dataParcel.WriteInt32(SINGLE_MODE_SIM_CARD);
723 dataParcel.WriteInt32(SIM_STATE_NOT_PRESENT);
724 dataParcel.WriteInt32(SIM_PIN);
725 int32_t ret = telephonyObserver.OnRemoteRequest(
726 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_SIM_STATE_UPDATED), dataParcel, reply,
727 option);
728 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
729 }
730
731 /**
732 * @tc.number TelephonyObserverTest_007
733 * @tc.name telephony observer test
734 * @tc.desc Function test
735 */
736 HWTEST_F(StateRegistryTest, TelephonyObserverTest_007, Function | MediumTest | Level1)
737 {
738 TELEPHONY_LOGI("TelephonyObserverTest_007 start!");
739 MessageOption option;
740 MessageParcel dataParcel;
741 MessageParcel reply;
742 option.SetFlags(MessageOption::TF_ASYNC);
743 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
744 TELEPHONY_LOGE("TelephonyObserverTest_007 WriteInterfaceToken failed!");
745 return;
746 }
747 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
748 dataParcel.WriteInt32(DATA_STATE_CONNECTING);
749 dataParcel.WriteInt32(NETWORK_TYPE_GSM);
750 int32_t ret = telephonyObserver.OnRemoteRequest(
751 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_CELLULAR_DATA_CONNECT_STATE_UPDATED),
752 dataParcel, reply, option);
753 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
754 }
755
756 /**
757 * @tc.number TelephonyObserverTest_008
758 * @tc.name telephony observer test
759 * @tc.desc Function test
760 */
761 HWTEST_F(StateRegistryTest, TelephonyObserverTest_008, Function | MediumTest | Level1)
762 {
763 TELEPHONY_LOGI("TelephonyObserverTest_008 start!");
764 MessageOption option;
765 MessageParcel dataParcel;
766 MessageParcel reply;
767 option.SetFlags(MessageOption::TF_ASYNC);
768 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
769 TELEPHONY_LOGE("TelephonyObserverTest_008 WriteInterfaceToken failed!");
770 return;
771 }
772 dataParcel.WriteInt32(DEFAULT_SIM_SLOT_ID);
773 dataParcel.WriteInt32(DATA_FLOW_TYPE_DOWN);
774 int32_t ret = telephonyObserver.OnRemoteRequest(
775 static_cast<uint32_t>(TelephonyObserverBroker::ObserverBrokerCode::ON_CELLULAR_DATA_FLOW_UPDATED), dataParcel,
776 reply, option);
777 EXPECT_EQ(TELEPHONY_ERR_SUCCESS, ret);
778 }
779
780 /**
781 * @tc.number TelephonyObserverTest_009
782 * @tc.name telephony observer test
783 * @tc.desc Function test
784 */
785 HWTEST_F(StateRegistryTest, TelephonyObserverTest_009, Function | MediumTest | Level1)
786 {
787 TELEPHONY_LOGI("TelephonyObserverTest_009 start!");
788 MessageOption option;
789 MessageParcel dataParcel;
790 MessageParcel reply;
791 option.SetFlags(MessageOption::TF_ASYNC);
792 if (!dataParcel.WriteInterfaceToken(TelephonyObserverProxy::GetDescriptor())) {
793 TELEPHONY_LOGE("TelephonyObserverTest_009 WriteInterfaceToken failed!");
794 return;
795 }
796 uint32_t testId = 123;
797 int32_t ret = telephonyObserver.OnRemoteRequest(testId, dataParcel, reply, option);
798 EXPECT_NE(TELEPHONY_ERR_SUCCESS, ret);
799 }
800
801 /**
802 * @tc.number TelephonyObserverTest_010
803 * @tc.name telephony observer test
804 * @tc.desc Function test
805 */
806 HWTEST_F(StateRegistryTest, TelephonyObserverTest_010, Function | MediumTest | Level1)
807 {
808 int32_t signalSize = 5;
809 int32_t cellInfoMaxSize = 11;
810 int32_t callState = 16;
811 std::u16string number = u"137xxxxxxxx";
812 std::unique_ptr<Telephony::TelephonyObserverClient> telephonyObserverClient =
813 std::make_unique<Telephony::TelephonyObserverClient>();
814 telephonyObserverClient->OnRemoteDied(nullptr);
815 telephonyObserverClient->proxy_ = nullptr;
816 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
817 sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
818 telephonyObserverClient->OnRemoteDied(obj);
819 telephonyObserverClient->proxy_ = iface_cast<ITelephonyStateNotify>(obj);
820 telephonyObserverClient->OnRemoteDied(obj);
821 std::shared_ptr<OHOS::Telephony::TelephonyObserverProxy> telephonyObserverProxy =
822 std::make_shared<OHOS::Telephony::TelephonyObserverProxy>(obj);
823 telephonyObserverProxy->OnCallStateUpdated(DEFAULT_SIM_SLOT_ID, callState, number);
824 std::vector<sptr<SignalInformation>> signalInformations;
825 telephonyObserverProxy->OnSignalInfoUpdated(DEFAULT_SIM_SLOT_ID, signalInformations);
826 for (int32_t i = 0; i < signalSize; i++) {
827 sptr<SignalInformation> signalInformation = sptr<SignalInformation>();
828 signalInformations.push_back(signalInformation);
829 }
830 telephonyObserverProxy->OnSignalInfoUpdated(DEFAULT_SIM_SLOT_ID, signalInformations);
831 std::vector<sptr<CellInformation>> cellInformations;
832 telephonyObserverProxy->OnCellInfoUpdated(DEFAULT_SIM_SLOT_ID, cellInformations);
833 for (int32_t i = 0; i < cellInfoMaxSize; i++) {
834 sptr<CellInformation> cellInfo = sptr<CellInformation>();
835 cellInformations.push_back(cellInfo);
836 }
837 telephonyObserverProxy->OnCellInfoUpdated(DEFAULT_SIM_SLOT_ID, cellInformations);
838 EXPECT_TRUE(telephonyObserverClient != nullptr);
839 EXPECT_TRUE(telephonyObserverProxy != nullptr);
840 EXPECT_GE(signalInformations.size(), static_cast<size_t>(0));
841 EXPECT_GE(cellInformations.size(), static_cast<size_t>(0));
842 }
843
844 /**
845 * @tc.number TelephonyObserverTest_011
846 * @tc.name telephony observer test
847 * @tc.desc Function test
848 */
849 HWTEST_F(StateRegistryTest, TelephonyObserverTest_011, Function | MediumTest | Level1)
850 {
851 int32_t slotId = 0;
852 bool cfuResult = false;
853 std::shared_ptr<OHOS::Telephony::TelephonyObserver> telephonyObserver =
854 std::make_shared<OHOS::Telephony::TelephonyObserver>();
855 telephonyObserver->OnCfuIndicatorUpdated(slotId, cfuResult);
856 telephonyObserver->OnVoiceMailMsgIndicatorUpdated(slotId, cfuResult);
857 MessageParcel data;
858 MessageParcel reply;
859 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
860 telephonyObserver->OnIccAccountUpdatedInner(data, reply);
861 sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
862 std::shared_ptr<OHOS::Telephony::TelephonyObserverProxy> telephonyObserverProxy =
863 std::make_shared<OHOS::Telephony::TelephonyObserverProxy>(obj);
864 telephonyObserverProxy->OnIccAccountUpdated();
865 EXPECT_TRUE(telephonyObserver != nullptr);
866 EXPECT_TRUE(telephonyObserverProxy != nullptr);
867 }
868
869 /**
870 * @tc.number TelephonyStateRegistryServiceTest_001
871 * @tc.name telephony state registry service test
872 * @tc.desc Function test
873 */
874 HWTEST_F(StateRegistryTest, TelephonyStateRegistryServiceTest_001, Function | MediumTest | Level1)
875 {
876 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
877 if (service == nullptr) {
878 TELEPHONY_LOGE("TelephonyStateRegistryServiceTest_001 service is nullptr");
879 return;
880 }
881 EXPECT_TRUE(service->IsCommonEventServiceAbilityExist());
882 }
883
884 #else // TEL_TEST_UNSUPPORT
885 /**
886 * @tc.number State_MockTest_001
887 * @tc.name Mock test for unsupport platform
888 * @tc.desc Function test
889 */
890 HWTEST_F(StateRegistryTest, State_MockTest_001, Function | MediumTest | Level1)
891 {
892 EXPECT_TRUE(true);
893 }
894 #endif // TEL_TEST_UNSUPPORT
895 } // namespace Telephony
896 } // namespace OHOS
897