def test_usage_with_fallback(): with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # We set the version to be less than Android 8 device.version = 7 device._verbose = True # Return what our shell call to dumpsys would give us shell_output = ' 34% 14781/org.mozilla.geckoview_example: 26% user + 7.5% kernel' device.shell_output.return_value = shell_output # Create a control server control_server.cpu_test = True control_server.test_name = 'cpuunittest' control_server.device = device control_server.app_name = 'org.mozilla.geckoview_example' raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example', cpu_test=True) raptor.device = device raptor.config['cpu_test'] = True raptor.control_server = control_server # Verify the response contains our expected CPU % of 34 cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_fallback', u'unit': u'%', u'values': { u'browser_cpu_usage': float(34) } } cpu.generate_android_cpu_profile(raptor, "usage_with_fallback") control_server.submit_supporting_data.assert_called_once_with( cpuinfo_data)
def test_no_device(): raptor = RaptorAndroid('geckoview', 'org.mozilla.org.mozilla.geckoview_example', cpu_test=True) raptor.device = None resp = cpu.start_android_cpu_profiler(raptor) assert resp is None
def test_usage_with_output(): with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # Override the shell output with sample CPU usage details filepath = os.path.abspath(os.path.dirname(__file__)) + '/files/' f = open(filepath + 'top-info.txt', 'r') device.shell_output.return_value = f.read() device._verbose = True device.version = 8 # Create a control server control_server.cpu_test = True control_server.test_name = 'cpuunittest' control_server.device = device control_server.app_name = 'org.mozilla.geckoview_example' raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example', cpu_test=True) raptor.device = device raptor.config['cpu_test'] = True raptor.control_server = control_server # Verify the response contains our expected CPU % of 93.7 cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_integer_cpu_info_output', u'unit': u'%', u'values': { u'browser_cpu_usage': float(93.7) } } cpu.generate_android_cpu_profile( raptor, "usage_with_integer_cpu_info_output") control_server.submit_supporting_data.assert_called_once_with( cpuinfo_data)
def test_usage_with_invalid_data_returns_zero(): with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # Create a device that returns invalid data device.shell_output.return_value = 'geckoview' device.version = 8 device._verbose = True # Create a control server control_server.cpu_test = True control_server.device = device raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example', cpu_test=True) raptor.config['cpu_test'] = True raptor.control_server = control_server raptor.device = device # Verify the call to submit data was made cpuinfo_data = { 'type': 'cpu', 'test': 'usage_with_invalid_data_returns_zero', 'unit': '%', 'values': { 'browser_cpu_usage': float(0) } } cpu.generate_android_cpu_profile( raptor, "usage_with_invalid_data_returns_zero") control_server.submit_supporting_data.assert_called_once_with( cpuinfo_data)
def test_usage_with_fallback(): with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: device._verbose = True # Return what our shell call to dumpsys would give us shell_output = ' 31093 u0_a196 10 -10 8% S ' + \ '66 1392100K 137012K fg org.mozilla.geckoview_example' # We set the version to be less than Android 8 device.shell_output.side_effect = ['7.0.0', shell_output] # Create a control server control_server.cpu_test = True control_server.test_name = 'cpuunittest' control_server.device = device control_server.app_name = 'org.mozilla.geckoview_example' raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example') raptor.device = device raptor.config['cpu_test'] = True raptor.control_server = control_server cpu_profiler = cpu.AndroidCPUProfiler(raptor) cpu_profiler.polls.append(cpu_profiler.get_app_cpu_usage()) cpu_profiler.polls.append(0) # Verify the response contains our expected CPU % of 8 avg_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_fallback-avg', u'unit': u'%', u'values': { u'avg': 8 / 2 } } min_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_fallback-min', u'unit': u'%', u'values': { u'min': 0 } } max_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_fallback-max', u'unit': u'%', u'values': { u'max': 8 } } cpu_profiler.generate_android_cpu_profile("usage_with_fallback") control_server.submit_supporting_data.assert_has_calls([ mock.call(avg_cpuinfo_data), mock.call(min_cpuinfo_data), mock.call(max_cpuinfo_data) ])
def test_no_device(): raptor = RaptorAndroid('geckoview', 'org.mozilla.org.mozilla.geckoview_example', cpu_test=True) raptor.device = None resp = cpu.generate_android_cpu_profile(raptor, 'no_control_server_device') assert resp is None
def test_usage_with_output(): with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # Override the shell output with sample CPU usage details filepath = os.path.abspath(os.path.dirname(__file__)) + '/files/' with open(filepath + 'top-info.txt', 'r') as f: test_data = f.read() device.shell_output.side_effect = ['8.0.0', test_data] device._verbose = True # Create a control server control_server.cpu_test = True control_server.test_name = 'cpuunittest' control_server.device = device control_server.app_name = 'org.mozilla.geckoview_example' raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example') raptor.device = device raptor.config['cpu_test'] = True raptor.control_server = control_server cpu_profiler = cpu.AndroidCPUProfiler(raptor) cpu_profiler.polls.append(cpu_profiler.get_app_cpu_usage()) cpu_profiler.polls.append(0) # Verify the response contains our expected CPU % of 93.7 avg_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_integer_cpu_info_output-avg', u'unit': u'%', u'values': { u'avg': 93.7 / 2 } } min_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_integer_cpu_info_output-min', u'unit': u'%', u'values': { u'min': 0 } } max_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_integer_cpu_info_output-max', u'unit': u'%', u'values': { u'max': 93.7 } } cpu_profiler.generate_android_cpu_profile( "usage_with_integer_cpu_info_output") control_server.submit_supporting_data.assert_has_calls([ mock.call(avg_cpuinfo_data), mock.call(min_cpuinfo_data), mock.call(max_cpuinfo_data) ])
def test_androidos_baseline_power(): if not os.getenv('MOZ_UPLOAD_DIR'): os.environ['MOZ_UPLOAD_DIR'] = tempfile.mkdtemp() with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # Override the shell output with sample CPU usage details filepath = os.path.abspath(os.path.dirname(__file__)) + '/files/' f = open(filepath + 'batterystats-android-8.txt', 'r') batterystats_return_value = f.read() # Multiple shell output calls are performed # and only those with non-None output are required device.shell_output.return_value = None device.shell_output.side_effect = [ None, None, 'Test value', 'Test value', batterystats_return_value, '8.0.0', ] device._verbose = True device.version = 8 # Create a control server control_server.power_test = True control_server.test_name = 'gve-pytest' control_server.device = device control_server.app_name = 'org.mozilla.geckoview_example' raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example', power_test=True) raptor.device = device raptor.config['power_test'] = True raptor.control_server = control_server # Expected OS baseline calculation result os_baseline_data = { u'type': u'power', u'test': u'gve-pytest', u'unit': u'mAh', u'values': { u'cpu': float(10.786654), u'wifi': float(2.26132), u'screen': float(51.66), u'proportional': float(11.294805199999999) } } # Verify the response contains our expected calculations power.finish_android_power_test( raptor, 'gve-pytest', os_baseline=True ) assert raptor.os_baseline_data == os_baseline_data
def test_usage_with_invalid_data_returns_zero(): with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # Create a device that returns invalid data device.shell_output.side_effect = ['8.0.0', 'geckoview'] device._verbose = True # Create a control server control_server.cpu_test = True control_server.device = device raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example') raptor.config['cpu_test'] = True raptor.control_server = control_server raptor.device = device cpu_profiler = cpu.AndroidCPUProfiler(raptor) cpu_profiler.get_app_cpu_usage() # Verify the call to submit data was made avg_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_invalid_data_returns_zero-avg', u'unit': u'%', u'values': { u'avg': 0 } } min_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_invalid_data_returns_zero-min', u'unit': u'%', u'values': { u'min': 0 } } max_cpuinfo_data = { u'type': u'cpu', u'test': u'usage_with_invalid_data_returns_zero-max', u'unit': u'%', u'values': { u'max': 0 } } cpu_profiler.generate_android_cpu_profile( "usage_with_invalid_data_returns_zero") control_server.submit_supporting_data.assert_has_calls([ mock.call(avg_cpuinfo_data), mock.call(min_cpuinfo_data), mock.call(max_cpuinfo_data) ])
def test_android8_power(): if not os.getenv('MOZ_UPLOAD_DIR'): os.environ['MOZ_UPLOAD_DIR'] = tempfile.mkdtemp() with mock.patch('mozdevice.adb.ADBDevice') as device: with mock.patch('raptor.raptor.RaptorControlServer') as control_server: # Override the shell output with sample CPU usage details filepath = os.path.abspath(os.path.dirname(__file__)) + '/files/' f = open(filepath + 'batterystats-android-8.txt', 'r') batterystats_return_value = f.read() print(type(batterystats_return_value)) # Multiple shell output calls are performed # and only those with non-None output are required device.shell_output.return_value = None device.shell_output.side_effect = [ None, None, 'Test value', 'Test value', batterystats_return_value, '8.0.0', ] device._verbose = True device.version = 8 # Create a control server control_server.power_test = True control_server.test_name = 'gve-pytest' control_server.device = device control_server.app_name = 'org.mozilla.geckoview_example' raptor = RaptorAndroid('geckoview', 'org.mozilla.geckoview_example', power_test=True) raptor.device = device raptor.config['power_test'] = True raptor.control_server = control_server raptor.power_test_time = 20 # minutes raptor.os_baseline_data = { u'type': u'power', u'test': u'gve-pytest', u'unit': u'mAh', u'values': { u'cpu': float(5), u'wifi': float(5), u'screen': float(5), u'proportional': float(5) } } # Verify the response contains our expected calculations power_data = { u'type': u'power', u'test': u'gve-pytest', u'unit': u'mAh', u'values': { u'cpu': float(4.7), u'wifi': float(0.000556), u'screen': float(51.5), u'proportional': float(11.2) } } pc_data = { u'type': u'power', u'test': u'gve-pytest-%change', u'unit': u'%', u'values': { u'cpu': float(4.700000000000017), u'wifi': float(0.0005559999999888987), u'screen': float(51.5), u'proportional': float(11.199999999999989) } } power.finish_android_power_test( raptor, 'gve-pytest' ) control_server.submit_supporting_data.assert_has_calls([ mock.call(power_data), mock.call(pc_data), mock.call(raptor.os_baseline_data) ])