1/*
2 * Copyright (C) 2022-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 "sms_delivery_callback_gtest.h"
17
18#include <iostream>
19
20#include "string_utils.h"
21#include "telephony_errors.h"
22#include "telephony_log_wrapper.h"
23
24namespace OHOS {
25namespace Telephony {
26static constexpr int SMS_DEFAULT_RESULT = 0;
27static constexpr int SMS_DEFAULT_ERROR = -1;
28
29SmsDeliveryCallbackGTest::SmsDeliveryCallbackGTest(SmsMmsTestHelper &helper)
30{
31    callbackHelper_ = &helper;
32}
33
34SmsDeliveryCallbackGTest::~SmsDeliveryCallbackGTest() {}
35
36void SmsDeliveryCallbackGTest::OnSmsDeliveryResult(const std::u16string &pdu)
37{
38    std::string pduStr = StringUtils::ToUtf8(pdu);
39    int32_t deliveryPduLen = static_cast<int32_t>(pduStr.size());
40    TELEPHONY_LOGI("SmsDeliveryCallbackGTest OnSmsDeliveryResult pduLen =  %{public}d", deliveryPduLen);
41
42    if (callbackHelper_ == nullptr) {
43        TELEPHONY_LOGE("SmsDeliveryCallbackGTest callbackHelper_ is nullptr");
44        return;
45    }
46    if (deliveryPduLen > 0) {
47        callbackHelper_->SetDeliverySmsIntResult(SMS_DEFAULT_RESULT);
48        callbackHelper_->NotifyAll();
49    } else {
50        callbackHelper_->SetDeliverySmsIntResult(SMS_DEFAULT_ERROR);
51        callbackHelper_->NotifyAll();
52    }
53    callbackHelper_ = nullptr;
54}
55
56int SmsDeliveryCallbackGTest::OnRemoteRequest(
57    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
58{
59    if (data.ReadInterfaceToken() != GetDescriptor()) {
60        TELEPHONY_LOGE("descriptor checked fail");
61        return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
62    }
63
64    switch (code) {
65        case static_cast<int>(DeliveryShortMessageCallbackInterfaceCode::ON_SMS_DELIVERY_RESULT): {
66            auto result = data.ReadString16();
67            OnSmsDeliveryResult(result);
68            return SMS_DEFAULT_RESULT;
69        }
70        default: {
71            OnSmsDeliveryResult(u"");
72            return SMS_DEFAULT_ERROR;
73        }
74    }
75}
76} // namespace Telephony
77} // namespace OHOS