• 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_bundle_service.h"
21 #include "notification_do_not_disturb_profile.h"
22 #include "ans_inner_errors.h"
23 #include "notification_clone_util.h"
24 #include "advanced_notification_service.h"
25 #include "mock/mock_notification_clone_util.h"
26 #undef private
27 #undef protected
28 
29 using namespace testing::ext;
30 using namespace testing;
31 using ::testing::_;
32 using ::testing::SetArgPointee;
33 using ::testing::Return;
34 using ::testing::DoAll;
35 using namespace OHOS;
36 using namespace Notification;
37 
38 // Test suite class
39 class NotificationCloneBundleTest : public ::testing::Test {
40 protected:
SetUp()41     void SetUp() override
42     {
43         // Initialize objects and dependencies
44         notificationCloneBundle = new NotificationCloneBundle();
45     }
46 
TearDown()47     void TearDown() override
48     {
49         // Clean up resources
50         delete notificationCloneBundle;
51         notificationCloneBundle = nullptr;
52     }
53 
54     NotificationCloneBundle* notificationCloneBundle;
55 };
56 
57 /**
58  * @tc.name: OnBackUp_00001
59  * @tc.desc: Test clone OnBackUp.
60  * @tc.type: FUNC
61  * @tc.require: issue
62  */
63 HWTEST_F(NotificationCloneBundleTest, OnBackUp_00001, Function | SmallTest | Level1)
64 {
65     nlohmann::json jsonObject;
66     int32_t userId = 100;
67     auto advancedNotificationService_ = AdvancedNotificationService::GetInstance();
68 
69     sptr<NotificationDoNotDisturbProfile> date = nullptr;
70     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles = { date };
71     auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles);
72 
73     ErrCode result = notificationCloneBundle->OnBackup(jsonObject);
74     notificationCloneBundle->OnRestore(jsonObject);
75     EXPECT_EQ(result, ERR_OK);
76 }
77 
78 /**
79  * @tc.name: OnRestoreStart_Test_001
80  * @tc.desc: Test that OnRestoreStart does nothing when bundlesInfo_ is empty.
81  * @tc.type: FUNC
82  * @tc.require: issue
83  */
84 HWTEST_F(NotificationCloneBundleTest, OnRestoreStart_Test_001, Function | SmallTest | Level1)
85 {
86     // Arrange
87     std::string bundleName = "testBundle";
88     int32_t appIndex = 1;
89     int32_t userId = 100;
90     int32_t uid = 200;
91 
92     // Ensure bundlesInfo_ is empty
93     notificationCloneBundle->bundlesInfo_.clear();
94 
95     // Act
96     notificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid);
97 
98     // Assert
99     EXPECT_TRUE(notificationCloneBundle->bundlesInfo_.empty());
100 }
101 
102 /**
103  * @tc.name: OnRestoreStart_Test_002
104  * @tc.desc: Test that OnRestoreStart updates and removes the bundle when a match is found.
105  * @tc.type: FUNC
106  * @tc.require: issue
107  */
108 HWTEST_F(NotificationCloneBundleTest, OnRestoreStart_Test_002, Function | SmallTest | Level1)
109 {
110     // Arrange
111     std::string bundleName = "testBundle";
112     int32_t appIndex = 1;
113     int32_t userId = 100;
114     int32_t uid = 200;
115 
116     // Create a matching bundle and add it to bundlesInfo_
117     NotificationCloneBundleInfo bundleInfo;
118     bundleInfo.SetBundleName(bundleName);
119     bundleInfo.SetAppIndex(appIndex);
120     bundleInfo.SetUid(150); // Original uid
121     notificationCloneBundle->bundlesInfo_.push_back(bundleInfo);
122 
123     // Act
124     notificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid);
125 
126     // Assert
127     EXPECT_EQ(notificationCloneBundle->bundlesInfo_.size(), 0);
128 }
129 
130 /**
131  * @tc.name: OnRestoreStart_Test_003
132  * @tc.desc: Test that OnRestoreStart updates and removes the bundle when a match is found.
133  * @tc.type: FUNC
134  * @tc.require: issue
135  */
136 HWTEST_F(NotificationCloneBundleTest, OnRestoreStart_Test_003, Function | SmallTest | Level1)
137 {
138     // Arrange
139     std::string bundleName = "testBundle";
140     int32_t appIndex = 1;
141     int32_t userId = 100;
142     int32_t uid = 200;
143 
144     // Create a non-matching bundle and add it to bundlesInfo_
145     NotificationCloneBundleInfo bundleInfo;
146     bundleInfo.SetBundleName("otherBundle");
147     bundleInfo.SetAppIndex(2);
148     bundleInfo.SetUid(150);
149     notificationCloneBundle->bundlesInfo_.push_back(bundleInfo);
150 
151     // Act
152     notificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid);
153 
154     // Assert
155     EXPECT_EQ(notificationCloneBundle->bundlesInfo_.size(), 1);
156 }
157 
158 /**
159  * @tc.name: OnUserSwitch_Test_001
160  * @tc.desc: Test OnUserSwitch function when cloneBundleQueue_ is empty
161  * @tc.type: FUNC
162  * @tc.require: issue
163  */
164 HWTEST_F(NotificationCloneBundleTest, OnUserSwitch_Test_001, Function | SmallTest | Level1)
165 {
166     // Ensure cloneDisturbQueue_ is not null
167     notificationCloneBundle->cloneBundleQueue_  = nullptr;
168 
169     // Call the function
170     notificationCloneBundle->OnUserSwitch(100);
171 
172     // Verify that the profile is deleted
173     EXPECT_EQ(notificationCloneBundle->cloneBundleQueue_, nullptr);
174 }