Add tests to all AppCompat lint rules that we have so far
* Add more coverage for color state list loading based on min SDK
* Add tests for color state list alpha and image view tinting
* Also remove an unneeded Material components dependency in the demo
Test: ./gradlew :lint-demo:lint-demo-appcompat:lint
Test: ./gradlew :appcompat:appcompat-lint:test
Bug: 148799598
Change-Id: I026f7bdecec5abf2650ff887b56f26cf185d539a
diff --git a/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListAlphaDetectorTest.kt b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListAlphaDetectorTest.kt
new file mode 100644
index 0000000..5ccf0c3
--- /dev/null
+++ b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListAlphaDetectorTest.kt
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.appcompat.lint.res
+
+import androidx.appcompat.lint.Stubs
+import androidx.appcompat.res.ColorStateListAlphaDetector
+import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
+import org.junit.Test
+
+class ColorStateListAlphaDetectorTest {
+ @Test
+ fun testIncompleteColorStateList() {
+ // We expect the definition of the color state list to be flagged since it has
+ // app:alpha but no android:alpha on one of the entries. We also expect a matching
+ // fix to add android:alpha attribute with the same value as the existing app:alpha one.
+ lint().files(
+ Stubs.COLOR_STATE_LIST
+ ).issues(ColorStateListAlphaDetector.NOT_USING_ANDROID_ALPHA)
+ .run()
+ .expect("""
+res/color/color_state_list.xml:4: Error: Must use android:alpha if app:alpha is used [UseAndroidAlpha]
+ <item app:alpha="?android:disabledAlpha"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+1 errors, 0 warnings
+ """.trimIndent())
+ .expectFixDiffs("""
+Fix for res/color/color_state_list.xml line 4: Set alpha="?android:disabledAlpha":
+@@ -6 +6
++ android:alpha="?android:disabledAlpha"
+ """.trimIndent())
+ }
+}
diff --git a/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt
index 3ded86f..45969ff 100644
--- a/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt
+++ b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt
@@ -19,6 +19,7 @@
import androidx.appcompat.lint.Stubs
import androidx.appcompat.res.ColorStateListLoadingDetector
import com.android.tools.lint.checks.infrastructure.LintDetectorTest.kotlin
+import com.android.tools.lint.checks.infrastructure.LintDetectorTest.manifest
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
import org.junit.Test
@@ -61,7 +62,7 @@
}
@Test
- fun testCoreGetColorStateList() {
+ fun testCoreGetColorStateListApi24() {
val customActivity = kotlin(
"com/example/CustomActivity.kt",
"""
@@ -78,10 +79,87 @@
"""
).indented().within("src")
- // We expect the call to Resources.getColorStateList to be flagged
+ // Manifest that sets min sdk to 24
+ val manifest = manifest(
+ """
+ <?xml version="1.0" encoding="utf-8"?>
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example">
+ <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="29" />
+ <application
+ android:hardwareAccelerated="true"
+ android:icon="@android:drawable/ic_delete"
+ android:label="Sample app"
+ android:allowBackup="false"
+ android:supportsRtl="true"
+ android:theme="@style/Theme.AppCompat">
+ <activity android:name=".CustomActivity"/>
+ </application>
+ </manifest>
+ """.trimIndent()
+ )
+
+ // We expect the call to Resources.getColorStateList to be flagged to use ContextCompat
+ // loading
lint().files(
Stubs.APPCOMPAT_ACTIVITY,
Stubs.COLOR_STATE_LIST,
+ manifest,
+ customActivity
+ ).issues(ColorStateListLoadingDetector.NOT_USING_COMPAT_LOADING)
+ .run()
+ .expect("""
+src/com/example/CustomActivity.kt:8: Warning: Use ContextCompat.getColorStateList() [UseCompatLoading]
+ getResources().getColorStateList(R.color.color_state_list)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+0 errors, 1 warnings
+ """.trimIndent())
+ }
+
+ @Test
+ fun testCoreGetColorStateListApi14() {
+ val customActivity = kotlin(
+ "com/example/CustomActivity.kt",
+ """
+ package com.example
+
+ import android.os.Bundle
+ import androidx.appcompat.app.AppCompatActivity
+
+ class CustomActivity: AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ getResources().getColorStateList(R.color.color_state_list)
+ }
+ }
+ """
+ ).indented().within("src")
+
+ // Manifest that sets min sdk to 14
+ val manifest = manifest(
+ """
+ <?xml version="1.0" encoding="utf-8"?>
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example">
+ <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29" />
+ <application
+ android:hardwareAccelerated="true"
+ android:icon="@android:drawable/ic_delete"
+ android:label="Sample app"
+ android:allowBackup="false"
+ android:supportsRtl="true"
+ android:theme="@style/Theme.AppCompat">
+ <activity android:name=".CustomActivity"/>
+ </application>
+ </manifest>
+ """.trimIndent()
+ )
+
+ // We expect the call to Resources.getColorStateList to be flagged to use AppCompatResources
+ // loading
+ lint().files(
+ Stubs.APPCOMPAT_ACTIVITY,
+ Stubs.COLOR_STATE_LIST,
+ manifest,
customActivity
).issues(ColorStateListLoadingDetector.NOT_USING_COMPAT_LOADING)
.run()
diff --git a/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ImageViewTintDetectorTest.kt b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ImageViewTintDetectorTest.kt
new file mode 100644
index 0000000..b896233
--- /dev/null
+++ b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ImageViewTintDetectorTest.kt
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.appcompat.lint.res
+
+import androidx.appcompat.res.ImageViewTintDetector
+import com.android.tools.lint.checks.infrastructure.LintDetectorTest
+import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
+import org.junit.Test
+
+class ImageViewTintDetectorTest {
+ @Test
+ fun testIncompleteColorStateList() {
+ val layout = LintDetectorTest.xml(
+ "layout/image_view.xml",
+ """
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+ <ImageView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:src="@android:drawable/ic_delete"
+ android:tint="#FF0000" />
+</LinearLayout>
+ """
+ ).indented().within("res")
+
+ // We expect the definition of the image view to be flagged since it has
+ // android:tint instead of app:tint. We also expect a matching
+ // fix to replace android:tint with app:tint, retaining the same value
+ lint().files(
+ layout
+ ).issues(ImageViewTintDetector.USING_ANDROID_TINT)
+ .run()
+ .expect("""
+res/layout/image_view.xml:10: Error: Must use app:tint instead of android:tint [UseAppTint]
+ android:tint="#FF0000" />
+ ~~~~~~~~~~~~~~~~~~~~~~
+1 errors, 0 warnings
+ """.trimIndent())
+ .expectFixDiffs("""
+Fix for res/layout/image_view.xml line 10: Set tint="#FF0000":
+@@ -3 +3
++ xmlns:app="http://schemas.android.com/apk/res-auto"
+@@ -11 +12
+- android:tint="#FF0000" />
++ app:tint="#FF0000" />
+ """.trimIndent())
+ }
+}
diff --git a/lint-demos/lint-demo-appcompat/build.gradle b/lint-demos/lint-demo-appcompat/build.gradle
index 9043a79..4558283 100644
--- a/lint-demos/lint-demo-appcompat/build.gradle
+++ b/lint-demos/lint-demo-appcompat/build.gradle
@@ -7,7 +7,6 @@
}
dependencies {
- api 'com.google.android.material:material:1.0.0'
implementation(project(":appcompat:appcompat"))
implementation(project(":core:core"))
api(KOTLIN_STDLIB)