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