def testValidReference(self): lines = ['<TextView', 'android:textColor="@color/color1" />'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/test.xml', lines)] errors = PRESUBMIT._CheckColorReferences(mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testFailure(self): input_api = MockInputApi() test_data = [ ('invalid_json_1.json', ['{ x }'], 'Expecting property name:'), ('invalid_json_2.json', ['// Hello world!', '{ "hello": "world }'], 'Unterminated string starting at:'), ('invalid_json_3.json', ['{ "a": "b", "c": "d", }'], 'Expecting property name:'), ('invalid_json_4.json', ['{ "a": "b" "c": "d" }'], 'Expecting , delimiter:'), ] input_api.files = [MockFile(filename, contents) for (filename, contents, _) in test_data] for (filename, _, expected_error) in test_data: actual_error = PRESUBMIT._GetJSONParseError(input_api, filename) self.assertTrue(expected_error in str(actual_error), "'%s' not found in '%s'" % (expected_error, actual_error))
def testCheckAndroidTestAnnotationUsage(self): mock_input_api = MockInputApi() mock_output_api = MockOutputApi() mock_input_api.files = [ MockAffectedFile('LalaLand.java', [ 'random stuff' ]), MockAffectedFile('CorrectUsage.java', [ 'import org.junit.ABC', 'import org.junit.XYZ;', ]), MockAffectedFile('UsedDeprecatedJUnit.java', [ 'import junit.framework.*;', ]), MockAffectedFile('UsedDeprecatedJUnitAssert.java', [ 'import junit.framework.Assert;', ]), ] msgs = PRESUBMIT._CheckAndroidTestJUnitFrameworkImport( mock_input_api, mock_output_api) self.assertEqual(1, len(msgs), 'Expected %d items, found %d: %s' % (1, len(msgs), msgs)) self.assertEqual(2, len(msgs[0].items), 'Expected %d items, found %d: %s' % (2, len(msgs[0].items), msgs[0].items)) self.assertTrue('UsedDeprecatedJUnit.java:1' in msgs[0].items, 'UsedDeprecatedJUnit.java not found in errors') self.assertTrue('UsedDeprecatedJUnitAssert.java:1' in msgs[0].items, 'UsedDeprecatedJUnitAssert not found in errors')
def testSucess(self): lines = ['xmlns:app="http://schemas.android.com/apk/res-auto"'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/file.xml', lines)] errors = PRESUBMIT._CheckXmlNamespacePrefixes( mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testSucess(self): lines = ['<color name="color1">#61000000</color>', '<color name="color1">#FFFFFF</color>'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/colors.xml', lines)] errors = PRESUBMIT._CheckDuplicateColors(mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testNoEatComments(self): input_api = MockInputApi() file_with_comments = 'file_with_comments.json' contents_with_comments = ['// This is a comment.', '{', ' "key1": ["value1", "value2"],', ' "key2": 3 // This is an inline comment.', '}' ] file_without_comments = 'file_without_comments.json' contents_without_comments = ['{', ' "key1": ["value1", "value2"],', ' "key2": 3', '}' ] input_api.files = [MockFile(file_with_comments, contents_with_comments), MockFile(file_without_comments, contents_without_comments)] self.assertEqual('No JSON object could be decoded', str(PRESUBMIT._GetJSONParseError(input_api, file_with_comments, eat_comments=False))) self.assertEqual(None, PRESUBMIT._GetJSONParseError(input_api, file_without_comments, eat_comments=False))
def testTypicalNotMatchedChange(self): diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile("some/path/foo.cc", diff_cc)] warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual("warning", warnings[0].type)
def _runChecks(self, files, exists=lambda f: True): input_api = MockInputApi() input_api.os_path.exists = exists input_api.files = [MockFile(f, '') for f in files] output_api = MockOutputApi() checker = ExternsChecker(input_api, output_api, self.API_PAIRS) return checker.RunChecks()
def testSingletonInCC(self): diff_cc = ['Foo* foo = Singleton<Foo>::get();'] mock_input_api = MockInputApi() mock_input_api.files = [MockAffectedFile('some/path/foo.cc', diff_cc)] warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api, MockOutputApi()) self.assertEqual(0, len(warnings))
def testFailure_Style(self): lines = [ '<resource>', '<style name="TestTextAppearance">', '<item name="android:textColor">@color/default_text_color_link</item>', '<item name="android:textSize">14sp</item>', '<item name="android:textStyle">bold</item>', '<item name="android:fontFamily">some-font</item>', '<item name="android:textAllCaps">true</item>', '</style>', '</resource>'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/test.xml', lines)] errors = PRESUBMIT._CheckTextAppearance(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(5, len(errors[0].items)) self.assertEqual( ' chrome/path/test.xml:2 contains attribute android:textColor', errors[0].items[0].splitlines()[0]) self.assertEqual( ' chrome/path/test.xml:2 contains attribute android:textSize', errors[0].items[1].splitlines()[0]) self.assertEqual( ' chrome/path/test.xml:2 contains attribute android:textStyle', errors[0].items[2].splitlines()[0]) self.assertEqual( ' chrome/path/test.xml:2 contains attribute android:fontFamily', errors[0].items[3].splitlines()[0]) self.assertEqual( ' chrome/path/test.xml:2 contains attribute android:textAllCaps', errors[0].items[4].splitlines()[0])
def testColorFormatIgnoredFile(self): lines = ['<color name="color1">#61000000</color>', '<color name="color2">#FFFFFF</color>', '<color name="color3">#CCC</color>'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/test.java', lines)] errors = PRESUBMIT._CheckColorFormat(mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testArrowsAllowedInChromeCode(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile('chrome/browser/resources/blah.js', 'arrow => OK here'), ] warnings = PRESUBMIT._CheckForRiskyJsFeatures( mock_input_api, MockOutputApi()) self.assertEqual(0, len(warnings))
def _testChange(self, lines): mock_input_api = MockInputApi() mock_input_api.files = [MockFile('path/test.html', lines)] action_xml_path = self._createActionXMLFile() return PRESUBMIT.CheckUserActionUpdate(mock_input_api, MockOutputApi(), action_xml_path)
def testUserMetricsActionInActions(self): input_api = MockInputApi() file_with_user_action = "file_with_user_action.cc" contents_with_user_action = ['base::UserMetricsAction("AboutChrome")'] input_api.files = [MockFile(file_with_user_action, contents_with_user_action)] self.assertEqual([], PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()))
def testGoodFiles(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('other/path/qux.h', ''), MockFile('other/path/qux.cc', ''), ] results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) self.assertEqual(0, len(results))
def testNoDeprecatedCompiledResourcsGYP(self): mock_input_api = MockInputApi() mock_input_api.files = [MockFile("some/js/compiled_resources.gyp", [])] errors = PRESUBMIT._CheckNoDeprecatedCompiledResourcesGYP(mock_input_api, MockOutputApi()) self.assertEquals(1, len(errors)) mock_input_api.files = [MockFile("some/js/compiled_resources2.gyp", [])] errors = PRESUBMIT._CheckNoDeprecatedCompiledResourcesGYP(mock_input_api, MockOutputApi()) self.assertEquals(0, len(errors))
def testVectorDrawbleIgnored(self): lines = ['<vector', 'tools:targetApi="21"', 'android:fillColor="#CCCCCC">', '</vector>'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/test.xml', lines)] errors = PRESUBMIT._CheckColorReferences(mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testAllowInComment(self): input_api = MockInputApi() input_api.files = [ MockFile('content/file.cc', ['char* host = "https://www.aol.com"; // google.com']) ] warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( input_api, MockOutputApi()) self.assertEqual(0, len(warnings))
def testTypicalCorrectlyMatchedChange(self): diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] diff_xml = ['<histogram name="Bla.Foo.Dummy"> </histogram>'] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile("some/path/foo.cc", diff_cc), MockFile("tools/metrics/histograms/histograms.xml", diff_xml), ] warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, MockOutputApi()) self.assertEqual(0, len(warnings))
def testCheckHeadersOnly(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile('somewhere/file.cc', [ 'class DummyClass;' ]) ] warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api, MockOutputApi()) self.assertEqual(0, len(warnings))
def testActionXMLChanged(self): mock_input_api = MockInputApi() lines = ['<input id="testinput" pref="testpref"', 'metric="validaction" type="checkbox" dialog-pref>'] mock_input_api.files = [MockFile('path/valid.html', lines)] mock_input_api.change = MockChange(['path/valid.html','actions.xml']) action_xml_path = self._createActionXMLFile() self.assertEqual([], PRESUBMIT.CheckUserActionUpdate(mock_input_api, MockOutputApi(), action_xml_path))
def testFailure(self): lines = ['xmlns:chrome="http://schemas.android.com/apk/res-auto"'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/file.xml', lines)] errors = PRESUBMIT._CheckXmlNamespacePrefixes( mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(1, len(errors[0].items)) self.assertEqual(' chrome/path/file.xml:1', errors[0].items[0].splitlines()[0])
def testInvalidReference(self): lines = ['<TextView', 'android:textColor="#FFFFFF" />'] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/test.xml', lines)] errors = PRESUBMIT._CheckColorReferences(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(1, len(errors[0].items)) self.assertEqual(' chrome/path/test.xml:2', errors[0].items[0].splitlines()[0])
def testAllowAnswerUrlParam(self): input_api = MockInputApi() input_api.files = [ MockFile('somewhere/file.cc', ['char* host = ' ' "https://support.google.com/chrome/?p=cpn_crash_reports";']), ] warnings = PRESUBMIT._CheckGoogleSupportAnswerUrl( input_api, MockOutputApi()) self.assertEqual(0, len(warnings))
def testRelativeIncludeNonWebKitProducesError(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile('test.cpp', ['#include "../header.h"']), ] mock_output_api = MockOutputApi() errors = PRESUBMIT._CheckForRelativeIncludes( mock_input_api, mock_output_api) self.assertEqual(1, len(errors))
def testNonCppFileIgnored(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile('test.py', '#include "../header.h"'), ] mock_output_api = MockOutputApi() errors = PRESUBMIT._CheckForRelativeIncludes( mock_input_api, mock_output_api) self.assertEqual(0, len(errors))
def testConstLetWarningIos9Code(self): mock_input_api = MockInputApi() mock_output_api = MockOutputApi() mock_input_api.files = [ MockAffectedFile('components/blah.js', [" const foo = 'bar';"]), MockAffectedFile('ui/webui/resources/blah.js', [" let foo = 3;"]), ] warnings = PRESUBMIT._CheckForRiskyJsFeatures( mock_input_api, mock_output_api) self.assertEqual(2, len(warnings))
def testCheckAndroidTestAnnotationUsage(self): mock_input_api = MockInputApi() mock_output_api = MockOutputApi() mock_input_api.files = [ MockAffectedFile('LalaLand.java', [ 'random stuff' ]), MockAffectedFile('CorrectTest.java', [ '@RunWith(ABC.class);' 'public class CorrectTest {', '}', ]), MockAffectedFile('HistoricallyIncorrectTest.java', [ 'public class Test extends BaseCaseA {', '}', ], old_contents=[ 'public class Test extends BaseCaseB {', '}', ]), MockAffectedFile('CorrectTestWithInterface.java', [ '@RunWith(ABC.class);' 'public class CorrectTest implement Interface {', '}', ]), MockAffectedFile('IncorrectTest.java', [ 'public class IncorrectTest extends TestCase {', '}', ]), MockAffectedFile('IncorrectTestWithInterface.java', [ 'public class Test implements X extends BaseClass {', '}', ]), MockAffectedFile('IncorrectTestMultiLine.java', [ 'public class Test implements X, Y, Z', ' extends TestBase {', '}', ]), ] msgs = PRESUBMIT._CheckAndroidTestJUnitInheritance( mock_input_api, mock_output_api) self.assertEqual(1, len(msgs), 'Expected %d items, found %d: %s' % (1, len(msgs), msgs)) self.assertEqual(3, len(msgs[0].items), 'Expected %d items, found %d: %s' % (3, len(msgs[0].items), msgs[0].items)) self.assertTrue('IncorrectTest.java:1' in msgs[0].items, 'IncorrectTest not found in errors') self.assertTrue('IncorrectTestWithInterface.java:1' in msgs[0].items, 'IncorrectTestWithInterface not found in errors') self.assertTrue('IncorrectTestMultiLine.java:2' in msgs[0].items, 'IncorrectTestMultiLine not found in errors')
def testBaseMacrosInHeadersBad(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile("foo.h", self._make_h("not_macros", "ASSIGN")), MockAffectedFile("bar.h", self._make_h("not_macros", "COPY")), MockAffectedFile("baz.h", self._make_h("not_macros", "COPY_AND_ASSIGN")), MockAffectedFile("qux.h", self._make_h("not_macros", "EVIL")), ] warnings = PRESUBMIT._CheckBaseMacrosInHeaders(mock_input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual(4, len(warnings[0].items))
def testWarnOnAssignedLiterals(self): input_api = MockInputApi() input_api.files = [ MockFile("content/file.cc", ['char* host = "https://www.google.com";']), MockFile("content/file.cc", ['char* host = "https://www.googleapis.com";']), MockFile("content/file.cc", ['char* host = "https://clients1.google.com";']), ] warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers(input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual(3, len(warnings[0].items))
def testReferenceInSemanticColors(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockFile(helpers.COLOR_PALETTE_PATH, ['<resources><color name="a">#f0f0f0</color></resources>']), MockFile('ui/android/java/res/values/semantic_colors_non_adaptive.xml', [ '<color name="b">@color/hello<color>', '<color name="c">@color/a<color>' ]), MockFile('ui/android/java/res/values/semantic_colors_adaptive.xml', ['<color name="c">@color/a<color>']) ] errors = checkxmlstyle._CheckSemanticColorsReferences( mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors))
def testCheckInternalHeaderWithBlinkMojo(self): """This verifies that _CheckForWrongMojomIncludes accepts -blink mojo headers in blink internal files. """ mock_input_api = MockInputApi() potentially_bad_content = '#include "public/platform/modules/cache_storage.mojom-blink.h"' mock_input_api.files = [ MockAffectedFile('third_party/WebKit/Source/public/AHeader.h', [potentially_bad_content], None) ] # Access to a protected member _CheckForWrongMojomIncludes # pylint: disable=W0212 errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api, MockOutputApi()) self.assertEquals([], errors)
def testCheckChangeOnUploadWithEmptyAffectedFileList(self, _): """This verifies that CheckChangeOnUpload will skip calling check-webkit-style if the affected file list is empty. """ diff_file_chromium1_h = ['some diff'] diff_file_chromium2_h = ['another diff'] mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile('first_file_chromium.h', diff_file_chromium1_h), MockAffectedFile('second_file_chromium.h', diff_file_chromium2_h) ] # Access to a protected member _CheckStyle # pylint: disable=W0212 PRESUBMIT._CheckStyle(mock_input_api, MockOutputApi()) # pylint: disable=E1101 subprocess.Popen.assert_not_called()
def testReferenceInColorPalette(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockFile(helpers.COLOR_PALETTE_PATH, ['<resources><color name="foo">#f0f0f0</color></resources>']), MockFile('ui/android/java/res/values/semantic_colors_adaptive.xml', ['<color name="b">@color/foo<color>']), MockFile('ui/android/java/res/values/colors.xml', [ '<color name="c">@color/b</color>', '<color name="d">@color/b</color>', '<color name="e">@color/foo</color>' ]) ] warnings = checkxmlstyle._CheckColorPaletteReferences( mock_input_api, MockOutputApi()) self.assertEqual(1, len(warnings))
def testSingletonInArbitraryHeader(self): diff_singleton_h = ['base::subtle::AtomicWord ' 'Singleton<Type, Traits, DifferentiatingType>::'] diff_foo_h = ['// Singleton<Foo> in comment.', 'friend class Singleton<Foo>'] diff_bad_h = ['Foo* foo = Singleton<Foo>::get();'] mock_input_api = MockInputApi() mock_input_api.files = [MockAffectedFile('base/memory/singleton.h', diff_singleton_h), MockAffectedFile('foo.h', diff_foo_h), MockAffectedFile('bad.h', diff_bad_h)] warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual('error', warnings[0].type) self.assertTrue('Found Singleton<T>' in warnings[0].message)
def testOrderAlreadyWrong(self): scope = [(1, '#include "b.h"'), (2, '#include "a.h"'), (3, '#include "c.h"')] mock_input_api = MockInputApi() warnings = PRESUBMIT._CheckIncludeOrderForScope( scope, mock_input_api, '', [3]) self.assertEqual(0, len(warnings))
def testSucess_WrongBuilderCheck(self): """Use of OS-dependent AlertDialog should not be flagged.""" mock_input = MockInputApi() mock_input.files = [ MockFile('path/One.java', [ 'import android.app.AlertDialog;', 'new AlertDialog.Builder()' ]), MockFile('path/Two.java', [ 'import android.app.AlertDialog;', 'new AlertDialog.Builder(context);' ]), ] errors = PRESUBMIT._CheckAlertDialogBuilder(mock_input, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(2, len(errors[0].items))
def testUncheckableIncludes(self): mock_input_api = MockInputApi() contents = ['#include <windows.h>', '#include "b.h"', '#include "a.h"'] mock_file = MockFile('', contents) warnings = PRESUBMIT._CheckIncludeOrderInFile( mock_input_api, mock_file, range(1, len(contents) + 1)) self.assertEqual(1, len(warnings)) contents = ['#include "gpu/command_buffer/gles_autogen.h"', '#include "b.h"', '#include "a.h"'] mock_file = MockFile('', contents) warnings = PRESUBMIT._CheckIncludeOrderInFile( mock_input_api, mock_file, range(1, len(contents) + 1)) self.assertEqual(1, len(warnings)) contents = ['#include "gl_mock_autogen.h"', '#include "b.h"', '#include "a.h"'] mock_file = MockFile('', contents) warnings = PRESUBMIT._CheckIncludeOrderInFile( mock_input_api, mock_file, range(1, len(contents) + 1)) self.assertEqual(1, len(warnings)) contents = ['#include "ipc/some_macros.h"', '#include "b.h"', '#include "a.h"'] mock_file = MockFile('', contents) warnings = PRESUBMIT._CheckIncludeOrderInFile( mock_input_api, mock_file, range(1, len(contents) + 1)) self.assertEqual(1, len(warnings))
def testTypicalNotMatchedChangeViaSuffixes(self): diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] diff_xml = [ '<histogram_suffixes name="SuperHistogram">', ' <suffix name="Dummy"/>', ' <affected-histogram name="Snafu.Dummy"/>', '</histogram>' ] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('some/path/foo.cc', diff_cc), MockFile('tools/metrics/histograms/histograms.xml', diff_xml), ] warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual('warning', warnings[0].type)
def inputApiContainingFileWithSourceSets(self, filename, source_sets): """Returns a MockInputApi object containing a single file having |filename| as its name and |source_sets| as its contents.""" mock_file = MockFile(filename, source_sets) mock_input_api = MockInputApi() mock_input_api.files.append(mock_file) return mock_input_api
def testSuccess_Widget(self): lines = [ '<RelativeLayout', 'xmlns:android="http://schemas.android.com/apk/res/android"', 'android:layout_width="match_parent"', 'android:layout_height="wrap_content">', '<View', 'android:background="@drawable/infobar_shadow_top"', 'android:visibility="gone" />', '</RelativeLayout>' ] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('chrome/java/res_test/test.xml', lines) ] errors = checkxmlstyle._CheckTextAppearance(mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testColorFormatLowerCase(self): lines = [ '<color name="color1">#61000000</color>', '<color name="color2">#EFFFFFFF</color>', '<color name="color3">#CcCcCC</color>' ] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('chrome/java/res_test/test.xml', lines) ] errors = checkxmlstyle._CheckColorFormat(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(1, len(errors[0].items)) self.assertEqual(' chrome/java/res_test/test.xml:3', errors[0].items[0].splitlines()[0])
def testBannedIosObcjFunctions(self): input_api = MockInputApi() input_api.files = [ MockFile('some/ios/file.mm', ['TEST(SomeClassTest, SomeInteraction) {', '}']), MockFile('some/mac/file.mm', ['TEST(SomeClassTest, SomeInteraction) {', '}']), MockFile('another/ios_file.mm', ['class SomeTest : public testing::Test {};']), ] errors = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertTrue('some/ios/file.mm' in errors[0].message) self.assertTrue('another/ios_file.mm' in errors[0].message) self.assertTrue('some/mac/file.mm' not in errors[0].message)
def testFailure_Widget(self): lines_top_level = [ '<TextView', 'xmlns:android="http://schemas.android.com/apk/res/android"', 'android:layout_width="match_parent"', 'android:layout_height="@dimen/snippets_article_header_height"', 'android:textColor="@color/snippets_list_header_text_color"', 'android:textSize="14sp" />'] lines_subcomponent_widget = [ '<RelativeLayout', 'xmlns:android="http://schemas.android.com/apk/res/android"', 'android:layout_width="match_parent"', 'android:layout_height="wrap_content">', '<View', 'android:textColor="@color/error_text_color"', 'android:textSize="@dimen/text_size_medium"', 'android:textAllCaps="true"', 'android:background="@drawable/infobar_shadow_top"', 'android:visibility="gone" />', '</RelativeLayout>'] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('chrome/java/res_test/test1.xml', lines_top_level), MockFile('chrome/java/res_test/test2.xml', lines_subcomponent_widget)] errors = checkxmlstyle._CheckTextAppearance(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(5, len(errors[0].items)) self.assertEqual( (' chrome/java/res_test/test1.xml:5 contains attribute ' 'android:textColor'), errors[0].items[0].splitlines()[0]) self.assertEqual( (' chrome/java/res_test/test1.xml:6 contains attribute ' 'android:textSize'), errors[0].items[1].splitlines()[0]) self.assertEqual( (' chrome/java/res_test/test2.xml:6 contains attribute ' 'android:textColor'), errors[0].items[2].splitlines()[0]) self.assertEqual( (' chrome/java/res_test/test2.xml:7 contains attribute ' 'android:textSize'), errors[0].items[3].splitlines()[0]) self.assertEqual( (' chrome/java/res_test/test2.xml:8 contains attribute ' 'android:textAllCaps'), errors[0].items[4].splitlines()[0])
def testChromeDoesNotUseOSIOS(self): lines = [ '#if BUILDFLAG(IS_IOS)', '#error OS_IOS not allowed', '#endif' ] errors = PRESUBMIT._CheckNoOSIOSMacrosInChromeFile( MockInputApi(), MockFile('chrome/path/foo_platform.cc', lines)) self.assertEqual(1, len(errors)) self.assertEqual(' chrome/path/foo_platform.cc:1', errors[0])
def testValidIfDefinedMacroNames(self): lines = [ '#if defined(FOO)', '#ifdef BAR', ] errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile( MockInputApi(), MockFile('some/path/source.cc', lines)) self.assertEqual(0, len(errors))
def testAndroidChangeIncluded(self): mock_input_api = MockInputApi() mock_input_api.files = [ MockAffectedFile('content/test/data/accessibility/event/foo.html', [''], action='A'), MockAffectedFile( 'accessibility/WebContentsAccessibilityEventsTest.java', [''], action='M') ] msgs = PRESUBMIT.CheckAccessibilityEventsTestIncludesAndroid( mock_input_api, MockOutputApi()) self.assertEqual( 0, len(msgs), 'Expected %d messages, found %d: %s' % (0, len(msgs), msgs))
def testFailure(self): lines = [ '<color name="color1">#61000000</color>', '<color name="color2">#61000000</color>' ] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('chrome/java/res_test/colors.xml', lines) ] errors = checkxmlstyle._CheckDuplicateColors(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(2, len(errors[0].items)) self.assertEqual(' chrome/java/res_test/colors.xml:1', errors[0].items[0].splitlines()[0]) self.assertEqual(' chrome/java/res_test/colors.xml:2', errors[0].items[1].splitlines()[0])
def testSystemHeaderOrder(self): scope = [(1, '#include <csystem.h>'), (2, '#include <cppsystem>'), (3, '#include "acustom.h"')] all_linenums = [linenum for (linenum, _) in scope] mock_input_api = MockInputApi() warnings = PRESUBMIT._CheckIncludeOrderForScope( scope, mock_input_api, '', all_linenums) self.assertEqual(0, len(warnings))
def testConflictAdded2(self): scope = [(1, '#include "c.h"'), (2, '#include "b.h"'), (3, '#include "d.h"')] mock_input_api = MockInputApi() warnings = PRESUBMIT._CheckIncludeOrderForScope( scope, mock_input_api, '', [2]) self.assertEqual(1, len(warnings)) self.assertTrue('2' in warnings[0])
def testCatchAnswerUrlId(self): input_api = MockInputApi() input_api.files = [ MockFile('somewhere/file.cc', [ 'char* host = ' ' "https://support.google.com/chrome/answer/123456";' ]), MockFile('somewhere_else/file.cc', [ 'char* host = ' ' "https://support.google.com/chrome/a/answer/123456";' ]), ] warnings = PRESUBMIT._CheckGoogleSupportAnswerUrl( input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual(2, len(warnings[0].items))
def testIgnoresReadmes(self): lines = [ 'A First Level Header', '====================', '', 'A Second Level Header', '---------------------' ] errors = PRESUBMIT._CheckForVersionControlConflictsInFile( MockInputApi(), MockFile('some/polymer/README.md', lines)) self.assertEqual(0, len(errors))
def testSpecialFirstInclude3(self): mock_input_api = MockInputApi() contents = ['#include "some/path/foo.h"', '#include "a/header.h"'] mock_file = MockFile('some/path/foo_platform.cc', contents) warnings = PRESUBMIT._CheckIncludeOrderInFile( mock_input_api, mock_file, range(1, len(contents) + 1)) self.assertEqual(0, len(warnings))
def testUserMetricsActionNotAddedToActions(self): input_api = MockInputApi() file_with_user_action = 'file_with_user_action.cc' contents_with_user_action = [ 'base::UserMetricsAction("NotInActionsXml")' ] input_api.files = [MockFile(file_with_user_action, contents_with_user_action)] output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()) self.assertEqual( ('File %s line %d: %s is missing in ' 'tools/metrics/actions/actions.xml. Please run ' 'tools/metrics/actions/extract_actions.py to update.' % (file_with_user_action, 1, 'NotInActionsXml')), output[0].message)
def testFailure(self): lines = [ '<resource>', '<style name="TextAppearance.Test">', '<item name="android:textColor">@color/default_text_color_link</item>', '<item name="android:textSize">14sp</item>', '</style>', '</resource>' ] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile('chrome/java/res_test/test.xml', lines) ] errors = checkxmlstyle._CheckNewTextAppearance(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(1, len(errors[0].items)) self.assertEqual(' chrome/java/res_test/test.xml:2', errors[0].items[0].splitlines()[0])
def testChromeDoesNotUseOSAPPLE(self): lines = ['#if defined(OS_APPLE)', '#error OS_APPLE not allowed', '#endif'] errors = PRESUBMIT._CheckNoOSAPPLEMacrosInChromeFile( MockInputApi(), MockFile('chrome/path/foo_platform.cc', lines)) self.assertEqual(1, len(errors)) self.assertEqual(' chrome/path/foo_platform.cc:1', errors[0])
def testFailure(self): lines = [ '<color name="color1">#61000000</color>', '<color name="color2">#61000000</color>' ] mock_input_api = MockInputApi() mock_input_api.files = [ MockFile(helpers.COLOR_PALETTE_RELATIVE_PATH, lines) ] errors = checkxmlstyle._CheckDuplicateColors(mock_input_api, MockOutputApi()) self.assertEqual(1, len(errors)) self.assertEqual(2, len(errors[0].items)) self.assertEqual(' %s:1' % helpers.COLOR_PALETTE_RELATIVE_PATH, errors[0].items[0].splitlines()[0]) self.assertEqual(' %s:2' % helpers.COLOR_PALETTE_RELATIVE_PATH, errors[0].items[1].splitlines()[0])
def testSuccess_Style(self): lines = [ '<resource>', '<style name="TextAppearance.Test">', '<item name="android:textColor">@color/default_text_color_link</item>', '<item name="android:textSize">14sp</item>', '<item name="android:textStyle">bold</item>', '<item name="android:fontFamily">some-font</item>', '<item name="android:textAllCaps">true</item>', '</style>', '<style name="TestStyle">', '<item name="android:background">some_background</item>', '</style>', '</resource>' ] mock_input_api = MockInputApi() mock_input_api.files = [MockFile('chrome/path/test.xml', lines)] errors = PRESUBMIT._CheckTextAppearance(mock_input_api, MockOutputApi()) self.assertEqual(0, len(errors))
def testCheckCrbugLinksHaveHttps(self): input_api = MockInputApi() input_api.files = [ MockFile('somewhere/file.cc', [ '// TODO(developer): crbug.com should be linkified', '// TODO(developer): (crbug.com) should be linkified', '// TODO(developer): crbug/123 should be well formed', '// TODO(developer): http://crbug.com it\'s OK', '// TODO(developer): https://crbug.com is just great', '// TODO(crbug.com/123456): this pattern it\'s also OK' ]), ] warnings = PRESUBMIT._CheckCrbugLinksHaveHttps(input_api, MockOutputApi()) self.assertEqual(1, len(warnings)) self.assertEqual(3, warnings[0].message.count('\n'))
def testSpecialFirstInclude6(self): mock_input_api = MockInputApi() contents = ['#include "some/other/path/foo_win.h"', '#include <set>', '#include "a/header.h"'] mock_file = MockFile('some/path/foo_unittest_win.h', contents) warnings = PRESUBMIT._CheckIncludeOrderInFile( mock_input_api, mock_file, range(1, len(contents) + 1)) self.assertEqual(0, len(warnings))