Upstream version 11.40.277.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 def _NavigateToTestCaseRunner(bc):
20   bc.NavigateToPath('/tvcm/unittest/module_test_case_runner.html')
21   bc.WaitForJavaScriptExpression('window.__readyToRun == true')
22   # Sanity checks that __readyToRun is working right. If any of these pop,
23   # it implies that __readyToRun isn't working.
24   sanity_checks = [
25     'window.tvcm !== undefined',
26     'window.tvcm.GUID.allocate !== undefined',
27     'window.tvcm.hasPanic !== undefined',
28     'window.discoverTestsInModules !== undefined',
29     'window.runTestNamed !== undefined'
30   ]
31   for check in sanity_checks:
32     if not bc.EvaluateJavaScript(check):
33       raise Exception('Load failed because sanity check %s failed')
34
35
36 class ModuleTestSuite(unittest.TestSuite):
37   def __init__(self, project):
38     super(ModuleTestSuite, self).__init__()
39     self._project = project
40     self._bc = None
41     self._bc_startup_exception_string = None
42
43   @property
44   def __class__(self):
45     def RecreateFunc():
46       return self.recreateEmptyVersion()
47     return RecreateFunc
48   def recreateEmptyVersion(self):
49     return ModuleTestSuite(self._project)
50
51   def __call__(self, *args):
52     return self.run(*args)
53
54   def run(self, result):
55     try:
56       self.setUp()
57       return super(ModuleTestSuite, self).run(result)
58     finally:
59       self.tearDown()
60
61   @property
62   def bc(self):
63     return self._bc
64
65   @property
66   def bc_startup_exception_string(self):
67     return self._bc_startup_exception_string
68
69   def setUp(self):
70     global _currently_active_module_test_suite
71     if _currently_active_module_test_suite:
72       self._bc_startup_exception_string = "Some previous suite did not shut down cleanly"
73       self._bc = None
74       return
75
76     try:
77       self._bc = browser_controller.BrowserController(self._project)
78       _NavigateToTestCaseRunner(self._bc)
79     except:
80       import traceback
81       traceback.print_exc()
82       self._bc_startup_exception_string = traceback.format_exc()
83       self._bc.Close()
84       self._bc = None
85
86     _currently_active_module_test_suite = self
87     if self._bc:
88       self._bc.stdout_enabled = True
89
90   def tearDown(self):
91     if self._bc:
92       self._bc.stdout_enabled = False
93       self._bc.Close()
94       self._bc = None
95
96     global _currently_active_module_test_suite
97     _currently_active_module_test_suite = None
98
99 def DiscoverTestsInModule(project, start_path):
100   try:
101     return _DiscoverTestsInModuleImpl(project, start_path)
102   except:
103     import traceback
104     sys.stderr.write("While discovering tests:\n")
105     sys.stderr.write(sys.exc_info()[1].message)
106     sys.stderr.write("\n\n")
107     raise
108
109 def _DiscoverTestsInModuleImpl(project, start_path):
110   if test_runner.PY_ONLY_TESTS:
111     return unittest.TestSuite()
112
113   if not browser_controller.IsSupported():
114     raise Exception('Cannot run all tests: telemetry could not be found')
115   rl = resource_loader.ResourceLoader(project)
116
117   test_modules = [x.name for x in
118                   project.FindAllTestModuleResources(start_path=start_path)]
119
120   bc = browser_controller.BrowserController(project)
121   try:
122     _NavigateToTestCaseRunner(bc)
123     if bc.EvaluateJavaScript('tvcm.hasPanic()'):
124       raise Exception('Runner failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
125
126     tests = bc.EvaluateThennableAndWait(
127       'discoverTestsInModules(%s)' % json.dumps(test_modules))
128
129     if bc.EvaluateJavaScript('tvcm.hasPanic()'):
130       raise Exception('Test loading failure: %s' % bc.EvaluateJavaScript('tvcm.getPanicText()'))
131
132     suite = ModuleTestSuite(project)
133     for fully_qualified_test_name in tests:
134       suite.addTest(ModuleTestCase(fully_qualified_test_name))
135     return suite
136   finally:
137     bc.Close()
138
139
140
141 class ModuleTestCase(unittest.TestCase):
142   def __init__(self, fully_qualified_test_name):
143     super(ModuleTestCase, self).__init__(methodName='runTest')
144     self.fully_qualified_test_name = fully_qualified_test_name
145
146   def id(self):
147     return self.fully_qualified_test_name
148
149   def shortDescription(self):
150     return None
151
152   def __str__(self):
153     i = self.fully_qualified_test_name.rfind('.')
154     modname = self.fully_qualified_test_name[0:i]
155     testname = self.fully_qualified_test_name[i+1:]
156     return '%s (%s)' % (testname, modname)
157
158   def runTest(self):
159     global _currently_active_module_test_suite
160     mts = _currently_active_module_test_suite
161     assert mts, 'Something is wrong: ModuleTestCase can only be run inside a ModuleTestSuite.run()'
162
163     if not mts.bc:
164       raise Exception('Test cannot run because browser did not start.\n\nOriginal %s\n\n' % \
165                       mts.bc_startup_exception_string)
166     bc = mts.bc
167     res = bc.EvaluateThennableAndWait(
168       'runTestNamed(%s)' % json.dumps(self.fully_qualified_test_name))