PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/browser/resources/optimize_webui_test.py

http://github.com/chromium/chromium
Python | 221 lines | 203 code | 12 blank | 6 comment | 5 complexity | 3e6f195a7eba532bf47873825bb749c2 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  1. #!/usr/bin/env python
  2. # Copyright 2017 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import optimize_webui
  6. import os
  7. import shutil
  8. import tempfile
  9. import unittest
  10. _HERE_DIR = os.path.dirname(__file__)
  11. class OptimizeWebUiTest(unittest.TestCase):
  12. def setUp(self):
  13. self._out_folder = None
  14. self._tmp_dirs = []
  15. self._tmp_src_dir = None
  16. def tearDown(self):
  17. for tmp_dir in self._tmp_dirs:
  18. shutil.rmtree(tmp_dir)
  19. def _write_file_to_src_dir(self, file_path, file_contents):
  20. if not self._tmp_src_dir:
  21. self._tmp_src_dir = self._create_tmp_dir()
  22. file_path_normalized = os.path.normpath(os.path.join(self._tmp_src_dir,
  23. file_path))
  24. file_dir = os.path.dirname(file_path_normalized)
  25. if not os.path.exists(file_dir):
  26. os.makedirs(file_dir)
  27. with open(file_path_normalized, 'w') as tmp_file:
  28. tmp_file.write(file_contents)
  29. def _create_tmp_dir(self):
  30. # TODO(dbeam): support cross-drive paths (i.e. d:\ vs c:\).
  31. tmp_dir = tempfile.mkdtemp(dir=_HERE_DIR)
  32. self._tmp_dirs.append(tmp_dir)
  33. return tmp_dir
  34. def _read_out_file(self, file_name):
  35. assert self._out_folder
  36. return open(os.path.join(self._out_folder, file_name), 'r').read()
  37. def _run_optimize(self, input_args):
  38. assert not self._out_folder
  39. self._out_folder = self._create_tmp_dir()
  40. # TODO(dbeam): make it possible to _run_optimize twice? Is that useful?
  41. args = input_args + [
  42. '--depfile', os.path.join(self._out_folder, 'depfile.d'),
  43. '--host', 'fake-host',
  44. '--input', self._tmp_src_dir,
  45. '--out_folder', self._out_folder,
  46. ]
  47. optimize_webui.main(args)
  48. def _write_files_to_src_dir(self):
  49. self._write_file_to_src_dir('element.html', '<div>got here!</div>')
  50. self._write_file_to_src_dir('element.js', "alert('yay');")
  51. self._write_file_to_src_dir('element_in_dir/element_in_dir.html',
  52. '<script src="element_in_dir.js">')
  53. self._write_file_to_src_dir('element_in_dir/element_in_dir.js',
  54. "alert('hello from element_in_dir');")
  55. self._write_file_to_src_dir('ui.html', '''
  56. <link rel="import" href="element.html">
  57. <link rel="import" href="element_in_dir/element_in_dir.html">
  58. <script src="element.js"></script>
  59. ''')
  60. def _write_v3_files_to_src_dir(self):
  61. self._write_file_to_src_dir('element.js', "alert('yay');")
  62. self._write_file_to_src_dir('element_in_dir/element_in_dir.js',
  63. "alert('hello from element_in_dir');")
  64. self._write_file_to_src_dir('ui.js', '''
  65. import './element.js';
  66. import './element_in_dir/element_in_dir.js';
  67. ''')
  68. self._write_file_to_src_dir('ui.html', '''
  69. <script type="module" src="ui.js"></script>
  70. ''')
  71. def _write_v3_files_with_resources_to_src_dir(self):
  72. resources_path = os.path.join(
  73. _HERE_DIR.replace('\\', '/'), 'gen', 'ui', 'webui', 'resources', 'js',
  74. 'fake_resource.m.js')
  75. os.makedirs(os.path.dirname(resources_path))
  76. self._tmp_dirs.append('gen')
  77. with open(resources_path, 'w') as tmp_file:
  78. tmp_file.write("alert('hello from shared resource');")
  79. self._write_file_to_src_dir('element.js', '''
  80. import 'chrome://resources/js/action_link.js';
  81. alert('yay');
  82. ''')
  83. self._write_file_to_src_dir('element_in_dir/element_in_dir.js', '''
  84. import {foo} from 'chrome://resources/js/fake_resource.m.js';
  85. import '../strings.m.js';
  86. alert('hello from element_in_dir');
  87. ''')
  88. self._write_file_to_src_dir('ui.js', '''
  89. import 'chrome://fake-host/strings.m.js';
  90. import './element.js';
  91. import './element_in_dir/element_in_dir.js';
  92. ''')
  93. self._write_file_to_src_dir('ui.html', '''
  94. <script type="module" src="ui.js"></script>
  95. ''')
  96. def _check_output_html(self, out_html):
  97. self.assertNotIn('element.html', out_html)
  98. self.assertNotIn('element.js', out_html)
  99. self.assertNotIn('element_in_dir.html', out_html)
  100. self.assertNotIn('element_in_dir.js', out_html)
  101. self.assertIn('got here!', out_html)
  102. def _check_output_js(self, output_js_name):
  103. output_js = self._read_out_file(output_js_name)
  104. self.assertIn('yay', output_js)
  105. self.assertIn('hello from element_in_dir', output_js)
  106. def _check_output_depfile(self, has_html):
  107. depfile_d = self._read_out_file('depfile.d')
  108. self.assertIn('element.js', depfile_d)
  109. self.assertIn(os.path.normpath('element_in_dir/element_in_dir.js'),
  110. depfile_d)
  111. if (has_html):
  112. self.assertIn('element.html', depfile_d)
  113. self.assertIn(os.path.normpath('element_in_dir/element_in_dir.html'),
  114. depfile_d)
  115. def testSimpleOptimize(self):
  116. self._write_files_to_src_dir()
  117. args = [
  118. '--html_in_files', 'ui.html',
  119. '--html_out_files', 'fast.html',
  120. '--js_out_files', 'fast.js',
  121. ]
  122. self._run_optimize(args)
  123. fast_html = self._read_out_file('fast.html')
  124. self._check_output_html(fast_html)
  125. self.assertIn('<script src="fast.js"></script>', fast_html)
  126. self._check_output_js('fast.js')
  127. self._check_output_depfile(True)
  128. def testV3SimpleOptimize(self):
  129. self._write_v3_files_to_src_dir()
  130. args = [
  131. '--js_module_in_files', 'ui.js',
  132. '--js_out_files', 'ui.rollup.js',
  133. ]
  134. self._run_optimize(args)
  135. self._check_output_js('ui.rollup.js')
  136. self._check_output_depfile(False)
  137. def testV3OptimizeWithResources(self):
  138. self._write_v3_files_with_resources_to_src_dir()
  139. args = [
  140. '--js_module_in_files', 'ui.js',
  141. '--js_out_files', 'ui.rollup.js',
  142. ]
  143. self._run_optimize(args)
  144. ui_rollup_js = self._read_out_file('ui.rollup.js')
  145. self.assertIn('yay', ui_rollup_js)
  146. self.assertIn('hello from element_in_dir', ui_rollup_js)
  147. self.assertIn('hello from shared resource', ui_rollup_js)
  148. depfile_d = self._read_out_file('depfile.d')
  149. self.assertIn('element.js', depfile_d)
  150. self.assertIn(os.path.normpath('element_in_dir/element_in_dir.js'),
  151. depfile_d)
  152. self.assertIn(
  153. os.path.normpath('../../../../ui/webui/resources/js/action_link.js'),
  154. depfile_d)
  155. self.assertIn(
  156. os.path.normpath('../gen/ui/webui/resources/js/fake_resource.m.js'),
  157. depfile_d)
  158. def testV3MultiBundleOptimize(self):
  159. self._write_v3_files_to_src_dir()
  160. self._write_file_to_src_dir('lazy_element.js',
  161. "alert('hello from lazy_element');")
  162. self._write_file_to_src_dir('lazy.js', '''
  163. import './lazy_element.js';
  164. import './element_in_dir/element_in_dir.js';
  165. ''')
  166. args = [
  167. '--js_module_in_files', 'ui.js', 'lazy.js',
  168. '--js_out_files', 'ui.rollup.js', 'lazy.rollup.js', 'shared.rollup.js',
  169. ]
  170. self._run_optimize(args)
  171. # Check that the shared element is in the shared bundle and the non-shared
  172. # elements are in the individual bundles.
  173. ui_js = self._read_out_file('ui.rollup.js')
  174. self.assertIn('yay', ui_js)
  175. self.assertNotIn('hello from lazy_element', ui_js)
  176. self.assertNotIn('hello from element_in_dir', ui_js)
  177. lazy_js = self._read_out_file('lazy.rollup.js')
  178. self.assertNotIn('yay', lazy_js)
  179. self.assertIn('hello from lazy_element', lazy_js)
  180. self.assertNotIn('hello from element_in_dir', lazy_js)
  181. shared_js = self._read_out_file('shared.rollup.js')
  182. self.assertNotIn('yay', shared_js)
  183. self.assertNotIn('hello from lazy_element', shared_js)
  184. self.assertIn('hello from element_in_dir', shared_js)
  185. # All 3 JS files should be in the depfile.
  186. self._check_output_depfile(False)
  187. depfile_d = self._read_out_file('depfile.d')
  188. self.assertIn('lazy_element.js', depfile_d)
  189. if __name__ == '__main__':
  190. unittest.main()