1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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 #include <gtest/gtest.h> 16 #include <cstdio> 17 #include <algorithm> 18 #include <iostream> 19 #include <sstream> 20 #include <queue> 21 #include <vector> 22 #include <map> 23 #include <string> 24 #include <ctime> 25 #include <thread> 26 #include <unistd.h> 27 #include <sys/time.h> 28 #include "profiler_fps.h" 29 #include "sp_log.h" 30 #include "sp_utils.h" 31 #include "ByTrace.h" 32 #include "startup_delay.h" 33 #include "common.h" 34 35 using namespace testing::ext; 36 using namespace std; 37 38 namespace OHOS { 39 namespace SmartPerf { 40 class ProfilerFPSTest : public testing::Test { 41 public: SetUpTestCase()42 static void SetUpTestCase() {} TearDownTestCase()43 static void TearDownTestCase() {} 44 SetUp()45 void SetUp() {} TearDown()46 void TearDown() {} 47 }; 48 49 HWTEST_F(ProfilerFPSTest, ItemDataTest01, TestSize.Level1) 50 { 51 bool processFlag = true; 52 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 53 std::map<std::string, std::string> result = profilerFps.ItemData(); 54 if (processFlag) { 55 result["fps"] = "NA"; 56 result["fpsJitters"] = "NA"; 57 } 58 EXPECT_EQ(result["fps"], "NA"); 59 EXPECT_EQ(result["fpsJitters"], "NA"); 60 } 61 62 HWTEST_F(ProfilerFPSTest, ItemDataTest02, TestSize.Level1) 63 { 64 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 65 FpsInfoProfiler finalResult = profilerFps.GetFpsInfo(); 66 finalResult.fps = 120; 67 finalResult.jitters = {1, 2, 3}; 68 std::map<std::string, std::string> result = profilerFps.ItemData(); 69 result["fps"] = "120"; 70 result["fpsJitters"] = "1;;2;;3"; 71 EXPECT_EQ(result["fps"], "120"); 72 EXPECT_EQ(result["fpsJitters"], "1;;2;;3"); 73 } 74 75 HWTEST_F(ProfilerFPSTest, GetFpsInfoTest01, TestSize.Level1) 76 { 77 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 78 bool isGameApp = true; 79 bool firstDump = true; 80 std::string gameLayerName = ""; 81 FpsInfoProfiler fpsInfoTime; 82 if (isGameApp) { 83 if (firstDump) { 84 if (gameLayerName.empty()) { 85 fpsInfoTime.fps = 0; 86 fpsInfoTime.jitters = {}; 87 } 88 } 89 } 90 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 91 result.fps = 0; 92 result.jitters = {}; 93 EXPECT_EQ(result.fps, fpsInfoTime.fps); 94 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 95 } 96 97 HWTEST_F(ProfilerFPSTest, GetFpsInfoTest02, TestSize.Level1) 98 { 99 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 100 bool isGameApp = true; 101 bool firstDump = false; 102 std::string gameLayerName = "UnityPlayerSurface"; 103 FpsInfoProfiler fpsInfoTime; 104 FpsInfoProfiler lastFpsInfoResult; 105 FpsInfoProfiler tmpFps; 106 lastFpsInfoResult.curTime = 1234; 107 tmpFps.curTime = 1235; 108 if (isGameApp) { 109 if (!firstDump) { 110 if (!gameLayerName.empty()) { 111 OHOS::SmartPerf::SPUtils::GetCurrentTime(lastFpsInfoResult.curTime); 112 fpsInfoTime = profilerFps.GetSurfaceFrame(gameLayerName); 113 fpsInfoTime.fps = 60; 114 fpsInfoTime.jitters = {1, 2, 3}; 115 } 116 } 117 } 118 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 119 result.fps = 60; 120 result.jitters = {1, 2, 3}; 121 EXPECT_EQ(result.fps, fpsInfoTime.fps); 122 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 123 } 124 125 HWTEST_F(ProfilerFPSTest, GetFpsInfoTest03, TestSize.Level1) 126 { 127 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 128 bool isGameApp = false; 129 bool firstDump = false; 130 std::string gameLayerName = "UniRender"; 131 FpsInfoProfiler fpsInfoTime; 132 FpsInfoProfiler lastFpsInfoResult; 133 lastFpsInfoResult.curTime = 1234; 134 if (!isGameApp) { 135 if (!firstDump) { 136 if (!gameLayerName.empty()) { 137 OHOS::SmartPerf::SPUtils::GetCurrentTime(lastFpsInfoResult.curTime); 138 fpsInfoTime = profilerFps.GetSurfaceFrame(gameLayerName); 139 fpsInfoTime.fps = 90; 140 fpsInfoTime.jitters = {1, 2, 3}; 141 } 142 } 143 } 144 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 145 result.fps = 90; 146 result.jitters = {1, 2, 3}; 147 EXPECT_EQ(result.fps, fpsInfoTime.fps); 148 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 149 } 150 151 HWTEST_F(ProfilerFPSTest, GetFpsInfoTest04, TestSize.Level1) 152 { 153 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 154 bool isGameApp = false; 155 bool ohFlag = true; 156 std::string uniteLayer = "setting0"; 157 FpsInfoProfiler fpsInfoTime; 158 FpsInfoProfiler lastFpsInfoResult; 159 lastFpsInfoResult.curTime = 1234; 160 if (!isGameApp) { 161 if (ohFlag) { 162 OHOS::SmartPerf::SPUtils::GetCurrentTime(lastFpsInfoResult.curTime); 163 fpsInfoTime = profilerFps.GetSurfaceFrame(uniteLayer); 164 fpsInfoTime.fps = 60; 165 fpsInfoTime.jitters = {1, 2, 3}; 166 } 167 } 168 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 169 result.fps = 60; 170 result.jitters = {1, 2, 3}; 171 EXPECT_EQ(result.fps, fpsInfoTime.fps); 172 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 173 } 174 175 HWTEST_F(ProfilerFPSTest, GetFpsInfoTest05, TestSize.Level1) 176 { 177 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 178 bool isGameApp = false; 179 bool ohFlag = false; 180 std::string uniteLayer = "UniRender"; 181 FpsInfoProfiler fpsInfoTime; 182 FpsInfoProfiler lastFpsInfoResult; 183 lastFpsInfoResult.curTime = 1234; 184 if (!isGameApp) { 185 if (!ohFlag) { 186 OHOS::SmartPerf::SPUtils::GetCurrentTime(lastFpsInfoResult.curTime); 187 fpsInfoTime = profilerFps.GetSurfaceFrame(uniteLayer); 188 fpsInfoTime.fps = 30; 189 fpsInfoTime.jitters = {1, 2, 3}; 190 } 191 } 192 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 193 result.fps = 30; 194 result.jitters = {1, 2, 3}; 195 EXPECT_EQ(result.fps, fpsInfoTime.fps); 196 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 197 } 198 199 HWTEST_F(ProfilerFPSTest, GetFpsInfoTest06, TestSize.Level1) 200 { 201 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 202 bool isGameApp = false; 203 bool ohFlag = false; 204 std::string pkgName = "sceneboard"; 205 std::string uniteLayer = "UniRender"; 206 FpsInfoProfiler fpsInfoTime; 207 FpsInfoProfiler lastFpsInfoResult; 208 FpsInfoProfiler tmpFps; 209 lastFpsInfoResult.curTime = 1234; 210 tmpFps.curTime = 1235; 211 if (!isGameApp) { 212 if (!ohFlag) { 213 OHOS::SmartPerf::SPUtils::GetCurrentTime(lastFpsInfoResult.curTime); 214 fpsInfoTime = profilerFps.GetSurfaceFrame(uniteLayer); 215 fpsInfoTime.fps = 30; 216 fpsInfoTime.jitters = {1, 2, 3}; 217 } 218 } 219 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 220 result.fps = 30; 221 result.jitters = {1, 2, 3}; 222 EXPECT_EQ(result.fps, fpsInfoTime.fps); 223 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 224 } 225 226 HWTEST_F(ProfilerFPSTest, GetChangedLayerFpsTest01, TestSize.Level1) 227 { 228 std::string processId = "1234"; 229 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 230 FpsInfoProfiler fpsInfoTime; 231 std::string gameLayerName = profilerFps.GetGameLayer(); 232 gameLayerName = "UnityPlayerSurface"; 233 if (!gameLayerName.empty()) { 234 if (!processId.empty()) { 235 fpsInfoTime = profilerFps.GetSurfaceFrame(gameLayerName); 236 fpsInfoTime.fps = 60; 237 fpsInfoTime.jitters = {1, 2, 3}; 238 } 239 } 240 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 241 result.fps = 60; 242 result.jitters = {1, 2, 3}; 243 EXPECT_EQ(result.fps, fpsInfoTime.fps); 244 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 245 } 246 247 HWTEST_F(ProfilerFPSTest, GetChangedLayerFpsTest02, TestSize.Level1) 248 { 249 std::string processId = "1234"; 250 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 251 FpsInfoProfiler fpsInfoTime; 252 std::string gameLayerName = profilerFps.GetGameLayer(); 253 gameLayerName = ""; 254 if (gameLayerName.empty()) { 255 if (!processId.empty()) { 256 fpsInfoTime = profilerFps.GetSurfaceFrame(gameLayerName); 257 fpsInfoTime.fps = 0; 258 fpsInfoTime.jitters = {}; 259 } 260 } 261 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 262 result.fps = 0; 263 result.jitters = {}; 264 EXPECT_EQ(result.fps, fpsInfoTime.fps); 265 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 266 } 267 268 HWTEST_F(ProfilerFPSTest, GetChangedLayerFpsTest03, TestSize.Level1) 269 { 270 std::string processId = ""; 271 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 272 FpsInfoProfiler fpsInfoTime; 273 std::string gameLayerName = profilerFps.GetGameLayer(); 274 gameLayerName = ""; 275 if (gameLayerName.empty()) { 276 if (processId.empty()) { 277 fpsInfoTime = profilerFps.GetSurfaceFrame(gameLayerName); 278 fpsInfoTime.fps = 0; 279 fpsInfoTime.jitters = {}; 280 } 281 } 282 FpsInfoProfiler result = profilerFps.GetFpsInfo(); 283 result.fps = 0; 284 result.jitters = {}; 285 EXPECT_EQ(result.fps, fpsInfoTime.fps); 286 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 287 } 288 289 HWTEST_F(ProfilerFPSTest, GetAppFpsTest01, TestSize.Level1) 290 { 291 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 292 std::string pkgName = "settings"; 293 bool onTop = OHOS::SmartPerf::SPUtils::IsForeGround(pkgName); 294 onTop = false; 295 std::string uniteLayer = "UniRender"; 296 FpsInfoProfiler fpsInfoTime; 297 if (!onTop) { 298 fpsInfoTime.fps = 0; 299 fpsInfoTime.jitters = {}; 300 } 301 FpsInfoProfiler result = profilerFps.GetAppFps(uniteLayer); 302 result.fps = 0; 303 result.jitters = {}; 304 EXPECT_EQ(result.fps, fpsInfoTime.fps); 305 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 306 } 307 308 HWTEST_F(ProfilerFPSTest, GetAppFpsTest02, TestSize.Level1) 309 { 310 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 311 std::string pkgName = "settings"; 312 bool onTop = OHOS::SmartPerf::SPUtils::IsForeGround(pkgName); 313 onTop = true; 314 std::string uniteLayer = "UniRender"; 315 FpsInfoProfiler lastFpsInfoResult; 316 FpsInfoProfiler fpsInfoTime; 317 lastFpsInfoResult.curTime = 1234; 318 if (onTop) { 319 OHOS::SmartPerf::SPUtils::GetCurrentTime(lastFpsInfoResult.curTime); 320 fpsInfoTime = profilerFps.GetSurfaceFrame(uniteLayer); 321 fpsInfoTime.fps = 120; 322 fpsInfoTime.jitters = {1, 2, 3}; 323 } 324 FpsInfoProfiler result = profilerFps.GetAppFps(uniteLayer); 325 result.fps = 120; 326 result.jitters = {1, 2, 3}; 327 EXPECT_EQ(result.fps, fpsInfoTime.fps); 328 EXPECT_EQ(result.jitters, fpsInfoTime.jitters); 329 } 330 331 HWTEST_F(ProfilerFPSTest, GetSurfaceFrameTest01, TestSize.Level1) 332 { 333 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 334 FpsInfoProfiler result = profilerFps.GetSurfaceFrame(""); 335 EXPECT_EQ(result, FpsInfoProfiler()); 336 } 337 338 HWTEST_F(ProfilerFPSTest, GetSurfaceFrameTest02, TestSize.Level1) 339 { 340 ProfilerFPS &profilerFps = ProfilerFPS::GetInstance(); 341 FpsInfoProfiler result = profilerFps.GetSurfaceFrame("test"); 342 EXPECT_EQ(result, FpsInfoProfiler()); 343 } 344 } 345 }