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 <functional>
17 #include <gtest/gtest.h>
18 
19 #include "work_sched_data_manager.h"
20 #include "work_scheduler_service.h"
21 #include "work_sched_utils.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace WorkScheduler {
27 class DataManagerTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
TearDownTestCase()30     static void TearDownTestCase() {}
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33     static std::shared_ptr<DataManager> dataManager_;
34 };
35 
36 std::shared_ptr<DataManager> DataManagerTest::dataManager_ = nullptr;
37 
SetUpTestCase()38 void DataManagerTest::SetUpTestCase()
39 {
40     dataManager_ = DelayedSingleton<DataManager>::GetInstance();
41 }
42 
43 /**
44  * @tc.name: AddDeviceStandyWhitelist_001
45  * @tc.desc: Test DataManager AddDeviceStandyWhitelist.
46  * @tc.type: FUNC
47  * @tc.require: I8JBRY
48  */
49 HWTEST_F(DataManagerTest, AddDeviceStandyWhitelist_001, TestSize.Level1)
50 {
51     dataManager_->ClearDeviceStandyWhitelist();
52     std::list<std::string> bundleNames = { "bundleName1", "bundleName2", "bundleName3" };
53     dataManager_->AddDeviceStandyWhitelist(bundleNames);
54     EXPECT_FALSE(dataManager_->IsDeviceStandyWhitelistEmpty());
55 }
56 
57 /**
58  * @tc.name: IsInDeviceStandyWhitelist_001
59  * @tc.desc: Test DataManager IsInDeviceStandyWhitelist.
60  * @tc.type: FUNC
61  * @tc.require: I8JBRY
62  */
63 HWTEST_F(DataManagerTest, IsInDeviceStandyWhitelist_001, TestSize.Level1)
64 {
65     dataManager_->ClearDeviceStandyWhitelist();
66     std::list<std::string> bundleNames = { "bundleName1", "bundleName2", "bundleName3" };
67     dataManager_->AddDeviceStandyWhitelist(bundleNames);
68     EXPECT_FALSE(dataManager_->IsInDeviceStandyWhitelist("bundleName4"));
69 }
70 
71 /**
72  * @tc.name: OnDeviceStandyWhitelistChanged_001
73  * @tc.desc: Test DataManager OnDeviceStandyWhitelistChanged.
74  * @tc.type: FUNC
75  * @tc.require: I8JBRY
76  */
77 HWTEST_F(DataManagerTest, OnDeviceStandyWhitelistChanged_001, TestSize.Level1)
78 {
79     dataManager_->ClearDeviceStandyWhitelist();
80     dataManager_->OnDeviceStandyWhitelistChanged("bundleName1", true);
81     EXPECT_FALSE(dataManager_->IsDeviceStandyWhitelistEmpty());
82 }
83 
84 /**
85  * @tc.name: OnDeviceStandyWhitelistChanged_002
86  * @tc.desc: Test DataManager OnDeviceStandyWhitelistChanged.
87  * @tc.type: FUNC
88  * @tc.require: I8JBRY
89  */
90 HWTEST_F(DataManagerTest, OnDeviceStandyWhitelistChanged_002, TestSize.Level1)
91 {
92     dataManager_->ClearDeviceStandyWhitelist();
93     dataManager_->OnDeviceStandyWhitelistChanged("bundleName1", true);
94     dataManager_->OnDeviceStandyWhitelistChanged("bundleName1", false);
95     EXPECT_TRUE(dataManager_->IsDeviceStandyWhitelistEmpty());
96 }
97 
98 /**
99  * @tc.name: AddGroup_001
100  * @tc.desc: Test DataManager AddGroup.
101  * @tc.type: FUNC
102  * @tc.require: I8JBRY
103  */
104 HWTEST_F(DataManagerTest, AddGroup_001, TestSize.Level1)
105 {
106     dataManager_->ClearAllGroup();
107     int32_t userId = 202220;
108     int32_t appGroup = 20;
109     dataManager_->AddGroup("bundleName1", userId, appGroup);
110     EXPECT_TRUE(dataManager_->FindGroup("bundleName1", userId, appGroup));
111 }
112 
113 /**
114  * @tc.name: AddGroup_002
115  * @tc.desc: Test DataManager AddGroup.
116  * @tc.type: FUNC
117  * @tc.require: I8JBRY
118  */
119 HWTEST_F(DataManagerTest, AddGroup_002, TestSize.Level1)
120 {
121     dataManager_->ClearAllGroup();
122     int32_t userId = 202220;
123     int32_t appGroup = 20;
124     dataManager_->AddGroup("bundleName1", userId, appGroup);
125     dataManager_->ClearGroup("bundleName1", userId);
126     EXPECT_FALSE(dataManager_->FindGroup("bundleName1", userId, appGroup));
127 }
128 
129 /**
130  * @tc.name: SetDeviceSleep_001
131  * @tc.desc: Test DataManager SetDeviceSleep.
132  * @tc.type: FUNC
133  * @tc.require: I8JBRY
134  */
135 HWTEST_F(DataManagerTest, SetDeviceSleep_001, TestSize.Level1)
136 {
137     dataManager_->SetDeviceSleep(false);
138     EXPECT_FALSE(dataManager_->GetDeviceSleep());
139 }
140 }
141 }