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