1 /*
2 * Copyright (c) 2023-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 <gtest/gtest.h>
17 #include <iostream>
18
19 #define private public
20 #include "notification_local_live_view_subscriber.h"
21 #include "notification_local_live_view_subscriber_manager.h"
22
23 #include "ans_inner_errors.h"
24
25 using namespace testing::ext;
26 using namespace testing;
27
28 namespace OHOS {
29 namespace Notification {
30 class NotificationLocalLiveViewSubscriberManagerTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36
37 private:
38 class TestAnsSubscriber : public NotificationLocalLiveViewSubscriber {
39 public:
OnConnected()40 void OnConnected() override
41 {}
OnDisconnected()42 void OnDisconnected() override
43 {}
OnDied()44 void OnDied() override
45 {}
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)46 void OnResponse(int32_t notificationId, sptr<NotificationButtonOption> buttonOption) override
47 {}
48 };
49
50 static std::shared_ptr<NotificationLocalLiveViewSubscriberManager> notificationLocalLiveViewSubscriberManager_;
51 static TestAnsSubscriber testAnsSubscriber_;
52 static sptr<IAnsSubscriberLocalLiveView> subscriber_;
53 };
54
55 std::shared_ptr<NotificationLocalLiveViewSubscriberManager>
56 NotificationLocalLiveViewSubscriberManagerTest::notificationLocalLiveViewSubscriberManager_ = nullptr;
57 NotificationLocalLiveViewSubscriberManagerTest::TestAnsSubscriber
58 NotificationLocalLiveViewSubscriberManagerTest::testAnsSubscriber_;
59 sptr<IAnsSubscriberLocalLiveView>
60 NotificationLocalLiveViewSubscriberManagerTest::subscriber_ = nullptr;
61
SetUpTestCase()62 void NotificationLocalLiveViewSubscriberManagerTest::SetUpTestCase()
63 {
64 notificationLocalLiveViewSubscriberManager_ = NotificationLocalLiveViewSubscriberManager::GetInstance();
65 subscriber_ = testAnsSubscriber_.GetImpl();
66 }
67
TearDownTestCase()68 void NotificationLocalLiveViewSubscriberManagerTest::TearDownTestCase()
69 {
70 subscriber_ = nullptr;
71 if (notificationLocalLiveViewSubscriberManager_ != nullptr) {
72 notificationLocalLiveViewSubscriberManager_->ResetFfrtQueue();
73 notificationLocalLiveViewSubscriberManager_ = nullptr;
74 }
75 }
76
SetUp()77 void NotificationLocalLiveViewSubscriberManagerTest::SetUp()
78 {
79 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
80 notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info);
81 }
82
TearDown()83 void NotificationLocalLiveViewSubscriberManagerTest::TearDown()
84 {
85 notificationLocalLiveViewSubscriberManager_->RemoveLocalLiveViewSubscriber(subscriber_, nullptr);
86 if (notificationLocalLiveViewSubscriberManager_->notificationButtonQueue_ == nullptr) {
87 notificationLocalLiveViewSubscriberManager_->notificationButtonQueue_ =
88 std::make_shared<ffrt::queue>("NotificationLocalLiveViewMgr");
89 }
90 notificationLocalLiveViewSubscriberManager_->buttonRecordList_.clear();
91 }
92
93 /**
94 * @tc.number : NotificationLocalLiveViewSubscriberManagerTest_001
95 * @tc.name : ANS_AddSubscriber_001
96 * @tc.desc : Test AddSubscriber function, return is ERR_OK.
97 */
98 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
99 NotificationLocalLiveViewSubscriberManagerTest_001, Function | SmallTest | Level1)
100 {
101 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
102 ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
103 }
104
105 /**
106 * @tc.number : NotificationLocalLiveViewSubscriberManagerTest_002
107 * @tc.name : ANS_AddSubscriber_002
108 * @tc.desc : Test AddSubscriber function AND RemoveSubscriberInner, return is ERR_OK.
109 */
110 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
111 NotificationLocalLiveViewSubscriberManagerTest_002, Function | SmallTest | Level1)
112 {
113 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
114 ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
115 ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->
116 RemoveLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
117 }
118
119 /**
120 * @tc.number : NotificationLocalLiveViewSubscriberManagerTest_003
121 * @tc.name : ANS_AddSubscriber_003
122 * @tc.desc : Test NotifyTriggerResponse, return is not nullptr.
123 */
124 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
125 NotificationLocalLiveViewSubscriberManagerTest_003, Function | SmallTest | Level1)
126 {
127 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
128 ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
129 sptr<NotificationButtonOption> buttonOption = new NotificationButtonOption();
130 sptr<NotificationRequest> request = new NotificationRequest();
131 sptr<Notification> notification = new Notification(request);
132 notificationLocalLiveViewSubscriberManager_->NotifyTriggerResponse(notification, buttonOption);
133 }
134
135 /**
136 * @tc.number : NotificationLocalLiveViewSubscriberManagerTest_004
137 * @tc.name : ANS_AddSubscriber_004
138 */
139 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
140 NotificationLocalLiveViewSubscriberManagerTest_004, Function | SmallTest | Level1)
141 {
142 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
143 auto res = notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(nullptr, info);
144 ASSERT_EQ(res, ERR_ANS_INVALID_PARAM);
145 }
146
147 /**
148 * @tc.number : NotificationLocalLiveViewSubscriberManagerTest_005
149 * @tc.name : ANS_AddSubscriber_005
150 */
151 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
152 NotificationLocalLiveViewSubscriberManagerTest_005, Function | SmallTest | Level1)
153 {
154 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
155 notificationLocalLiveViewSubscriberManager_->notificationButtonQueue_ = nullptr;
156 auto res = notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info);
157
158 ASSERT_EQ(res, ERR_ANS_TASK_ERR);
159 }
160
161 /**
162 * @tc.number : RemoveLocalLiveViewSubscriberTest_001
163 * @tc.name : RemoveLocalLiveViewSubscriberTest_001
164 */
165 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
166 RemoveLocalLiveViewSubscriberTest_001, Function | SmallTest | Level1)
167 {
168 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
169 auto res = notificationLocalLiveViewSubscriberManager_->RemoveLocalLiveViewSubscriber(nullptr, info);
170 ASSERT_EQ(res, ERR_ANS_INVALID_PARAM);
171 }
172
173 /**
174 * @tc.number : RemoveLocalLiveViewSubscriberTest_002
175 * @tc.name : RemoveLocalLiveViewSubscriberTest_002
176 */
177 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
178 RemoveLocalLiveViewSubscriberTest_002, Function | SmallTest | Level1)
179 {
180 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
181 notificationLocalLiveViewSubscriberManager_->notificationButtonQueue_ = nullptr;
182 auto res = notificationLocalLiveViewSubscriberManager_->RemoveLocalLiveViewSubscriber(subscriber_, info);
183 ASSERT_EQ(res, ERR_ANS_TASK_ERR);
184 }
185
186 /**
187 * @tc.number : OnRemoteDied_001
188 * @tc.name : OnRemoteDied_001
189 */
190 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest, OnRemoteDied_001, Function | SmallTest | Level1)
191 {
192 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
193 ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
194
195 auto size = notificationLocalLiveViewSubscriberManager_->buttonRecordList_.size();
196 ASSERT_EQ(size, 1);
197
198 wptr<IRemoteObject> obj = subscriber_->AsObject();
199 notificationLocalLiveViewSubscriberManager_->OnRemoteDied(obj);
200
201 size = notificationLocalLiveViewSubscriberManager_->buttonRecordList_.size();
202 ASSERT_EQ(size, 0);
203 }
204
205 /**
206 * @tc.number : OnRemoteDied_002
207 * @tc.name : OnRemoteDied_002
208 */
209 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest, OnRemoteDied_002, Function | SmallTest | Level1)
210 {
211 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
212 ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
213 auto size = notificationLocalLiveViewSubscriberManager_->buttonRecordList_.size();
214 ASSERT_EQ(size, 1);
215
216 notificationLocalLiveViewSubscriberManager_->notificationButtonQueue_ = nullptr;
217 wptr<IRemoteObject> obj = subscriber_->AsObject();
218 notificationLocalLiveViewSubscriberManager_->OnRemoteDied(obj);
219 size = notificationLocalLiveViewSubscriberManager_->buttonRecordList_.size();
220 ASSERT_EQ(size, 1);
221 }
222
223 /**
224 * @tc.number : IsSystemUser_001
225 * @tc.name : IsSystemUser_001
226 */
227 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest, IsSystemUser_001, Function | SmallTest | Level1)
228 {
229 auto res = notificationLocalLiveViewSubscriberManager_->IsSystemUser(100);
230 ASSERT_FALSE(res);
231 }
232 } // namespace Notification
233 } // namespace OHOS
234