Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / tvcm / js_module_unittest.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
6 from tvcm import fake_fs
7 from tvcm import module
8 from tvcm import js_module
9 from tvcm import project as project_module
10 from tvcm import resource as resource_module
11 from tvcm import resource_loader
12 from tvcm import strip_js_comments
13
14 class SmokeTest(unittest.TestCase):
15   def testBasic(self):
16     with fake_fs.FakeFS({"/src/test.js": "// blahblahblah\n\n'use strict';\n\ntvcm.require('dependency1');"}):
17       project = project_module.Project(['/src/'], include_tvcm_paths=False)
18       loader = resource_loader.ResourceLoader(project)
19       resource = resource_module.Resource('/src/', '/src/test.js')
20       my_module = js_module.JSModule(loader, 'test', resource)
21
22 class ValidateStrictModeTests(unittest.TestCase):
23   """Test case for ValidateUsesStrictMode."""
24
25   def test_ValidateUsesStrictMode_returns_true(self):
26     text = "// blahblahblah\n\n'use strict';\n\ntvcm.require('dependency1');"
27     stripped_text = strip_js_comments.strip_js_comments(text)
28     self.assertIsNone(js_module.ValidateUsesStrictMode('module', stripped_text))
29
30   def test_ValidateUsesStrictModeOneLiner(self):
31     text = "'use strict'; tvcm.require('dependency1');"
32     stripped_text = strip_js_comments.strip_js_comments(text)
33     self.assertIsNone(js_module.ValidateUsesStrictMode('module', stripped_text))
34
35   def test_ValidateUsesStrictMode_catches_missing_strict_mode(self):
36     text = "// blahblahblah\n\ntvcm.require('dependency1');"
37     stripped_text = strip_js_comments.strip_js_comments(text)
38     self.assertRaises(
39         lambda: js_module.ValidateUsesStrictMode('module', stripped_text))
40
41 class ValidateTestSuiteDefinition(unittest.TestCase):
42   def test_basic_success(self):
43     text = """
44 tvcm.unittest.testSuite('foo.bar_test', function() {
45 });
46 """
47     js_module.ValidateTestSuiteDefinition('foo.bar_test', text)
48
49
50   def test_wrong_name(self):
51     text = """
52 tvcm.unittest.testSuite('foo.bar', function() {
53 });
54 """
55     self.assertRaises(
56       lambda: js_module.ValidateTestSuiteDefinition('foo.bar_test', text))
57
58   def test_no_suite_failure(self):
59     text = """
60 """
61     self.assertRaises(
62       lambda: js_module.ValidateTestSuiteDefinition('foo.bar_test', text))
63
64   def test_multiple_suites_failure(self):
65     text = """
66 tvcm.unittest.testSuite('foo.bar_test', function() {
67 });
68 tvcm.unittest.testSuite('foo.bar_test', function() {
69 });
70 """
71     self.assertRaises(
72       lambda: js_module.ValidateTestSuiteDefinition('foo.bar_test', text))
73
74 class ParseDefinitionTests(unittest.TestCase):
75   """Test case for js_module.Parse."""
76
77   def test_Parse_populates_resource_name_lists(self):
78     # Dependencies to resources can be specified in a my_module "definition",
79     # and lists of resource names for the my_module are populated correctly.
80     text = (
81         "// blahblahblah\n"
82         "'use strict';\n"
83         "tvcm.require('dependency1');\n"
84         "tvcm.require('dependency2');\n"
85         "tvcm.requireStylesheet('myStylesheet');\n"
86         "tvcm.requireTemplate('myTemplate');\n")
87     stripped_text = strip_js_comments.strip_js_comments(text)
88     deps = js_module.Parse('module_name', stripped_text)
89     self.assertEquals(['myStylesheet'], deps.style_sheet_names)
90     self.assertEquals(['myTemplate'], deps.html_template_names)
91     self.assertEquals(['dependency1', 'dependency2'],
92                       deps.dependent_module_names)
93
94   def test_Parse_missing_semicolons(self):
95     # Semicolons can be omitted after tvcm.require statements.
96     text = (
97         "// blahblahblah\n"
98         "'use strict';\n"
99         "tvcm.require('dependency1')\n"
100         "tvcm.require('dependency2');\n"
101         "tvcm.requireStylesheet('myStylesheet')\n")
102     # Gross hack. We should separate parsing from the module object.
103     stripped_text = strip_js_comments.strip_js_comments(text)
104     deps = js_module.Parse('module_name', stripped_text)
105     self.assertEquals(['myStylesheet'], deps.style_sheet_names)
106     self.assertEquals(['dependency1', 'dependency2'],
107                       deps.dependent_module_names)
108
109   def test_Parse_with_deps_and_stylesheet_swapped(self):
110     # The dependencies can be specified in different orders.
111     text = (
112         "// blahblahblah\n"
113         "'use strict';\n"
114         "tvcm.require('dependency1');\n"
115         "tvcm.requireStylesheet('myStylesheet');\n"
116         "tvcm.require('dependency2');\n")
117     # Gross hack. We should separate parsing from the module object.
118     stripped_text = strip_js_comments.strip_js_comments(text)
119     deps = js_module.Parse('module_name', stripped_text)
120     self.assertEquals(['myStylesheet'], deps.style_sheet_names)
121     self.assertEquals(['dependency1', 'dependency2'],
122                       deps.dependent_module_names)
123
124   def test_Parse_empty_definition(self):
125     # If there are no tvcm.require statements, the lists of resource names
126     # for the module are all empty.
127     text = "// blahblahblah\n'use strict';"
128     # Gross hack. We should separate parsing from the module object.
129     stripped_text = strip_js_comments.strip_js_comments(text)
130     deps = js_module.Parse('module_name', stripped_text)
131     self.assertEquals([], deps.style_sheet_names)
132     self.assertEquals([], deps.dependent_module_names)
133
134   def test_Parse_with_commented_out_dependency(self):
135     # Commented-out tvcm.require statements don't count.
136     text = (
137         "// blahblahblah\n"
138         "'use strict';\n"
139         "tvcm.require('dependency1');\n"
140         "//tvcm.require('dependency2');\n")
141     # Gross hack. We should separate parsing from the module object.
142     stripped_text = strip_js_comments.strip_js_comments(text)
143     deps = js_module.Parse('module_name', stripped_text)
144     self.assertEquals([], deps.style_sheet_names)
145     self.assertEquals(['dependency1'], deps.dependent_module_names)
146
147   def test_Parse_with_multiline_comment_before(self):
148     # There can be long comments before the tvcm.require lines.
149     text = (
150         "// Copyright (c) 2012 The Chromium Authors. All rights reserved.\n"
151         "// Use of this source code is governed by a BSD-style license that"
152         " can be\n"
153         "// found in the LICENSE file.\n\n"
154         "'use strict';\n\n"
155         "/**\n"
156         " * @fileoverview TimelineView visualizes TRACE_EVENT events using\n"
157         " * the tracing.TimelineTrackView component and adds in selection\n"
158         " * summary and control buttons.\n"
159         " */\n"
160         "tvcm.requireStylesheet('timeline_view')\n"
161         "tvcm.require('timeline_track_view');\n"
162         "tvcm.require('timeline_analysis');\n"
163         "tvcm.require('overlay');\n"
164         "tvcm.require('trace_event_importer');\n"
165         "tvcm.require('linux_perf_importer');\n"
166         "tvcm.exportsTo('tracing', function() {\n")
167     # Gross hack. We should separate parsing from the module object.
168     stripped_text = strip_js_comments.strip_js_comments(text)
169     deps = js_module.Parse('module_name', stripped_text)
170     self.assertEquals(['timeline_view'], deps.style_sheet_names)
171     self.assertEquals(['timeline_track_view',
172                        'timeline_analysis',
173                        'overlay',
174                        'trace_event_importer',
175                        'linux_perf_importer'], deps.dependent_module_names)
176
177   def test_Parse_with_definition_in_comments(self):
178     # Statements inside multi-line comments are ignored.
179     text = (
180         "// SomeComment\n"
181         "/*\n"
182         " * All subclasses should depend on linux_perfParser, e.g.\n"
183         " *\n"
184         " * tvcm.require('linux_perfParser');\n"
185         " * tvcm.exportTo('tracing', function() { });\n"
186         " *\n"
187         " */\n"
188         "'use strict';\n"
189         "tvcm.require('dependency1');\n"
190         "tvcm.require('dependency2');\n")
191     # Gross hack. We should separate parsing from the module object.
192     stripped_text = strip_js_comments.strip_js_comments(text)
193     deps = js_module.Parse('module_name', stripped_text)
194     self.assertEquals([], deps.style_sheet_names)
195     self.assertEquals(['dependency1', 'dependency2'],
196                       deps.dependent_module_names)
197
198   def test_Parse_dependency_with_slashes_throws_error(self):
199     # An error should be thrown if a slash is found in a resource name.
200     text = "tvcm.require('foo/dependency1')"
201     # Gross hack. We should separate parsing from the module object.
202     self.assertRaises(module.DepsException,
203                       lambda: js_module.Parse('module_name', text))
204
205   def test_Parse_dependency_with_dots_is_okay(self):
206     # Module names can contain dots.
207     text = "tvcm.require('foo.dependency1')"
208     # Gross hack. We should separate parsing from the module object.
209     stripped_text = strip_js_comments.strip_js_comments(text)
210     deps = js_module.Parse('module_name', stripped_text)
211     self.assertEquals([], deps.style_sheet_names)
212     self.assertEquals(['foo.dependency1'],
213                       deps.dependent_module_names)
214
215
216 class IsJSModuleTests(unittest.TestCase):
217   """Test case for ValidateUsesStrictMode."""
218   def testPositive(self):
219     js = """'use strict';
220
221 tvcm.requireRawScript('gl-matrix/src/gl-matrix/vec4.js');
222
223 tvcm.exportTo('tvcm', function() {
224   var tmp_vec2 = vec2.create();"""
225     self.assertTrue(js_module.IsJSModule(js))
226
227   def testPositive(self):
228     js = """'use strict';
229
230 tvcm.require('tvcm.bbox2');
231
232 tvcm.unittest.testSuite('tvcm.bbox2_test', function() {
233   test('addVec2', function() {"""
234     self.assertTrue(js_module.IsJSModule(js))
235
236   def testNegative(self):
237     js = """/**
238  * @class 4x4 Matrix
239  * @name mat4
240  */
241 var mat4 = {};"""
242     self.assertFalse(js_module.IsJSModule(js))