Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / tvcm / module_test_case.py
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.
4 import unittest
5 import sys
6 import os
7 import json
8
9
10 from tvcm import dev_server
11 from tvcm import browser_controller
12 from tvcm import test_runner
13 from tvcm import resource_loader
14
15
16 _currently_active_module_test_suite = None
17
18
19 class ModuleTestSuite(unittest.TestSuite):
20   def __init__(self, project):
21     super(ModuleTestSuite, self).__init__()
22     self._project = project
23     self._bc = None
24
25   def recreateEmptyVersion(self):
26     return ModuleTestSuite(self._project)
27
28   def run(self, result):
29     self.setUp()
30     try:
31       super(ModuleTestSuite, self).run(result)
32     finally:
33       self.tearDown()
34
35   @property
36   def bc(self):
37     return self._bc
38
39   def setUp(self):
40     self._bc = browser_controller.BrowserController(self._project)
41     self._bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
42
43     global _currently_active_module_test_suite
44     assert _currently_active_module_test_suite == None
45     _currently_active_module_test_suite = self
46
47   def tearDown(self):
48     if self._bc:
49       self._bc.Close()
50       self._bc = None
51
52     global _currently_active_module_test_suite
53     _currently_active_module_test_suite = None
54
55 def DiscoverTestsInModule(project, start_path):
56   if test_runner.PY_ONLY_TESTS:
57     return unittest.TestSuite()
58
59   if not browser_controller.IsSupported():
60     raise Exception('Cannot run all tests: telemetry could not be found')
61   rl = resource_loader.ResourceLoader(project)
62
63   test_modules = project.FindAllTestModuleNames(start_path=start_path)
64
65   bc = browser_controller.BrowserController(project)
66
67   bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
68   try:
69     if bc.EvaluateJavaScript('tvcm.hasPanic()'):
70       raise Exception('Runner failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
71
72     tests = bc.EvaluateThennableAndWait(
73       'discoverTestsInModules(%s)' % json.dumps(test_modules))
74
75     if bc.EvaluateJavaScript('tvcm.hasPanic()'):
76       raise Exception('Test loading failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
77
78     suite = ModuleTestSuite(project)
79     for fully_qualified_test_name in tests:
80       suite.addTest(ModuleTestCase(fully_qualified_test_name))
81     return suite
82   finally:
83     bc.Close()
84
85
86
87 class ModuleTestCase(unittest.TestCase):
88   def __init__(self, fully_qualified_test_name):
89     super(ModuleTestCase, self).__init__(methodName='runTest')
90     self.fully_qualified_test_name = fully_qualified_test_name
91
92   def id(self):
93     return self.fully_qualified_test_name
94
95   def shortDescription(self):
96     return None
97
98   def __str__(self):
99     i = self.fully_qualified_test_name.rfind('.')
100     modname = self.fully_qualified_test_name[0:i]
101     testname = self.fully_qualified_test_name[i+1:]
102     return '%s (%s)' % (testname, modname)
103
104   def runTest(self):
105     mts = _currently_active_module_test_suite
106     assert mts, 'Something is wrong: ModuleTestCase can only be run inside a ModuleTestSuite.run()'
107
108     bc = mts.bc
109     res = bc.EvaluateThennableAndWait(
110       'runTestNamed(%s)' % json.dumps(self.fully_qualified_test_name))