1 /*
2  * Copyright (C) 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 "gtest/gtest.h"
17 #include "radio_event.h"
18 #include "satellite/satellite_sms_service_ipc_interface_code.h"
19 #include "satellite_sms_callback.h"
20 #include "satellite_sms_proxy.h"
21 #include "sms_common.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 using namespace testing::ext;
28 
29 namespace {
30 class MockIRemoteObject : public IRemoteObject {
31 public:
32     uint32_t requestCode_ = -1;
33 
34 public:
MockIRemoteObject()35     MockIRemoteObject() : IRemoteObject(u"mock_i_remote_object") {}
36 
~MockIRemoteObject()37     ~MockIRemoteObject() {}
38 
GetObjectRefCount()39     int32_t GetObjectRefCount() override
40     {
41         return 0;
42     }
43 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
45     {
46         TELEPHONY_LOGI("Mock SendRequest");
47         requestCode_ = code;
48         reply.WriteInt32(0);
49         return 0;
50     }
51 
IsProxyObject() const52     bool IsProxyObject() const override
53     {
54         return true;
55     }
56 
CheckObjectLegality() const57     bool CheckObjectLegality() const override
58     {
59         return true;
60     }
61 
AddDeathRecipient(const sptr<DeathRecipient> & recipient)62     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
63     {
64         return true;
65     }
66 
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)67     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
68     {
69         return true;
70     }
71 
Marshalling(Parcel & parcel) const72     bool Marshalling(Parcel &parcel) const override
73     {
74         return true;
75     }
76 
AsInterface()77     sptr<IRemoteBroker> AsInterface() override
78     {
79         return nullptr;
80     }
81 
Dump(int fd,const std::vector<std::u16string> & args)82     int Dump(int fd, const std::vector<std::u16string> &args) override
83     {
84         return 0;
85     }
86 
GetObjectDescriptor() const87     std::u16string GetObjectDescriptor() const
88     {
89         std::u16string descriptor = std::u16string();
90         return descriptor;
91     }
92 };
93 } // namespace
94 
95 class SmsSatelliteGtest : public testing::Test {
96 public:
97     int32_t slotId_ = 0;
98     static void SetUpTestCase();
99     static void TearDownTestCase();
100     void SetUp();
101     void TearDown();
102 };
103 
TearDownTestCase()104 void SmsSatelliteGtest::TearDownTestCase() {}
105 
SetUp()106 void SmsSatelliteGtest::SetUp() {}
107 
TearDown()108 void SmsSatelliteGtest::TearDown() {}
109 
SetUpTestCase()110 void SmsSatelliteGtest::SetUpTestCase() {}
111 
ToCode(SatelliteSmsServiceInterfaceCode code)112 uint32_t ToCode(SatelliteSmsServiceInterfaceCode code)
113 {
114     return static_cast<uint32_t>(code);
115 }
116 
117 /**
118  * @tc.number   Telephony_SmsSatelliteGtest_RegisterSmsNotify_0001
119  * @tc.name     register sms callback
120  * @tc.desc     Function test
121  */
122 HWTEST_F(SmsSatelliteGtest, RegisterSmsNotify_0001, Function | MediumTest | Level2)
123 {
124     TELEPHONY_LOGI("SmsSatelliteGtest::RegisterSmsNotify_0001 -->");
125     sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
126     SatelliteSmsProxy proxy(remote);
127 
128     int32_t ret = proxy.RegisterSmsNotify(slotId_, RadioEvent::RADIO_SEND_SMS, nullptr);
129     ASSERT_NE(ret, TELEPHONY_SUCCESS);
130 
131     sptr<ISatelliteSmsCallback> callback = std::make_unique<SatelliteSmsCallback>(nullptr).release();
132     ret = proxy.RegisterSmsNotify(slotId_, RadioEvent::RADIO_SEND_SMS, callback);
133     ASSERT_EQ(remote->requestCode_, ToCode(SatelliteSmsServiceInterfaceCode::REGISTER_SMS_NOTIFY));
134     ASSERT_EQ(ret, TELEPHONY_SUCCESS);
135 }
136 
137 /**
138  * @tc.number   Telephony_SmsSatelliteGtest_UnRegisterSmsNotify_0001
139  * @tc.name     unregister sms callback
140  * @tc.desc     Function test
141  */
142 HWTEST_F(SmsSatelliteGtest, UnRegisterSmsNotify_0001, Function | MediumTest | Level2)
143 {
144     TELEPHONY_LOGI("SmsSatelliteGtest::UnRegisterSmsNotify_0001 -->");
145     sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
146     SatelliteSmsProxy proxy(remote);
147 
148     int32_t ret = proxy.UnRegisterSmsNotify(slotId_, RadioEvent::RADIO_SEND_SMS);
149     ASSERT_EQ(remote->requestCode_, ToCode(SatelliteSmsServiceInterfaceCode::UNREGISTER_SMS_NOTIFY));
150     ASSERT_EQ(ret, TELEPHONY_SUCCESS);
151 }
152 
153 /**
154  * @tc.number   Telephony_SmsSatelliteGtest_SendSms_0001
155  * @tc.name     send sms
156  * @tc.desc     Function test
157  */
158 HWTEST_F(SmsSatelliteGtest, SendSms_0001, Function | MediumTest | Level2)
159 {
160     TELEPHONY_LOGI("SmsSatelliteGtest::SendSms_0001 -->");
161     sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
162     SatelliteSmsProxy proxy(remote);
163 
164     SatelliteMessage message;
165     int32_t ret = proxy.SendSms(slotId_, RadioEvent::RADIO_SEND_SMS, message);
166     ASSERT_EQ(remote->requestCode_, ToCode(SatelliteSmsServiceInterfaceCode::SEND_SMS));
167     ASSERT_EQ(ret, TELEPHONY_SUCCESS);
168 }
169 
170 /**
171  * @tc.number   Telephony_SmsSatelliteGtest_SendSmsMoreMode_0001
172  * @tc.name     send sms more
173  * @tc.desc     Function test
174  */
175 HWTEST_F(SmsSatelliteGtest, SendSmsMoreMode_0001, Function | MediumTest | Level2)
176 {
177     TELEPHONY_LOGI("SmsSatelliteGtest::SendSmsMoreMode_0001 -->");
178     sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
179     SatelliteSmsProxy proxy(remote);
180 
181     SatelliteMessage message;
182     int32_t ret = proxy.SendSmsMoreMode(slotId_, RadioEvent::RADIO_SEND_SMS_EXPECT_MORE, message);
183     ASSERT_EQ(remote->requestCode_, ToCode(SatelliteSmsServiceInterfaceCode::SEND_SMS_MORE_MODE));
184     ASSERT_EQ(ret, TELEPHONY_SUCCESS);
185 }
186 
187 /**
188  * @tc.number   Telephony_SmsSatelliteGtest_SendSmsAck_0001
189  * @tc.name     send sms ack
190  * @tc.desc     Function test
191  */
192 HWTEST_F(SmsSatelliteGtest, SendSmsAck_0001, Function | MediumTest | Level2)
193 {
194     TELEPHONY_LOGI("SmsSatelliteGtest::SendSmsAck_0001 -->");
195     sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
196     SatelliteSmsProxy proxy(remote);
197 
198     int32_t ret = proxy.SendSmsAck(slotId_, SMS_EVENT_NEW_SMS_REPLY, true, AckIncomeCause::SMS_ACK_RESULT_OK);
199     ASSERT_EQ(remote->requestCode_, ToCode(SatelliteSmsServiceInterfaceCode::SEND_SMS_ACK));
200     ASSERT_EQ(ret, TELEPHONY_SUCCESS);
201 }
202 
203 } // namespace Telephony
204 } // namespace OHOS