diff options
| author | 2024-09-27 03:38:09 +0000 | |
|---|---|---|
| committer | 2024-10-01 20:03:08 +0000 | |
| commit | 46c3608b3967ff65e4d61a7e602c49834fcce511 (patch) | |
| tree | 80ca33cb84e3c460c2388d8399dec0f1f72d9386 | |
| parent | 88d879f655fb61d2f98bb508adfa376046345207 (diff) | |
Explicit UiThreadTest, Rule to block @UiThreadTest
The @UiThreadTest annotation wasn't working with AndroidJUnit, because AndroidJUnit4ClassRunner was replacing with FailOnTimeout. This rule will assert on the use of @UiThreadTest, and a utility to execute runnables on the UI thread explcitly, with associated tests.
Bug: 369878945
Test: tested manually with atest
Flag: TEST_ONLY
Change-Id: I583bb0878b1dfe21fb17eec30dc600026ba482f4
4 files changed, 218 insertions, 0 deletions
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/util/NoUiThreadTestRule.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/util/NoUiThreadTestRule.kt new file mode 100644 index 000000000000..e4c793dae950 --- /dev/null +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/util/NoUiThreadTestRule.kt @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 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.systemui.util + +import android.testing.UiThreadTest +import org.junit.Assert.fail +import org.junit.rules.MethodRule +import org.junit.runners.model.FrameworkMethod +import org.junit.runners.model.Statement + +/** + * A Test rule which prevents us from using the UiThreadTest annotation. See + * go/android_junit4_uithreadtest (b/352170965) + */ +public class NoUiThreadTestRule : MethodRule { + override fun apply(base: Statement, method: FrameworkMethod, target: Any): Statement? { + if (hasUiThreadAnnotation(method, target)) { + fail("UiThreadTest doesn't actually run on the UiThread") + } + return base + } + + private fun hasUiThreadAnnotation(method: FrameworkMethod, target: Any): Boolean { + if (method.getAnnotation(UiThreadTest::class.java) != null) { + return true + } else { + return target.javaClass.getAnnotation(UiThreadTest::class.java) != null + } + } +} diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/util/NoUiThreadTestRuleTest.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/util/NoUiThreadTestRuleTest.kt new file mode 100644 index 000000000000..70dd10384db6 --- /dev/null +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/util/NoUiThreadTestRuleTest.kt @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2024 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.systemui.util + +import android.testing.UiThreadTest +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SmallTest +import com.android.systemui.SysuiTestCase +import java.lang.AssertionError +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.model.FrameworkMethod +import org.junit.runners.model.Statement + +/** + * Test that NoUiThreadTestRule asserts when it finds a framework method with a UiThreadTest + * annotation. + */ +@RunWith(AndroidJUnit4::class) +@SmallTest +public class NoUiThreadTestRuleTest : SysuiTestCase() { + + class TestStatement : Statement() { + override fun evaluate() {} + } + + inner class TestInner { + @Test @UiThreadTest fun simpleUiTest() {} + + @Test fun simpleTest() {} + } + + /** + * Test that NoUiThreadTestRule throws an asserts false if a test is annotated + * with @UiThreadTest + */ + @Test(expected = AssertionError::class) + fun testNoUiThreadFail() { + val method = TestInner::class.java.getDeclaredMethod("simpleUiTest") + val frameworkMethod = FrameworkMethod(method) + val noUiThreadTestRule = NoUiThreadTestRule() + val testStatement = TestStatement() + // target needs to be non-null + val obj = Object() + noUiThreadTestRule.apply(testStatement, frameworkMethod, obj) + } + + /** + * Test that NoUiThreadTestRule throws an asserts false if a test is annotated + * with @UiThreadTest + */ + fun testNoUiThreadOK() { + val method = TestInner::class.java.getDeclaredMethod("simpleUiTest") + val frameworkMethod = FrameworkMethod(method) + val noUiThreadTestRule = NoUiThreadTestRule() + val testStatement = TestStatement() + + // because target needs to be non-null + val obj = Object() + val newStatement = noUiThreadTestRule.apply(testStatement, frameworkMethod, obj) + Assert.assertEquals(newStatement, testStatement) + } +} diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/util/UiThread.java b/packages/SystemUI/tests/utils/src/com/android/systemui/util/UiThread.java new file mode 100644 index 000000000000..f81b7de86d00 --- /dev/null +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/util/UiThread.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2023 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.systemui.util; + +import android.os.Looper; +import android.util.Log; + +import androidx.test.platform.app.InstrumentationRegistry; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; + +/** + * A class to launch runnables on the UI thread explicitly. + */ +public class UiThread { + private static final String TAG = "UiThread"; + + /** + * Run a runnable on the UI thread using instrumentation.runOnMainSync. + * + * @param runnable code to run on the UI thread. + * @throws Throwable if the code threw an exception, so it can be reported + * to the test. + */ + public static void runOnUiThread(final Runnable runnable) throws Throwable { + if (Looper.myLooper() == Looper.getMainLooper()) { + Log.w( + TAG, + "UiThread.runOnUiThread() should not be called from the " + + "main application thread"); + runnable.run(); + } else { + FutureTask<Void> task = new FutureTask<>(runnable, null); + InstrumentationRegistry.getInstrumentation().runOnMainSync(task); + try { + task.get(); + } catch (ExecutionException e) { + // Expose the original exception + throw e.getCause(); + } + } + } +} diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/util/UiThreadRunTest.java b/packages/SystemUI/tests/utils/src/com/android/systemui/util/UiThreadRunTest.java new file mode 100644 index 000000000000..abf2e8d04d7e --- /dev/null +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/util/UiThreadRunTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023 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.systemui.util; + +import android.os.Looper; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import com.android.systemui.SysuiTestCase; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + + +/** + * Test that UiThread.runOnUiThread() actually runs on the UI Thread. + */ +@RunWith(AndroidJUnit4.class) +@SmallTest +public class UiThreadRunTest extends SysuiTestCase { + + @Test + public void testUiThread() throws Throwable { + UiThread.runOnUiThread(() -> { + Assert.assertEquals(Looper.getMainLooper().getThread(), Thread.currentThread()); + }); + } +} |