1#!/usr/bin/env python3 2# 3# Copyright 2020 - 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. 16 17import unittest 18from acts.controllers import bits 19from acts.controllers import power_metrics 20 21 22class BitsTest(unittest.TestCase): 23 24 def test_metric_name_transformation_for_legacy_support(self): 25 avg_current = bits._transform_name('default_name.Monsoon.Monsoon:mA') 26 avg_power = bits._transform_name('default_name.Monsoon.Monsoon:mW') 27 28 self.assertEqual('avg_current', avg_current) 29 self.assertEqual('avg_power', avg_power) 30 31 def test_metric_name_transformation(self): 32 avg_current = bits._transform_name('default_name.slider.XYZ:mA') 33 avg_power = bits._transform_name('default_name.slider.ABCD:mW') 34 unknown_unit = bits._transform_name('default_name.aaaaa.QWERTY:unknown') 35 36 self.assertEqual('XYZ_avg_current', avg_current) 37 self.assertEqual('ABCD_avg_power', avg_power) 38 self.assertEqual('QWERTY', unknown_unit) 39 40 def test_raw_data_to_metrics(self): 41 raw_data = {'data': [ 42 {'name': 'default_device.Monsoon.Monsoon:mA', 43 'avg': 21, 44 'unit': 'mA'}, 45 {'name': 'default_device.Monsoon.Monsoon:mW', 46 'avg': 91, 47 'unit': 'mW'}]} 48 49 metrics = bits._raw_data_to_metrics(raw_data) 50 self.assertEqual(2, len(metrics)) 51 self.assertEqual( 52 power_metrics.Metric(21, 'current', 'mA', 'avg_current'), 53 metrics[0]) 54 self.assertEqual( 55 power_metrics.Metric(91, 'power', 'mW', 'avg_power'), 56 metrics[1]) 57 58 def test_raw_data_to_metrics_messages_are_ignored(self): 59 raw_data = {'data': [ 60 {'name': 'default_device.Log.UserInputs', 61 'avg': float('nan'), 62 'unit': 'Msg'}, 63 {'name': 'default_device.Log.Warnings', 64 'avg': float('nan'), 65 'unit': 'Msg'}]} 66 67 metrics = bits._raw_data_to_metrics(raw_data) 68 self.assertEqual(0, len(metrics)) 69 70 def test_get_single_file_get_first_element_of_a_list(self): 71 registry = {'some_key': ['first_element', 'second_element']} 72 73 result = bits._get_single_file(registry, 'some_key') 74 75 self.assertEqual('first_element', result) 76 77 def test_get_single_file_gets_string_if_registry_contains_string(self): 78 registry = {'some_key': 'this_is_a_string'} 79 80 result = bits._get_single_file(registry, 'some_key') 81 82 self.assertEqual('this_is_a_string', result) 83 84 def test_get_single_file_gets_none_if_value_is_undefined_or_empty_list(self): 85 registry = {'some_key': []} 86 87 result1 = bits._get_single_file(registry, 'some_key') 88 result2 = bits._get_single_file(registry, 'key_that_is_not_in_registry') 89 90 self.assertEqual(None, result1) 91 self.assertEqual(None, result2) 92 93 94 95if __name__ == '__main__': 96 unittest.main() 97