Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / telemetry / telemetry / internal / platform / profiler / vtune_profiler_unittest.py
blobd763d7720417c46b3d81981e162d30a351cf2ee5
1 # Copyright 2014 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.
5 import sys
6 import unittest
8 from telemetry import decorators
9 from telemetry.internal.platform.profiler import vtune_profiler
10 from telemetry.testing import options_for_unittests
11 from telemetry.testing import simple_mock
12 from telemetry.testing import tab_test_case
15 class MockPopen(object):
16 def __init__(self, returncode, stdout=None, stderr=None):
17 self.returncode = returncode
18 self.stdout = stdout
19 self.stderr = stderr
21 def communicate(self):
22 return (self.stdout, self.stderr)
24 def wait(self):
25 return self.returncode
28 class MockSubprocess(object):
29 def __init__(self):
30 self.PIPE = simple_mock.MockObject()
31 self.STDOUT = simple_mock.MockObject()
32 self._num_collect_calls = 0
33 self._num_stop_calls = 0
35 @property
36 def num_collect_calls(self):
37 return self._num_collect_calls
39 @property
40 def num_stop_calls(self):
41 return self._num_stop_calls
43 def Popen(self, cmd, **_):
44 self._AnalyzeCommand(cmd)
45 return MockPopen(0)
47 def call(self, cmd):
48 self._AnalyzeCommand(cmd)
50 def _AnalyzeCommand(self, cmd):
51 if MockSubprocess._IsCollectCommand(cmd):
52 self._num_collect_calls += 1
53 elif MockSubprocess._IsStopCommand(cmd):
54 self._num_stop_calls += 1
56 @staticmethod
57 def _IsCollectCommand(cmd):
58 return '-collect' in cmd
60 @staticmethod
61 def _IsStopCommand(cmd):
62 try:
63 cmd_idx = cmd.index('-command') + 1
64 return cmd_idx < len(cmd) and cmd[cmd_idx] == 'stop'
65 except ValueError:
66 return False
69 class TestVTuneProfiler(unittest.TestCase):
71 def testVTuneProfilerIsSupported(self):
72 options = options_for_unittests.GetCopy()
74 mock_subprocess = simple_mock.MockObject()
75 mock_subprocess.ExpectCall(
76 'Popen').WithArgs(simple_mock.DONT_CARE).WillReturn(MockPopen(0))
77 mock_subprocess.SetAttribute('PIPE', simple_mock.MockObject())
78 mock_subprocess.SetAttribute('STDOUT', simple_mock.MockObject())
80 real_subprocess = vtune_profiler.subprocess
81 vtune_profiler.subprocess = mock_subprocess
83 if options.browser_type.startswith('android'):
84 # On Android we're querying if 'su' is available.
85 mock_subprocess.ExpectCall('Popen').WithArgs(
86 simple_mock.DONT_CARE).WillReturn(MockPopen(0, 'su', None))
88 try:
89 self.assertTrue(
90 vtune_profiler.VTuneProfiler.is_supported(options.browser_type) or
91 sys.platform != 'linux2' or
92 options.browser_type.startswith('cros'))
93 finally:
94 vtune_profiler.subprocess = real_subprocess
97 class TestVTuneProfilerTabTestCase(tab_test_case.TabTestCase):
99 # This test is only meant to be run if VTune is installed locally. Please
100 # run it locally if you are modifying related code, but it's disabled on the
101 # bots because they don't have VTune. See crbug.com/437085
102 @decorators.Disabled('all')
103 def testVTuneProfiler(self):
104 mock_subprocess = MockSubprocess()
105 real_subprocess = vtune_profiler.subprocess
106 vtune_profiler.subprocess = mock_subprocess
108 try:
109 # pylint: disable=protected-access
110 profiler = vtune_profiler.VTuneProfiler(self._browser._browser_backend,
111 self._browser._platform_backend,
112 'tmp',
114 profiler.CollectProfile()
115 self.assertEqual(mock_subprocess.num_collect_calls,
116 mock_subprocess.num_stop_calls)
117 finally:
118 vtune_profiler.subprocess = real_subprocess