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