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.
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
21 def communicate(self
):
22 return (self
.stdout
, self
.stderr
)
25 return self
.returncode
28 class MockSubprocess(object):
30 self
.PIPE
= simple_mock
.MockObject()
31 self
.STDOUT
= simple_mock
.MockObject()
32 self
._num
_collect
_calls
= 0
33 self
._num
_stop
_calls
= 0
36 def num_collect_calls(self
):
37 return self
._num
_collect
_calls
40 def num_stop_calls(self
):
41 return self
._num
_stop
_calls
43 def Popen(self
, cmd
, **_
):
44 self
._AnalyzeCommand
(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
57 def _IsCollectCommand(cmd
):
58 return '-collect' in cmd
61 def _IsStopCommand(cmd
):
63 cmd_idx
= cmd
.index('-command') + 1
64 return cmd_idx
< len(cmd
) and cmd
[cmd_idx
] == 'stop'
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))
90 vtune_profiler
.VTuneProfiler
.is_supported(options
.browser_type
) or
91 sys
.platform
!= 'linux2' or
92 options
.browser_type
.startswith('cros'))
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
109 # pylint: disable=protected-access
110 profiler
= vtune_profiler
.VTuneProfiler(self
._browser
._browser
_backend
,
111 self
._browser
._platform
_backend
,
114 profiler
.CollectProfile()
115 self
.assertEqual(mock_subprocess
.num_collect_calls
,
116 mock_subprocess
.num_stop_calls
)
118 vtune_profiler
.subprocess
= real_subprocess