1 /* 2 * Copyright (C) 2025-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 <gtest/gtest.h> 17 #include "cJSON.h" 18 #include "ims_call.h" 19 #include "server_session.h" 20 #include "call_object_manager.h" 21 #include "interoperable_server_manager.h" 22 #include "interoperable_data_controller.h" 23 24 namespace OHOS { 25 namespace Telephony { 26 using namespace testing::ext; 27 28 class SessionCallbackForTest : public ISessionCallback { 29 public: OnConnected()30 void OnConnected() override {} OnReceiveMsg(const char * data,uint32_t dataLen)31 void OnReceiveMsg(const char* data, uint32_t dataLen) override {} 32 }; 33 34 class InteroperableClientManagerTest : public testing::Test { 35 public: SetUpTestCase()36 static void SetUpTestCase() {} TearDownTestCase()37 static void TearDownTestCase() {} SetUp()38 virtual void SetUp() {} TearDown()39 virtual void TearDown() {} 40 }; 41 42 /** 43 * @tc.number Telephony_InteroperableDataControllerTest_001 44 * @tc.name test send requisites data 2 client 45 * @tc.desc Function test 46 */ 47 HWTEST_F(InteroperableClientManagerTest, Telephony_InteroperableDataControllerTest_001, 48 Function | MediumTest | Level1) 49 { 50 int32_t slotId = 0; 51 std::string phoneNum = "123"; 52 std::shared_ptr<ISessionCallback> callback = std::make_shared<SessionCallbackForTest>(); 53 auto session = std::make_shared<ServerSession>(callback); 54 std::shared_ptr<InteroperableDataController> dataController = std::make_shared<InteroperableServerManager>(); 55 EXPECT_NO_THROW(dataController->SendRequisiteDataToPeer(slotId, phoneNum)); // session nullptr 56 57 dataController->session_ = session; 58 EXPECT_NO_THROW(dataController->SendRequisiteDataToPeer(slotId, phoneNum)); 59 } 60 61 /** 62 * @tc.number Telephony_InteroperableDataControllerTest_002 63 * @tc.name test create msg data 64 * @tc.desc Function test 65 */ 66 HWTEST_F(InteroperableClientManagerTest, Telephony_InteroperableDataControllerTest_002, 67 Function | MediumTest | Level1) 68 { 69 int32_t slotId = 0; 70 std::string phoneNum = "123"; 71 InteroperableMsgType msgType = InteroperableMsgType::DATA_TYPE_UNKNOWN; 72 std::shared_ptr<ISessionCallback> callback = std::make_shared<SessionCallbackForTest>(); 73 auto session = std::make_shared<ServerSession>(callback); 74 std::shared_ptr<InteroperableDataController> dataController = std::make_shared<InteroperableServerManager>(); 75 cJSON *msg = nullptr; 76 EXPECT_NO_THROW(dataController->HandleMuted(msg)); 77 msg = cJSON_Parse("{ \"mute\": true }"); 78 EXPECT_NO_THROW(dataController->HandleMuted(msg)); 79 EXPECT_NO_THROW(dataController->MuteRinger()); 80 EXPECT_NO_THROW(dataController->SetMuted(true)); 81 82 dataController->session_ = session; 83 EXPECT_NO_THROW(dataController->MuteRinger()); 84 EXPECT_NO_THROW(dataController->SetMuted(true)); 85 EXPECT_NE(dataController->CreateRequisitesDataMsg(msgType, slotId, phoneNum), ""); 86 EXPECT_NE(dataController->CreateQueryRequisitesDataMsg(msgType, phoneNum), ""); 87 EXPECT_NE(dataController->CreateMuteRingerMsg(msgType), ""); 88 EXPECT_NE(dataController->CreateMuteMsg(msgType, true), ""); 89 cJSON_Delete(msg); 90 } 91 92 /** 93 * @tc.number Telephony_InteroperableDataControllerTest_003 94 * @tc.name test parse data 95 * @tc.desc Function test 96 */ 97 HWTEST_F(InteroperableClientManagerTest, Telephony_InteroperableDataControllerTest_003, 98 Function | MediumTest | Level1) 99 { 100 std::shared_ptr<InteroperableDataController> dataController = std::make_shared<InteroperableServerManager>(); 101 cJSON *msg = cJSON_Parse("{ \"test\": 0 }"); 102 std::string name = "test"; 103 std::string stringValue = ""; 104 int32_t intValue = 0; 105 cJSON *dataJson = cJSON_GetObjectItem(msg, name.c_str()); 106 EXPECT_TRUE(dataController->GetInt32Value(msg, name, intValue)); 107 EXPECT_FALSE(dataController->GetStringValue(msg, name, stringValue)); 108 cJSON_Delete(msg); 109 110 msg = cJSON_Parse("{ \"test\": \"hello\" }"); 111 EXPECT_FALSE(dataController->GetInt32Value(msg, name, intValue)); 112 EXPECT_TRUE(dataController->GetStringValue(msg, name, stringValue)); 113 bool boolValue = false; 114 EXPECT_FALSE(dataController->GetBoolValue(msg, name, boolValue)); 115 cJSON_Delete(msg); 116 117 msg = cJSON_Parse("{ \"test\": true }"); 118 EXPECT_TRUE(dataController->GetBoolValue(msg, name, boolValue)); 119 cJSON_Delete(msg); 120 } 121 122 /** 123 * @tc.number Telephony_InteroperableDataControllerTest_004 124 * @tc.name test ercv requisites data 125 * @tc.desc Function test 126 */ 127 HWTEST_F(InteroperableClientManagerTest, Telephony_InteroperableDataControllerTest_004, 128 Function | MediumTest | Level1) 129 { 130 std::shared_ptr<InteroperableDataController> dataController = std::make_shared<InteroperableServerManager>(); 131 cJSON *msg = cJSON_Parse("{ \"test\": 0 }"); 132 EXPECT_NO_THROW(dataController->HandleRequisitesData(msg)); 133 cJSON_Delete(msg); 134 135 msg = cJSON_Parse("{ \"phoneNumber\": \"123\" }"); 136 EXPECT_NO_THROW(dataController->HandleRequisitesData(msg)); 137 cJSON_Delete(msg); 138 139 msg = cJSON_Parse("{ \"phoneNumber\": \"123\", \"slotId\": 0 }"); 140 EXPECT_NO_THROW(dataController->HandleRequisitesData(msg)); 141 cJSON_Delete(msg); 142 143 msg = cJSON_Parse("{ \"phoneNumber\": \"123\", \"slotId\": 0, \"callType\": true }"); 144 EXPECT_NO_THROW(dataController->HandleRequisitesData(msg)); 145 146 DialParaInfo info; 147 sptr<CallBase> call = new IMSCall(info); 148 call->SetAccountNumber("123"); 149 CallObjectManager::callObjectPtrList_.push_back(call); 150 EXPECT_NO_THROW(dataController->HandleRequisitesData(msg)); 151 CallObjectManager::callObjectPtrList_.clear(); 152 cJSON_Delete(msg); 153 } 154 } // namespace Telephony 155 } // namespace OHOS