PRESUBMIT.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright 2018 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. """Presubmit tests for android_webview/javatests/
  5. Runs various style checks before upload.
  6. """
  7. USE_PYTHON3 = True
  8. def CheckChangeOnUpload(input_api, output_api):
  9. results = []
  10. results.extend(_CheckAwJUnitTestRunner(input_api, output_api))
  11. results.extend(_CheckNoSkipCommandLineAnnotation(input_api, output_api))
  12. results.extend(_CheckNoSandboxedRendererSwitch(input_api, output_api))
  13. results.extend(_CheckNoDomUtils(input_api, output_api))
  14. return results
  15. def _CheckAwJUnitTestRunner(input_api, output_api):
  16. """Checks that new tests use the AwJUnit4ClassRunner instead of some other
  17. test runner. This is because WebView has special logic in the
  18. AwJUnit4ClassRunner.
  19. """
  20. run_with_pattern = input_api.re.compile(
  21. r'^@RunWith\((.*)\)$')
  22. correct_runner = 'AwJUnit4ClassRunner.class'
  23. errors = []
  24. def _FilterFile(affected_file):
  25. return input_api.FilterSourceFile(
  26. affected_file,
  27. files_to_skip=input_api.DEFAULT_FILES_TO_SKIP,
  28. files_to_check=[r'.*\.java$'])
  29. for f in input_api.AffectedSourceFiles(_FilterFile):
  30. for line_num, line in f.ChangedContents():
  31. match = run_with_pattern.search(line)
  32. if match and match.group(1) != correct_runner:
  33. errors.append("%s:%d" % (f.LocalPath(), line_num))
  34. results = []
  35. if errors:
  36. results.append(output_api.PresubmitPromptWarning("""
  37. android_webview/javatests/ should use the AwJUnit4ClassRunner test runner, not
  38. any other test runner (e.g., BaseJUnit4ClassRunner).
  39. """, errors))
  40. return results
  41. def _CheckNoSkipCommandLineAnnotation(input_api, output_api):
  42. """Checks that tests do not add @SkipCommandLineParameterization annotation.
  43. This was previously used to run the test in single-process-mode only (or,
  44. multi-process-mode only if used with
  45. @CommandLineFlags.Add(AwSwitches.WEBVIEW_SANDBOXED_RENDERER)). This is
  46. obsolete because we have dedicated annotations (@OnlyRunInSingleProcessMode
  47. and @OnlyRunInMultiProcessMode).
  48. """
  49. skip_command_line_annotation = input_api.re.compile(
  50. r'^\s*@SkipCommandLineParameterization.*$')
  51. errors = []
  52. def _FilterFile(affected_file):
  53. return input_api.FilterSourceFile(
  54. affected_file,
  55. files_to_skip=input_api.DEFAULT_FILES_TO_SKIP,
  56. files_to_check=[r'.*\.java$'])
  57. for f in input_api.AffectedSourceFiles(_FilterFile):
  58. for line_num, line in f.ChangedContents():
  59. match = skip_command_line_annotation.search(line)
  60. if match:
  61. errors.append("%s:%d" % (f.LocalPath(), line_num))
  62. results = []
  63. if errors:
  64. results.append(output_api.PresubmitPromptWarning("""
  65. android_webview/javatests/ should not use @SkipCommandLineParameterization to
  66. run in either multi-process or single-process only. Instead, use @OnlyRunIn.
  67. """, errors))
  68. return results
  69. def _CheckNoSandboxedRendererSwitch(input_api, output_api):
  70. """Checks that tests do not add the AwSwitches.WEBVIEW_SANDBOXED_RENDERER
  71. command line flag. Tests should instead use @OnlyRunIn(MULTI_PROCESS).
  72. """
  73. # This will not catch multi-line annotations (which are valid if adding
  74. # multiple switches), but is better than nothing (and avoids false positives).
  75. sandboxed_renderer_pattern = input_api.re.compile(
  76. r'^\s*@CommandLineFlags\.Add\(.*'
  77. r'\bAwSwitches\.WEBVIEW_SANDBOXED_RENDERER\b.*\)$')
  78. errors = []
  79. def _FilterFile(affected_file):
  80. return input_api.FilterSourceFile(
  81. affected_file,
  82. files_to_skip=input_api.DEFAULT_FILES_TO_SKIP,
  83. files_to_check=[r'.*\.java$'])
  84. for f in input_api.AffectedSourceFiles(_FilterFile):
  85. for line_num, line in f.ChangedContents():
  86. match = sandboxed_renderer_pattern.search(line)
  87. if match:
  88. errors.append("%s:%d" % (f.LocalPath(), line_num))
  89. results = []
  90. if errors:
  91. results.append(output_api.PresubmitPromptWarning("""
  92. android_webview/javatests/ should not use AwSwitches.WEBVIEW_SANDBOXED_RENDERER
  93. to run in multi-process only. Instead, use @OnlyRunIn(MULTI_PROCESS).
  94. """, errors))
  95. return results
  96. def _CheckNoDomUtils(input_api, output_api):
  97. """Checks that tests prefer JSUtils.clickNodeWithUserGesture() over
  98. DOMUtils.clickNode().
  99. """
  100. dom_utils_pattern = input_api.re.compile(r'DOMUtils\.clickNode\(')
  101. errors = []
  102. def _FilterFile(affected_file):
  103. return input_api.FilterSourceFile(
  104. affected_file,
  105. files_to_skip=input_api.DEFAULT_FILES_TO_SKIP,
  106. files_to_check=[r'.*\.java$'])
  107. for f in input_api.AffectedSourceFiles(_FilterFile):
  108. for line_num, line in f.ChangedContents():
  109. m = dom_utils_pattern.search(line)
  110. if m:
  111. errors.append("%s:%d" % (f.LocalPath(), line_num))
  112. results = []
  113. if errors:
  114. results.append(output_api.PresubmitPromptWarning("""
  115. DOMUtils.clickNode() has been observed to cause flakiness in WebView tests.
  116. Prefer using JSUtils.clickNodeWithUserGesture() as a more reliable replacement
  117. where possible. This is a "soft" warning, so you can bypass this if
  118. DOMUtils.clickNode() is the only way.
  119. """, errors))
  120. return results