Upstream version 7.36.149.0
[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 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
14
15
16 class MockAdbCommands(object):
17   def __init__(self, mock_content, system_properties):
18     self.mock_content = mock_content
19     self.system_properties = system_properties
20     if self.system_properties.get('ro.product.cpu.abi') == None:
21       self.system_properties['ro.product.cpu.abi'] = 'armeabi-v7a'
22
23   def CanAccessProtectedFileContents(self):
24     return True
25
26   # pylint: disable=W0613
27   def GetProtectedFileContents(self, file_name):
28     return self.mock_content
29
30   def PushIfNeeded(self, host_binary, device_path):
31     pass
32
33   def RunShellCommand(self, command):
34     return []
35
36
37 class MockDevice(object):
38   def __init__(self, mock_adb_commands):
39     self.old_interface = mock_adb_commands
40
41
42 class AndroidPlatformBackendTest(unittest.TestCase):
43   def setUp(self):
44     self._stubs = system_stub.Override(android_platform_backend,
45                                        ['perf_control', 'thermal_throttle'])
46
47   def tearDown(self):
48     self._stubs.Restore()
49
50   @test.Disabled('chromeos')
51   def testGetCpuStats(self):
52     proc_stat_content = [
53         '7702 (.android.chrome) S 167 167 0 0 -1 1077936448 '
54         '3247 0 0 0 4 1 0 0 20 0 9 0 5603962 337379328 5867 '
55         '4294967295 1074458624 1074463824 3197495984 3197494152 '
56         '1074767676 0 4612 0 38136 4294967295 0 0 17 0 0 0 0 0 0 '
57         '1074470376 1074470912 1102155776']
58     adb_valid_proc_content = MockDevice(MockAdbCommands(proc_stat_content, {}))
59     backend = android_platform_backend.AndroidPlatformBackend(
60         adb_valid_proc_content, False)
61     cpu_stats = backend.GetCpuStats('7702')
62     self.assertEquals(cpu_stats, {'CpuProcessTime': 5.0})
63
64   @test.Disabled('chromeos')
65   def testGetCpuStatsInvalidPID(self):
66     # Mock an empty /proc/pid/stat.
67     adb_empty_proc_stat = MockDevice(MockAdbCommands([], {}))
68     backend = android_platform_backend.AndroidPlatformBackend(
69         adb_empty_proc_stat, False)
70     cpu_stats = backend.GetCpuStats('7702')
71     self.assertEquals(cpu_stats, {})
72
73   @test.Disabled
74   def testFramesFromMp4(self):
75     mock_adb = MockDevice(MockAdbCommands([]))
76     backend = android_platform_backend.AndroidPlatformBackend(mock_adb, False)
77
78     try:
79       backend.InstallApplication('avconv')
80     finally:
81       if not backend.CanLaunchApplication('avconv'):
82         logging.warning('Test not supported on this platform')
83         return  # pylint: disable=W0150
84
85     vid = os.path.join(util.GetUnittestDataDir(), 'vid.mp4')
86     expected_timestamps = [
87       0,
88       763,
89       783,
90       940,
91       1715,
92       1732,
93       1842,
94       1926,
95       ]
96
97     # pylint: disable=W0212
98     for i, timestamp_bitmap in enumerate(backend._FramesFromMp4(vid)):
99       timestamp, bmp = timestamp_bitmap
100       self.assertEquals(timestamp, expected_timestamps[i])
101       expected_bitmap = bitmap.Bitmap.FromPngFile(os.path.join(
102           util.GetUnittestDataDir(), 'frame%d.png' % i))
103       self.assertTrue(expected_bitmap.IsEqual(bmp))