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 <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<AnsSubscriberLocalLiveViewInterface> subscriber_;
53 };
54 
55 std::shared_ptr<NotificationLocalLiveViewSubscriberManager>
56     NotificationLocalLiveViewSubscriberManagerTest::notificationLocalLiveViewSubscriberManager_ = nullptr;
57 NotificationLocalLiveViewSubscriberManagerTest::TestAnsSubscriber
58     NotificationLocalLiveViewSubscriberManagerTest::testAnsSubscriber_;
59 sptr<AnsSubscriberLocalLiveViewInterface>
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 }
87 
88 /**
89  * @tc.number    : NotificationLocalLiveViewSubscriberManagerTest_001
90  * @tc.name      : ANS_AddSubscriber_001
91  * @tc.desc      : Test AddSubscriber function, return is ERR_OK.
92  */
93 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
94     NotificationLocalLiveViewSubscriberManagerTest_001, Function | SmallTest | Level1)
95 {
96     sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
97     ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
98 }
99 
100 /**
101  * @tc.number    : NotificationLocalLiveViewSubscriberManagerTest_002
102  * @tc.name      : ANS_AddSubscriber_002
103  * @tc.desc      : Test AddSubscriber function AND RemoveSubscriberInner, return is ERR_OK.
104  */
105 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
106     NotificationLocalLiveViewSubscriberManagerTest_002, Function | SmallTest | Level1)
107 {
108     sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
109     ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
110     ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->
111         RemoveLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
112 }
113 
114 /**
115  * @tc.number    : NotificationLocalLiveViewSubscriberManagerTest_003
116  * @tc.name      : ANS_AddSubscriber_003
117  * @tc.desc      : Test NotifyTriggerResponse, return is not nullptr.
118  */
119 HWTEST_F(NotificationLocalLiveViewSubscriberManagerTest,
120     NotificationLocalLiveViewSubscriberManagerTest_003, Function | SmallTest | Level1)
121 {
122     sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
123     ASSERT_EQ(notificationLocalLiveViewSubscriberManager_->AddLocalLiveViewSubscriber(subscriber_, info), (int)ERR_OK);
124     sptr<NotificationButtonOption> buttonOption = new NotificationButtonOption();
125     sptr<NotificationRequest> request = new NotificationRequest();
126     sptr<Notification> notification = new Notification(request);
127     notificationLocalLiveViewSubscriberManager_->NotifyTriggerResponse(notification, buttonOption);
128 }
129 }  // namespace Notification
130 }  // namespace OHOS
131