1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from telemetry import benchmark
8from telemetry.core.platform import android_device
9from telemetry.core.platform import android_platform_backend
10from telemetry.unittest import system_stub
11
12
13class AndroidPlatformBackendTest(unittest.TestCase):
14  def setUp(self):
15    self._stubs = system_stub.Override(
16        android_platform_backend,
17        ['perf_control', 'thermal_throttle', 'adb_commands'])
18
19  def tearDown(self):
20    self._stubs.Restore()
21
22  @benchmark.Disabled('chromeos')
23  def testGetCpuStats(self):
24    proc_stat_content = [
25        '7702 (.android.chrome) S 167 167 0 0 -1 1077936448 '
26        '3247 0 0 0 4 1 0 0 20 0 9 0 5603962 337379328 5867 '
27        '4294967295 1074458624 1074463824 3197495984 3197494152 '
28        '1074767676 0 4612 0 38136 4294967295 0 0 17 0 0 0 0 0 0 '
29        '1074470376 1074470912 1102155776']
30    self._stubs.adb_commands.adb_device.mock_content = proc_stat_content
31    old_interface = self._stubs.adb_commands.adb_device.old_interface
32    old_interface.can_access_protected_file_contents = True
33    backend = android_platform_backend.AndroidPlatformBackend(
34        android_device.AndroidDevice('12345'))
35    cpu_stats = backend.GetCpuStats('7702')
36    self.assertEquals(cpu_stats, {'CpuProcessTime': 5.0})
37
38  @benchmark.Disabled('chromeos')
39  def testGetCpuStatsInvalidPID(self):
40    # Mock an empty /proc/pid/stat.
41    backend = android_platform_backend.AndroidPlatformBackend(
42        android_device.AndroidDevice('1234'))
43    cpu_stats = backend.GetCpuStats('7702')
44    self.assertEquals(cpu_stats, {})
45
46  def testAndroidParseCpuStates(self):
47    cstate = {
48      'cpu0': 'C0\nC1\n103203424\n5342040\n300\n500\n1403232500',
49      'cpu1': 'C0\n124361858\n300\n1403232500'
50    }
51    expected_cstate = {
52      'cpu0': {
53        'WFI': 103203424,
54        'C0': 1403232391454536,
55        'C1': 5342040
56      },
57      'cpu1': {
58        'WFI': 124361858,
59        'C0': 1403232375638142
60      }
61    }
62    # Use mock start and end times to allow for the test to calculate C0.
63    result = android_platform_backend.AndroidPlatformBackend.ParseCStateSample(
64        cstate)
65    for cpu in result:
66      for state in result[cpu]:
67        self.assertAlmostEqual(result[cpu][state], expected_cstate[cpu][state])
68