1 /*
2  * Copyright (c) 2024 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 
18 #include "ability_info.h"
19 #include "hilog_tag_wrapper.h"
20 #define private public
21 #include "res_sched_util.h"
22 #undef private
23 #include "parameters.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
30 using AbilityInfo = AppExecFwk::AbilityInfo;
31 class ResSchedUtilTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp() override;
36     void TearDown() override;
37 };
38 
SetUpTestCase(void)39 void ResSchedUtilTest::SetUpTestCase(void)
40 {}
41 
TearDownTestCase(void)42 void ResSchedUtilTest::TearDownTestCase(void)
43 {}
44 
SetUp()45 void ResSchedUtilTest::SetUp()
46 {}
47 
TearDown()48 void ResSchedUtilTest::TearDown()
49 {}
50 
51 /**
52  * @tc.number: ResSchedUtilTest_0100
53  * @tc.desc: Test ReportAbilityStartInfoToRSS works
54  * @tc.type: FUNC
55  */
56 HWTEST_F(ResSchedUtilTest, ResSchedUtilTest_0100, TestSize.Level0)
57 {
58     TAG_LOGI(AAFwkTag::TEST, "ResSchedUtilTest_0100 called.");
59     AbilityInfo abilityInfo;
60     int64_t resSchedType = -1;
61     AAFwk::ResSchedUtil::GetInstance().ReportAbilityStartInfoToRSS(abilityInfo, -1, false);
62     AAFwk::ResSchedUtil::GetInstance().ReportAbilityAssociatedStartInfoToRSS(abilityInfo, resSchedType, 0, 0);
63     int64_t ret = AAFwk::ResSchedUtil::GetInstance().convertType(resSchedType);
64     EXPECT_EQ(resSchedType, ret);
65 }
66 
67 /**
68  * @tc.number: ResSchedUtilTest_0200
69  * @tc.desc: Test ReportAbilityStartInfoToRSS works
70  * @tc.type: FUNC
71  */
72 HWTEST_F(ResSchedUtilTest, ResSchedUtilTest_0200, TestSize.Level0)
73 {
74     TAG_LOGI(AAFwkTag::TEST, "ResSchedUtilTest_0200 called.");
75     std::string testName = "ResSchedUtilTest";
76     AAFwk::ResSchedUtil::GetInstance().ReportEventToRSS(0, testName, testName);
77     std::unordered_set<int32_t> frozenPids;
78     AAFwk::ResSchedUtil::GetInstance().GetAllFrozenPidsFromRSS(frozenPids);
79     int64_t resSchedType = AAFwk::RES_TYPE_SCB_START_ABILITY;
80     int64_t ret = AAFwk::ResSchedUtil::GetInstance().convertType(resSchedType);
81     EXPECT_EQ(resSchedType, ret);
82 }
83 
84 /**
85  * @tc.number: ResSchedUtilTest_0300
86  * @tc.desc: Test ReportLoadingEventToRss works
87  * @tc.type: FUNC
88  */
89 HWTEST_F(ResSchedUtilTest, ResSchedUtilTest_0300, TestSize.Level0)
90 {
91     TAG_LOGI(AAFwkTag::TEST, "ResSchedUtilTest_0300 called.");
92     AAFwk::ResSchedUtil::GetInstance().ReportLoadingEventToRss(AAFwk::LoadingStage::LOAD_BEGIN,
93         0, 0, 0);
94     int64_t resSchedType = AAFwk::RES_TYPE_SCB_START_ABILITY;
95     int64_t ret = AAFwk::ResSchedUtil::GetInstance().convertType(resSchedType);
96     EXPECT_EQ(resSchedType, ret);
97 }
98 
99 /**
100  * @tc.number: GetThawReasonByAbilityType
101  * @tc.desc: Test GetThawReasonByAbilityType
102  * @tc.type: FUNC
103  */
104 HWTEST_F(ResSchedUtilTest, GetThawReasonByAbilityType, TestSize.Level0)
105 {
106     TAG_LOGI(AAFwkTag::TEST, "GetThawReasonByAbilityType called.");
107     AbilityInfo abilityInfo;
108     std::string reason = AAFwk::ResSchedUtil::GetInstance().GetThawReasonByAbilityType(abilityInfo);
109     EXPECT_EQ(reason, "THAW_BY_START_NOT_PAGE_ABILITY");
110 
111     abilityInfo.type = AppExecFwk::AbilityType::PAGE;
112     reason = AAFwk::ResSchedUtil::GetInstance().GetThawReasonByAbilityType(abilityInfo);
113     EXPECT_EQ(reason, "THAW_BY_START_PAGE_ABILITY");
114 
115     abilityInfo.type = AppExecFwk::AbilityType::EXTENSION;
116     abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::SERVICE;
117     reason = AAFwk::ResSchedUtil::GetInstance().GetThawReasonByAbilityType(abilityInfo);
118     EXPECT_EQ(reason, "THAW_BY_START_SERVICE_EXTENSION");
119 
120     abilityInfo.type = AppExecFwk::AbilityType::EXTENSION;
121     abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI;
122     reason = AAFwk::ResSchedUtil::GetInstance().GetThawReasonByAbilityType(abilityInfo);
123     EXPECT_EQ(reason, "THAW_BY_START_UI_EXTENSION");
124 }
125 
126 }  // namespace AbilityRuntime
127 }  // namespace OHOS
128