09e9a5437cb0b7389c0257514aa027e085995581
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / android_platform_backend_unittest.py
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
5 import logging
6 import os
7 import unittest
8
9 from telemetry.core import bitmap
10 from telemetry.core import util
11 from telemetry.core.platform import android_platform_backend
12 from telemetry.unittest import DisabledTest, system_stub
13
14
15 class MockAdbCommands(object):
16   def __init__(self, mock_content):
17     self.mock_content = mock_content
18
19   def CanAccessProtectedFileContents(self):
20     return True
21
22   # pylint: disable=W0613
23   def GetProtectedFileContents(self, file_name, log_result):
24     return self.mock_content
25
26
27 class AndroidPlatformBackendTest(unittest.TestCase):
28   def setUp(self):
29     self._stubs = system_stub.Override(android_platform_backend,
30                                        ['perf_control', 'thermal_throttle'])
31
32   def tearDown(self):
33     self._stubs.Restore()
34
35   def testGetCpuStats(self):
36     proc_stat_content = [
37         '7702 (.android.chrome) S 167 167 0 0 -1 1077936448 '
38         '3247 0 0 0 4 1 0 0 20 0 9 0 5603962 337379328 5867 '
39         '4294967295 1074458624 1074463824 3197495984 3197494152 '
40         '1074767676 0 4612 0 38136 4294967295 0 0 17 0 0 0 0 0 0 '
41         '1074470376 1074470912 1102155776']
42     adb_valid_proc_content = MockAdbCommands(proc_stat_content)
43     backend = android_platform_backend.AndroidPlatformBackend(
44         adb_valid_proc_content, False)
45     cpu_stats = backend.GetCpuStats('7702')
46     self.assertEquals(cpu_stats, {'CpuProcessTime': 5.0})
47
48   def testGetCpuStatsInvalidPID(self):
49     # Mock an empty /proc/pid/stat.
50     adb_empty_proc_stat = MockAdbCommands([])
51     backend = android_platform_backend.AndroidPlatformBackend(
52         adb_empty_proc_stat, False)
53     cpu_stats = backend.GetCpuStats('7702')
54     self.assertEquals(cpu_stats, {})
55
56   @DisabledTest
57   def testFramesFromMp4(self):
58     mock_adb = MockAdbCommands([])
59     backend = android_platform_backend.AndroidPlatformBackend(mock_adb, False)
60
61     try:
62       backend.InstallApplication('avconv')
63     finally:
64       if not backend.CanLaunchApplication('avconv'):
65         logging.warning('Test not supported on this platform')
66         return  # pylint: disable=W0150
67
68     vid = os.path.join(util.GetUnittestDataDir(), 'vid.mp4')
69     expected_timestamps = [
70       0,
71       763,
72       783,
73       940,
74       1715,
75       1732,
76       1842,
77       1926,
78       ]
79
80     # pylint: disable=W0212
81     for i, timestamp_bitmap in enumerate(backend._FramesFromMp4(vid)):
82       timestamp, bmp = timestamp_bitmap
83       self.assertEquals(timestamp, expected_timestamps[i])
84       expected_bitmap = bitmap.Bitmap.FromPngFile(os.path.join(
85           util.GetUnittestDataDir(), 'frame%d.png' % i))
86       self.assertTrue(expected_bitmap.IsEqual(bmp))