• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gmock/gmock.h"
18 #define private public
19 #define protected public
20 #include "notification_clone_manager.h"
21 #include "ans_inner_errors.h"
22 #undef private
23 #undef protected
24 
25 using namespace testing::ext;
26 using namespace testing;
27 using namespace OHOS;
28 using namespace Notification;
29 
30 // Test suite class
31 class AncoRestoreStartEventSubscriberTest : public ::testing::Test {
32 protected:
SetUp()33     void SetUp() override
34     {
35         // Initialize objects and dependencies
36         ancoRestoreStartEventSubscriber = AncoRestoreStartEventSubscriber::create();
37         notificationCloneManager = &NotificationCloneManager::GetInstance();
38     }
39 
TearDown()40     void TearDown() override
41     {}
42 
43     std::shared_ptr<AncoRestoreStartEventSubscriber> ancoRestoreStartEventSubscriber = nullptr;
44     NotificationCloneManager* notificationCloneManager;
45 };
46 
47 /**
48  * @tc.name: OnReceiveEvent_Test_001
49  * @tc.desc: Test that error is reported when uid is 0
50  * @tc.type: FUNC
51  * @tc.require: issue
52  */
53 HWTEST_F(AncoRestoreStartEventSubscriberTest, OnReceiveEvent_Test_001, Function | SmallTest | Level1)
54 {
55     EventFwk::Want want;
56     want.SetParam("bundleName", std::string("testBundle"));
57     want.SetParam("uid", 0);
58     EventFwk::CommonEventData data{want};
59 
60     // Act
61     ancoRestoreStartEventSubscriber->OnReceiveEvent(data);
62     EXPECT_EQ(data.GetWant().GetIntParam("uid", 0), 0);
63 }
64 
65 /**
66  * @tc.name: OnReceiveEvent_Test_002
67  * @tc.desc: Test that error is reported when uid is negative
68  * @tc.type: FUNC
69  * @tc.require: issue
70  */
71 HWTEST_F(AncoRestoreStartEventSubscriberTest, OnReceiveEvent_Test_002, Function | SmallTest | Level1)
72 {
73     // Arrange
74     EventFwk::Want want;
75     want.SetParam("bundleName", std::string("testBundle"));
76     want.SetParam("uid", -1);
77     EventFwk::CommonEventData data{want};
78 
79     // Act
80     ancoRestoreStartEventSubscriber->OnReceiveEvent(data);
81     EXPECT_EQ(data.GetWant().GetIntParam("uid", 0), -1);
82 }
83 
84 /**
85  * @tc.name: OnReceiveEvent_Test_003
86  * @tc.desc: Test that error is reported when uid is negative
87  * @tc.type: FUNC
88  * @tc.require: issue
89  */
90 HWTEST_F(AncoRestoreStartEventSubscriberTest, OnReceiveEvent_Test_003, Function | SmallTest | Level1)
91 {
92     // Arrange
93     EventFwk::Want want;
94     want.SetParam("bundleName", std::string("testBundle"));
95     want.SetParam("uid", 1);
96     EventFwk::CommonEventData data{want};
97 
98     // Act
99     ancoRestoreStartEventSubscriber->OnReceiveEvent(data);
100     EXPECT_EQ(data.GetWant().GetIntParam("uid", 0), 1);
101 }
102 
103 /**
104  * @tc.name: OnRestoreStart_Test_001
105  * @tc.desc: Test that error is reported when uid is negative
106  * @tc.type: FUNC
107  * @tc.require: issue
108  */
109 HWTEST_F(AncoRestoreStartEventSubscriberTest, OnRestoreStart_Test_001, Function | SmallTest | Level1)
110 {
111     // Arrange
112     EventFwk::Want want;
113     want.SetParam("bundleName", std::string("testBundle"));
114     want.SetParam("index", -1);
115 
116     // Act
117     notificationCloneManager->OnRestoreStart(want);
118     EXPECT_EQ(want.GetIntParam("index", 0), -1);
119 }
120 
121 /**
122  * @tc.name: OnRestoreStart_Test_002
123  * @tc.desc: Test that error is reported when uid is negative
124  * @tc.type: FUNC
125  * @tc.require: issue
126  */
127 HWTEST_F(AncoRestoreStartEventSubscriberTest, OnRestoreStart_Test_002, Function | SmallTest | Level1)
128 {
129     // Arrange
130     EventFwk::Want want;
131     want.SetParam("bundleName", std::string(""));
132     want.SetParam("index", 1);
133 
134     // Act
135     notificationCloneManager->OnRestoreStart(want);
136     EXPECT_EQ(want.GetIntParam("index", 0), 1);
137 }
138 
139 /**
140  * @tc.name: OnDhRestoreStart_Test_001
141  * @tc.desc: Test that error is reported when uid is negative
142  * @tc.type: FUNC
143  * @tc.require: issue
144  */
145 HWTEST_F(AncoRestoreStartEventSubscriberTest, OnDhRestoreStart_Test_001, Function | SmallTest | Level1)
146 {
147     std::string bundleName = "testBundle";
148     int32_t uid = 12345;
149 
150     // Ensure cloneTemplates is empty
151     notificationCloneManager->cloneTemplates.clear();
152 
153     // Call the function
154     notificationCloneManager->OnDhRestoreStart(bundleName, uid);
155 
156     EXPECT_TRUE(notificationCloneManager->cloneTemplates.empty());
157 }