Skip to content

Commit

Permalink
Test old/new thread annotations for call graph
Browse files Browse the repository at this point in the history
In WrongThreadInterproceduralTest, test with both the old
android.support thread annotations and the new androidx
thread annotations.

Test: Unit tests in tools/idea/callgraph
Bug: n/a
Change-Id: I85f0244e6c05e64d7a6e3d8f04264fa57cc3480a
  • Loading branch information
gharrma authored and intellij-monorepo-bot committed Jul 27, 2018
1 parent bb89196 commit 961fd9c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
20 changes: 20 additions & 0 deletions kotlin-integration/testData/callgraph/AndroidxAnnotations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2018 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.annotation;

public @interface UiThread {}

public @interface WorkerThread {}
20 changes: 20 additions & 0 deletions kotlin-integration/testData/callgraph/AndroidxAnnotations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2018 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.annotation

annotation class UiThread

annotation class WorkerThread
20 changes: 20 additions & 0 deletions kotlin-integration/testData/callgraph/SupportAnnotations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2018 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 android.support.annotation;

public @interface UiThread {}

public @interface WorkerThread {}
20 changes: 20 additions & 0 deletions kotlin-integration/testData/callgraph/SupportAnnotations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2018 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 android.support.annotation

annotation class UiThread

annotation class WorkerThread
13 changes: 8 additions & 5 deletions kotlin-integration/testData/callgraph/ThreadAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.annotation;

public @interface UiThread {}

public @interface WorkerThread {}
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;

@FunctionalInterface
public interface Runnable {
public abstract void run();
}

class Test {

// Make sure the old annotation names still work.
@android.support.annotation.UiThread static void oldAnnotationA() { oldAnnotationB() }
static void oldAnnotationB() { oldAnnotationC() }
@android.support.annotation.WorkerThread static void oldAnnotationC() {}

@UiThread static void uiThreadStatic() { unannotatedStatic(); }
static void unannotatedStatic() { workerThreadStatic(); }
@WorkerThread static void workerThreadStatic() {}
Expand Down
12 changes: 7 additions & 5 deletions kotlin-integration/testData/callgraph/ThreadAnnotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.annotation

annotation class UiThread

annotation class WorkerThread
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread

class Test {

// Make sure the old annotation names still work.
@android.support.annotation.UiThread fun oldAnnotationA() { oldAnnotationB() }
fun oldAnnotationB() { oldAnnotationC() }
@android.support.annotation.WorkerThread fun oldAnnotationC() {}

@UiThread fun uiThread() { unannotated() }
fun unannotated() { workerThread() }
@WorkerThread fun workerThread() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ class WrongThreadInterproceduralTest : AndroidTestCase() {

fun testKotlinThreadAnnotations() = doTest(".kt")

private fun addFile(file: String) = myFixture.copyFileToProject("callgraph/$file", "src/$file")

private fun doTest(ext: String) {
myFixture.testDataPath = PathManager.getHomePath() + "/../adt/idea/kotlin-integration/testData"
val virtualFile = myFixture.copyFileToProject("callgraph/ThreadAnnotations$ext", "src/ThreadAnnotations$ext")

// Most of the test uses the new AndroidX annotations, but we make sure that the old annotations work too.
addFile("AndroidxAnnotations$ext")
addFile("SupportAnnotations$ext")

val virtualFile = addFile("ThreadAnnotations$ext")
val (_, receiverEval, graph) = buildInterproceduralAnalysesForTest(virtualFile, myFixture.project)
val paths = searchForInterproceduralThreadAnnotationViolations(graph, receiverEval)

Expand All @@ -43,6 +50,7 @@ class WrongThreadInterproceduralTest : AndroidTestCase() {
.joinToString(separator = "\n")

val expectedPathStrs = listOf(
"Test#oldAnnotationA -> Test#oldAnnotationB -> Test#oldAnnotationC",
"Test#uiThreadStatic -> Test#unannotatedStatic -> Test#workerThreadStatic",
"Test#uiThread -> Test#unannotated -> Test#workerThread",
"Test#callRunIt -> Test#runIt -> Test#callRunIt#lambda -> Test#runUi",
Expand Down

0 comments on commit 961fd9c

Please sign in to comment.