From 805815a15ab5f6a33fdb643f7341dc14ed8ce7e1 Mon Sep 17 00:00:00 2001 From: Peter Kalauskas Date: Mon, 3 Oct 2022 12:27:10 -0700 Subject: [PATCH] Move API lint stubs to common class Additionally, - Move linters to new directory to match package name. This allows the tests to be run at the package level in Android Studio. - Use @Suppress("UnstableApiUsage") for all linter classes - Add missing copyright headers Test: manual Bug: 238923086 Change-Id: I1ee616a34e6e16586847c3a68f751efe40d7f565 --- .../RegisterReceiverViaContextDetector.kt | 1 + .../internal/systemui/lint/AndroidStubs.kt | 154 ++++++++++++++++++ .../lint/BindServiceViaContextDetectorTest.kt | 90 ++++------ .../BroadcastSentViaContextDetectorTest.kt | 127 ++++++++------- .../GetMainLooperViaContextDetectorTest.kt | 84 ++++------ .../RegisterReceiverViaContextDetectorTest.kt | 110 ++++--------- .../lint/SlowUserQueryDetectorTest.kt | 76 ++------- .../lint/SoftwareBitmapDetectorTest.kt | 52 +++--- 8 files changed, 355 insertions(+), 339 deletions(-) create mode 100644 packages/SystemUI/checks/tests/com/android/internal/systemui/lint/AndroidStubs.kt rename packages/SystemUI/checks/tests/com/android/{ => internal}/systemui/lint/BindServiceViaContextDetectorTest.kt (61%) rename packages/SystemUI/checks/tests/com/android/{ => internal}/systemui/lint/BroadcastSentViaContextDetectorTest.kt (62%) rename packages/SystemUI/checks/tests/com/android/{ => internal}/systemui/lint/GetMainLooperViaContextDetectorTest.kt (64%) rename packages/SystemUI/checks/tests/com/android/{ => internal}/systemui/lint/RegisterReceiverViaContextDetectorTest.kt (60%) rename packages/SystemUI/checks/tests/com/android/{ => internal}/systemui/lint/SlowUserQueryDetectorTest.kt (74%) rename packages/SystemUI/checks/tests/com/android/{ => internal}/systemui/lint/SoftwareBitmapDetectorTest.kt (70%) diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt index b72d03db0617d..eb71d32b2d8be 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt @@ -27,6 +27,7 @@ import com.android.tools.lint.detector.api.SourceCodeScanner import com.intellij.psi.PsiMethod import org.jetbrains.uast.UCallExpression +@Suppress("UnstableApiUsage") class RegisterReceiverViaContextDetector : Detector(), SourceCodeScanner { override fun getApplicableMethodNames(): List { diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/AndroidStubs.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/AndroidStubs.kt new file mode 100644 index 0000000000000..ba99da871a19f --- /dev/null +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/AndroidStubs.kt @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2022 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 com.android.internal.systemui.lint + +import com.android.tools.lint.checks.infrastructure.LintDetectorTest.java + +/* + * This file contains stubs of framework APIs and System UI classes for testing purposes only. The + * stubs are not used in the lint detectors themselves. + */ +@Suppress("UnstableApiUsage") +internal val androidStubs = + arrayOf( + java( + """ +package android.app; + +public class ActivityManager { + public static int getCurrentUser() {} +} +""" + ), + java( + """ +package android.os; +import android.content.pm.UserInfo; +import android.annotation.UserIdInt; + +public class UserManager { + public UserInfo getUserInfo(@UserIdInt int userId) {} +} +""" + ), + java(""" +package android.annotation; + +public @interface UserIdInt {} +"""), + java(""" +package android.content.pm; + +public class UserInfo {} +"""), + java(""" +package android.os; + +public class Looper {} +"""), + java(""" +package android.os; + +public class Handler {} +"""), + java(""" +package android.content; + +public class ServiceConnection {} +"""), + java(""" +package android.os; + +public enum UserHandle { + ALL +} +"""), + java( + """ +package android.content; +import android.os.UserHandle; +import android.os.Handler; +import android.os.Looper; +import java.util.concurrent.Executor; + +public class Context { + public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {} + public void registerReceiverAsUser( + BroadcastReceiver receiver, UserHandle user, IntentFilter filter, + String broadcastPermission, Handler scheduler) {} + public void registerReceiverForAllUsers( + BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, + Handler scheduler) {} + public void sendBroadcast(Intent intent) {} + public void sendBroadcast(Intent intent, String receiverPermission) {} + public void sendBroadcastAsUser(Intent intent, UserHandle userHandle, String permission) {} + public void bindService(Intent intent) {} + public void bindServiceAsUser( + Intent intent, ServiceConnection connection, int flags, UserHandle userHandle) {} + public void unbindService(ServiceConnection connection) {} + public Looper getMainLooper() { return null; } + public Executor getMainExecutor() { return null; } + public Handler getMainThreadHandler() { return null; } +} +""" + ), + java( + """ +package android.app; +import android.content.Context; + +public class Activity extends Context {} +""" + ), + java( + """ +package android.graphics; + +public class Bitmap { + public enum Config { + ARGB_8888, + RGB_565, + HARDWARE + } + public static Bitmap createBitmap(int width, int height, Config config) { + return null; + } +} +""" + ), + java(""" +package android.content; + +public class BroadcastReceiver {} +"""), + java(""" +package android.content; + +public class IntentFilter {} +"""), + java( + """ +package com.android.systemui.settings; +import android.content.pm.UserInfo; + +public interface UserTracker { + int getUserId(); + UserInfo getUserInfo(); +} +""" + ), + ) diff --git a/packages/SystemUI/checks/tests/com/android/systemui/lint/BindServiceViaContextDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceViaContextDetectorTest.kt similarity index 61% rename from packages/SystemUI/checks/tests/com/android/systemui/lint/BindServiceViaContextDetectorTest.kt rename to packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceViaContextDetectorTest.kt index bf685f7c178ec..564afcb773fdb 100644 --- a/packages/SystemUI/checks/tests/com/android/systemui/lint/BindServiceViaContextDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceViaContextDetectorTest.kt @@ -17,26 +17,26 @@ package com.android.internal.systemui.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles import com.android.tools.lint.checks.infrastructure.TestLintTask import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test +@Suppress("UnstableApiUsage") class BindServiceViaContextDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = BindServiceViaContextDetector() override fun lint(): TestLintTask = super.lint().allowMissingSdk(true) - override fun getIssues(): List = listOf( - BindServiceViaContextDetector.ISSUE) + override fun getIssues(): List = listOf(BindServiceViaContextDetector.ISSUE) private val explanation = "Binding or unbinding services are synchronous calls" @Test fun testBindService() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -49,17 +49,20 @@ class BindServiceViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(BindServiceViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(BindServiceViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } @Test fun testBindServiceAsUser() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -73,17 +76,20 @@ class BindServiceViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(BindServiceViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(BindServiceViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } @Test fun testUnbindService() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -96,45 +102,15 @@ class BindServiceViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(BindServiceViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(BindServiceViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } - private val contextStub: TestFile = java( - """ - package android.content; - import android.os.UserHandle; - - public class Context { - public void bindService(Intent intent) {}; - public void bindServiceAsUser(Intent intent, ServiceConnection connection, int flags, - UserHandle userHandle) {}; - public void unbindService(ServiceConnection connection) {}; - } - """ - ) - - private val serviceConnectionStub: TestFile = java( - """ - package android.content; - - public class ServiceConnection {} - """ - ) - - private val userHandleStub: TestFile = java( - """ - package android.os; - - public enum UserHandle { - ALL - } - """ - ) - - private val stubs = arrayOf(contextStub, serviceConnectionStub, userHandleStub) + private val stubs = androidStubs } diff --git a/packages/SystemUI/checks/tests/com/android/systemui/lint/BroadcastSentViaContextDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt similarity index 62% rename from packages/SystemUI/checks/tests/com/android/systemui/lint/BroadcastSentViaContextDetectorTest.kt rename to packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt index da010212f211b..06aee8e358984 100644 --- a/packages/SystemUI/checks/tests/com/android/systemui/lint/BroadcastSentViaContextDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt @@ -1,26 +1,43 @@ +/* + * Copyright (C) 2022 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 com.android.internal.systemui.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest import com.android.tools.lint.checks.infrastructure.TestFiles -import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestLintTask import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test +@Suppress("UnstableApiUsage") class BroadcastSentViaContextDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = BroadcastSentViaContextDetector() override fun lint(): TestLintTask = super.lint().allowMissingSdk(true) - override fun getIssues(): List = listOf( - BroadcastSentViaContextDetector.ISSUE) + override fun getIssues(): List = listOf(BroadcastSentViaContextDetector.ISSUE) @Test fun testSendBroadcast() { - lint().files( - TestFiles.java( - """ + println(stubs.size) + lint() + .files( + TestFiles.java( + """ package test.pkg; import android.content.Context; @@ -31,21 +48,25 @@ class BroadcastSentViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) + ) + .indented(), + *stubs + ) .issues(BroadcastSentViaContextDetector.ISSUE) .run() .expectWarningCount(1) .expectContains( - "Please don't call sendBroadcast/sendBroadcastAsUser directly on " + - "Context, use com.android.systemui.broadcast.BroadcastSender instead.") + "Please don't call sendBroadcast/sendBroadcastAsUser directly on " + + "Context, use com.android.systemui.broadcast.BroadcastSender instead." + ) } @Test fun testSendBroadcastAsUser() { - lint().files( - TestFiles.java( - """ + lint() + .files( + TestFiles.java( + """ package test.pkg; import android.content.Context; import android.os.UserHandle; @@ -56,21 +77,26 @@ class BroadcastSentViaContextDetectorTest : LintDetectorTest() { context.sendBroadcastAsUser(intent, UserHandle.ALL, "permission"); } } - """).indented(), - *stubs) + """ + ) + .indented(), + *stubs + ) .issues(BroadcastSentViaContextDetector.ISSUE) .run() .expectWarningCount(1) .expectContains( - "Please don't call sendBroadcast/sendBroadcastAsUser directly on " + - "Context, use com.android.systemui.broadcast.BroadcastSender instead.") + "Please don't call sendBroadcast/sendBroadcastAsUser directly on " + + "Context, use com.android.systemui.broadcast.BroadcastSender instead." + ) } @Test fun testSendBroadcastInActivity() { - lint().files( - TestFiles.java( - """ + lint() + .files( + TestFiles.java( + """ package test.pkg; import android.app.Activity; import android.os.UserHandle; @@ -82,21 +108,26 @@ class BroadcastSentViaContextDetectorTest : LintDetectorTest() { } } - """).indented(), - *stubs) + """ + ) + .indented(), + *stubs + ) .issues(BroadcastSentViaContextDetector.ISSUE) .run() .expectWarningCount(1) .expectContains( - "Please don't call sendBroadcast/sendBroadcastAsUser directly on " + - "Context, use com.android.systemui.broadcast.BroadcastSender instead.") + "Please don't call sendBroadcast/sendBroadcastAsUser directly on " + + "Context, use com.android.systemui.broadcast.BroadcastSender instead." + ) } @Test fun testNoopIfNoCall() { - lint().files( - TestFiles.java( - """ + lint() + .files( + TestFiles.java( + """ package test.pkg; import android.content.Context; @@ -106,45 +137,15 @@ class BroadcastSentViaContextDetectorTest : LintDetectorTest() { context.startActivity(intent); } } - """).indented(), - *stubs) + """ + ) + .indented(), + *stubs + ) .issues(BroadcastSentViaContextDetector.ISSUE) .run() .expectClean() } - private val contextStub: TestFile = java( - """ - package android.content; - import android.os.UserHandle; - - public class Context { - public void sendBroadcast(Intent intent) {}; - public void sendBroadcast(Intent intent, String receiverPermission) {}; - public void sendBroadcastAsUser(Intent intent, UserHandle userHandle, - String permission) {}; - } - """ - ) - - private val activityStub: TestFile = java( - """ - package android.app; - import android.content.Context; - - public class Activity extends Context {} - """ - ) - - private val userHandleStub: TestFile = java( - """ - package android.os; - - public enum UserHandle { - ALL - } - """ - ) - - private val stubs = arrayOf(contextStub, activityStub, userHandleStub) + private val stubs = androidStubs } diff --git a/packages/SystemUI/checks/tests/com/android/systemui/lint/GetMainLooperViaContextDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/GetMainLooperViaContextDetectorTest.kt similarity index 64% rename from packages/SystemUI/checks/tests/com/android/systemui/lint/GetMainLooperViaContextDetectorTest.kt rename to packages/SystemUI/checks/tests/com/android/internal/systemui/lint/GetMainLooperViaContextDetectorTest.kt index ec761cd7660d2..c55f3995f1027 100644 --- a/packages/SystemUI/checks/tests/com/android/systemui/lint/GetMainLooperViaContextDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/GetMainLooperViaContextDetectorTest.kt @@ -17,13 +17,13 @@ package com.android.internal.systemui.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles import com.android.tools.lint.checks.infrastructure.TestLintTask import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test +@Suppress("UnstableApiUsage") class GetMainLooperViaContextDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = GetMainLooperViaContextDetector() @@ -35,7 +35,8 @@ class GetMainLooperViaContextDetectorTest : LintDetectorTest() { @Test fun testGetMainThreadHandler() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -48,17 +49,20 @@ class GetMainLooperViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(GetMainLooperViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(GetMainLooperViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } @Test fun testGetMainLooper() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -71,17 +75,20 @@ class GetMainLooperViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(GetMainLooperViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(GetMainLooperViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } @Test fun testGetMainExecutor() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -94,42 +101,15 @@ class GetMainLooperViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(GetMainLooperViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(GetMainLooperViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } - private val contextStub: TestFile = java( - """ - package android.content; - import android.os.Handler;import android.os.Looper;import java.util.concurrent.Executor; - - public class Context { - public Looper getMainLooper() { return null; }; - public Executor getMainExecutor() { return null; }; - public Handler getMainThreadHandler() { return null; }; - } - """ - ) - - private val looperStub: TestFile = java( - """ - package android.os; - - public class Looper {} - """ - ) - - private val handlerStub: TestFile = java( - """ - package android.os; - - public class Handler {} - """ - ) - - private val stubs = arrayOf(contextStub, looperStub, handlerStub) + private val stubs = androidStubs } diff --git a/packages/SystemUI/checks/tests/com/android/systemui/lint/RegisterReceiverViaContextDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt similarity index 60% rename from packages/SystemUI/checks/tests/com/android/systemui/lint/RegisterReceiverViaContextDetectorTest.kt rename to packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt index 76c0519d917d3..802ceba4196cb 100644 --- a/packages/SystemUI/checks/tests/com/android/systemui/lint/RegisterReceiverViaContextDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt @@ -17,26 +17,26 @@ package com.android.internal.systemui.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles import com.android.tools.lint.checks.infrastructure.TestLintTask import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test +@Suppress("UnstableApiUsage") class RegisterReceiverViaContextDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = RegisterReceiverViaContextDetector() override fun lint(): TestLintTask = super.lint().allowMissingSdk(true) - override fun getIssues(): List = listOf( - RegisterReceiverViaContextDetector.ISSUE) + override fun getIssues(): List = listOf(RegisterReceiverViaContextDetector.ISSUE) private val explanation = "BroadcastReceivers should be registered via BroadcastDispatcher." @Test fun testRegisterReceiver() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -51,17 +51,20 @@ class RegisterReceiverViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(RegisterReceiverViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(RegisterReceiverViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } @Test fun testRegisterReceiverAsUser() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -79,17 +82,20 @@ class RegisterReceiverViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(RegisterReceiverViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(RegisterReceiverViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } @Test fun testRegisterReceiverForAllUsers() { - lint().files( + lint() + .files( TestFiles.java( """ package test.pkg; @@ -107,65 +113,15 @@ class RegisterReceiverViaContextDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(RegisterReceiverViaContextDetector.ISSUE) - .run() - .expectWarningCount(1) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(RegisterReceiverViaContextDetector.ISSUE) + .run() + .expectWarningCount(1) + .expectContains(explanation) } - private val contextStub: TestFile = java( - """ - package android.content; - import android.os.Handler; - import android.os.UserHandle; - - public class Context { - public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter, - int flags) {}; - public void registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, - IntentFilter filter, String broadcastPermission, Handler scheduler) {}; - public void registerReceiverForAllUsers(BroadcastReceiver receiver, IntentFilter filter, - String broadcastPermission, Handler scheduler) {}; - } - """ - ) - - private val broadcastReceiverStub: TestFile = java( - """ - package android.content; - - public class BroadcastReceiver {} - """ - ) - - private val intentFilterStub: TestFile = java( - """ - package android.content; - - public class IntentFilter {} - """ - ) - - private val handlerStub: TestFile = java( - """ - package android.os; - - public class Handler {} - """ - ) - - private val userHandleStub: TestFile = java( - """ - package android.os; - - public enum UserHandle { - ALL - } - """ - ) - - private val stubs = arrayOf(contextStub, broadcastReceiverStub, intentFilterStub, handlerStub, - userHandleStub) + private val stubs = androidStubs } diff --git a/packages/SystemUI/checks/tests/com/android/systemui/lint/SlowUserQueryDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt similarity index 74% rename from packages/SystemUI/checks/tests/com/android/systemui/lint/SlowUserQueryDetectorTest.kt rename to packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt index 2738f0409fd05..e26583793e205 100644 --- a/packages/SystemUI/checks/tests/com/android/systemui/lint/SlowUserQueryDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt @@ -1,13 +1,29 @@ +/* + * Copyright (C) 2022 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 com.android.internal.systemui.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles import com.android.tools.lint.checks.infrastructure.TestLintTask import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test +@Suppress("UnstableApiUsage") class SlowUserQueryDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = SlowUserQueryDetector() @@ -134,61 +150,5 @@ class SlowUserQueryDetectorTest : LintDetectorTest() { .expectClean() } - private val activityManagerStub: TestFile = - java( - """ - package android.app; - - public class ActivityManager { - public static int getCurrentUser() {}; - } - """ - ) - - private val userManagerStub: TestFile = - java( - """ - package android.os; - import android.content.pm.UserInfo; - import android.annotation.UserIdInt; - - public class UserManager { - public UserInfo getUserInfo(@UserIdInt int userId) {}; - } - """ - ) - - private val userIdIntStub: TestFile = - java( - """ - package android.annotation; - - public @interface UserIdInt {} - """ - ) - - private val userInfoStub: TestFile = - java( - """ - package android.content.pm; - - public class UserInfo {} - """ - ) - - private val userTrackerStub: TestFile = - java( - """ - package com.android.systemui.settings; - import android.content.pm.UserInfo; - - public interface UserTracker { - public int getUserId(); - public UserInfo getUserInfo(); - } - """ - ) - - private val stubs = - arrayOf(activityManagerStub, userManagerStub, userIdIntStub, userInfoStub, userTrackerStub) + private val stubs = androidStubs } diff --git a/packages/SystemUI/checks/tests/com/android/systemui/lint/SoftwareBitmapDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt similarity index 70% rename from packages/SystemUI/checks/tests/com/android/systemui/lint/SoftwareBitmapDetectorTest.kt rename to packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt index 890f2b8eb924c..fd6ab09a2ccde 100644 --- a/packages/SystemUI/checks/tests/com/android/systemui/lint/SoftwareBitmapDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt @@ -17,7 +17,6 @@ package com.android.internal.systemui.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest -import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles import com.android.tools.lint.checks.infrastructure.TestLintTask import com.android.tools.lint.detector.api.Detector @@ -36,7 +35,8 @@ class SoftwareBitmapDetectorTest : LintDetectorTest() { @Test fun testSoftwareBitmap() { - lint().files( + lint() + .files( TestFiles.java( """ import android.graphics.Bitmap; @@ -48,17 +48,20 @@ class SoftwareBitmapDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(SoftwareBitmapDetector.ISSUE) - .run() - .expectWarningCount(2) - .expectContains(explanation) + ) + .indented(), + *stubs + ) + .issues(SoftwareBitmapDetector.ISSUE) + .run() + .expectWarningCount(2) + .expectContains(explanation) } @Test fun testHardwareBitmap() { - lint().files( + lint() + .files( TestFiles.java( """ import android.graphics.Bitmap; @@ -69,29 +72,14 @@ class SoftwareBitmapDetectorTest : LintDetectorTest() { } } """ - ).indented(), - *stubs) - .issues(SoftwareBitmapDetector.ISSUE) - .run() - .expectWarningCount(0) + ) + .indented(), + *stubs + ) + .issues(SoftwareBitmapDetector.ISSUE) + .run() + .expectWarningCount(0) } - private val bitmapStub: TestFile = java( - """ - package android.graphics; - - public class Bitmap { - public enum Config { - ARGB_8888, - RGB_565, - HARDWARE - } - public static Bitmap createBitmap(int width, int height, Config config) { - return null; - } - } - """ - ) - - private val stubs = arrayOf(bitmapStub) + private val stubs = androidStubs }