Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / posix_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 import os
5 import sys
6 import unittest
7
8 from telemetry import benchmark
9 from telemetry.core import platform as platform_module
10 from telemetry.core.platform import posix_platform_backend
11
12
13 class TestBackend(posix_platform_backend.PosixPlatformBackend):
14
15   # pylint: disable=W0223
16
17   def __init__(self):
18     super(TestBackend, self).__init__()
19     self._mock_ps_output = None
20
21   def SetMockPsOutput(self, output):
22     self._mock_ps_output = output
23
24   def _GetPsOutput(self, columns, pid=None):
25     return self._mock_ps_output
26
27
28 class PosixPlatformBackendTest(unittest.TestCase):
29
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]))
35
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())
41
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]))
47
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]))
53
54   @benchmark.Enabled('linux', 'mac')
55   def testIsApplicationRunning(self):
56     platform = platform_module.GetHostPlatform()
57
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))
61     self.assertFalse(
62         platform.IsApplicationRunning('%s append_bad_after_space' % sys_exe))