79c056e9df6d312c4bbf3991850b896725ebc104
[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 def _NavigateToTestCaseRunner(bc):
20   bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
21   bc.WaitForJavaScriptExpression('window.__readyToRun == true')
22   bc.WaitForJavaScriptExpression('window.tvcm !== undefined')
23   bc.WaitForJavaScriptExpression('window.tvcm.hasPanic !== undefined')
24   bc.WaitForJavaScriptExpression('window.discoverTestsInModules !== undefined')
25   bc.WaitForJavaScriptExpression('window.runTestNamed !== undefined')
26
27
28 class ModuleTestSuite(unittest.TestSuite):
29   def __init__(self, project):
30     super(ModuleTestSuite, self).__init__()
31     self._project = project
32     self._bc = None
33
34   @property
35   def __class__(self):
36     def RecreateFunc():
37       return self.recreateEmptyVersion()
38     return RecreateFunc
39   def recreateEmptyVersion(self):
40     return ModuleTestSuite(self._project)
41
42   def __call__(self, *args):
43     return self.run(*args)
44
45   def run(self, result):
46     self.setUp()
47     try:
48       return super(ModuleTestSuite, self).run(result)
49     finally:
50       self.tearDown()
51
52   @property
53   def bc(self):
54     return self._bc
55
56   def setUp(self):
57     try:
58       self._bc = browser_controller.BrowserController(self._project)
59       _NavigateToTestCaseRunner(self._bc)
60     except:
61       self._bc.Close()
62       self._bc = None
63       return
64
65     global _currently_active_module_test_suite
66     assert _currently_active_module_test_suite == None
67     _currently_active_module_test_suite = self
68
69   def tearDown(self):
70     if self._bc:
71       self._bc.Close()
72       self._bc = None
73
74     global _currently_active_module_test_suite
75     _currently_active_module_test_suite = None
76
77 def DiscoverTestsInModule(project, start_path):
78   try:
79     return _DiscoverTestsInModuleImpl(project, start_path)
80   except:
81     import traceback
82     sys.stderr.write("While discovering tests in %s:\n" % repr(project))
83     traceback.print_exc()
84     sys.stderr.write("\n\n")
85     raise
86
87 def _DiscoverTestsInModuleImpl(project, start_path):
88   if test_runner.PY_ONLY_TESTS:
89     return unittest.TestSuite()
90
91   if not browser_controller.IsSupported():
92     raise Exception('Cannot run all tests: telemetry could not be found')
93   rl = resource_loader.ResourceLoader(project)
94
95   test_modules = [x.name for x in
96                   project.FindAllTestModuleResources(start_path=start_path)]
97
98   bc = browser_controller.BrowserController(project)
99   try:
100     _NavigateToTestCaseRunner(bc)
101     if bc.EvaluateJavaScript('tvcm.hasPanic()'):
102       raise Exception('Runner failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
103
104     tests = bc.EvaluateThennableAndWait(
105       'discoverTestsInModules(%s)' % json.dumps(test_modules))
106
107     if bc.EvaluateJavaScript('tvcm.hasPanic()'):
108       raise Exception('Test loading failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
109
110     suite = ModuleTestSuite(project)
111     for fully_qualified_test_name in tests:
112       suite.addTest(ModuleTestCase(fully_qualified_test_name))
113     return suite
114   finally:
115     bc.Close()
116
117
118
119 class ModuleTestCase(unittest.TestCase):
120   def __init__(self, fully_qualified_test_name):
121     super(ModuleTestCase, self).__init__(methodName='runTest')
122     self.fully_qualified_test_name = fully_qualified_test_name
123
124   def id(self):
125     return self.fully_qualified_test_name
126
127   def shortDescription(self):
128     return None
129
130   def __str__(self):
131     i = self.fully_qualified_test_name.rfind('.')
132     modname = self.fully_qualified_test_name[0:i]
133     testname = self.fully_qualified_test_name[i+1:]
134     return '%s (%s)' % (testname, modname)
135
136   def runTest(self):
137     global _currently_active_module_test_suite
138     mts = _currently_active_module_test_suite
139     assert mts, 'Something is wrong: ModuleTestCase can only be run inside a ModuleTestSuite.run()'
140
141     bc = mts.bc
142     res = bc.EvaluateThennableAndWait(
143       'runTestNamed(%s)' % json.dumps(self.fully_qualified_test_name))