Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
The Tor Project
Applications
android-components
Commits
2712336d
Commit
2712336d
authored
Mar 01, 2020
by
ekager
Browse files
Adds lint rule for using android:tint with ImageView
parent
18409f74
Changes
3
Hide whitespace changes
Inline
Side-by-side
components/tooling/lint/src/main/java/mozilla/components/tooling/lint/ImageViewAndroidTintXmlDetector.kt
0 → 100644
View file @
2712336d
/* 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
mozilla.components.tooling.lint
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.annotations.VisibleForTesting
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"
@VisibleForTesting
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
<
String
>?
{
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
)
}
}
components/tooling/lint/src/main/java/mozilla/components/tooling/lint/LintIssueRegistry.kt
View file @
2712336d
...
...
@@ -16,6 +16,7 @@ class LintIssueRegistry : IssueRegistry() {
override
val
issues
:
List
<
Issue
>
=
listOf
(
LintLogChecks
.
ISSUE_LOG_USAGE
,
AndroidSrcXmlDetector
.
ISSUE_XML_SRC_USAGE
,
TextViewAndroidSrcXmlDetector
.
ISSUE_XML_SRC_USAGE
TextViewAndroidSrcXmlDetector
.
ISSUE_XML_SRC_USAGE
,
ImageViewAndroidTintXmlDetector
.
ISSUE_XML_SRC_USAGE
)
}
components/tooling/lint/src/test/java/mozilla/components/tooling/lint/ImageViewAndroidTintXmlDetectorTest.kt
0 → 100644
View file @
2712336d
/* 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
mozilla.components.tooling.lint
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
/**
* Tests for the [ImageViewAndroidTintXmlDetector] custom lint check.
*/
@RunWith
(
JUnit4
::
class
)
class
ImageViewAndroidTintXmlDetectorTest
:
LintDetectorTest
()
{
override
fun
getIssues
():
MutableList
<
Issue
>
=
mutableListOf
(
ImageViewAndroidTintXmlDetector
.
ISSUE_XML_SRC_USAGE
)
override
fun
getDetector
():
Detector
=
ImageViewAndroidTintXmlDetector
()
@Test
fun
expectPass
()
{
lint
()
.
files
(
xml
(
"res/layout/layout.xml"
,
"""
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
"""
)
).
allowMissingSdk
(
true
)
.
run
()
.
expectClean
()
}
@Test
fun
expectFail
()
{
lint
()
.
files
(
xml
(
"res/layout/layout.xml"
,
"""
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_close"
android:tint="@color/photonBlue90"
/>
"""
)
).
allowMissingSdk
(
true
)
.
run
()
.
expect
(
"""
res/layout/layout.xml:6: Error: Using android:tint to tint ImageView instead of app:tint with AppCompatImageView [AndroidSrcXmlDetector]
android:tint="@color/photonBlue90"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
)
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment