• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <limits>
18 #include <test_header.h>
19 
20 #include "hgm_app_page_url_strategy.h"
21 #include "hgm_frame_rate_manager.h"
22 #include "hgm_test_base.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
30 constexpr int32_t DEF_PID = 1000;
31 constexpr int32_t MIN_FPS = 60;
32 constexpr int32_t MAX_FPS = 90;
33 const std::string STRATEGY = "5";
34 const std::string PKG_NAME = "com.pkg.other";
35 const std::string PAGE_NAME = "other";
36 const std::string PAGE_NAME1 = "other1";
37 }
38 
39 class HgmAppPageUrlStrategyTest : public HgmTestBase {
40 public:
SetUpTestCase()41     static void SetUpTestCase()
42     {
43         HgmTestBase::SetUpTestCase();
44     }
TearDownTestCase()45     static void TearDownTestCase() {}
46     void SetUp();
TearDown()47     void TearDown() {}
48     HgmErrCode GetPageUrlVoteInfo(uint32_t pid);
49 
50     std::shared_ptr<HgmAppPageUrlStrategy> appPageUrlStrategy_;
51     PolicyConfigData::ScreenSetting screenSetting_;
52 };
53 
SetUp()54 void HgmAppPageUrlStrategyTest::SetUp()
55 {
56     appPageUrlStrategy_ = std::make_shared<HgmAppPageUrlStrategy>();
57     PolicyConfigData::PageUrlConfig pageUrlConfig;
58     pageUrlConfig[PAGE_NAME] = STRATEGY;
59     screenSetting_.pageUrlConfig[PKG_NAME] = pageUrlConfig;
60 }
61 
GetPageUrlVoteInfo(uint32_t pid)62 HgmErrCode HgmAppPageUrlStrategyTest::GetPageUrlVoteInfo(uint32_t pid)
63 {
64     for (auto& pageUrlVoter : appPageUrlStrategy_->pageUrlVoterInfo_) {
65         auto voterPid = pageUrlVoter.first;
66         auto voterInfo = pageUrlVoter.second;
67         if (voterPid == pid && voterInfo.hasVoter) {
68             return EXEC_SUCCESS;
69         }
70     }
71     return HGM_ERROR;
72 }
73 
74 /**
75  * @tc.name: NotifyPageName001
76  * @tc.desc: Verify the result of NotifyPageName001 function
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(HgmAppPageUrlStrategyTest, NotifyPageName001, Function | SmallTest | Level1)
81 {
82     PolicyConfigData::ScreenSetting screenSetting;
83     appPageUrlStrategy_->SetPageUrlConfig(screenSetting.pageUrlConfig);
84     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, true);
85     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
86     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, false);
87     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
88 }
89 
90 /**
91  * @tc.name: NotifyPageName002
92  * @tc.desc: Verify the result of NotifyPageName002 function
93  * @tc.type: FUNC
94  * @tc.require:
95  */
96 HWTEST_F(HgmAppPageUrlStrategyTest, NotifyPageName002, Function | SmallTest | Level0)
97 {
98     appPageUrlStrategy_->SetPageUrlConfig(screenSetting_.pageUrlConfig);
99     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, true);
100     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), EXEC_SUCCESS);
101     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, false);
102     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
103 }
104 
105 /**
106  * @tc.name: NotifyScreenSettingChange001
107  * @tc.desc: Verify the result of NotifyScreenSettingChange001 function
108  * @tc.type: FUNC
109  * @tc.require:
110  */
111 HWTEST_F(HgmAppPageUrlStrategyTest, NotifyScreenSettingChange001, Function | SmallTest | Level0)
112 {
113     PolicyConfigData::ScreenSetting screenSetting;
114 
115     appPageUrlStrategy_->SetPageUrlConfig(screenSetting.pageUrlConfig);
116     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, true);
117     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
118 
119     appPageUrlStrategy_->SetPageUrlConfig(screenSetting_.pageUrlConfig);
120     appPageUrlStrategy_->NotifyScreenSettingChange();
121     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), EXEC_SUCCESS);
122     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, false);
123     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
124 }
125 
126 /**
127  * @tc.name: NotifyScreenSettingChange002
128  * @tc.desc: Verify the result of NotifyScreenSettingChange002 function
129  * @tc.type: FUNC
130  * @tc.require:
131  */
132 HWTEST_F(HgmAppPageUrlStrategyTest, NotifyScreenSettingChange002, Function | SmallTest | Level0)
133 {
134     pid_t pid = 1;
135     PolicyConfigData::ScreenSetting screenSetting;
136 
137     appPageUrlStrategy_->SetPageUrlConfig(screenSetting_.pageUrlConfig);
138     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, true);
139     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), EXEC_SUCCESS);
140 
141     appPageUrlStrategy_->SetPageUrlConfig(screenSetting.pageUrlConfig);
142     appPageUrlStrategy_->NotifyScreenSettingChange();
143     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
144     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, false);
145     ASSERT_EQ(GetPageUrlVoteInfo(DEF_PID), HGM_ERROR);
146     appPageUrlStrategy_->CleanPageUrlVote(pid);
147     HgmAppPageUrlStrategy::VoterInfo voterInfo = {false, PKG_NAME, PAGE_NAME};
148     appPageUrlStrategy_->pageUrlVoterInfo_.try_emplace(0, voterInfo);
149     std::unordered_map<std::string, std::string> pageUrlConfig;
150     pageUrlConfig.try_emplace(PAGE_NAME1, PAGE_NAME1);
151     appPageUrlStrategy_->pageUrlConfig_.try_emplace(PKG_NAME, pageUrlConfig);
152     ASSERT_LE(appPageUrlStrategy_->pageUrlConfig_[PKG_NAME].count(PAGE_NAME), 0);
153     appPageUrlStrategy_->NotifyScreenSettingChange();
154     appPageUrlStrategy_->NotifyPageName(DEF_PID, PKG_NAME, PAGE_NAME, false);
155     ASSERT_LE(appPageUrlStrategy_->pageUrlConfig_[PKG_NAME].count(PAGE_NAME), 0);
156 }
157 } // namespace Rosen
158 } // namespace OHOS