• 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 "ans_inner_errors.h"
21 #include "dh_notification_clone_bundle_service.h"
22 #include "advanced_notification_service.h"
23 #undef private
24 #undef protected
25 
26 using namespace testing::ext;
27 using namespace testing;
28 using namespace OHOS;
29 using namespace Notification;
30 
31 // Test suite class
32 class DhNotificationCloneBundleTest : public ::testing::Test {
33 protected:
SetUp()34     void SetUp() override
35     {
36         // Initialize objects and dependencies
37         dhNotificationCloneBundle = DhNotificationCloneBundle::GetInstance();
38     }
39 
TearDown()40     void TearDown() override
41     {}
42 
43     std::shared_ptr<DhNotificationCloneBundle> dhNotificationCloneBundle = nullptr;
44 };
45 
46 /**
47  * @tc.name: OnRestore_Test_001
48  * @tc.desc: Test that error is reported when appIndex is -1
49  * @tc.type: FUNC
50  * @tc.require: issue
51  */
52 HWTEST_F(DhNotificationCloneBundleTest, OnRestore_Test_001, Function | SmallTest | Level1)
53 {
54     nlohmann::json jsonObject;
55     int32_t userId = 100;
56     auto advancedNotificationService_ = AdvancedNotificationService::GetInstance();
57 
58     sptr<NotificationDoNotDisturbProfile> date = nullptr;
59     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles = { date };
60     auto ret = advancedNotificationService_->AddDoNotDisturbProfiles(profiles);
61 
62     ErrCode result = dhNotificationCloneBundle->OnBackup(jsonObject);
63     dhNotificationCloneBundle->OnRestore(jsonObject);
64     EXPECT_EQ(result, ERR_OK);
65 }
66 
67 /**
68  * @tc.name: OnRestoreStart_Test_001
69  * @tc.desc: Test that error is reported when bundlesInfo_ is null.
70  * @tc.type: FUNC
71  * @tc.require: issue
72  */
73 HWTEST_F(DhNotificationCloneBundleTest, OnRestoreStart_Test_001, Function | SmallTest | Level1)
74 {
75     // Arrange
76     std::string bundleName = "testBundle";
77     int32_t appIndex = 0;
78     int32_t userId = 0;
79     int32_t uid = 12345;
80 
81     // Ensure bundlesInfo_ is empty
82     dhNotificationCloneBundle->bundlesInfo_.clear();
83 
84     // Act
85     dhNotificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid);
86 
87     EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.size(), 0);
88 }
89 
90 /**
91  * @tc.name: OnRestoreStart_Test_002
92  * @tc.desc: Test that error is reported when bundlesInfo_ is not null.
93  * @tc.type: FUNC
94  * @tc.require: issue
95  */
96 HWTEST_F(DhNotificationCloneBundleTest, OnRestoreStart_Test_002, Function | SmallTest | Level1)
97 {
98     // Arrange
99     std::string bundleName = "testBundle";
100     int32_t appIndex = 0;
101     int32_t userId = 0;
102     int32_t uid = 12345;
103 
104     // Create a bundle with the same name
105     NotificationCloneBundleInfo bundleInfo;
106     bundleInfo.SetBundleName(bundleName);
107     bundleInfo.SetUid(54321); // Original UID
108 
109     // Add the bundle to bundlesInfo_
110     dhNotificationCloneBundle->bundlesInfo_.push_back(bundleInfo);
111 
112     // Act
113     dhNotificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid);
114 
115     // Assert
116     // Check if the bundle was updated and deleted
117     EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.size(), 0);
118 }
119 
120 /**
121  * @tc.name: OnRestoreStart_Test_003
122  * @tc.desc: Test that error is reported when bundlesInfo_ is not null.
123  * @tc.type: FUNC
124  * @tc.require: issue
125  */
126 HWTEST_F(DhNotificationCloneBundleTest, OnRestoreStart_Test_003, Function | SmallTest | Level1)
127 {
128     // Arrange
129     std::string bundleName = "testBundle";
130     int32_t appIndex = 0;
131     int32_t userId = 0;
132     int32_t uid = 12345;
133 
134     // Create a bundle with a different name
135     NotificationCloneBundleInfo bundleInfo;
136     bundleInfo.SetBundleName("differentBundle");
137     bundleInfo.SetUid(54321);
138 
139     // Add the bundle to bundlesInfo_
140     dhNotificationCloneBundle->bundlesInfo_.push_back(bundleInfo);
141 
142     // Act
143     dhNotificationCloneBundle->OnRestoreStart(bundleName, appIndex, userId, uid);
144 
145     // Assert
146     // Check if the bundle was not modified
147     EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.size(), 1);
148     EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.front().GetBundleName(), "differentBundle");
149     EXPECT_EQ(dhNotificationCloneBundle->bundlesInfo_.front().GetUid(), 54321);
150 }
151 
152 /**
153  * @tc.name: OnUserSwitch_Test_001
154  * @tc.desc: Test that error is reported when dhCloneBundleQueue_ is null.
155  * @tc.type: FUNC
156  * @tc.require: issue
157  */
158 HWTEST_F(DhNotificationCloneBundleTest, OnUserSwitch_Test_001, Function | SmallTest | Level1)
159 {
160     dhNotificationCloneBundle->dhCloneBundleQueue_ = nullptr;
161 
162     // Call the function under test
163     dhNotificationCloneBundle->OnUserSwitch(1);
164 
165     EXPECT_EQ(dhNotificationCloneBundle->dhCloneBundleQueue_, nullptr);
166 }