• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-2025 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 "call_manager_info.h"
17 #include "call_manager_errors.h"
18 #include <memory>
19 #define PRIVATE public
20 #define PROTECTED public
21 #include "gtest/gtest.h"
22 #include "spam_call_adapter.h"
23 #include "callback_stub_helper.h"
24 #include "reminder_callback_stub_helper.h"
25 #include "spam_call_connection.h"
26 #include "spam_call_stub.h"
27 #include "time_wait_helper.h"
28 #include "spam_call_proxy.h"
29 #include "call_ability_connection.h"
30 #include "call_setting_ability_connection.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 using namespace testing::ext;
35 
36 class SpamCallTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
SetUpTestCase()43 void SpamCallTest::SetUpTestCase() {}
44 
TearDownTestCase()45 void SpamCallTest::TearDownTestCase() {}
46 
SetUp()47 void SpamCallTest::SetUp() {}
48 
TearDown()49 void SpamCallTest::TearDown() {}
50 
51 /**
52  * @tc.number   Telephony_SpamCallAdapter_001
53  * @tc.name     test error branch
54  * @tc.desc     Function test
55  */
56 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_001, Function | MediumTest | Level1)
57 {
58     std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
59     int32_t errCode = -1;
60     std::string result = "";
61     spamCallAdapter_->GetDetectResult(errCode, result);
62     ASSERT_EQ(errCode, -1);
63     ASSERT_EQ(result, "");
64 }
65 
66 /**
67  * @tc.number   Telephony_SpamCallAdapter_002
68  * @tc.name     test error branch
69  * @tc.desc     Function test
70  */
71 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_002, Function | MediumTest | Level1)
72 {
73     std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
74     const std::string phoneNumber = "12345678900";
75     const int32_t slotId = 0;
76     ASSERT_FALSE(spamCallAdapter_->DetectSpamCall(phoneNumber, slotId));
77 }
78 
79 /**
80  * @tc.number   Telephony_SpamCallAdapter_003
81  * @tc.name     test error branch
82  * @tc.desc     Function test
83  */
84 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_003, Function | MediumTest | Level1)
85 {
86     std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
87     std::string jsonData = "{\"detectResult\":1,\"decisionReason\":1}";
88     NumberMarkInfo info;
89     bool isBlock = false;
90     int32_t blockReason = 0;
91     std::string detectDetails = "";
92     spamCallAdapter_->ParseDetectResult(jsonData, isBlock, info, blockReason, detectDetails);
93     ASSERT_TRUE(isBlock);
94     ASSERT_EQ(blockReason, 1);
95 }
96 
97 /**
98  * @tc.number   Telephony_CallbackStubHelper_001
99  * @tc.name     test error branch
100  * @tc.desc     Function test
101  */
102 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_001, Function | MediumTest | Level1)
103 {
104     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
105 
106     CallbackStubHelper callbackStubHelper(spamCallAdapter);
107     int32_t errCode = 0;
108     std::string result = "{\"detectResult\":0,\"decisionReason\":1002,\"markType\":0}";
109     ASSERT_EQ(callbackStubHelper.OnResult(errCode, result), TELEPHONY_SUCCESS);
110     ReminderCallbackStubHelper reminderCallbackStubHelper(spamCallAdapter);
111     result = "{\"reminderResult\":false,\"slotId\":0,\"reminderTime\":1736428340229,\"remindedTimes\":0}";
112     ASSERT_EQ(reminderCallbackStubHelper.OnResult(errCode, result), TELEPHONY_SUCCESS);
113 }
114 
115 /**
116  * @tc.number   Telephony_SpamCallConnection_001
117  * @tc.name     test error branch
118  * @tc.desc     Function test
119  */
120 HWTEST_F(SpamCallTest, Telephony_SpamCallConnection_001, Function | MediumTest | Level1)
121 {
122     std::string phoneNumber = "123456789012";
123     int32_t slotId = 0;
124     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
125     ASSERT_NE(spamCallAdapter, nullptr);
126     SpamCallConnection spamCallConnection(phoneNumber, slotId, spamCallAdapter);
127     std::string bundle = "111";
128     std::string ability = "222";
129     AppExecFwk::ElementName element("", bundle, ability);
130     sptr<IRemoteObject> remoteObject;
131     int resultCode = 0;
132     spamCallConnection.OnAbilityConnectDone(element, remoteObject, resultCode);
133     ASSERT_EQ(resultCode, 0);
134     spamCallConnection.OnAbilityDisconnectDone(element, resultCode);
135     ASSERT_EQ(resultCode, 0);
136 }
137 
138 /**
139  * @tc.number   Telephony_TimeWaitHelper_001
140  * @tc.name     test error branch
141  * @tc.desc     Function test
142  */
143 HWTEST_F(SpamCallTest, Telephony_TimeWaitHelper_001, Function | MediumTest | Level1)
144 {
145     TimeWaitHelper timeWaitHelper(std::chrono::milliseconds(10000));
146     timeWaitHelper.NotifyAll();
147     ASSERT_NE(timeWaitHelper.WaitForResult(), true);
148 }
149 
150 /**
151  * @tc.number   Telephony_SpamCallProxy_001
152  * @tc.name     test error branch
153  * @tc.desc     Function test
154  */
155 HWTEST_F(SpamCallTest, Telephony_SpamCallProxy_001, Function | MediumTest | Level1)
156 {
157     sptr<IRemoteObject> remoteObject;
158     SpamCallProxy spamCallProxy(remoteObject);
159     std::string phoneNumber = "123456789012";
160     int32_t slotId = 0;
161     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
162     ASSERT_NE(spamCallProxy.DetectSpamCall(phoneNumber, slotId, spamCallAdapter), 0);
163     ASSERT_NE(spamCallProxy.RequireCallReminder(slotId, spamCallAdapter), 0);
164 }
165 
166 /**
167  * @tc.number   Telephony_CallAbilityConnection_001
168  * @tc.name     test error branch
169  * @tc.desc     Function test
170  */
171 HWTEST_F(SpamCallTest, Telephony_CallAbilityConnection_001, Function | MediumTest | Level1)
172 {
173     std::string commandStr = "11111";
174     CallAbilityConnection callAbilityConnection(commandStr);
175     std::string bundle = "111";
176     std::string ability = "222";
177     AppExecFwk::ElementName element("", bundle, ability);
178     int resultCode = 0;
179     callAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
180     ASSERT_EQ(resultCode, 0);
181 }
182 
183 /**
184  * @tc.number   Telephony_CallSettingAbilityConnection_001
185  * @tc.name     test error branch
186  * @tc.desc     Function test
187  */
188 HWTEST_F(SpamCallTest, Telephony_CallSettingAbilityConnection_001, Function | MediumTest | Level1)
189 {
190     std::string commandStr = "11111";
191     CallSettingAbilityConnection callSettingAbilityConnection(commandStr);
192     std::string bundle = "111";
193     std::string ability = "222";
194     AppExecFwk::ElementName element("", bundle, ability);
195     int resultCode = 0;
196     callSettingAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
197     ASSERT_EQ(resultCode, 0);
198 }
199 
200 }
201 }
202