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 #define private public
17 #define protected public
18 #include "call_manager_gtest.h"
19
20 #include <gtest/gtest.h>
21 #include <cstring>
22 #include <string>
23
24 #include "bluetooth_hfp_ag.h"
25 #include "call_manager_connect.h"
26 #include "call_manager_service.h"
27 #include "surface_utils.h"
28 #include "telephony_types.h"
29 #include "voip_call.h"
30
31 using namespace OHOS::Bluetooth;
32 namespace OHOS {
33 namespace Telephony {
34 using namespace testing::ext;
35 constexpr int16_t SIM2_SLOTID = 1;
36 constexpr int16_t SIM1_SLOTID_NO_CARD = 0;
37 constexpr int16_t RETURN_VALUE_IS_ZERO = 0;
38 constexpr int16_t INVALID_NEGATIVE_ID = -100;
39 constexpr int16_t INVALID_POSITIVE_ID = 100;
40 constexpr int INVALID_DIAL_TYPE = 3;
41 constexpr int WAIT_TIME = 3;
42 constexpr int16_t CAMERA_ROTATION_90 = 90;
43 constexpr int16_t CAMERA_ROTATION_ERROR = 50;
44 constexpr int16_t SLEEP_1000_MS = 1000;
45 constexpr int BASE_TIME_MS = 1000;
46 constexpr int SLEEP_TIME_MS = 50;
47 constexpr int MAX_LIMIT_TIME = 18000;
48 constexpr int16_t SIM1_SLOTID = 0;
49 const std::string PHONE_NUMBER = "0000000000";
50
51 BluetoothCallClient &bluetoothCallClient = DelayedRefSingleton<BluetoothCallClient>::GetInstance();
52 std::unordered_map<int32_t, std::unordered_set<int32_t>> g_callStateMap;
53 int32_t g_newCallId = -1;
54 std::mutex CallInfoManager::mutex_;
55 int16_t CallInfoManager::newCallState_;
56 CallAttributeInfo CallInfoManager::updateCallInfo_;
57 std::unordered_set<int32_t> CallInfoManager::callIdSet_;
58
CallDetailsChange(const CallAttributeInfo & info)59 int32_t CallInfoManager::CallDetailsChange(const CallAttributeInfo &info)
60 {
61 TELEPHONY_LOGI("CallDetailsChange Start");
62 std::lock_guard<std::mutex> lock(mutex_);
63 updateCallInfo_ = info;
64 if (callIdSet_.find(updateCallInfo_.callId) == callIdSet_.end()) {
65 TELEPHONY_LOGI("CallDetailsChange new call");
66 callIdSet_.insert(updateCallInfo_.callId);
67 g_newCallId = updateCallInfo_.callId;
68 newCallState_ = (int32_t)updateCallInfo_.callState;
69 std::unordered_set<int32_t> newSet;
70 newSet.clear();
71 g_callStateMap.insert(std::pair<int32_t, std::unordered_set<int32_t>>(g_newCallId, newSet));
72 }
73 g_callStateMap[updateCallInfo_.callId].insert((int32_t)updateCallInfo_.callState);
74 return TELEPHONY_SUCCESS;
75 }
76
MeeTimeDetailsChange(const CallAttributeInfo & info)77 int32_t CallInfoManager::MeeTimeDetailsChange(const CallAttributeInfo &info)
78 {
79 TELEPHONY_LOGI("MeeTimeDetailsChange Start");
80 std::lock_guard<std::mutex> lock(mutex_);
81 updateCallInfo_ = info;
82 if (callIdSet_.find(updateCallInfo_.callId) == callIdSet_.end()) {
83 TELEPHONY_LOGI("MeeTimeDetailsChange new call");
84 callIdSet_.insert(updateCallInfo_.callId);
85 g_newCallId = updateCallInfo_.callId;
86 newCallState_ = (int32_t)updateCallInfo_.callState;
87 std::unordered_set<int32_t> newSet;
88 newSet.clear();
89 g_callStateMap.insert(std::pair<int32_t, std::unordered_set<int32_t>>(g_newCallId, newSet));
90 }
91 g_callStateMap[updateCallInfo_.callId].insert((int32_t)updateCallInfo_.callState);
92 return TELEPHONY_SUCCESS;
93 }
94
HasActiveStatus()95 bool CallInfoManager::HasActiveStatus()
96 {
97 TELEPHONY_LOGI("Waiting for activation !");
98 int sumUseTime = 0;
99 int slipMs = SLEEP_TIME_MS;
100 do {
101 if (!(HasState(g_newCallId, static_cast<int32_t>(TelCallState::CALL_STATUS_ACTIVE)))) {
102 usleep(slipMs * BASE_TIME_MS);
103 sumUseTime += slipMs;
104 } else {
105 TELEPHONY_LOGI("===========wait %d ms callStatus:%d==============", sumUseTime, newCallState_);
106 return true;
107 }
108 } while (sumUseTime < MAX_LIMIT_TIME);
109 TELEPHONY_LOGI("===========wait %d ms callStatus:%d=====not active=========", sumUseTime, newCallState_);
110 return false;
111 }
112
HasState(int callId,int32_t callState)113 bool CallInfoManager::HasState(int callId, int32_t callState)
114 {
115 if (g_callStateMap.find(callId) == g_callStateMap.end()) {
116 return false;
117 }
118 if (g_callStateMap[callId].find(callState) == g_callStateMap[callId].end()) {
119 return false;
120 }
121 return true;
122 }
123
CallEventChange(const CallEventInfo & info)124 int32_t CallInfoManager::CallEventChange(const CallEventInfo &info)
125 {
126 return TELEPHONY_SUCCESS;
127 }
128
Init()129 void CallInfoManager::Init()
130 {
131 g_newCallId = -1;
132 updateCallInfo_.speakerphoneOn = -1;
133 updateCallInfo_.startTime = -1;
134 updateCallInfo_.isEcc = -1;
135 updateCallInfo_.accountId = -1;
136 updateCallInfo_.callType = CallType::TYPE_CS;
137 updateCallInfo_.callId = -1;
138 updateCallInfo_.callState = TelCallState::CALL_STATUS_IDLE;
139 updateCallInfo_.videoState = VideoStateType::TYPE_VOICE;
140 }
141
LockCallId(bool eq,int32_t targetId,int32_t slipMs,int32_t timeoutMs)142 void CallInfoManager::LockCallId(bool eq, int32_t targetId, int32_t slipMs, int32_t timeoutMs)
143 {
144 int32_t useTimeMs = 0;
145 std::cout << "wait for a few seconds......" << std::endl;
146 while ((updateCallInfo_.callId != targetId) && (useTimeMs < timeoutMs)) {
147 usleep(slipMs * SLEEP_1000_MS);
148 useTimeMs += slipMs;
149 }
150 std::cout << "\n===========wait " << useTimeMs << " ms target:" << updateCallInfo_.callId << std::endl;
151 if (eq) {
152 EXPECT_EQ(updateCallInfo_.callId, targetId);
153 } else {
154 EXPECT_NE(updateCallInfo_.callId, targetId);
155 }
156 }
157
LockCallState(bool eq,int32_t targetState,int32_t slipMs,int32_t timeoutMs)158 void CallInfoManager::LockCallState(bool eq, int32_t targetState, int32_t slipMs, int32_t timeoutMs)
159 {
160 if (CallManagerGtest::clientPtr_->GetCallState() == targetState) {
161 return;
162 }
163 int32_t usedTimeMs = 0;
164 std::cout << "wait for a few seconds......" << std::endl;
165 while ((CallManagerGtest::clientPtr_->GetCallState() != targetState) && (usedTimeMs < timeoutMs)) {
166 usleep(slipMs * SLEEP_1000_MS);
167 usedTimeMs += slipMs;
168 }
169 int32_t callState = CallManagerGtest::clientPtr_->GetCallState();
170 std::cout << "waited " << usedTimeMs << " seconds" << std::endl;
171 std::cout << "target call state:" << targetState << std::endl;
172 EXPECT_EQ(callState, targetState);
173 }
174
HangUpCall()175 void CallManagerGtest::HangUpCall()
176 {
177 clientPtr_->HangUpCall(INVALID_CALLID);
178 }
179
180 class ClientErrorBranchTest : public testing::Test {
181 public:
SetUpTestCase()182 static void SetUpTestCase() {};
TearDownTestCase()183 static void TearDownTestCase() {};
SetUp()184 void SetUp() {};
TearDown()185 void TearDown() {};
186 };
187
188 /**************************** Test ReportAudioDeviceInfo() ****************************/
189 /**
190 * @tc.number Telephony_CallManager_ReportAudioDeviceInfo_0200
191 * @tc.name test report audio device info without active call
192 * @tc.desc Function test
193 */
194 HWTEST_F(CallManagerGtest, Telephony_CallManager_ReportAudioDeviceInfo_0200, Function | MediumTest | Level3)
195 {
196 AccessToken token;
197 if (!HasSimCard(SIM1_SLOTID) && !HasSimCard(SIM2_SLOTID)) {
198 return;
199 }
200 if (clientPtr_->GetCallState() == static_cast<int>(CallStateToApp::CALL_STATE_OFFHOOK)) {
201 HangUpCall();
202 }
203 EXPECT_GE(CallManagerGtest::clientPtr_->ReportAudioDeviceInfo(), RETURN_VALUE_IS_ZERO);
204 }
205
206 /**
207 * @tc.number Telephony_CallManager_ReportAudioDeviceInfo_0300
208 * @tc.name test report audio device info without permission
209 * @tc.desc Function test
210 */
211 HWTEST_F(CallManagerGtest, Telephony_CallManager_ReportAudioDeviceInfo_0300, Function | MediumTest | Level3)
212 {
213 if (!HasSimCard(SIM1_SLOTID) && !HasSimCard(SIM2_SLOTID)) {
214 return;
215 }
216 EXPECT_NE(CallManagerGtest::clientPtr_->ReportAudioDeviceInfo(), RETURN_VALUE_IS_ZERO);
217 }
218 } // namespace Telephony
219 } // namespace OHOS