1/*
2 * Copyright (C) 2021-2023 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 "ims_test.h"
17
18#define private public
19#define protected public
20#include "cellular_call_config.h"
21#include "cellular_call_handler.h"
22#include "cellular_call_proxy.h"
23#include "cellular_call_register.h"
24#include "cellular_call_service.h"
25#include "tel_ril_call_parcel.h"
26#include "ims_call_callback_proxy.h"
27#include "ims_call_callback_stub.h"
28#include "ims_call_client.h"
29#include "ims_control.h"
30#include "ims_error.h"
31#include "securec.h"
32
33namespace OHOS {
34namespace Telephony {
35using namespace testing::ext;
36const int32_t SIM1_SLOTID = 0;
37const std::string PHONE_NUMBER = "0000000";
38const std::string PHONE_NUMBER_SECOND = "1111111";
39const std::string PHONE_NUMBER_THIRD = "2222222";
40const std::string PHONE_NUMBER_FOUR = "3333333";
41const int32_t CELLULAR_CALL_SUCCESS = 0;
42const int32_t DEFAULT_INDEX = 1;
43
44void ImsTest::SetUpTestCase(void)
45{
46    // step 3: Set Up Test Case
47    std::cout << "---------- ImsCoreServiceClient start ------------" << std::endl;
48    DelayedSingleton<ImsCoreServiceClient>::GetInstance()->Init();
49}
50
51void ImsTest::TearDownTestCase(void)
52{
53    // step 3: Tear Down Test Case
54}
55
56void ImsTest::SetUp(void)
57{
58    // step 3: input testcase setup step
59    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_DOMAIN_PREFERENCE_MODE)] =
60        &ImsTest::SetDomainPreferenceMode;
61    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::GET_DOMAIN_PREFERENCE_MODE)] =
62        &ImsTest::GetDomainPreferenceMode;
63    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_IMS_SWITCH_STATUS)] =
64        &ImsTest::SetImsSwitchStatus;
65    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::GET_IMS_SWITCH_STATUS)] =
66        &ImsTest::GetImsSwitchStatus;
67    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_IMS_CONFIG_STRING)] =
68        &ImsTest::SetImsConfigString;
69    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_IMS_CONFIG_INT)] = &ImsTest::SetImsConfigInt;
70    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::GET_IMS_CONFIG)] = &ImsTest::GetImsConfig;
71    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_IMS_FEATURE)] = &ImsTest::SetImsFeatureValue;
72    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::GET_IMS_FEATURE)] = &ImsTest::GetImsFeatureValue;
73    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::CTRL_CAMERA)] = &ImsTest::ControlCamera;
74    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_PREVIEW_WINDOW)] = &ImsTest::SetPreviewWindow;
75    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_DISPLAY_WINDOW)] = &ImsTest::SetDisplayWindow;
76    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_CAMERA_ZOOM)] = &ImsTest::SetCameraZoom;
77    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_PAUSE_IMAGE)] = &ImsTest::SetPausePicture;
78    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SET_DEVICE_DIRECTION)] =
79        &ImsTest::SetDeviceDirection;
80    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SEND_CALL_MEDIA_MODE_REQUEST)] =
81        &ImsTest::SendUpdateCallMediaModeRequest;
82    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::SEND_CALL_MEDIA_MODE_RESPONSE)] =
83        &ImsTest::SendUpdateCallMediaModeResponse;
84    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::CANCEL_CALL_UPGRADE)] =
85        &ImsTest::CancelCallUpgrade;
86    requestFuncMap_[static_cast<int32_t>(CellularCallInterfaceCode::REQUEST_CAMERA_CAPABILITY)] =
87        &ImsTest::RequestCameraCapabilities;
88}
89
90void ImsTest::TearDown(void)
91{
92    // step 3: input testcase teardown step
93}
94
95int32_t ImsTest::SetDomainPreferenceMode(const sptr<CellularCallInterface> &telephonyService) const
96{
97    std::cout << "test SetDomainPreferenceMode entry." << std::endl;
98    std::cout << "please enter the need mode:(1 - 4)";
99    int32_t mode;
100    std::cin >> mode;
101    int32_t slotId = 0;
102    std::cout << "please enter the slotId:(0   1)";
103    std::cin >> slotId;
104    return telephonyService->SetDomainPreferenceMode(slotId, mode);
105}
106
107int32_t ImsTest::GetDomainPreferenceMode(const sptr<CellularCallInterface> &telephonyService) const
108{
109    std::cout << "test GetDomainPreferenceMode entry." << std::endl;
110    int32_t slotId = 0;
111    std::cout << "please enter the slotId:(0   1)";
112    std::cin >> slotId;
113    return telephonyService->GetDomainPreferenceMode(slotId);
114}
115
116int32_t ImsTest::SetImsSwitchStatus(const sptr<CellularCallInterface> &telephonyService) const
117{
118    std::cout << "test SetImsSwitchStatus entry." << std::endl;
119    std::cout << "please enter the switch state:";
120    bool active;
121    std::cin >> active;
122    int32_t slotId = 0;
123    std::cout << "please enter the slotId:(0   1)";
124    std::cin >> slotId;
125    return telephonyService->SetImsSwitchStatus(slotId, active);
126}
127
128int32_t ImsTest::GetImsSwitchStatus(const sptr<CellularCallInterface> &telephonyService) const
129{
130    std::cout << "test GetImsSwitchStatus entry." << std::endl;
131    int32_t slotId = 0;
132    std::cout << "please enter the slotId:(0   1)";
133    std::cin >> slotId;
134    bool enabled;
135    return telephonyService->GetImsSwitchStatus(slotId, enabled);
136}
137
138int32_t ImsTest::SetImsConfigString(const sptr<CellularCallInterface> &telephonyService) const
139{
140    std::cout << "test SetImsConfigString entry." << std::endl;
141    std::cout << "please enter the item id:";
142    int32_t item = 0;
143    std::cin >> item;
144    std::cout << "please enter the item value:";
145    std::string value;
146    std::cin >> value;
147    int32_t slotId = 0;
148    return telephonyService->SetImsConfig(slotId, static_cast<ImsConfigItem>(item), value);
149}
150
151int32_t ImsTest::SetImsConfigInt(const sptr<CellularCallInterface> &telephonyService) const
152{
153    std::cout << "test SetImsConfigInt entry." << std::endl;
154    std::cout << "please enter the item id:";
155    int32_t item = 0;
156    std::cin >> item;
157    std::cout << "please enter the item value:";
158    int32_t value = 0;
159    std::cin >> value;
160    int32_t slotId = 0;
161    return telephonyService->SetImsConfig(slotId, static_cast<ImsConfigItem>(item), value);
162}
163
164int32_t ImsTest::GetImsConfig(const sptr<CellularCallInterface> &telephonyService) const
165{
166    std::cout << "test GetImsConfig entry." << std::endl;
167    std::cout << "please enter the item id:";
168    int32_t item = 0;
169    std::cin >> item;
170    int32_t slotId = 0;
171    return telephonyService->GetImsConfig(slotId, static_cast<ImsConfigItem>(item));
172}
173
174int32_t ImsTest::SetImsFeatureValue(const sptr<CellularCallInterface> &telephonyService) const
175{
176    std::cout << "test SetImsFeatureValue entry." << std::endl;
177    std::cout << "please enter the feature type:";
178    int32_t type = 0;
179    std::cin >> type;
180    std::cout << "please enter the feature value:";
181    int32_t value = 0;
182    std::cin >> value;
183    int32_t slotId = 0;
184    return telephonyService->SetImsFeatureValue(slotId, static_cast<FeatureType>(type), value);
185}
186
187int32_t ImsTest::GetImsFeatureValue(const sptr<CellularCallInterface> &telephonyService) const
188{
189    std::cout << "test GetImsFeatureValue entry." << std::endl;
190    std::cout << "please enter the feature type:";
191    int32_t type = 0;
192    std::cin >> type;
193    int32_t slotId = 0;
194    return telephonyService->GetImsFeatureValue(slotId, static_cast<FeatureType>(type));
195}
196
197int32_t ImsTest::ControlCamera(const sptr<CellularCallInterface> &telephonyService) const
198{
199    std::cout << "test ControlCamera entry." << std::endl;
200    int32_t slotId = SIM1_SLOTID;
201    int32_t callIndex = DEFAULT_INDEX;
202    std::string cameraId = "cameraId";
203    return telephonyService->ControlCamera(slotId, callIndex, cameraId);
204}
205
206int32_t ImsTest::SetPreviewWindow(const sptr<CellularCallInterface> &telephonyService) const
207{
208    std::cout << "test SetPreviewWindow entry." << std::endl;
209    int32_t slotId = SIM1_SLOTID;
210    int32_t callIndex = DEFAULT_INDEX;
211    std::string surfaceId = "123";
212    std::string subSurfaceId = surfaceId.substr(0, 1);
213    uint64_t tmpSurfaceId = std::stoull(subSurfaceId);
214    auto surface = SurfaceUtils::GetInstance()->GetSurface(tmpSurfaceId);
215    if (surface == nullptr) {
216        surfaceId = "";
217    }
218    return telephonyService->SetPreviewWindow(slotId, callIndex, subSurfaceId, surface);
219}
220
221int32_t ImsTest::SetDisplayWindow(const sptr<CellularCallInterface> &telephonyService) const
222{
223    std::cout << "test SetDisplayWindow entry." << std::endl;
224    int32_t slotId = SIM1_SLOTID;
225    int32_t callIndex = DEFAULT_INDEX;
226    std::string surfaceId = "123";
227    std::string subSurfaceId = surfaceId.substr(0, 1);
228    uint64_t tmpSurfaceId = std::stoull(subSurfaceId);
229    auto surface = SurfaceUtils::GetInstance()->GetSurface(tmpSurfaceId);
230    if (surface == nullptr) {
231        surfaceId = "";
232    }
233    return telephonyService->SetDisplayWindow(slotId, callIndex, subSurfaceId, surface);
234}
235
236int32_t ImsTest::SetCameraZoom(const sptr<CellularCallInterface> &telephonyService) const
237{
238    std::cout << "test SetCameraZoom entry." << std::endl;
239    float zoomRatio = 1.0;
240    return telephonyService->SetCameraZoom(zoomRatio);
241}
242
243int32_t ImsTest::SetPausePicture(const sptr<CellularCallInterface> &telephonyService) const
244{
245    std::cout << "test SetPausePicture entry." << std::endl;
246    int32_t slotId = SIM1_SLOTID;
247    int32_t callIndex = DEFAULT_INDEX;
248    std::string path = "path";
249    return telephonyService->SetPausePicture(slotId, callIndex, path);
250}
251
252int32_t ImsTest::SetDeviceDirection(const sptr<CellularCallInterface> &telephonyService) const
253{
254    std::cout << "test SetDeviceDirection entry." << std::endl;
255    int32_t slotId = SIM1_SLOTID;
256    int32_t callIndex = DEFAULT_INDEX;
257    int32_t rotation = 0;
258    return telephonyService->SetDeviceDirection(slotId, callIndex, rotation);
259}
260
261int32_t ImsTest::SendUpdateCallMediaModeRequest(const sptr<CellularCallInterface> &telephonyService) const
262{
263    std::cout << "test SendUpdateCallMediaModeRequest entry." << std::endl;
264    CellularCallInfo callInfo;
265    callInfo.accountId = SIM1_SLOTID;
266    callInfo.slotId = SIM1_SLOTID;
267    callInfo.index = DEFAULT_INDEX;
268    callInfo.callType = CallType::TYPE_IMS;
269    callInfo.videoState = 0; // 0 means audio
270    if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
271        return TELEPHONY_ERR_MEMSET_FAIL;
272    }
273    if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, PHONE_NUMBER.c_str(), PHONE_NUMBER.length()) != EOK) {
274        return TELEPHONY_ERR_MEMCPY_FAIL;
275    }
276    std::cout << "please enter the ims call mode:";
277    std::cout << "0:audio_only, 1:send_only, 2:receive_only, 3:send_receive, 4:video_paused";
278    ImsCallMode callMode = ImsCallMode::CALL_MODE_AUDIO_ONLY;
279    int32_t input = 0;
280    std::cin >> input;
281    callMode = static_cast<ImsCallMode>(input);
282    return telephonyService->SendUpdateCallMediaModeRequest(callInfo, callMode);
283}
284
285int32_t ImsTest::SendUpdateCallMediaModeResponse(const sptr<CellularCallInterface> &telephonyService) const
286{
287    std::cout << "test SendUpdateCallMediaModeResponse entry." << std::endl;
288    CellularCallInfo callInfo;
289    callInfo.accountId = SIM1_SLOTID;
290    callInfo.slotId = SIM1_SLOTID;
291    callInfo.index = DEFAULT_INDEX;
292    callInfo.callType = CallType::TYPE_IMS;
293    callInfo.videoState = 0; // 0 means audio
294    if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
295        return TELEPHONY_ERR_MEMSET_FAIL;
296    }
297    if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, PHONE_NUMBER.c_str(), PHONE_NUMBER.length()) != EOK) {
298        return TELEPHONY_ERR_MEMCPY_FAIL;
299    }
300    std::cout << "please enter the ims call mode:";
301    std::cout << "0:audio_only, 1:send_only, 2:receive_only, 3:send_receive, 4:video_paused";
302    ImsCallMode callMode = ImsCallMode::CALL_MODE_AUDIO_ONLY;
303    int32_t input = 0;
304    std::cin >> input;
305    callMode = static_cast<ImsCallMode>(input);
306    return telephonyService->SendUpdateCallMediaModeResponse(callInfo, callMode);
307}
308
309int32_t ImsTest::CancelCallUpgrade(const sptr<CellularCallInterface> &telephonyService) const
310{
311    std::cout << "test CancelCallUpgrade entry." << std::endl;
312    int32_t slotId = SIM1_SLOTID;
313    int32_t callIndex = DEFAULT_INDEX;
314    return telephonyService->CancelCallUpgrade(slotId, callIndex);
315}
316
317int32_t ImsTest::RequestCameraCapabilities(const sptr<CellularCallInterface> &telephonyService) const
318{
319    std::cout << "test RequestCameraCapabilities entry." << std::endl;
320    int32_t slotId = SIM1_SLOTID;
321    int32_t callIndex = DEFAULT_INDEX;
322    return telephonyService->RequestCameraCapabilities(slotId, callIndex);
323}
324
325int32_t ImsTest::InputNumForInterface(const sptr<CellularCallInterface> &telephonyService) const
326{
327    int32_t ret = -1;
328    bool loopFlag = true;
329    int32_t inputNumber = 0;
330    int32_t returnNum = 1000; // end loop variable value
331    while (loopFlag) {
332        std::cout << "\n**********Unit Test Start**********\n"
333                     "Usage: please input a cmd num:\n"
334                     "300:SetCallPreferenceMode\n"
335                     "301:GetCallPreferenceMode\n"
336                     "302:SetImsSwitchStatus\n"
337                     "303:GetImsSwitchStatus\n"
338                     "304:SetImsConfigString\n"
339                     "305:SetImsConfigInt\n"
340                     "306:GetImsConfig\n"
341                     "307:SetImsFeatureValue\n"
342                     "308:GetImsFeatureValue\n"
343                     "400:ControlCamera\n"
344                     "401:SetPreviewWindow\n"
345                     "402:SetDisplayWindow\n"
346                     "403:SetCameraZoom\n"
347                     "404:SetPausePicture\n"
348                     "405:SetDeviceDirection\n"
349                     "406:SendUpdateCallMediaModeRequest\n"
350                     "407:SendUpdateCallMediaModeResponse\n"
351                     "408:CancelCallUpgrade\n"
352                     "409:RequestCameraCapabilities\n"
353                     "1000:Exit\n"
354                     "***********************************\n"
355                     "Your choice: ";
356        std::cin >> inputNumber;
357        if (inputNumber == returnNum) {
358            std::cout << "exiting...\n";
359            break;
360        }
361        auto itFunction = requestFuncMap_.find(inputNumber);
362        if (itFunction != requestFuncMap_.end()) {
363            auto requestFunction = itFunction->second;
364            if (requestFunction != nullptr) {
365                ret = (this->*requestFunction)(telephonyService);
366            }
367        }
368        if (ret != CELLULAR_CALL_SUCCESS) {
369            std::cout << "this execution did not return a successful value, please check.\n";
370            return ret;
371        }
372    }
373    return CELLULAR_CALL_SUCCESS;
374}
375} // namespace Telephony
376} // namespace OHOS
377