• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         DelayedRefSingleton<BluetoothCallClient>::GetInstance().Init();
63         isConnected_ = true;
64         std::cout << "connect callManager server success!!!" << std::endl;
65 
66         CallInfoManager::Init();
67     }
68 
HasSimCard(int32_t slotId)69     bool HasSimCard(int32_t slotId)
70     {
71         bool hasSimCard = false;
72         DelayedRefSingleton<CoreServiceClient>::GetInstance().HasSimCard(slotId, hasSimCard);
73         return hasSimCard;
74     }
75 
IsServiceConnected()76     static bool IsServiceConnected()
77     {
78         if (!isConnected_) {
79             std::cout << "call manager service not connected" << std::endl;
80         }
81         return isConnected_;
82     }
83 
IsAirplaneModeOn()84     bool IsAirplaneModeOn()
85     {
86         bool isAirplaneModeOn = false;
87         std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
88         if (callDataPtr == nullptr) {
89             return false;
90         }
91         int32_t ret = callDataPtr->GetAirplaneMode(isAirplaneModeOn);
92         return ret == TELEPHONY_SUCCESS && isAirplaneModeOn;
93     }
94 
IsRegServiceInService(int32_t slotId)95     bool IsRegServiceInService(int32_t slotId)
96     {
97         sptr<NetworkState> networkState = nullptr;
98         DelayedRefSingleton<CoreServiceClient>::GetInstance().GetNetworkState(slotId, networkState);
99         RegServiceState regStatus = RegServiceState::REG_STATE_UNKNOWN;
100         if (networkState != nullptr) {
101             regStatus = networkState->GetRegStatus();
102         }
103         if (regStatus == RegServiceState::REG_STATE_IN_SERVICE) {
104             return true;
105         }
106         return false;
107     }
108 
IsCtCardWithoutIms(int32_t slotId)109     bool IsCtCardWithoutIms(int32_t slotId)
110     {
111         ImsRegInfo info;
112         DelayedRefSingleton<CoreServiceClient>::GetInstance().GetImsRegStatus(slotId, ImsServiceType::TYPE_VOICE, info);
113         bool isImsRegistered = info.imsRegState == ImsRegState::IMS_REGISTERED;
114         bool isCTSimCard = false;
115         DelayedRefSingleton<CoreServiceClient>::GetInstance().IsCTSimCard(slotId, isCTSimCard);
116         if (isCTSimCard && !isImsRegistered) {
117             return true;
118         }
119         return false;
120     }
121 
CanDialCall(int32_t slotId1,int32_t slotId2)122     bool CanDialCall(int32_t slotId1, int32_t slotId2)
123     {
124         if (IsAirplaneModeOn()) {
125             return false;
126         }
127         if (!IsRegServiceInService(slotId1) && !IsRegServiceInService(slotId2)) {
128             return false;
129         }
130         if (IsCtCardWithoutIms(slotId1) && IsCtCardWithoutIms(slotId2)) {
131             return false;
132         }
133         return true;
134     }
135 
SleepForSeconds(int32_t seconds)136     inline void SleepForSeconds(int32_t seconds)
137     {
138         std::this_thread::sleep_for(std::chrono::seconds(seconds));
139     }
140 
InitDialInfo(int32_t accountId,int32_t videoState,int32_t dialScene,int32_t dialType)141     void InitDialInfo(int32_t accountId, int32_t videoState, int32_t dialScene, int32_t dialType)
142     {
143         dialInfo_.PutIntValue("accountId", accountId);
144         dialInfo_.PutIntValue("videoState", videoState);
145         dialInfo_.PutIntValue("dialScene", dialScene);
146         dialInfo_.PutIntValue("dialType", dialType);
147     }
148 
149     // execute before each testcase
SetUp()150     void SetUp()
151     {
152         std::this_thread::sleep_for(std::chrono::seconds(SLEEP_ONE_SECONDS));
153     }
154 
155     // execute after each testcase
TearDown()156     void TearDown() {}
157 
158     // execute after last testcase
TearDownTestCase()159     static void TearDownTestCase()
160     {
161         if (clientPtr_ != nullptr) {
162             clientPtr_->UnInit();
163         }
164         if (servicePtr_ != nullptr) {
165             servicePtr_->UnInit();
166         }
167         DelayedRefSingleton<BluetoothCallClient>::GetInstance().UnInit();
168         std::cout << "---------- gtest end ------------" << std::endl;
169     }
170 
171     void HangUpCall();
172     void DialCall(const std::string &phoneNumber);
173 
174 public:
175     static bool isConnected_;
176     static std::shared_ptr<CallManagerClient> clientPtr_;
177     static std::unique_ptr<CallManagerConnect> servicePtr_;
178     AppExecFwk::PacMap dialInfo_;
179 
180     const int32_t SLEEP_50_MS = 50;
181     const int32_t SLEEP_100_MS = 100;
182     const int32_t SLEEP_200_MS = 200;
183     const int32_t SLEEP_1000_MS = 1000;
184     const int32_t SLEEP_12000_MS = 12000;
185     const int32_t SLEEP_30000_MS = 30000;
186     const std::string EMPTY_DEFAULT = "";
187     const int32_t FALSE_DEFAULT = -1;
188     const int32_t DIAL_SCENE_TEST = 100;
189     const int32_t INVALID_VIDEO_STATE = -1;
190     const int32_t INVALID_SLOT_ID = -1;
191 };
192 
193 bool CallManagerGtest::isConnected_ = false;
194 std::shared_ptr<CallManagerClient> CallManagerGtest::clientPtr_ = nullptr;
195 std::unique_ptr<CallManagerConnect> CallManagerGtest::servicePtr_ = nullptr;
196 } // namespace Telephony
197 } // namespace OHOS
198 
199 #endif