1#!/usr/bin/env python3 2# 3# Copyright 2018 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the 'License'); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an 'AS IS' BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16import os 17 18import acts_contrib.test_utils.power.PowerBaseTest as PBT 19import acts_contrib.test_utils.cellular.cellular_base_test as CBT 20from acts_contrib.test_utils.power import plot_utils 21 22 23class PowerCellularLabBaseTest(CBT.CellularBaseTest, PBT.PowerBaseTest): 24 """ Base class for Cellular power related tests. 25 26 Inherits from both PowerBaseTest and CellularBaseTest so it has methods to 27 collect power measurements and run a cellular simulation. 28 """ 29 def __init__(self, controllers): 30 """ Class initialization. 31 32 Sets class attributes to None. 33 """ 34 35 super().__init__(controllers) 36 self.power_results = {} 37 38 def setup_test(self): 39 """ Turn screen on before starting a test. """ 40 41 super().setup_test() 42 43 # Make the device go to sleep 44 self.dut.droid.goToSleepNow() 45 46 return True 47 48 def collect_power_data(self): 49 """ Collect power data using base class method and plot result 50 histogram. """ 51 52 samples = super().collect_power_data() 53 plot_title = '{}_{}_{}_histogram'.format( 54 self.test_name, self.dut.model, self.dut.build_info['build_id']) 55 plot_utils.monsoon_histogram_plot(samples, self.mon_info.data_path, 56 plot_title) 57 return samples 58 59 def teardown_test(self): 60 """ Executed after every test case, even if it failed or an exception 61 happened. 62 63 Save results to dictionary so they can be displayed after completing 64 the test batch. 65 """ 66 super().teardown_test() 67 68 self.power_results[self.test_name] = self.power_result.metric_value 69 70 def teardown_class(self): 71 """Clean up the test class after tests finish running. 72 73 Stops the simulation and disconnects from the Anritsu Callbox. Then 74 displays the test results. 75 76 """ 77 super().teardown_class() 78 79 # Log a summary of results 80 results_table_log = 'Results for cellular power tests:' 81 82 for test_name, value in self.power_results.items(): 83 results_table_log += '\n{}\t{}'.format(test_name, value) 84 85 # Save this summary to a csv file in the logs directory 86 self.save_summary_to_file() 87 88 self.log.info(results_table_log) 89 90 def save_summary_to_file(self): 91 """ Creates CSV format files with a summary of results. 92 93 This CSV files can be easily imported in a spreadsheet to analyze the 94 results obtained from the tests. 95 """ 96 97 # Save a csv file with the power measurements done in all the tests 98 99 path = os.path.join(self.log_path, self.RESULTS_SUMMARY_FILENAME) 100 101 with open(path, 'w') as csvfile: 102 csvfile.write('test,avg_power') 103 for test_name, value in self.power_results.items(): 104 csvfile.write('\n{},{}'.format(test_name, value)) 105 106 # Save a csv file with the calibration table for each simulation type 107 108 for sim_type in self.calibration_table: 109 110 path = os.path.join( 111 self.log_path, '{}_{}'.format(sim_type, 112 self.CALIBRATION_TABLE_FILENAME)) 113 114 with open(path, 'w') as csvfile: 115 csvfile.write('band,dl_pathloss, ul_pathloss') 116 for band, pathloss in self.calibration_table[sim_type].items(): 117 csvfile.write('\n{},{},{}'.format( 118 band, pathloss.get('dl', 'Error'), 119 pathloss.get('ul', 'Error'))) 120