diff --git a/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/AndroidSrcXmlDetector.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/AndroidSrcXmlDetector.kt index 5799871f4a7e..b9e3cff6b6e0 100644 --- a/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/AndroidSrcXmlDetector.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/AndroidSrcXmlDetector.kt @@ -35,7 +35,6 @@ class AndroidSrcXmlDetector : ResourceXmlDetector() { const val ERROR_MESSAGE = "Using android:src to define resource instead of app:srcCompat" - @VisibleForTesting val ISSUE_XML_SRC_USAGE = Issue.create( id = "AndroidSrcXmlDetector", briefDescription = "Prohibits using android:src in ImageViews and ImageButtons", diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ButtonStyleXmlDetector.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ButtonStyleXmlDetector.kt similarity index 98% rename from mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ButtonStyleXmlDetector.kt rename to mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ButtonStyleXmlDetector.kt index 3a1a2006df94..98c679347e52 100644 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ButtonStyleXmlDetector.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ButtonStyleXmlDetector.kt @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.mozilla.fenix.lintrules +package mozilla.components.tooling.lint import com.android.SdkConstants import com.android.SdkConstants.ATTR_STYLE diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/perf/ConstraintLayoutPerfDetector.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ConstraintLayoutPerfDetector.kt similarity index 99% rename from mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/perf/ConstraintLayoutPerfDetector.kt rename to mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ConstraintLayoutPerfDetector.kt index d7e08b57a83e..f5963367c3b8 100644 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/perf/ConstraintLayoutPerfDetector.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ConstraintLayoutPerfDetector.kt @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.mozilla.fenix.lintrules.perf +package mozilla.components.tooling.lint import com.android.resources.ResourceFolderType import com.android.tools.lint.detector.api.Category diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ContextCompatDetector.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ContextCompatDetector.kt similarity index 98% rename from mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ContextCompatDetector.kt rename to mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ContextCompatDetector.kt index 6ffdac6d5fde..0067c5125989 100644 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ContextCompatDetector.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ContextCompatDetector.kt @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.mozilla.fenix.lintrules +package mozilla.components.tooling.lint import com.android.tools.lint.detector.api.* import com.intellij.psi.PsiMethod diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LicenseCommentChecker.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LicenseCommentChecker.kt similarity index 98% rename from mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LicenseCommentChecker.kt rename to mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LicenseCommentChecker.kt index efc4b938d239..b9b2a6f38a46 100644 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LicenseCommentChecker.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LicenseCommentChecker.kt @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.mozilla.fenix.lintrules +package mozilla.components.tooling.lint import com.android.tools.lint.client.api.UElementHandler import com.android.tools.lint.detector.api.JavaContext diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LicenseDetector.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LicenseDetector.kt similarity index 74% rename from mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LicenseDetector.kt rename to mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LicenseDetector.kt index 81c6b6552fef..628fe857bdd0 100644 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LicenseDetector.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LicenseDetector.kt @@ -2,10 +2,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package org.mozilla.fenix.lintrules +package mozilla.components.tooling.lint import com.android.tools.lint.client.api.UElementHandler -import com.android.tools.lint.detector.api.* +import com.android.tools.lint.detector.api.Category +import com.android.tools.lint.detector.api.Detector +import com.android.tools.lint.detector.api.Implementation +import com.android.tools.lint.detector.api.Issue +import com.android.tools.lint.detector.api.JavaContext +import com.android.tools.lint.detector.api.Scope +import com.android.tools.lint.detector.api.Severity +import com.android.tools.lint.detector.api.SourceCodeScanner import org.jetbrains.uast.UElement import org.jetbrains.uast.UFile @@ -22,7 +29,7 @@ class LicenseDetector : Detector(), SourceCodeScanner { id = "MissingLicense", briefDescription = "File doesn't start with the license comment", explanation = "Every file must start with the license comment:\n" + - LicenseCommentChecker.ValidLicenseForKotlinFiles, + LicenseCommentChecker.ValidLicenseForKotlinFiles, category = Category.CORRECTNESS, severity = Severity.WARNING, implementation = Implementation, diff --git a/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LintIssueRegistry.kt b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LintIssueRegistry.kt index 0aba1ac98434..5f9d0ddb73d8 100644 --- a/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LintIssueRegistry.kt +++ b/mobile/android/android-components/components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LintIssueRegistry.kt @@ -15,6 +15,9 @@ import com.android.tools.lint.detector.api.Issue class LintIssueRegistry : IssueRegistry() { override val api: Int = com.android.tools.lint.detector.api.CURRENT_API override val issues: List = listOf( + ButtonStyleXmlDetector.ISSUE_XML_STYLE, + LicenseDetector.ISSUE_MISSING_LICENSE, + LicenseDetector.ISSUE_INVALID_LICENSE_FORMAT, LintLogChecks.ISSUE_LOG_USAGE, AndroidSrcXmlDetector.ISSUE_XML_SRC_USAGE, TextViewAndroidSrcXmlDetector.ISSUE_XML_SRC_USAGE, @@ -28,7 +31,7 @@ class LintIssueRegistry : IssueRegistry() { StringLintXmlDetector.ISSUE_STRAIGHT_DOUBLE_QUOTE_USAGE, StringLintXmlDetector.ISSUE_BRAND_USAGE, StringLintXmlDetector.ISSUE_PLACEHOLDER_COMMENT, - ) + ) + ConstraintLayoutPerfDetector.ISSUES + ContextCompatDetector.ISSUES override val vendor: Vendor = Vendor( vendorName = "Mozilla", identifier = "mozilla-android-components", diff --git a/mobile/android/fenix/app/build.gradle b/mobile/android/fenix/app/build.gradle index 51cec678beb7..81cb32f30d7e 100644 --- a/mobile/android/fenix/app/build.gradle +++ b/mobile/android/fenix/app/build.gradle @@ -765,7 +765,6 @@ dependencies { // depend on a separate library for unit tests. This will be removed in future releases. testImplementation "org.mozilla.telemetry:glean-native-forUnitTests:${project.ext.glean_version}" - lintChecks project(":mozilla-lint-rules") lintChecks project(':tooling-lint') } diff --git a/mobile/android/fenix/config/pre-push-recommended.sh b/mobile/android/fenix/config/pre-push-recommended.sh index fa78dc28d036..beaa45d8b067 100755 --- a/mobile/android/fenix/config/pre-push-recommended.sh +++ b/mobile/android/fenix/config/pre-push-recommended.sh @@ -32,5 +32,4 @@ fi assembleDebug \ assembleDebugAndroidTest \ mozilla-detekt-rules:test \ - mozilla-lint-rules:test \ testDebug diff --git a/mobile/android/fenix/mozilla-lint-rules/.gitignore b/mobile/android/fenix/mozilla-lint-rules/.gitignore deleted file mode 100644 index 796b96d1c402..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/build diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/AndroidSrcXmlDetector.kt b/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/AndroidSrcXmlDetector.kt deleted file mode 100644 index 3becda8457af..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/AndroidSrcXmlDetector.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules - -import com.android.SdkConstants.ATTR_SRC -import com.android.SdkConstants.FQCN_IMAGE_BUTTON -import com.android.SdkConstants.FQCN_IMAGE_VIEW -import com.android.SdkConstants.IMAGE_BUTTON -import com.android.SdkConstants.IMAGE_VIEW -import com.android.resources.ResourceFolderType -import com.android.tools.lint.detector.api.Category -import com.android.tools.lint.detector.api.Implementation -import com.android.tools.lint.detector.api.Issue -import com.android.tools.lint.detector.api.ResourceXmlDetector -import com.android.tools.lint.detector.api.Scope -import com.android.tools.lint.detector.api.Severity -import com.android.tools.lint.detector.api.XmlContext -import org.w3c.dom.Element - -/** - * A custom lint check that prohibits not using the app:srcCompat for ImageViews - */ -class AndroidSrcXmlDetector : ResourceXmlDetector() { - companion object { - const val SCHEMA = "http://schemas.android.com/apk/res/android" - const val FULLY_QUALIFIED_APP_COMPAT_IMAGE_BUTTON = - "androidx.appcompat.widget.AppCompatImageButton" - const val FULLY_QUALIFIED_APP_COMPAT_VIEW_CLASS = - "androidx.appcompat.widget.AppCompatImageView" - const val APP_COMPAT_IMAGE_BUTTON = "AppCompatImageButton" - const val APP_COMPAT_IMAGE_VIEW = "AppCompatImageView" - - const val ERROR_MESSAGE = "Using android:src to define resource instead of app:srcCompat" - - val ISSUE_XML_SRC_USAGE = Issue.create( - id = "AndroidSrcXmlDetector", - briefDescription = "Prohibits using android:src in ImageViews and ImageButtons", - explanation = "ImageView (and descendants) images should be declared using app:srcCompat", - category = Category.CORRECTNESS, - severity = Severity.ERROR, - implementation = Implementation( - AndroidSrcXmlDetector::class.java, - Scope.RESOURCE_FILE_SCOPE, - ), - ) - } - - override fun appliesTo(folderType: ResourceFolderType): Boolean { - // Return true if we want to analyze resource files in the specified resource - // folder type. In this case we only need to analyze layout resource files. - return folderType == ResourceFolderType.LAYOUT - } - - override fun getApplicableElements(): Collection? { - return setOf( - FQCN_IMAGE_VIEW, - IMAGE_VIEW, - FQCN_IMAGE_BUTTON, - IMAGE_BUTTON, - FULLY_QUALIFIED_APP_COMPAT_IMAGE_BUTTON, - FULLY_QUALIFIED_APP_COMPAT_VIEW_CLASS, - APP_COMPAT_IMAGE_BUTTON, - APP_COMPAT_IMAGE_VIEW, - ) - } - - override fun visitElement(context: XmlContext, element: Element) { - if (!element.hasAttributeNS(SCHEMA, ATTR_SRC)) return - val node = element.getAttributeNodeNS(SCHEMA, ATTR_SRC) - - context.report( - issue = ISSUE_XML_SRC_USAGE, - scope = node, - location = context.getLocation(node), - message = ERROR_MESSAGE, - ) - } -} diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ImageViewAndroidTintXmlDetector.kt b/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ImageViewAndroidTintXmlDetector.kt deleted file mode 100644 index 4fdf0ec2fbaa..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/ImageViewAndroidTintXmlDetector.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules - -import com.android.SdkConstants.ATTR_TINT -import com.android.SdkConstants.FQCN_IMAGE_BUTTON -import com.android.SdkConstants.FQCN_IMAGE_VIEW -import com.android.SdkConstants.IMAGE_BUTTON -import com.android.SdkConstants.IMAGE_VIEW -import com.android.resources.ResourceFolderType -import com.android.tools.lint.detector.api.Category -import com.android.tools.lint.detector.api.Implementation -import com.android.tools.lint.detector.api.Issue -import com.android.tools.lint.detector.api.ResourceXmlDetector -import com.android.tools.lint.detector.api.Scope -import com.android.tools.lint.detector.api.Severity -import com.android.tools.lint.detector.api.XmlContext -import org.w3c.dom.Element - -/** - * A custom lint check that prohibits not using the app:tint for ImageViews - */ -class ImageViewAndroidTintXmlDetector : ResourceXmlDetector() { - companion object { - const val SCHEMA = "http://schemas.android.com/apk/res/android" - const val FULLY_QUALIFIED_APP_COMPAT_IMAGE_BUTTON = - "androidx.appcompat.widget.AppCompatImageButton" - const val FULLY_QUALIFIED_APP_COMPAT_VIEW_CLASS = - "androidx.appcompat.widget.AppCompatImageView" - const val APP_COMPAT_IMAGE_BUTTON = "AppCompatImageButton" - const val APP_COMPAT_IMAGE_VIEW = "AppCompatImageView" - - const val ERROR_MESSAGE = - "Using android:tint to tint ImageView instead of app:tint with AppCompatImageView" - - val ISSUE_XML_SRC_USAGE = Issue.create( - id = "AndroidSrcXmlDetector", - briefDescription = "Prohibits using android:tint in ImageViews and ImageButtons", - explanation = "ImageView (and descendants) should be tinted using app:tint", - category = Category.CORRECTNESS, - severity = Severity.ERROR, - implementation = Implementation( - ImageViewAndroidTintXmlDetector::class.java, - Scope.RESOURCE_FILE_SCOPE, - ), - ) - } - - override fun appliesTo(folderType: ResourceFolderType): Boolean { - // Return true if we want to analyze resource files in the specified resource - // folder type. In this case we only need to analyze layout resource files. - return folderType == ResourceFolderType.LAYOUT - } - - override fun getApplicableElements(): Collection? { - return setOf( - FQCN_IMAGE_VIEW, - IMAGE_VIEW, - FQCN_IMAGE_BUTTON, - IMAGE_BUTTON, - FULLY_QUALIFIED_APP_COMPAT_IMAGE_BUTTON, - FULLY_QUALIFIED_APP_COMPAT_VIEW_CLASS, - APP_COMPAT_IMAGE_BUTTON, - APP_COMPAT_IMAGE_VIEW, - ) - } - - override fun visitElement(context: XmlContext, element: Element) { - if (!element.hasAttributeNS(SCHEMA, ATTR_TINT)) return - val node = element.getAttributeNodeNS(SCHEMA, ATTR_TINT) - - context.report( - issue = ISSUE_XML_SRC_USAGE, - scope = node, - location = context.getLocation(node), - message = ERROR_MESSAGE, - ) - } -} diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LintIssueRegistry.kt b/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LintIssueRegistry.kt deleted file mode 100644 index 86c70b80e1b5..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/LintIssueRegistry.kt +++ /dev/null @@ -1,27 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules - -import com.android.tools.lint.client.api.IssueRegistry -import com.android.tools.lint.client.api.Vendor -import com.android.tools.lint.detector.api.Issue -import org.mozilla.fenix.lintrules.perf.ConstraintLayoutPerfDetector - -/** - * Registry which provides a list of our custom lint checks to be performed on an Android project. - */ -@Suppress("unused") -class LintIssueRegistry : IssueRegistry() { - override val api: Int = com.android.tools.lint.detector.api.CURRENT_API - override val issues: List = listOf( - ButtonStyleXmlDetector.ISSUE_XML_STYLE, - LicenseDetector.ISSUE_MISSING_LICENSE, - LicenseDetector.ISSUE_INVALID_LICENSE_FORMAT, - ) + ConstraintLayoutPerfDetector.ISSUES + ContextCompatDetector.ISSUES - override val vendor: Vendor = Vendor( - vendorName = "Mozilla", - identifier = "mozilla-fenix", - ) -} diff --git a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/TextViewAndroidSrcXmlDetector.kt b/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/TextViewAndroidSrcXmlDetector.kt deleted file mode 100644 index 0ec8d690cb5e..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/main/java/org/mozilla/fenix/lintrules/TextViewAndroidSrcXmlDetector.kt +++ /dev/null @@ -1,97 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules - -import com.android.SdkConstants.ATTR_DRAWABLE_BOTTOM -import com.android.SdkConstants.ATTR_DRAWABLE_END -import com.android.SdkConstants.ATTR_DRAWABLE_LEFT -import com.android.SdkConstants.ATTR_DRAWABLE_RIGHT -import com.android.SdkConstants.ATTR_DRAWABLE_START -import com.android.SdkConstants.ATTR_DRAWABLE_TOP -import com.android.SdkConstants.FQCN_TEXT_VIEW -import com.android.SdkConstants.TEXT_VIEW -import com.android.resources.ResourceFolderType -import com.android.tools.lint.detector.api.Category -import com.android.tools.lint.detector.api.Implementation -import com.android.tools.lint.detector.api.Issue -import com.android.tools.lint.detector.api.ResourceXmlDetector -import com.android.tools.lint.detector.api.Scope -import com.android.tools.lint.detector.api.Severity -import com.android.tools.lint.detector.api.XmlContext -import org.w3c.dom.Element - -/** - * A custom lint check that prohibits not using the app:drawableXCompat to define drawables in TextViews - */ -class TextViewAndroidSrcXmlDetector : ResourceXmlDetector() { - companion object { - const val SCHEMA = "http://schemas.android.com/apk/res/android" - - const val ERROR_MESSAGE = - "Using android:drawableX to define resource instead of app:drawableXCompat" - - val ISSUE_XML_SRC_USAGE = Issue.create( - id = "TextViewAndroidSrcXmlDetector", - briefDescription = "Prohibits using android namespace to define drawables in TextViews", - explanation = "TextView drawables should be declared using app:drawableXCompat", - category = Category.CORRECTNESS, - severity = Severity.ERROR, - implementation = Implementation( - TextViewAndroidSrcXmlDetector::class.java, - Scope.RESOURCE_FILE_SCOPE, - ), - ) - } - - override fun appliesTo(folderType: ResourceFolderType): Boolean { - // Return true if we want to analyze resource files in the specified resource - // folder type. In this case we only need to analyze layout resource files. - return folderType == ResourceFolderType.LAYOUT - } - - override fun getApplicableElements(): Collection? { - return setOf( - FQCN_TEXT_VIEW, - TEXT_VIEW, - ) - } - - override fun visitElement(context: XmlContext, element: Element) { - val node = when { - element.hasAttributeNS(SCHEMA, ATTR_DRAWABLE_BOTTOM) -> element.getAttributeNodeNS( - SCHEMA, - ATTR_DRAWABLE_BOTTOM, - ) - element.hasAttributeNS(SCHEMA, ATTR_DRAWABLE_END) -> element.getAttributeNodeNS( - SCHEMA, - ATTR_DRAWABLE_END, - ) - element.hasAttributeNS(SCHEMA, ATTR_DRAWABLE_LEFT) -> element.getAttributeNodeNS( - SCHEMA, - ATTR_DRAWABLE_LEFT, - ) - element.hasAttributeNS( - SCHEMA, - ATTR_DRAWABLE_RIGHT, - ) -> element.getAttributeNodeNS(SCHEMA, ATTR_DRAWABLE_RIGHT) - element.hasAttributeNS( - SCHEMA, - ATTR_DRAWABLE_START, - ) -> element.getAttributeNodeNS(SCHEMA, ATTR_DRAWABLE_START) - element.hasAttributeNS(SCHEMA, ATTR_DRAWABLE_TOP) -> element.getAttributeNodeNS( - SCHEMA, - ATTR_DRAWABLE_TOP, - ) - else -> null - } ?: return - - context.report( - issue = ISSUE_XML_SRC_USAGE, - scope = node, - location = context.getLocation(node), - message = ERROR_MESSAGE, - ) - } -} diff --git a/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/ContextCompatDetectorTest.kt b/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/ContextCompatDetectorTest.kt deleted file mode 100644 index 9a8a7fa99c6f..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/ContextCompatDetectorTest.kt +++ /dev/null @@ -1,163 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules - -import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.detector.api.Detector -import com.android.tools.lint.detector.api.Issue -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 - -@RunWith(JUnit4::class) -class ContextCompatDetectorTest : LintDetectorTest() { - - override fun getDetector(): Detector = ContextCompatDetector() - - override fun getIssues(): MutableList = ContextCompatDetector.ISSUES.toMutableList() - - @Test - fun `report error when the ContextCompat getDrawable method is called`() { - val code = """ - |package example - | - |import androidx.core.content.ContextCompat - |import android.content.Context - | - |class Fake { - | - | fun trigger(context: Context, id: Int) { - | ContextCompat.getDrawable(context, id) - | } - | - |}""".trimMargin() - - val expectedReport = """ - |src/example/Fake.kt:9: Error: This call can lead to crashes, replace with AppCompatResources.getDrawable [UnsafeCompatGetDrawable] - | ContextCompat.getDrawable(context, id) - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - |1 errors, 0 warnings""".trimMargin() - - val expectedFixOutput = """ - |Fix for src/example/Fake.kt line 9: Replace with AppCompatResources.getDrawable: - |@@ -9 +9 - |- ContextCompat.getDrawable(context, id) - |+ AppCompatResources.getDrawable(context, id)""".trimMargin() - - lint() - .files(Context, ContextCompat, kotlin(code)) - .allowMissingSdk(true) - .run() - .expect(expectedReport) - .expectFixDiffs(expectedFixOutput) - } - - @Test - fun `report error when the ContextCompat getColorStateList method is called`() { - val code = """ - |package example - | - |import androidx.core.content.ContextCompat - |import android.content.Context - | - |class Fake { - | - | fun trigger(context: Context, id: Int) { - | ContextCompat.getColorStateList(context, id) - | } - | - |}""".trimMargin() - - val expectedReport = """ - |src/example/Fake.kt:9: Error: This call can lead to crashes, replace with AppCompatResources.getColorStateList [UnsafeCompatGetColorStateList] - | ContextCompat.getColorStateList(context, id) - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - |1 errors, 0 warnings""".trimMargin() - - val expectedFixOutput = """ - |Fix for src/example/Fake.kt line 9: Replace with AppCompatResources.getColorStateList: - |@@ -9 +9 - |- ContextCompat.getColorStateList(context, id) - |+ AppCompatResources.getColorStateList(context, id)""".trimMargin() - - lint() - .files(Context, ContextCompat, kotlin(code)) - .allowMissingSdk(true) - .run() - .expect(expectedReport) - .expectFixDiffs(expectedFixOutput) - } - - @Test - fun `does not report any errors if the getDrawable method being called is not from the ContextCompat class`() { - val code = """ - |package example - | - |import android.content.Context - | - |class Fake { - | - | fun trigger(context: Context, id: Int) { - | getDrawable(context, id) - | } - | - | fun getDrawable(context: Context, id: Int) {} - | - |}""".trimMargin() - - lint() - .files(Context, kotlin(code)) - .allowMissingSdk(true) - .run() - .expectClean() - } - - @Test - fun `does not report any errors if the getColorStateList method being called is not from the ContextCompat class`() { - val code = """ - |package example - | - |import android.content.Context - | - |class Fake { - | - | fun trigger(context: Context, id: Int) { - | getColorStateList(context, id) - | } - | - | fun getColorStateList(context: Context, id: Int) {} - | - |}""".trimMargin() - - lint() - .files(Context, kotlin(code)) - .allowMissingSdk(true) - .run() - .expectClean() - } - - // As Lint doesn't have access to the real corresponding classes, we have to provide stubs - companion object Stubs { - - private val Context = java( - """ - |package android.content; - | - |public class Context {} - |""".trimMargin() - ) - - private val ContextCompat = java( - """ - |package androidx.core.content; - | - |public class ContextCompat { - | public static void getDrawable(Context context, int resId) {} - | public static void getColorStateList(Context context, int resId) {} - |} - |""".trimMargin() - ) - } -} diff --git a/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/LicenseDetectorTest.kt b/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/LicenseDetectorTest.kt deleted file mode 100644 index c04b237e01ca..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/LicenseDetectorTest.kt +++ /dev/null @@ -1,155 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules - -import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.checks.infrastructure.TestFiles -import com.android.tools.lint.checks.infrastructure.TestMode -import com.android.tools.lint.detector.api.Detector -import com.android.tools.lint.detector.api.Issue -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 -import org.mozilla.fenix.lintrules.LicenseCommentChecker.Companion.ValidLicenseForKotlinFiles -import org.mozilla.fenix.lintrules.LicenseDetector.Companion.ISSUE_INVALID_LICENSE_FORMAT -import org.mozilla.fenix.lintrules.LicenseDetector.Companion.ISSUE_MISSING_LICENSE - -@RunWith(JUnit4::class) -class LicenseDetectorTest : LintDetectorTest() { - - override fun getIssues(): MutableList = - mutableListOf(ISSUE_MISSING_LICENSE, ISSUE_INVALID_LICENSE_FORMAT) - - override fun getDetector(): Detector = LicenseDetector() - - @Test - fun `report missing license if it isn't at the top of the file`() { - val code = """ - |package example - | - |class SomeExample { - | fun test() { - | val x = 10 - | println("Hello") - | } - |}""".trimMargin() - - val expectedReport = """ - |src/example/SomeExample.kt:1: Warning: The file must start with a comment containing the license [MissingLicense] - |package example - |~~~~~~~~~~~~~~~ - |0 errors, 1 warnings""".trimMargin() - - val expectedFixOutput = """ - |Fix for src/example/SomeExample.kt line 1: Insert license at the top of the file: - |@@ -1 +1 - |+ /* This Source Code Form is subject to the terms of the Mozilla Public - |+ * License, v. 2.0. If a copy of the MPL was not distributed with this - |+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - |+""".trimMargin() - - lint() - .files(TestFiles.kt(code)) - .allowMissingSdk(true) - .testModes(TestMode.DEFAULT) - .run() - .expect(expectedReport) - .expectFixDiffs(expectedFixOutput) - } - - @Test - fun `report invalid license format if it doesn't have a leading newline character`() { - val code = """ - |$ValidLicenseForKotlinFiles - |package example - | - |class SomeExample { - | fun test() { - | val x = 10 - | println("Hello") - | } - |}""".trimMargin() - - val expectedReport = """ - |src/example/SomeExample.kt:3: Warning: The license comment must be followed by a newline character [InvalidLicenseFormat] - | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - | ~ - |0 errors, 1 warnings""".trimMargin() - - val expectedFixOutput = """ - |Fix for src/example/SomeExample.kt line 3: Insert newline character after the license: - |@@ -4 +4 - |+""".trimMargin() - - lint() - .files(TestFiles.kt(code)) - .allowMissingSdk(true) - .testModes(TestMode.DEFAULT) - .run() - .expect(expectedReport) - .expectFixDiffs(expectedFixOutput) - } - - @Test - fun `valid license does not generate a warning`() { - val code = """ - |$ValidLicenseForKotlinFiles - | - |package example - | - |class SomeExample { - | fun test() { - | val x = 10 - | println("Hello") - | } - |}""".trimMargin() - - lint() - .files(TestFiles.kt(code)) - .allowMissingSdk(true) - .testModes(TestMode.DEFAULT) - .run() - .expectClean() - } - - @Test - fun `report incorrectly formatted license`() { - val code = """ - |/* This Source Code Form is subject to the terms of the Mozilla Public - | License, v. 2.0. If a copy of the MPL was not distributed with this - | file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - | - |package example - | - |class SomeExample { - | fun test() { - | val x = 10 - | println("Hello") - | } - |}""".trimMargin() - - val expectedReport = """ - |src/example/SomeExample.kt:1: Warning: The license comment doesn't have the appropriate format [InvalidLicenseFormat] - |/* This Source Code Form is subject to the terms of the Mozilla Public - |^ - |0 errors, 1 warnings""".trimMargin() - - val expectedFixOutput = """ - |Fix for src/example/SomeExample.kt line 1: Replace with correctly formatted license: - |@@ -2 +2 - |- License, v. 2.0. If a copy of the MPL was not distributed with this - |- file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - |+ * License, v. 2.0. If a copy of the MPL was not distributed with this - |+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */""".trimMargin() - - lint() - .files(TestFiles.kt(code)) - .allowMissingSdk(true) - .testModes(TestMode.DEFAULT) - .run() - .expect(expectedReport) - .expectFixDiffs(expectedFixOutput) - } -} diff --git a/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/perf/ConstraintLayoutPerfDetectorTest.kt b/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/perf/ConstraintLayoutPerfDetectorTest.kt deleted file mode 100644 index d6245f8f3946..000000000000 --- a/mobile/android/fenix/mozilla-lint-rules/src/test/java/org/mozilla/fenix/lintrules/perf/ConstraintLayoutPerfDetectorTest.kt +++ /dev/null @@ -1,117 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.lintrules.perf - -import com.android.resources.ResourceFolderType -import com.android.tools.lint.checks.infrastructure.TestFiles -import com.android.tools.lint.checks.infrastructure.TestLintTask.lint -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Ignore -import org.junit.Test - -// FQCN = fully qualified class name -private const val FQCN_CONSTRAINT_LAYOUT = "androidx.constraintlayout.widget.ConstraintLayout" -private const val FQCN_CUSTOM_VIEW = "org.mozilla.fenix.library.LibrarySiteItemView" // chosen arbitrarily - -private val ALL_ISSUES = ConstraintLayoutPerfDetector.ISSUES.toTypedArray() - -class ConstraintLayoutPerfDetectorTest { - - private lateinit var detector: ConstraintLayoutPerfDetector - - @Before - fun setUp() { - detector = ConstraintLayoutPerfDetector() - } - - @Test - fun `WHEN checking file types THEN it only applies layout`() { - val excludedType = ResourceFolderType.LAYOUT - - val allTypeButLayout = ResourceFolderType.entries.filter { it != excludedType } - allTypeButLayout.forEach { assertFalse(detector.appliesTo(it)) } - - assertTrue(detector.appliesTo(excludedType)) - } - - @Test - fun `WHEN checking applicable elements THEN it applies to ConstraintLayout` () { - assertTrue(detector.getApplicableElements()!!.contains( - "androidx.constraintlayout.widget.ConstraintLayout" - )) - } - - @Test - fun `WHEN checking applicable elements THEN it doesnt apply to some common ones`() { - val applicableElements = detector.getApplicableElements()!! - listOf( - "LinearLayout", - "fragment" - ).forEach { assertFalse(it, applicableElements.contains(it)) } - } - - @Test - fun `GIVEN a file with one ConstraintLayout WHEN detecting THEN pass`() { - val layout = getLayoutWithConstraintLayoutRoot("") - lint() - .files(getTestXml(layout)) - .issues(*ALL_ISSUES) - .run() - .expectClean() - } - - @Test - fun `GIVEN a file with two ConstraintLayouts WHEN detecting THEN fail`() { - // Since we're not the root view, we need to indent extra. - val innerLayout = """<$FQCN_CONSTRAINT_LAYOUT - | android:layout_width="match_parent" - | android:layout_height="match_parent"> - | - | - """.trimMargin() - val layout = getLayoutWithConstraintLayoutRoot(innerLayout) - lint() - .files(getTestXml(layout)) - .issues(*ALL_ISSUES) - .run() - .expectWarningCount(0) - .expectErrorCount(1) - } - - @Ignore("the lint test API seems to be broken: project0: Error: Unexpected failure during " + - "lint analysis (this is a bug in lint or one of the libraries it depends on). Message: The " + - "prefix \"android\" for attribute \"android:layout_width\" associated with an element type \"TextView\" is not bound.") - @Test - fun `GIVEN a file with a ConstraintLayout and a custom view ConstraintLayout layout WHEN detecting THEN warning`() { - val innerLayout = """ - | - | - """.trimMargin() - lint() - .files(getTestXml(innerLayout)) - .issues(*ALL_ISSUES) - .run() - .expectWarningCount(1) - .expectErrorCount(0) - } - - private fun getTestXml(code: String) = TestFiles.xml("res/layout/activity_home.xml", code) - - private fun getLayoutWithConstraintLayoutRoot(innerLayout: String = "") = """ - | <$FQCN_CONSTRAINT_LAYOUT - | xmlns:android="http://schemas.android.com/apk/res/android" - | android:layout_width="match_parent" - | android:layout_height="match_parent"> - | - | $innerLayout - | - | """.trimMargin() -} diff --git a/mobile/android/fenix/settings.gradle b/mobile/android/fenix/settings.gradle index 372f8288145a..7e461f97ccee 100644 --- a/mobile/android/fenix/settings.gradle +++ b/mobile/android/fenix/settings.gradle @@ -45,7 +45,6 @@ apply from: file('../autopublish-settings.gradle') include ':app' include ':mozilla-detekt-rules' -include ':mozilla-lint-rules' include ':benchmark' gradle.projectsLoaded { -> diff --git a/mobile/android/focus-android/app/lint-baseline.xml b/mobile/android/focus-android/app/lint-baseline.xml index a64ec4e12c93..09f95e249db0 100644 --- a/mobile/android/focus-android/app/lint-baseline.xml +++ b/mobile/android/focus-android/app/lint-baseline.xml @@ -452,6 +452,578 @@ column="9"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +