PageRenderTime 73ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/chrome/android/java/src/PRESUBMIT_test.py

https://github.com/chromium/chromium
Python | 231 lines | 187 code | 22 blank | 22 comment | 1 complexity | 67b60dd53362e8b22ec63005702874a4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. #!/usr/bin/env python
  2. # Copyright 2018 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 os
  6. import sys
  7. import unittest
  8. import PRESUBMIT
  9. sys.path.append(os.path.abspath(os.path.dirname(os.path.abspath(__file__))
  10. + '/../../../..'))
  11. from PRESUBMIT_test_mocks import MockFile, MockInputApi, MockOutputApi
  12. class CheckNotificationConstructors(unittest.TestCase):
  13. """Test the _CheckNotificationConstructors presubmit check."""
  14. def testTruePositives(self):
  15. """Examples of when Notification.Builder use is correctly flagged."""
  16. mock_input = MockInputApi()
  17. mock_input.files = [
  18. MockFile('path/One.java', ['new Notification.Builder()']),
  19. MockFile('path/Two.java', ['new NotificationCompat.Builder()']),
  20. ]
  21. errors = PRESUBMIT._CheckNotificationConstructors(
  22. mock_input, MockOutputApi())
  23. self.assertEqual(1, len(errors))
  24. self.assertEqual(2, len(errors[0].items))
  25. self.assertIn('One.java', errors[0].items[0])
  26. self.assertIn('Two.java', errors[0].items[1])
  27. def testFalsePositives(self):
  28. """Examples of when Notification.Builder should not be flagged."""
  29. mock_input = MockInputApi()
  30. mock_input.files = [
  31. MockFile(
  32. 'chrome/android/java/src/org/chromium/chrome/browser/notifications/'
  33. 'ChromeNotificationWrapperBuilder.java',
  34. ['new Notification.Builder()']),
  35. MockFile(
  36. 'chrome/android/java/src/org/chromium/chrome/browser/notifications/'
  37. 'ChromeNotificationWrapperCompatBuilder.java',
  38. ['new NotificationCompat.Builder()']),
  39. MockFile('path/One.java', ['Notification.Builder']),
  40. MockFile('path/Two.java', ['// do not: new Notification.Builder()']),
  41. MockFile('path/Three.java',
  42. ['/** NotificationWrapperBuilder',
  43. ' * replaces: new Notification.Builder()']),
  44. MockFile('path/PRESUBMIT.py', ['new Notification.Builder()']),
  45. MockFile('path/Four.java', ['new NotificationCompat.Builder()'],
  46. action='D'),
  47. ]
  48. errors = PRESUBMIT._CheckNotificationConstructors(
  49. mock_input, MockOutputApi())
  50. self.assertEqual(0, len(errors))
  51. class CheckAlertDialogBuilder(unittest.TestCase):
  52. """Test the _CheckAlertDialogBuilder presubmit check."""
  53. def testTruePositives(self):
  54. """Examples of when AlertDialog.Builder use is correctly flagged."""
  55. mock_input = MockInputApi()
  56. mock_input.files = [
  57. MockFile('path/One.java', ['new AlertDialog.Builder()']),
  58. MockFile('path/Two.java', ['new AlertDialog.Builder(context);']),
  59. ]
  60. errors = PRESUBMIT._CheckAlertDialogBuilder(mock_input, MockOutputApi())
  61. self.assertEqual(1, len(errors))
  62. self.assertEqual(2, len(errors[0].items))
  63. self.assertIn('One.java', errors[0].items[0])
  64. self.assertIn('Two.java', errors[0].items[1])
  65. def testFalsePositives(self):
  66. """Examples of when AlertDialog.Builder should not be flagged."""
  67. mock_input = MockInputApi()
  68. mock_input.files = [
  69. MockFile('path/One.java', ['AlertDialog.Builder']),
  70. MockFile('path/Two.java', ['// do not: new AlertDialog.Builder()']),
  71. MockFile('path/Three.java',
  72. ['/** ChromeAlertDialogBuilder',
  73. ' * replaces: new AlertDialog.Builder()']),
  74. MockFile('path/PRESUBMIT.py', ['new AlertDialog.Builder()']),
  75. MockFile('path/Four.java', ['new AlertDialog.Builder()'],
  76. action='D'),
  77. ]
  78. errors = PRESUBMIT._CheckAlertDialogBuilder(
  79. mock_input, MockOutputApi())
  80. self.assertEqual(0, len(errors))
  81. def testFailure_WrongBuilderCheck(self):
  82. """Use of AppCompat AlertDialog.Builder is correctly flagged."""
  83. mock_input = MockInputApi()
  84. mock_input.files = [
  85. MockFile('path/One.java',
  86. ['import android.support.v7.app.AlertDialog;',
  87. 'new AlertDialog.Builder()']),
  88. MockFile('path/Two.java',
  89. ['import android.app.AlertDialog;',
  90. 'new AlertDialog.Builder(context);']),
  91. ]
  92. errors = PRESUBMIT._CheckAlertDialogBuilder(
  93. mock_input, MockOutputApi())
  94. self.assertEqual(2, len(errors))
  95. self.assertEqual(1, len(errors[1].items))
  96. self.assertIn('One.java', errors[1].items[0])
  97. def testSuccess_WrongBuilderCheck(self):
  98. """Use of OS-dependent AlertDialog should not be flagged."""
  99. mock_input = MockInputApi()
  100. mock_input.files = [
  101. MockFile('path/One.java',
  102. ['import android.app.AlertDialog;',
  103. 'new AlertDialog.Builder()']),
  104. MockFile('path/Two.java',
  105. ['import android.app.AlertDialog;',
  106. 'new AlertDialog.Builder(context);']),
  107. ]
  108. errors = PRESUBMIT._CheckAlertDialogBuilder(mock_input, MockOutputApi())
  109. self.assertEqual(1, len(errors))
  110. self.assertEqual(2, len(errors[0].items))
  111. class CheckCompatibleAlertDialogBuilder(unittest.TestCase):
  112. """Test the _CheckCompatibleAlertDialogBuilder presubmit check."""
  113. def testFailure(self):
  114. """Use of CompatibleAlertDialogBuilder use is correctly flagged."""
  115. mock_input = MockInputApi()
  116. mock_input.files = [
  117. MockFile('path/One.java',
  118. ['import '
  119. 'org.chromium.ui.UiUtils.CompatibleAlertDialogBuilder;',
  120. 'new CompatibleAlertDialogBuilder()',
  121. 'A new line to make sure there is no duplicate error.']),
  122. MockFile('path/Two.java',
  123. ['new UiUtils.CompatibleAlertDialogBuilder()']),
  124. MockFile('path/Three.java',
  125. ['new UiUtils',
  126. '.CompatibleAlertDialogBuilder(context)']),
  127. MockFile('path/Four.java',
  128. ['new UiUtils',
  129. ' .CompatibleAlertDialogBuilder()']),
  130. ]
  131. errors = PRESUBMIT._CheckCompatibleAlertDialogBuilder(
  132. mock_input, MockOutputApi())
  133. self.assertEqual(1, len(errors))
  134. self.assertEqual(4, len(errors[0].items))
  135. self.assertIn('One.java', errors[0].items[0])
  136. self.assertIn('Two.java', errors[0].items[1])
  137. self.assertIn('Three.java', errors[0].items[2])
  138. self.assertIn('Four.java', errors[0].items[3])
  139. def testSuccess(self):
  140. """Examples of when AlertDialog.Builder should not be flagged."""
  141. mock_input = MockInputApi()
  142. mock_input.files = [
  143. MockFile('chrome/android/java/src/org/chromium/chrome/browser/payments/'
  144. 'AndroidPaymentApp.java',
  145. ['new UiUtils.CompatibleAlertDialogBuilder()']),
  146. MockFile('path/One.java', ['UiUtils.CompatibleAlertDialogBuilder']),
  147. MockFile('path/Two.java',
  148. ['// do not: new UiUtils.CompatibleAlertDialogBuilder']),
  149. MockFile('path/Three.java',
  150. ['/** ChromeAlertDialogBuilder',
  151. ' * replaces: new UiUtils.CompatibleAlertDialogBuilder()']),
  152. MockFile('path/PRESUBMIT.py',
  153. ['new UiUtils.CompatibleAlertDialogBuilder()']),
  154. MockFile('path/Four.java',
  155. ['new UiUtils.CompatibleAlertDialogBuilder()'],
  156. action='D'),
  157. ]
  158. errors = PRESUBMIT._CheckCompatibleAlertDialogBuilder(
  159. mock_input, MockOutputApi())
  160. self.assertEqual(0, len(errors))
  161. class CheckBundleUtilsIdentifierName(unittest.TestCase):
  162. """Test the _CheckBundleUtilsIdentifierName presubmit check."""
  163. def testFailure(self):
  164. """
  165. BundleUtils.getIdentifierName() without a String literal is flagged.
  166. """
  167. mock_input = MockInputApi()
  168. mock_input.files = [
  169. MockFile('path/One.java',
  170. [
  171. 'BundleUtils.getIdentifierName(foo)',
  172. 'A new line to make sure there is no duplicate error.']),
  173. MockFile('path/Two.java',
  174. ['BundleUtils.getIdentifierName( foo)']),
  175. MockFile('path/Three.java',
  176. ['BundleUtils.getIdentifierName(',
  177. ' bar)']),
  178. ]
  179. errors = PRESUBMIT._CheckBundleUtilsIdentifierName(
  180. mock_input, MockOutputApi())
  181. self.assertEqual(1, len(errors))
  182. self.assertEqual(3, len(errors[0].items))
  183. self.assertIn('One.java', errors[0].items[0])
  184. self.assertIn('Two.java', errors[0].items[1])
  185. self.assertIn('Three.java', errors[0].items[2])
  186. def testSuccess(self):
  187. """
  188. Examples of when BundleUtils.getIdentifierName() should not be flagged.
  189. """
  190. mock_input = MockInputApi()
  191. mock_input.files = [
  192. MockFile('path/One.java',
  193. [
  194. 'BundleUtils.getIdentifierName("foo")',
  195. 'A new line.']),
  196. MockFile('path/Two.java',
  197. ['BundleUtils.getIdentifierName( "foo")']),
  198. MockFile('path/Three.java',
  199. ['BundleUtils.getIdentifierName(',
  200. ' "bar")']),
  201. MockFile('path/Four.java',
  202. [' super(BundleUtils.getIdentifierName(',
  203. '"bar"))']),
  204. ]
  205. errors = PRESUBMIT._CheckBundleUtilsIdentifierName(
  206. mock_input, MockOutputApi())
  207. self.assertEqual(0, len(errors))
  208. if __name__ == '__main__':
  209. unittest.main()