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 "standby_service_impl.h"
24 #ifdef ENABLE_BACKGROUND_TASK_MGR
25 #include "resource_type.h"
26 #endif
27 #include "system_ability_definition.h"
28
29 #include "mock_bundle_manager_helper.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 DevStandbyMgr {
38 static constexpr int32_t NETWORK_INDEX = 101;
39 static constexpr int32_t TIMER_INDEX = 103;
40 static constexpr int32_t EXEMPT_ALL_RESOURCES = 100;
41
42 class MockStandbyServiceUnitTest : public testing::Test {
43 public:
SetUpTestCase()44 static void SetUpTestCase() {}
45 static void TearDownTestCase();
46 void SetUp();
47 void TearDown();
48
49 static std::shared_ptr<MockBundleManagerHelper> bundleManagerHelperMock_;
50 };
51
52 std::shared_ptr<MockBundleManagerHelper> MockStandbyServiceUnitTest::bundleManagerHelperMock_ = nullptr;
53
TearDownTestCase()54 void MockStandbyServiceUnitTest::TearDownTestCase()
55 {
56 bundleManagerHelperMock_.reset();
57 }
58
SetUp()59 void MockStandbyServiceUnitTest::SetUp()
60 {
61 bundleManagerHelperMock_ = std::make_shared<MockBundleManagerHelper>();
62 SetBundleManagerHelper(bundleManagerHelperMock_);
63 }
64
TearDown()65 void MockStandbyServiceUnitTest::TearDown()
66 {
67 CleanBundleManagerHelper();
68 }
69
70 /**
71 * @tc.name: GetExemptedResourceType_001
72 * @tc.desc: should return 0 when failed to get application info
73 * @tc.type: FUNC
74 */
75 HWTEST_F(MockStandbyServiceUnitTest, GetExemptedResourceType_001, TestSize.Level1)
76 {
77 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _)).WillOnce(Return(false));
78 auto exemptedType = StandbyServiceImpl::GetInstance()->GetExemptedResourceType(MAX_ALLOW_TYPE_NUMBER);
79 EXPECT_EQ(exemptedType, 0u);
80 }
81
82 /**
83 * @tc.name: GetExemptedResourceType_002
84 * @tc.desc: should return 0 when resource type is inconsistence with configuration
85 * @tc.type: FUNC
86 */
87 HWTEST_F(MockStandbyServiceUnitTest, GetExemptedResourceType_002, TestSize.Level1)
88 {
89 AppExecFwk::ApplicationInfo info {};
90 info.resourcesApply = { TIMER_INDEX };
91 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _))
92 .WillOnce(DoAll(SetArgReferee<3>(info), Return(true)));
93 auto exemptedType = StandbyServiceImpl::GetInstance()->GetExemptedResourceType(AllowType::NETWORK);
94 EXPECT_EQ(exemptedType, 0u);
95 }
96
97 /**
98 * @tc.name: GetExemptedResourceType_003
99 * @tc.desc: should return origin number when resource list contain 0
100 * @tc.type: FUNC
101 */
102 HWTEST_F(MockStandbyServiceUnitTest, GetExemptedResourceType_003, TestSize.Level1)
103 {
104 AppExecFwk::ApplicationInfo info {};
105 info.resourcesApply = { EXEMPT_ALL_RESOURCES };
106 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _))
107 .WillOnce(DoAll(SetArgReferee<3>(info), Return(true)));
108 auto exemptedType = StandbyServiceImpl::GetInstance()->GetExemptedResourceType(MAX_ALLOW_TYPE_NUMBER);
109 EXPECT_EQ(exemptedType, MAX_ALLOW_TYPE_NUMBER);
110 }
111
112 /**
113 * @tc.name: GetExemptedResourceType_004
114 * @tc.desc: should return CPU when resource list contain CPU_INDEX
115 * @tc.type: FUNC
116 */
117 HWTEST_F(MockStandbyServiceUnitTest, GetExemptedResourceType_004, TestSize.Level1)
118 {
119 AppExecFwk::ApplicationInfo info {};
120 info.resourcesApply = { NETWORK_INDEX };
121 EXPECT_CALL(*bundleManagerHelperMock_, GetApplicationInfo(_, _, _, _))
122 .WillOnce(DoAll(SetArgReferee<3>(info), Return(true)));
123 auto exemptedType = StandbyServiceImpl::GetInstance()->GetExemptedResourceType(MAX_ALLOW_TYPE_NUMBER);
124 EXPECT_EQ(exemptedType, AllowType::NETWORK);
125 }
126 } // namespace DevStandbyMgr
127 } // namespace OHOS
128