1 /*
2 * Copyright (c) 2021 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_subscriber.h"
21 #include "notification_subscriber_manager.h"
22
23 #include "ans_inner_errors.h"
24
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Notification {
28 class NotificationSubscriberManagerTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
33 void TearDown();
34
35 private:
36 class TestAnsSubscriber : public NotificationSubscriber {
37 public:
OnConnected()38 void OnConnected() override
39 {}
OnDisconnected()40 void OnDisconnected() override
41 {}
OnDied()42 void OnDied() override
43 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)44 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
45 {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)46 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
47 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)48 void OnEnabledNotificationChanged(
49 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
50 {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)51 void OnCanceled(const std::shared_ptr<Notification> &request,
52 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
53 {}
OnConsumed(const std::shared_ptr<Notification> & request)54 void OnConsumed(const std::shared_ptr<Notification> &request) override
55 {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)56 void OnConsumed(const std::shared_ptr<Notification> &request,
57 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
58 {}
59 };
60
61 static std::shared_ptr<NotificationSubscriberManager> notificationSubscriberManager_;
62 static TestAnsSubscriber testAnsSubscriber_;
63 static sptr<AnsSubscriberInterface> subscriber_;
64 };
65
66 std::shared_ptr<NotificationSubscriberManager> NotificationSubscriberManagerTest::notificationSubscriberManager_ =
67 nullptr;
68 NotificationSubscriberManagerTest::TestAnsSubscriber NotificationSubscriberManagerTest::testAnsSubscriber_;
69 sptr<AnsSubscriberInterface> NotificationSubscriberManagerTest::subscriber_ = nullptr;
70
SetUpTestCase()71 void NotificationSubscriberManagerTest::SetUpTestCase()
72 {
73 notificationSubscriberManager_ = NotificationSubscriberManager::GetInstance();
74 subscriber_ = testAnsSubscriber_.GetImpl();
75 }
76
TearDownTestCase()77 void NotificationSubscriberManagerTest::TearDownTestCase()
78 {
79 subscriber_ = nullptr;
80 notificationSubscriberManager_ = nullptr;
81 }
82
SetUp()83 void NotificationSubscriberManagerTest::SetUp()
84 {
85 notificationSubscriberManager_->AddSubscriber(subscriber_, nullptr);
86 }
87
TearDown()88 void NotificationSubscriberManagerTest::TearDown()
89 {
90 notificationSubscriberManager_->RemoveSubscriber(subscriber_, nullptr);
91 }
92
93 /**
94 * @tc.number : NotificationSubscriberManagerTest_001
95 * @tc.name : ANS_AddSubscriber_0100
96 * @tc.desc : Test AddSubscriber function, return is ERR_OK.
97 */
98 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_001, Function | SmallTest | Level1)
99 {
100 // Test NotifyUpdated function.
101 const std::vector<NotificationSorting> sortingList;
102 sptr<NotificationSortingMap> map = new NotificationSortingMap(sortingList);
103 notificationSubscriberManager_->NotifyUpdated(map);
104
105 // Test AddSubscriber function.
106 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
107 info->AddAppName("test_bundle");
108 EXPECT_EQ(notificationSubscriberManager_->AddSubscriber(subscriber_, info), (int)ERR_OK);
109 EXPECT_EQ(notificationSubscriberManager_->AddSubscriber(subscriber_, nullptr), (int)ERR_OK);
110 }
111
112 /**
113 * @tc.number : NotificationSubscriberManagerTest_002
114 * @tc.name : ANS_AddSubscriber_0100
115 * @tc.desc : Test AddSubscriber function when subscriber is nullptr, return is ERR_ANS_INVALID_PARAM.
116 */
117 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_002, Function | SmallTest | Level1)
118 {
119 // Test NotifyDisturbModeChanged function.
120 sptr<NotificationDoNotDisturbDate> date =
121 new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
122 notificationSubscriberManager_->NotifyDoNotDisturbDateChanged(date);
123
124 // Test AddSubscriber function.
125 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
126 EXPECT_EQ(notificationSubscriberManager_->AddSubscriber(nullptr, info), (int)ERR_ANS_INVALID_PARAM);
127 }
128
129 /**
130 * @tc.number : NotificationSubscriberManagerTest_003
131 * @tc.name : ANS_RemoveSubscriber_0100
132 * @tc.desc : Test RemoveSubscriber function, return is ERR_OK.
133 */
134 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_003, Function | SmallTest | Level1)
135 {
136 // Test NotifyConsumed function.
137 std::vector<NotificationSorting> sortingList;
138 sptr<NotificationRequest> request = new NotificationRequest();
139 sptr<Notification> notification = new Notification(request);
140 sptr<NotificationSortingMap> notificationMap = new NotificationSortingMap(sortingList);
141 notificationSubscriberManager_->NotifyConsumed(notification, notificationMap);
142
143 // Test RemoveSubscriber function.
144 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
145 info->AddAppName("test_bundle");
146 EXPECT_EQ(notificationSubscriberManager_->RemoveSubscriber(subscriber_, info), (int)ERR_OK);
147 }
148
149 /**
150 * @tc.number : NotificationSubscriberManagerTest_004
151 * @tc.name : ANS_AddSubscriber_0100
152 * @tc.desc : Test RemoveSubscriber function when subscriber is nullptr, return is ERR_ANS_INVALID_PARAM.
153 */
154 HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_004, Function | SmallTest | Level1)
155 {
156 // Test NotifyCanceled function.
157 std::vector<NotificationSorting> sortingList;
158 sptr<NotificationRequest> request = new NotificationRequest();
159 sptr<Notification> notification = new Notification(request);
160 sptr<NotificationSortingMap> notificationMap = new NotificationSortingMap(sortingList);
161 int deleteReason = 0;
162 notificationSubscriberManager_->NotifyCanceled(notification, notificationMap, deleteReason);
163
164 // Test RemoveSubscriber function.
165 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
166 EXPECT_EQ(notificationSubscriberManager_->RemoveSubscriber(nullptr, info), (int)ERR_ANS_INVALID_PARAM);
167 }
168 } // namespace Notification
169 } // namespace OHOS