• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 
16 #include <fcntl.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <string>
20 
21 #define private public
22 #include "parser/common_types.h"
23 #include "pbreader_xpower_parser.h"
24 #include "trace_streamer_selector.h"
25 #include "xpower_plugin_result.pb.h"
26 
27 using namespace testing::ext;
28 using namespace SysTuning::TraceStreamer;
29 
30 namespace SysTuning {
31 namespace TraceStreamer {
32 namespace PbReaderXPowerDataParserUnitTest {
33 const int64_t START_TS1 = 1;
34 const int64_t END_TS1 = 3;
35 const int64_t BATTERY_CAPACITY = 0;
36 const int64_t BATTERY_CHARGE = 1;
37 const int64_t BATTERY_GAS_GAUGE = 2;
38 const int64_t BATTERY_LEVEL = 3;
39 const int64_t BATTERY_SCREEN = 4;
40 const int64_t BATTERY_REAL_CURRENT1 = 5;
41 const int32_t BATTERY_REAL_CURRENT2 = 6;
42 const std::string BUNDLE_NAME = "test";
43 const int64_t SHELL_TEMP = 0;
44 const int64_t THERMAL_LEVEL = 1;
45 const int64_t AUDIO_ENERGY = 2;
46 const int64_t WIFI_ENERGY = 1;
47 const int64_t THREAD_LOAD = 2;
48 const std::string THREAD_NAME = "thread";
49 const int64_t THREAD_TIME = 1;
50 const int64_t THREAD_ENERGY = 2;
51 const int64_t GPU_FREQUENCY = 100;
52 const int64_t GPU_IDLE_TIME = 101;
53 const int64_t GPU_RUN_TIME = 102;
54 const int64_t WIFI_TX_PACKETS = 200;
55 const int64_t WIFI_RX_PACKETS = 201;
56 const int64_t WIFI_TX_BYTES = 202;
57 const int64_t WIFI_RX_BYTES = 203;
58 const int64_t DISPLAY_COUNT_1HZ = 301;
59 const int64_t DISPLAY_COUNT_5HZ = 302;
60 const int64_t DISPLAY_COUNT_10HZ = 303;
61 const int64_t DISPLAY_COUNT_15HZ = 304;
62 const int64_t DISPLAY_COUNT_24HZ = 305;
63 const int64_t DISPLAY_COUNT_30HZ = 306;
64 const int64_t DISPLAY_COUNT_45HZ = 307;
65 const int64_t DISPLAY_COUNT_60HZ = 308;
66 const int64_t DISPLAY_COUNT_90HZ = 309;
67 const int64_t DISPLAY_COUNT_120HZ = 310;
68 const int64_t DISPLAY_COUNT_180HZ = 311;
69 const uint64_t START_TIME = 585717485310726;
70 const uint64_t END_TIME = 585717485310900;
71 const int64_t BACKGROUN_DDURATION = 9000;
72 const int64_t BACKGROUND_ENERGY = 57;
73 const int64_t FOREGROUND_DURATION = 11109;
74 const int64_t FOREGROUND_ENERGY = 2884;
75 const int64_t SCREEN_OFF_DURATION = 2884;
76 const int64_t SCREEN_OFF_ENERGY = 2884;
77 const int64_t SCREEN_ON_DURATION = 2884;
78 const int64_t SCREEN_ON_ENERGY = 2884;
79 const int64_t UID = 2001;
80 const int64_t LOAD = 112;
81 
82 class PbReaderXPowerParserTest : public ::testing::Test {
83 public:
SetUp()84     void SetUp()
85     {
86         stream_.InitFilter();
87         optimizeReport_ = std::make_unique<OptimizeReport>();
88         optimizeReport_->set_start_time(START_TS1);
89         optimizeReport_->set_end_time(END_TS1);
90         optimizeReport_->set_bundle_name(BUNDLE_NAME.c_str());
91         dataSeg_ = std::make_unique<PbreaderDataSegment>();
92         dataSeg_->dataType = DATA_SOURCE_TYPE_XPOWER;
93         dataSeg_->clockId = TS_CLOCK_BOOTTIME;
94         dataSeg_->status = TS_PARSE_STATUS_PARSED;
95         dataSeg_->timeStamp = START_TS1;
96         xPowerParser_ =
97             std::make_unique<PbreaderXpowerParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
98     }
InitPbReaderAndParse()99     void InitPbReaderAndParse()
100     {
101         std::string optimizeReportStrMsg = "";
102         optimizeReport_->SerializeToString(&optimizeReportStrMsg);
103         ProtoReader::BytesView optimizeReportBytesView(reinterpret_cast<const uint8_t *>(optimizeReportStrMsg.data()),
104                                                        optimizeReportStrMsg.size());
105         if (dataSeg_ != nullptr && xPowerParser_ != nullptr) {
106             dataSeg_->protoData = optimizeReportBytesView;
107             xPowerParser_->Parse(*dataSeg_.get(), dataSeg_->timeStamp, dataSeg_->clockId);
108         }
109     }
TearDown()110     void TearDown()
111     {
112         optimizeReport_.reset();
113         dataSeg_.reset();
114         xPowerParser_.reset();
115     }
116 
117 public:
118     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
119     std::unique_ptr<OptimizeReport> optimizeReport_ = nullptr;
120     std::unique_ptr<PbreaderDataSegment> dataSeg_ = nullptr;
121     std::unique_ptr<PbreaderXpowerParser> xPowerParser_ = nullptr;
122 };
123 
124 /**
125  * @tc.name: ParseRealBatteryTest
126  * @tc.desc: test RealBattery message parse
127  * @tc.type: FUNC
128  */
129 HWTEST_F(PbReaderXPowerParserTest, ParseRealBatteryTest, TestSize.Level1)
130 {
131     TS_LOGI("test47-1");
132     RealBattery *realBattery = new RealBattery();
133     realBattery->set_capacity(BATTERY_CAPACITY);
134     realBattery->set_charge(BATTERY_CHARGE);
135     realBattery->set_gas_gauge(BATTERY_GAS_GAUGE);
136     realBattery->set_level(BATTERY_LEVEL);
137     realBattery->set_screen(BATTERY_SCREEN);
138     auto realCurrent = realBattery->mutable_real_current();
139     realCurrent->Add(BATTERY_REAL_CURRENT1);
140     realCurrent->Add(BATTERY_REAL_CURRENT2);
141     optimizeReport_->set_allocated_real_battery(realBattery);
142     InitPbReaderAndParse();
143     EXPECT_STREQ(stream_.traceDataCache_->GetConstTraceConfigData().Value()[0].c_str(), BUNDLE_NAME.c_str());
144     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().TimeStampData()[0], START_TS1 * MSEC_TO_NS);
145     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_CAPACITY], BATTERY_CAPACITY);
146     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_CHARGE], BATTERY_CHARGE);
147     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_GAS_GAUGE], BATTERY_GAS_GAUGE);
148     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_LEVEL], BATTERY_LEVEL);
149     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_SCREEN], BATTERY_SCREEN);
150     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_REAL_CURRENT1],
151               BATTERY_REAL_CURRENT1);
152     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[BATTERY_REAL_CURRENT2],
153               BATTERY_REAL_CURRENT2);
154 }
155 
156 /**
157  * @tc.name: ParseThermalReportTest
158  * @tc.desc: test ThermalReport message parse
159  * @tc.type: FUNC
160  */
161 HWTEST_F(PbReaderXPowerParserTest, ParseThermalReportTest, TestSize.Level1)
162 {
163     TS_LOGI("test47-2");
164     ThermalReport *thermalReport = new ThermalReport();
165     thermalReport->set_shell_temp(SHELL_TEMP);
166     thermalReport->set_thermal_level(THERMAL_LEVEL);
167     optimizeReport_->set_allocated_thermal_report(thermalReport);
168     InitPbReaderAndParse();
169     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[SHELL_TEMP], SHELL_TEMP);
170     EXPECT_EQ(stream_.traceDataCache_->GetConstXpowerMeasureData().ValuesData()[THERMAL_LEVEL], THERMAL_LEVEL);
171 }
172 
173 /**
174  * @tc.name: ParseAppStatisticTest
175  * @tc.desc: test AppStatistic message parse
176  * @tc.type: FUNC
177  */
178 HWTEST_F(PbReaderXPowerParserTest, ParseAppStatisticTest, TestSize.Level1)
179 {
180     TS_LOGI("test47-3");
181     AppStatisticCommon *audio = new AppStatisticCommon();
182     audio->set_energy(AUDIO_ENERGY);
183     audio->set_time(START_TS1);
184     AppStatisticCommon *wifi = new AppStatisticCommon();
185     wifi->set_energy(WIFI_ENERGY);
186     wifi->set_time(END_TS1);
187 
188     AppStatistic *appStatistic = new AppStatistic();
189     appStatistic->set_allocated_audio(audio);
190     appStatistic->set_allocated_wifi(wifi);
191 
192     optimizeReport_->set_allocated_app_statistic(appStatistic);
193     InitPbReaderAndParse();
194     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppStatisticInfo().EnergysData()[0], AUDIO_ENERGY);
195     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppStatisticInfo().EnergysData()[WIFI_ENERGY], WIFI_ENERGY);
196 }
197 
198 /**
199  * @tc.name: ParseAppDetailCPUTest
200  * @tc.desc: test AppDetailCPU message parse
201  * @tc.type: FUNC
202  */
203 HWTEST_F(PbReaderXPowerParserTest, ParseAppDetailCPUTest, TestSize.Level1)
204 {
205     TS_LOGI("test47-4");
206     AppDetailCPU *cpuDetail = new AppDetailCPU();
207     cpuDetail->add_thread_load(THREAD_LOAD);
208     cpuDetail->add_thread_name(THREAD_NAME.c_str());
209     cpuDetail->add_thread_time(THREAD_TIME);
210     cpuDetail->add_thread_energy(THREAD_ENERGY);
211 
212     AppDetail *appDetail = new AppDetail();
213     appDetail->set_allocated_cpu(cpuDetail);
214 
215     optimizeReport_->set_allocated_app_detail(appDetail);
216     InitPbReaderAndParse();
217     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailCPUInfo().ThreadNamesData()[0],
218               stream_.traceDataCache_->GetDataIndex(THREAD_NAME.c_str()));
219     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailCPUInfo().ThreadTimesData()[0], THREAD_TIME);
220     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailCPUInfo().ThreadLoadsData()[0], THREAD_LOAD);
221     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailCPUInfo().ThreadEnergysData()[0], THREAD_ENERGY);
222 }
223 
224 /**
225  * @tc.name: ParseAppDetailGPUTest
226  * @tc.desc: test AppDetailGPU message parse
227  * @tc.type: FUNC
228  */
229 HWTEST_F(PbReaderXPowerParserTest, ParseAppDetailGPUTest, TestSize.Level1)
230 {
231     TS_LOGI("test47-5");
232     AppDetailGPU *gpuDetail = new AppDetailGPU();
233     gpuDetail->add_frequency(GPU_FREQUENCY);
234     gpuDetail->add_idle_time(GPU_IDLE_TIME);
235     gpuDetail->add_run_time(GPU_RUN_TIME);
236 
237     AppDetail *appDetail = new AppDetail();
238     appDetail->set_allocated_gpu(gpuDetail);
239 
240     optimizeReport_->set_allocated_app_detail(appDetail);
241     InitPbReaderAndParse();
242     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailGPUInfo().FrequencysData()[0], GPU_FREQUENCY);
243     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailGPUInfo().IdleTimesData()[0], GPU_IDLE_TIME);
244     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailGPUInfo().RuntimesData()[0], GPU_RUN_TIME);
245 }
246 
247 /**
248  * @tc.name: ParseAppDetailWifiTest
249  * @tc.desc: test AppDetailWifi message parse
250  * @tc.type: FUNC
251  */
252 HWTEST_F(PbReaderXPowerParserTest, ParseAppDetailWifiTest, TestSize.Level1)
253 {
254     TS_LOGI("test47-6");
255     AppDetailWifi *wifiDetail = new AppDetailWifi();
256     wifiDetail->set_tx_packets(WIFI_TX_PACKETS);
257     wifiDetail->set_rx_packets(WIFI_RX_PACKETS);
258     wifiDetail->set_tx_bytes(WIFI_TX_BYTES);
259     wifiDetail->set_rx_bytes(WIFI_RX_BYTES);
260 
261     AppDetail *appDetail = new AppDetail();
262     appDetail->set_allocated_wifi(wifiDetail);
263 
264     optimizeReport_->set_allocated_app_detail(appDetail);
265     InitPbReaderAndParse();
266     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailWifiInfo().TxPacketsData()[0], WIFI_TX_PACKETS);
267     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailWifiInfo().RxPacketsData()[0], WIFI_RX_PACKETS);
268     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailWifiInfo().TxBytesData()[0], WIFI_TX_BYTES);
269     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailWifiInfo().RxBytesData()[0], WIFI_RX_BYTES);
270 }
271 
272 /**
273  * @tc.name: ParseAppDetailDisplayTest
274  * @tc.desc: test AppDetailDisplay message parse
275  * @tc.type: FUNC
276  */
277 HWTEST_F(PbReaderXPowerParserTest, ParseAppDetailDisplayTest, TestSize.Level1)
278 {
279     TS_LOGI("test47-7");
280     AppDetailDisplay *displayDetail = new AppDetailDisplay();
281     displayDetail->set_count_1hz(DISPLAY_COUNT_1HZ);
282     displayDetail->set_count_5hz(DISPLAY_COUNT_5HZ);
283     displayDetail->set_count_10hz(DISPLAY_COUNT_10HZ);
284     displayDetail->set_count_15hz(DISPLAY_COUNT_15HZ);
285     displayDetail->set_count_24hz(DISPLAY_COUNT_24HZ);
286     displayDetail->set_count_30hz(DISPLAY_COUNT_30HZ);
287     displayDetail->set_count_45hz(DISPLAY_COUNT_45HZ);
288     displayDetail->set_count_60hz(DISPLAY_COUNT_60HZ);
289     displayDetail->set_count_90hz(DISPLAY_COUNT_90HZ);
290     displayDetail->set_count_120hz(DISPLAY_COUNT_120HZ);
291     displayDetail->set_count_180hz(DISPLAY_COUNT_180HZ);
292 
293     AppDetail *appDetail = new AppDetail();
294     appDetail->set_allocated_display(displayDetail);
295 
296     optimizeReport_->set_allocated_app_detail(appDetail);
297     InitPbReaderAndParse();
298     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count1HertzsData()[0], DISPLAY_COUNT_1HZ);
299     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count5HertzsData()[0], DISPLAY_COUNT_5HZ);
300     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count10HertzsData()[0], DISPLAY_COUNT_10HZ);
301     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count15HertzsData()[0], DISPLAY_COUNT_15HZ);
302     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count24HertzsData()[0], DISPLAY_COUNT_24HZ);
303     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count30HertzsData()[0], DISPLAY_COUNT_30HZ);
304     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count45HertzsData()[0], DISPLAY_COUNT_45HZ);
305     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count60HertzsData()[0], DISPLAY_COUNT_60HZ);
306     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count90HertzsData()[0], DISPLAY_COUNT_90HZ);
307     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count120HertzsData()[0],
308               DISPLAY_COUNT_120HZ);
309     EXPECT_EQ(stream_.traceDataCache_->GetConstXPowerAppDetailDisplayInfo().Count180HertzsData()[0],
310               DISPLAY_COUNT_180HZ);
311 }
312 
FillComponentTopAudioData(OptimizeReport & report)313 void FillComponentTopAudioData(OptimizeReport &report)
314 {
315     report.mutable_component_top()->mutable_audio()->set_count(1);
316     report.mutable_component_top()->mutable_audio()->add_appname("audio_app");
317     report.mutable_component_top()->mutable_audio()->add_background_duration(BACKGROUN_DDURATION);
318     report.mutable_component_top()->mutable_audio()->add_background_energy(BACKGROUND_ENERGY);
319     report.mutable_component_top()->mutable_audio()->add_foreground_duration(FOREGROUND_DURATION);
320     report.mutable_component_top()->mutable_audio()->add_foreground_energy(FOREGROUND_ENERGY);
321     report.mutable_component_top()->mutable_audio()->add_screen_off_duration(SCREEN_OFF_DURATION);
322     report.mutable_component_top()->mutable_audio()->add_screen_off_energy(SCREEN_OFF_ENERGY);
323     report.mutable_component_top()->mutable_audio()->add_screen_on_duration(SCREEN_ON_ENERGY);
324     report.mutable_component_top()->mutable_audio()->add_screen_on_energy(SCREEN_ON_ENERGY);
325 }
326 
FillComponentTopBluetoothData(OptimizeReport & report)327 void FillComponentTopBluetoothData(OptimizeReport &report)
328 {
329     report.mutable_component_top()->mutable_bluetooth()->set_count(1);
330     report.mutable_component_top()->mutable_bluetooth()->add_appname("bluetooth_app");
331     report.mutable_component_top()->mutable_bluetooth()->add_background_duration(BACKGROUN_DDURATION);
332     report.mutable_component_top()->mutable_bluetooth()->add_background_energy(BACKGROUND_ENERGY);
333     report.mutable_component_top()->mutable_bluetooth()->add_foreground_duration(FOREGROUND_DURATION);
334     report.mutable_component_top()->mutable_bluetooth()->add_foreground_energy(FOREGROUND_ENERGY);
335     report.mutable_component_top()->mutable_bluetooth()->add_screen_off_duration(SCREEN_OFF_DURATION);
336     report.mutable_component_top()->mutable_bluetooth()->add_screen_off_energy(SCREEN_OFF_ENERGY);
337     report.mutable_component_top()->mutable_bluetooth()->add_screen_on_duration(SCREEN_ON_ENERGY);
338     report.mutable_component_top()->mutable_bluetooth()->add_screen_on_energy(SCREEN_ON_ENERGY);
339 }
340 
FillComponentTopFlashlightData(OptimizeReport & report)341 void FillComponentTopFlashlightData(OptimizeReport &report)
342 {
343     report.mutable_component_top()->mutable_flashlight()->set_count(1);
344     report.mutable_component_top()->mutable_flashlight()->add_appname("flashlight_app");
345     report.mutable_component_top()->mutable_flashlight()->add_background_duration(BACKGROUN_DDURATION);
346     report.mutable_component_top()->mutable_flashlight()->add_background_energy(BACKGROUND_ENERGY);
347     report.mutable_component_top()->mutable_flashlight()->add_foreground_duration(FOREGROUND_DURATION);
348     report.mutable_component_top()->mutable_flashlight()->add_foreground_energy(FOREGROUND_ENERGY);
349     report.mutable_component_top()->mutable_flashlight()->add_screen_off_duration(SCREEN_OFF_DURATION);
350     report.mutable_component_top()->mutable_flashlight()->add_screen_off_energy(SCREEN_OFF_ENERGY);
351     report.mutable_component_top()->mutable_flashlight()->add_screen_on_duration(SCREEN_ON_ENERGY);
352     report.mutable_component_top()->mutable_flashlight()->add_screen_on_energy(SCREEN_ON_ENERGY);
353 }
354 
FillComponentTopLocationData(OptimizeReport & report)355 void FillComponentTopLocationData(OptimizeReport &report)
356 {
357     report.mutable_component_top()->mutable_location()->set_count(1);
358     report.mutable_component_top()->mutable_location()->add_appname("location_app");
359     report.mutable_component_top()->mutable_location()->add_background_duration(BACKGROUN_DDURATION);
360     report.mutable_component_top()->mutable_location()->add_background_energy(BACKGROUND_ENERGY);
361     report.mutable_component_top()->mutable_location()->add_foreground_duration(FOREGROUND_DURATION);
362     report.mutable_component_top()->mutable_location()->add_foreground_energy(FOREGROUND_ENERGY);
363     report.mutable_component_top()->mutable_location()->add_screen_off_duration(SCREEN_OFF_DURATION);
364     report.mutable_component_top()->mutable_location()->add_screen_off_energy(SCREEN_OFF_ENERGY);
365     report.mutable_component_top()->mutable_location()->add_screen_on_duration(SCREEN_ON_ENERGY);
366     report.mutable_component_top()->mutable_location()->add_screen_on_energy(SCREEN_ON_ENERGY);
367 }
368 
FillComponentTopWifiscanData(OptimizeReport & report)369 void FillComponentTopWifiscanData(OptimizeReport &report)
370 {
371     report.mutable_component_top()->mutable_wifiscan()->set_count(1);
372     report.mutable_component_top()->mutable_wifiscan()->add_appname("wifiscan_app");
373     report.mutable_component_top()->mutable_wifiscan()->add_background_duration(BACKGROUN_DDURATION);
374     report.mutable_component_top()->mutable_wifiscan()->add_background_energy(BACKGROUND_ENERGY);
375     report.mutable_component_top()->mutable_wifiscan()->add_foreground_duration(FOREGROUND_DURATION);
376     report.mutable_component_top()->mutable_wifiscan()->add_foreground_energy(FOREGROUND_ENERGY);
377     report.mutable_component_top()->mutable_wifiscan()->add_screen_off_duration(SCREEN_OFF_DURATION);
378     report.mutable_component_top()->mutable_wifiscan()->add_screen_off_energy(SCREEN_OFF_ENERGY);
379     report.mutable_component_top()->mutable_wifiscan()->add_screen_on_duration(SCREEN_ON_ENERGY);
380     report.mutable_component_top()->mutable_wifiscan()->add_screen_on_energy(SCREEN_ON_ENERGY);
381 }
382 
FillComponentTopDisplayData(OptimizeReport & report)383 void FillComponentTopDisplayData(OptimizeReport &report)
384 {
385     report.mutable_component_top()->mutable_display()->set_count(1);
386     report.mutable_component_top()->mutable_display()->add_appname("display_app");
387     report.mutable_component_top()->mutable_display()->add_time(SCREEN_ON_ENERGY);
388     report.mutable_component_top()->mutable_display()->add_energy(SCREEN_ON_ENERGY);
389 }
390 
FillComponentTopGpuData(OptimizeReport & report)391 void FillComponentTopGpuData(OptimizeReport &report)
392 {
393     report.mutable_component_top()->mutable_gpu()->set_count(1);
394     report.mutable_component_top()->mutable_gpu()->add_appname("gpu_app");
395     report.mutable_component_top()->mutable_gpu()->add_time(SCREEN_ON_ENERGY);
396     report.mutable_component_top()->mutable_gpu()->add_energy(SCREEN_ON_ENERGY);
397 }
398 
FillComponentTopCameraData(OptimizeReport & report)399 void FillComponentTopCameraData(OptimizeReport &report)
400 {
401     report.mutable_component_top()->mutable_camera()->set_count(1);
402     report.mutable_component_top()->mutable_camera()->add_appname("camera_app");
403     report.mutable_component_top()->mutable_camera()->add_camera_id(0);
404     report.mutable_component_top()->mutable_camera()->add_background_duration(BACKGROUN_DDURATION);
405     report.mutable_component_top()->mutable_camera()->add_background_energy(BACKGROUND_ENERGY);
406     report.mutable_component_top()->mutable_camera()->add_foreground_duration(FOREGROUND_DURATION);
407     report.mutable_component_top()->mutable_camera()->add_foreground_energy(FOREGROUND_ENERGY);
408     report.mutable_component_top()->mutable_camera()->add_screen_off_duration(SCREEN_OFF_DURATION);
409     report.mutable_component_top()->mutable_camera()->add_screen_off_energy(SCREEN_OFF_ENERGY);
410     report.mutable_component_top()->mutable_camera()->add_screen_on_duration(SCREEN_ON_ENERGY);
411     report.mutable_component_top()->mutable_camera()->add_screen_on_energy(SCREEN_ON_ENERGY);
412 }
413 
FillComponentTopCpuData(OptimizeReport & report)414 void FillComponentTopCpuData(OptimizeReport &report)
415 {
416     report.mutable_component_top()->mutable_cpu()->set_count(1);
417     report.mutable_component_top()->mutable_cpu()->add_appname("cpu_app");
418     report.mutable_component_top()->mutable_cpu()->add_uid(UID);
419     report.mutable_component_top()->mutable_cpu()->add_background_duration(BACKGROUN_DDURATION);
420     report.mutable_component_top()->mutable_cpu()->add_background_energy(BACKGROUND_ENERGY);
421     report.mutable_component_top()->mutable_cpu()->add_foreground_duration(FOREGROUND_DURATION);
422     report.mutable_component_top()->mutable_cpu()->add_foreground_energy(FOREGROUND_ENERGY);
423     report.mutable_component_top()->mutable_cpu()->add_screen_off_duration(SCREEN_OFF_DURATION);
424     report.mutable_component_top()->mutable_cpu()->add_screen_off_energy(SCREEN_OFF_ENERGY);
425     report.mutable_component_top()->mutable_cpu()->add_screen_on_duration(SCREEN_ON_ENERGY);
426     report.mutable_component_top()->mutable_cpu()->add_screen_on_energy(SCREEN_ON_ENERGY);
427     report.mutable_component_top()->mutable_cpu()->add_load(LOAD);
428 }
429 
430 /**
431  * @tc.name: ParseXpowerParseComponentTopData
432  * @tc.desc: Parse Xpower Parse ComponentTop Data
433  * @tc.type: FUNC
434  */
435 HWTEST_F(PbReaderXPowerParserTest, ParseXpowerParseComponentTopData, TestSize.Level1)
436 {
437     TS_LOGI("test47-8");
438     OptimizeReport report;
439     FillComponentTopAudioData(report);
440     FillComponentTopBluetoothData(report);
441     FillComponentTopFlashlightData(report);
442     FillComponentTopLocationData(report);
443     FillComponentTopWifiscanData(report);
444     FillComponentTopDisplayData(report);
445     FillComponentTopGpuData(report);
446     FillComponentTopCameraData(report);
447     FillComponentTopCpuData(report);
448 
449     std::string reportData;
450     report.SerializeToString(&reportData);
451     PbreaderDataSegment seg;
452     seg.protoData.data_ = reinterpret_cast<const uint8_t *>(reportData.data());
453     seg.protoData.size_ = reportData.size();
454     PbreaderXpowerParser xpowerParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
455     xpowerParser.Parse(seg, START_TIME, TS_CLOCK_REALTIME);
456     EXPECT_TRUE(xpowerParser.timeSet_.empty());
457 
458     report.set_start_time(START_TIME);
459     report.SerializeToString(&reportData);
460     seg.protoData.data_ = reinterpret_cast<const uint8_t *>(reportData.data());
461     seg.protoData.size_ = reportData.size();
462     xpowerParser.Parse(seg, START_TIME, TS_CLOCK_REALTIME);
463     EXPECT_TRUE(xpowerParser.timeSet_.empty());
464 
465     report.set_end_time(END_TIME);
466     report.SerializeToString(&reportData);
467     seg.protoData.data_ = reinterpret_cast<const uint8_t *>(reportData.data());
468     seg.protoData.size_ = reportData.size();
469     xpowerParser.Parse(seg, START_TIME, TS_CLOCK_REALTIME);
470     EXPECT_FALSE(xpowerParser.timeSet_.empty());
471     XPowerComponentTop *cmpTopDataCache = xpowerParser.traceDataCache_->GetXPowerComponentTopInfo();
472     EXPECT_TRUE(
473         !cmpTopDataCache->ComponentTypesData().empty() && !cmpTopDataCache->AppnamesData().empty() &&
474         !cmpTopDataCache->BackgroundDurationsData().empty() && !cmpTopDataCache->BackgroundEnergysData().empty() &&
475         !cmpTopDataCache->ForegroundDurationsData().empty() && !cmpTopDataCache->ForegroundEnergysData().empty() &&
476         !cmpTopDataCache->ScreenOffDurationsData().empty() && !cmpTopDataCache->ScreenOffEnergysData().empty() &&
477         !cmpTopDataCache->ScreenOnDurationsData().empty() && !cmpTopDataCache->ScreenOnEnergysData().empty() &&
478         !cmpTopDataCache->CameraIdsData().empty() && !cmpTopDataCache->UidsData().empty() &&
479         !cmpTopDataCache->LoadsData().empty() && !cmpTopDataCache->AppUsageDurationsData().empty() &&
480         !cmpTopDataCache->AppUsageEnergysData().empty());
481     cmpTopDataCache->Clear();
482     cmpTopDataCache->ClearExportedData();
483 }
484 } // namespace PbReaderXPowerDataParserUnitTest
485 } // namespace TraceStreamer
486 } // namespace SysTuning
487