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.
9 from telemetry import test
10 from telemetry.core import bitmap
11 from telemetry.core import util
12 from telemetry.core.platform import android_platform_backend
13 from telemetry.unittest import system_stub
16 class MockAdbCommands(object):
17 def __init__(self, mock_content):
18 self.mock_content = mock_content
20 def CanAccessProtectedFileContents(self):
23 # pylint: disable=W0613
24 def GetProtectedFileContents(self, file_name, log_result):
25 return self.mock_content
27 def PushIfNeeded(self, host_binary, device_path):
30 def RunShellCommand(self, command):
34 class AndroidPlatformBackendTest(unittest.TestCase):
36 self._stubs = system_stub.Override(android_platform_backend,
37 ['perf_control', 'thermal_throttle'])
42 @test.Disabled('chromeos')
43 def testGetCpuStats(self):
45 '7702 (.android.chrome) S 167 167 0 0 -1 1077936448 '
46 '3247 0 0 0 4 1 0 0 20 0 9 0 5603962 337379328 5867 '
47 '4294967295 1074458624 1074463824 3197495984 3197494152 '
48 '1074767676 0 4612 0 38136 4294967295 0 0 17 0 0 0 0 0 0 '
49 '1074470376 1074470912 1102155776']
50 adb_valid_proc_content = MockAdbCommands(proc_stat_content)
51 backend = android_platform_backend.AndroidPlatformBackend(
52 adb_valid_proc_content, False)
53 cpu_stats = backend.GetCpuStats('7702')
54 self.assertEquals(cpu_stats, {'CpuProcessTime': 5.0})
56 @test.Disabled('chromeos')
57 def testGetCpuStatsInvalidPID(self):
58 # Mock an empty /proc/pid/stat.
59 adb_empty_proc_stat = MockAdbCommands([])
60 backend = android_platform_backend.AndroidPlatformBackend(
61 adb_empty_proc_stat, False)
62 cpu_stats = backend.GetCpuStats('7702')
63 self.assertEquals(cpu_stats, {})
66 def testFramesFromMp4(self):
67 mock_adb = MockAdbCommands([])
68 backend = android_platform_backend.AndroidPlatformBackend(mock_adb, False)
71 backend.InstallApplication('avconv')
73 if not backend.CanLaunchApplication('avconv'):
74 logging.warning('Test not supported on this platform')
75 return # pylint: disable=W0150
77 vid = os.path.join(util.GetUnittestDataDir(), 'vid.mp4')
78 expected_timestamps = [
89 # pylint: disable=W0212
90 for i, timestamp_bitmap in enumerate(backend._FramesFromMp4(vid)):
91 timestamp, bmp = timestamp_bitmap
92 self.assertEquals(timestamp, expected_timestamps[i])
93 expected_bitmap = bitmap.Bitmap.FromPngFile(os.path.join(
94 util.GetUnittestDataDir(), 'frame%d.png' % i))
95 self.assertTrue(expected_bitmap.IsEqual(bmp))