1 /*
2 * Copyright (c) 2022 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 <chrono>
18 #include <thread>
19
20 #include "gtest/gtest.h"
21 #include "gmock/gmock.h"
22
23 #include "bgtaskmgr_inner_errors.h"
24 #include "background_task_subscriber.h"
25 #include "bg_efficiency_resources_mgr.h"
26 #include "resource_type.h"
27 #include "system_ability_definition.h"
28
29 #include "ibundle_manager_helper_mock.h"
30
31 using namespace testing::ext;
32 using namespace testing;
33 using testing::Return;
34 using testing::DoAll;
35
36 namespace OHOS {
37 namespace BackgroundTaskMgr {
38 static constexpr int32_t CPU_INDEX = 1;
39 static constexpr int32_t TIMER_INDEX = 3;
40 static const uint32_t MAX_RESOURCES_TYPE_NUM = ResourceTypeName.size();
41 static const uint32_t MAX_RESOURCES_MASK = (1 << MAX_RESOURCES_TYPE_NUM) - 1;
42
43 class MockEfficiencyResourcesMgrTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49
50 static std::shared_ptr<BgEfficiencyResourcesMgr> bgEfficiencyResourcesMgr_;
51 static std::shared_ptr<MockBundleManagerHelper> bundleManagerHelperMock_;
52 };
53
54 std::shared_ptr<BgEfficiencyResourcesMgr> MockEfficiencyResourcesMgrTest::bgEfficiencyResourcesMgr_ = nullptr;
55 std::shared_ptr<MockBundleManagerHelper> MockEfficiencyResourcesMgrTest::bundleManagerHelperMock_ = nullptr;
56
SetUpTestCase()57 void MockEfficiencyResourcesMgrTest::SetUpTestCase()
58 {
59 bgEfficiencyResourcesMgr_ = DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance();
60 }
61
TearDownTestCase()62 void MockEfficiencyResourcesMgrTest::TearDownTestCase()
63 {
64 bundleManagerHelperMock_.reset();
65 }
66
SetUp()67 void MockEfficiencyResourcesMgrTest::SetUp()
68 {
69 bundleManagerHelperMock_ = std::make_shared<MockBundleManagerHelper>();
70 SetBundleManagerHelper(bundleManagerHelperMock_);
71 }
72
TearDown()73 void MockEfficiencyResourcesMgrTest::TearDown()
74 {
75 CleanBundleManagerHelper();
76 }
77
78 /**
79 * @tc.name: GetExemptedResourceType_001
80 * @tc.desc: should return 0 when failed to get application info
81 * @tc.type: FUNC
82 */
83 HWTEST_F(MockEfficiencyResourcesMgrTest, GetExemptedResourceType_001, TestSize.Level1)
84 {
85 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _)).WillOnce(Return(false));
86 auto exemptedType = bgEfficiencyResourcesMgr_->GetExemptedResourceType(MAX_RESOURCES_MASK, -1, "");
87 EXPECT_EQ(exemptedType, 0u);
88 }
89
90 /**
91 * @tc.name: GetExemptedResourceType_002
92 * @tc.desc: should return 0 when resource type is inconsistence with configuration
93 * @tc.type: FUNC
94 */
95 HWTEST_F(MockEfficiencyResourcesMgrTest, GetExemptedResourceType_002, TestSize.Level1)
96 {
97 AppExecFwk::ApplicationInfo info {};
98 info.resourcesApply = { TIMER_INDEX };
99 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _))
100 .WillOnce(DoAll(SetArgReferee<3>(info), Return(true)));
101 auto exemptedType = bgEfficiencyResourcesMgr_->GetExemptedResourceType(ResourceType::CPU, -1, "");
102 EXPECT_EQ(exemptedType, 0u);
103 }
104
105 /**
106 * @tc.name: GetExemptedResourceType_003
107 * @tc.desc: should return origin number when resource list contain 0
108 * @tc.type: FUNC
109 */
110 HWTEST_F(MockEfficiencyResourcesMgrTest, GetExemptedResourceType_003, TestSize.Level1)
111 {
112 AppExecFwk::ApplicationInfo info {};
113 info.resourcesApply = { 0 };
114 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _))
115 .WillOnce(DoAll(SetArgReferee<3>(info), Return(true)));
116 auto exemptedType = bgEfficiencyResourcesMgr_->GetExemptedResourceType(MAX_RESOURCES_MASK, -1, "");
117 EXPECT_EQ(exemptedType, MAX_RESOURCES_MASK);
118 }
119
120 /**
121 * @tc.name: GetExemptedResourceType_004
122 * @tc.desc: should return CPU when resource list contain CPU_INDEX
123 * @tc.type: FUNC
124 */
125 HWTEST_F(MockEfficiencyResourcesMgrTest, GetExemptedResourceType_004, TestSize.Level1)
126 {
127 AppExecFwk::ApplicationInfo info {};
128 info.resourcesApply = { CPU_INDEX };
129 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _))
130 .WillOnce(DoAll(SetArgReferee<3>(info), Return(true)));
131 auto exemptedType = bgEfficiencyResourcesMgr_->GetExemptedResourceType(MAX_RESOURCES_MASK, -1, "");
132 EXPECT_EQ(exemptedType, ResourceType::CPU);
133 }
134 } // namespace BackgroundTaskMgr
135 } // namespace OHOS
136