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 <functional>
17 #include <gtest/gtest.h>
18 
19 #include "ans_inner_errors.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 #include "parcel.h"
23 #include "push_callback_proxy.h"
24 #include "push_callback_stub.h"
25 
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace Notification {
29 class PushCallBackStubTest : public testing::Test {
30 public:
SetUpTestCase()31     static void SetUpTestCase() {};
TearDownTestCase()32     static void TearDownTestCase() {};
SetUp()33     void SetUp() {};
TearDown()34     void TearDown() {};
35 };
36 
37 class TestPushCallBackStub : public PushCallBackStub {
38 public:
39     TestPushCallBackStub() = default;
~TestPushCallBackStub()40     virtual ~TestPushCallBackStub()
41     {};
OnCheckNotification(const std::string & notificationData,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)42     int32_t OnCheckNotification(
43         const std::string &notificationData, const std::shared_ptr<PushCallBackParam> &pushCallBackParam) override
44     {
45         return 0;
46     }
47 };
48 
49 /**
50  * @tc.number    : PushCallBackStubTest_00100
51  * @tc.name      : OnRemoteRequest_0100
52  * @tc.desc      : Test OnRemoteRequest function
53  */
54 HWTEST_F(PushCallBackStubTest, PushCallBackStubTest_00100, Function | SmallTest | Level1)
55 {
56     uint32_t code = static_cast<uint32_t>(NotificationInterfaceCode::ON_CHECK_NOTIFICATION);
57     MessageParcel data;
58     MessageParcel reply;
59     MessageOption option = {MessageOption::TF_SYNC};
60     data.WriteInterfaceToken(u"error.GetDescriptor");
61 
62     TestPushCallBackStub testPushCallBackStub;
63     int ret = testPushCallBackStub.OnRemoteRequest(code, data, reply, option);
64     ASSERT_EQ(ret, (int)ERR_INVALID_STATE);
65 }
66 
67 /**
68  * @tc.number    : PushCallBackStubTest_00300
69  * @tc.name      : OnRemoteRequest_0300
70  * @tc.desc      : Test OnRemoteRequest function
71  */
72 HWTEST_F(PushCallBackStubTest, PushCallBackStubTest_00300, Function | SmallTest | Level1)
73 {
74     uint32_t code = 10;
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option = {MessageOption::TF_SYNC};
78 
79     TestPushCallBackStub testPushCallBackStub;
80     data.WriteInterfaceToken(testPushCallBackStub.GetDescriptor());
81 
82     int ret = testPushCallBackStub.OnRemoteRequest(code, data, reply, option);
83     int errcode = 305;
84     ASSERT_EQ(ret, errcode);
85 }
86 
87 /**
88  * @tc.number    : PushCallBackStubTest_00400
89  * @tc.name      : OnCheckNotification_0100
90  * @tc.desc      : Test OnCheckNotification function
91  */
92 HWTEST_F(PushCallBackStubTest, PushCallBackStubTest_00400, Function | SmallTest | Level1)
93 {
94     sptr<IRemoteObject> impl;
95     PushCallBackProxy pushCallBackProxy(impl);
96 
97     int ret = pushCallBackProxy.OnCheckNotification("", nullptr);
98     ASSERT_EQ(ret, false);
99 }
100 
101 /**
102  * @tc.number    : PushCallBackStubTest_00600
103  * @tc.name      : OnCheckNotification_0200
104  * @tc.desc      : Test OnCheckNotification function
105  */
106 HWTEST_F(PushCallBackStubTest, PushCallBackStubTest_00600, Function | SmallTest | Level1)
107 {
108     sptr<IRemoteObject> impl;
109     PushCallBackProxy pushCallBackProxy(impl);
110 
111     std::string notificationData = "this is notificationData";
112     int ret = pushCallBackProxy.OnCheckNotification(notificationData, nullptr);
113     ASSERT_EQ(ret, false);
114 }
115 
116 /**
117  * @tc.number    : ConvertPushCheckCodeToErrCode_00100
118  * @tc.name      : ConvertPushCheckCodeToErrCode_00100
119  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
120  */
121 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00100, Function | SmallTest | Level1)
122 {
123     int pushCheckCode = 0;
124     TestPushCallBackStub testPushCallBackStub;
125     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
126     ASSERT_EQ(ret, (int)ERR_OK);
127 }
128 
129 /**
130  * @tc.number    : ConvertPushCheckCodeToErrCode_00200
131  * @tc.name      : ConvertPushCheckCodeToErrCode_00200
132  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
133  */
134 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00200, Function | SmallTest | Level1)
135 {
136     int pushCheckCode = 1;
137     TestPushCallBackStub testPushCallBackStub;
138     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
139     ASSERT_EQ(ret, (int)ERR_ANS_TASK_ERR);
140 }
141 
142 /**
143  * @tc.number    : ConvertPushCheckCodeToErrCode_00300
144  * @tc.name      : ConvertPushCheckCodeToErrCode_00300
145  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
146  */
147 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00300, Function | SmallTest | Level1)
148 {
149     int pushCheckCode = 2;
150     TestPushCallBackStub testPushCallBackStub;
151     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
152     ASSERT_EQ(ret, (int)ERR_ANS_PUSH_CHECK_NETWORK_UNREACHABLE);
153 }
154 
155 /**
156  * @tc.number    : ConvertPushCheckCodeToErrCode_00400
157  * @tc.name      : ConvertPushCheckCodeToErrCode_00400
158  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
159  */
160 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00400, Function | SmallTest | Level1)
161 {
162     int pushCheckCode = 3;
163     TestPushCallBackStub testPushCallBackStub;
164     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
165     ASSERT_EQ(ret, (int)ERR_ANS_PUSH_CHECK_FAILED);
166 }
167 
168 /**
169  * @tc.number    : ConvertPushCheckCodeToErrCode_00500
170  * @tc.name      : ConvertPushCheckCodeToErrCode_00500
171  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
172  */
173 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00500, Function | SmallTest | Level1)
174 {
175     int pushCheckCode = 4;
176     TestPushCallBackStub testPushCallBackStub;
177     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
178     ASSERT_EQ(ret, (int)ERR_ANS_TASK_ERR);
179 }
180 
181 /**
182  * @tc.number    : ConvertPushCheckCodeToErrCode_00600
183  * @tc.name      : ConvertPushCheckCodeToErrCode_00600
184  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
185  */
186 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00600, Function | SmallTest | Level1)
187 {
188     int pushCheckCode = 5;
189     TestPushCallBackStub testPushCallBackStub;
190     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
191     ASSERT_EQ(ret, (int)ERR_ANS_PUSH_CHECK_EXTRAINFO_INVALID);
192 }
193 
194 /**
195  * @tc.number    : ConvertPushCheckCodeToErrCode_00700
196  * @tc.name      : ConvertPushCheckCodeToErrCode_00700
197  * @tc.desc      : Test ConvertPushCheckCodeToErrCode function
198  */
199 HWTEST_F(PushCallBackStubTest, ConvertPushCheckCodeToErrCode_00700, Function | SmallTest | Level1)
200 {
201     int pushCheckCode = -1;
202     TestPushCallBackStub testPushCallBackStub;
203     ErrCode ret = testPushCallBackStub.ConvertPushCheckCodeToErrCode(pushCheckCode);
204     ASSERT_EQ(ret, (int)ERR_ANS_PUSH_CHECK_FAILED);
205 }
206 }  // namespace Notification
207 }  // namespace OHOS
208