1 /*
2  * Copyright (C) 2021-2022 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 #ifndef CALL_MANAGER_GTEST
17 #define CALL_MANAGER_GTEST
18 
19 #include <chrono>
20 #include <gtest/gtest.h>
21 #include <iostream>
22 #include <string_ex.h>
23 #include <thread>
24 #include <unordered_set>
25 
26 #include "bluetooth_call_client.h"
27 #include "call_data_base_helper.h"
28 #include "call_manager_client.h"
29 #include "call_manager_connect.h"
30 #include "common_event.h"
31 #include "common_event_manager.h"
32 #include "core_service_client.h"
33 
34 namespace OHOS {
35 namespace Telephony {
36 constexpr int16_t SLEEP_ONE_SECONDS = 1;
37 
38 class CallManagerGtest : public testing::Test {
39 public:
40     // execute before first testcase
SetUpTestCase()41     static void SetUpTestCase()
42     {
43         std::cout << "---------- warning ------------" << std::endl;
44         std::cout << "---Please modify PHONE_NUMBER first in the file call_manager_gtest.cpp---" << std::endl;
45         std::cout << "---------- gtest start ------------" << std::endl;
46         isConnected_ = false;
47         clientPtr_ = DelayedSingleton<CallManagerClient>::GetInstance();
48         if (clientPtr_ == nullptr) {
49             std::cout << "clientPtr_ is nullptr!" << std::endl;
50             return;
51         }
52         clientPtr_->Init(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
53         servicePtr_ = std::make_unique<CallManagerConnect>();
54         if (servicePtr_ == nullptr) {
55             std::cout << "make_unique CallManagerConnect failed!" << std::endl;
56             return;
57         }
58         if (servicePtr_->Init(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID) != TELEPHONY_SUCCESS) {
59             std::cout << "connect callManager server failed!" << std::endl;
60             return;
61         }
62         blueToothClientPtr_ = DelayedSingleton<BluetoothCallClient>::GetInstance();
63         if (blueToothClientPtr_ == nullptr) {
64             std::cout << "make_unique CallManagerConnect failed!" << std::endl;
65             return;
66         }
67         blueToothClientPtr_->Init();
68         isConnected_ = true;
69         std::cout << "connect callManager server success!!!" << std::endl;
70 
71         CallInfoManager::Init();
72     }
73 
HasSimCard(int32_t slotId)74     bool HasSimCard(int32_t slotId)
75     {
76         bool hasSimCard = false;
77         DelayedRefSingleton<CoreServiceClient>::GetInstance().HasSimCard(slotId, hasSimCard);
78         return hasSimCard;
79     }
80 
IsServiceConnected()81     static bool IsServiceConnected()
82     {
83         if (!isConnected_) {
84             std::cout << "call manager service not connected" << std::endl;
85         }
86         return isConnected_;
87     }
88 
IsAirplaneModeOn()89     bool IsAirplaneModeOn()
90     {
91         bool isAirplaneModeOn = false;
92         std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
93         if (callDataPtr == nullptr) {
94             return false;
95         }
96         int32_t ret = callDataPtr->GetAirplaneMode(isAirplaneModeOn);
97         return ret == TELEPHONY_SUCCESS && isAirplaneModeOn;
98     }
99 
IsRegServiceInService(int32_t slotId)100     bool IsRegServiceInService(int32_t slotId)
101     {
102         sptr<NetworkState> networkState = nullptr;
103         DelayedRefSingleton<CoreServiceClient>::GetInstance().GetNetworkState(slotId, networkState);
104         RegServiceState regStatus = RegServiceState::REG_STATE_UNKNOWN;
105         if (networkState != nullptr) {
106             regStatus = networkState->GetRegStatus();
107         }
108         if (regStatus == RegServiceState::REG_STATE_IN_SERVICE) {
109             return true;
110         }
111         return false;
112     }
113 
IsCtCardWithoutIms(int32_t slotId)114     bool IsCtCardWithoutIms(int32_t slotId)
115     {
116         ImsRegInfo info;
117         DelayedRefSingleton<CoreServiceClient>::GetInstance().GetImsRegStatus(slotId, ImsServiceType::TYPE_VOICE, info);
118         bool isImsRegistered = info.imsRegState == ImsRegState::IMS_REGISTERED;
119         bool isCTSimCard = false;
120         DelayedRefSingleton<CoreServiceClient>::GetInstance().IsCTSimCard(slotId, isCTSimCard);
121         if (isCTSimCard && !isImsRegistered) {
122             return true;
123         }
124         return false;
125     }
126 
CanDialCall(int32_t slotId1,int32_t slotId2)127     bool CanDialCall(int32_t slotId1, int32_t slotId2)
128     {
129         if (IsAirplaneModeOn()) {
130             return false;
131         }
132         if (!IsRegServiceInService(slotId1) && !IsRegServiceInService(slotId2)) {
133             return false;
134         }
135         if (IsCtCardWithoutIms(slotId1) && IsCtCardWithoutIms(slotId2)) {
136             return false;
137         }
138         return true;
139     }
140 
SleepForSeconds(int32_t seconds)141     inline void SleepForSeconds(int32_t seconds)
142     {
143         std::this_thread::sleep_for(std::chrono::seconds(seconds));
144     }
145 
InitDialInfo(int32_t accountId,int32_t videoState,int32_t dialScene,int32_t dialType)146     void InitDialInfo(int32_t accountId, int32_t videoState, int32_t dialScene, int32_t dialType)
147     {
148         dialInfo_.PutIntValue("accountId", accountId);
149         dialInfo_.PutIntValue("videoState", videoState);
150         dialInfo_.PutIntValue("dialScene", dialScene);
151         dialInfo_.PutIntValue("dialType", dialType);
152     }
153 
154     // execute before each testcase
SetUp()155     void SetUp()
156     {
157         std::this_thread::sleep_for(std::chrono::seconds(SLEEP_ONE_SECONDS));
158     }
159 
160     // execute after each testcase
TearDown()161     void TearDown() {}
162 
163     // execute after last testcase
TearDownTestCase()164     static void TearDownTestCase()
165     {
166         if (clientPtr_ != nullptr) {
167             clientPtr_->UnInit();
168         }
169         if (servicePtr_ != nullptr) {
170             servicePtr_->UnInit();
171         }
172         if (blueToothClientPtr_ != nullptr) {
173             blueToothClientPtr_->UnInit();
174         }
175         std::cout << "---------- gtest end ------------" << std::endl;
176     }
177 
178     void HangUpCall();
179     void DialCall(const std::string &phoneNumber);
180 
181 public:
182     static bool isConnected_;
183     static std::shared_ptr<CallManagerClient> clientPtr_;
184     static std::shared_ptr<BluetoothCallClient> blueToothClientPtr_;
185     static std::unique_ptr<CallManagerConnect> servicePtr_;
186     AppExecFwk::PacMap dialInfo_;
187 
188     const int32_t SLEEP_50_MS = 50;
189     const int32_t SLEEP_100_MS = 100;
190     const int32_t SLEEP_200_MS = 200;
191     const int32_t SLEEP_1000_MS = 1000;
192     const int32_t SLEEP_12000_MS = 12000;
193     const int32_t SLEEP_30000_MS = 30000;
194     const std::string EMPTY_DEFAULT = "";
195     const int32_t FALSE_DEFAULT = -1;
196     const int32_t DIAL_SCENE_TEST = 100;
197     const int32_t INVALID_VIDEO_STATE = -1;
198     const int32_t INVALID_SLOT_ID = -1;
199 };
200 
201 bool CallManagerGtest::isConnected_ = false;
202 std::shared_ptr<CallManagerClient> CallManagerGtest::clientPtr_ = nullptr;
203 std::shared_ptr<BluetoothCallClient> CallManagerGtest::blueToothClientPtr_ = nullptr;
204 std::unique_ptr<CallManagerConnect> CallManagerGtest::servicePtr_ = nullptr;
205 } // namespace Telephony
206 } // namespace OHOS
207 
208 #endif