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.
8 from telemetry import benchmark
9 from telemetry.core import platform as platform_module
10 from telemetry.core.platform import posix_platform_backend
13 class TestBackend(posix_platform_backend.PosixPlatformBackend):
15 # pylint: disable=W0223
18 super(TestBackend, self).__init__()
19 self._mock_ps_output = None
21 def SetMockPsOutput(self, output):
22 self._mock_ps_output = output
24 def _GetPsOutput(self, columns, pid=None):
25 return self._mock_ps_output
28 class PosixPlatformBackendTest(unittest.TestCase):
30 def testGetChildPidsWithGrandChildren(self):
31 backend = TestBackend()
32 backend.SetMockPsOutput(['1 0 S', '2 1 R', '3 2 S', '4 1 R', '5 4 R'])
33 result = backend.GetChildPids(1)
34 self.assertEquals(set(result), set([2, 3, 4, 5]))
36 def testGetChildPidsWithNoSuchPid(self):
37 backend = TestBackend()
38 backend.SetMockPsOutput(['1 0 S', '2 1 R', '3 2 S', '4 1 R', '5 4 R'])
39 result = backend.GetChildPids(6)
40 self.assertEquals(set(result), set())
42 def testGetChildPidsWithZombieChildren(self):
43 backend = TestBackend()
44 backend.SetMockPsOutput(['1 0 S', '2 1 R', '3 2 S', '4 1 R', '5 4 Z'])
45 result = backend.GetChildPids(1)
46 self.assertEquals(set(result), set([2, 3, 4]))
48 def testGetChildPidsWithMissingState(self):
49 backend = TestBackend()
50 backend.SetMockPsOutput([' 1 0 S ', ' 2 1', '3 2 '])
51 result = backend.GetChildPids(1)
52 self.assertEquals(set(result), set([2, 3]))
54 @benchmark.Enabled('linux', 'mac')
55 def testIsApplicationRunning(self):
56 platform = platform_module.GetHostPlatform()
58 self.assertFalse(platform.IsApplicationRunning('This_Is_A_Bad___App__Name'))
59 sys_exe = os.path.basename(sys.executable)
60 self.assertTrue(platform.IsApplicationRunning(sys_exe))
62 platform.IsApplicationRunning('%s append_bad_after_space' % sys_exe))