Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ba364329 authored by Jason Monk's avatar Jason Monk
Browse files

Add version of @UiThreadTest that works at class level

Saves a bunch of lines of tagging every test with @UiThreadTest

Test: runtest systemui
Change-Id: Id565dbe6bf1ae3dd7b3b23c507d1b3c386a36974
parent 137b460e
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ public class SysUIRunner extends BlockJUnit4ClassRunner {

    @Override
    protected Statement methodInvoker(FrameworkMethod method, Object test) {
        return UiThreadStatement.shouldRunOnUiThread(method) ? new UiThreadStatement(
        return shouldRunOnUiThread(method) ? new UiThreadStatement(
                methodInvokerInt(method, test), true) : methodInvokerInt(method, test);
    }

@@ -84,4 +84,12 @@ public class SysUIRunner extends BlockJUnit4ClassRunner {
    private long getTimeout(Test annotation) {
        return annotation == null ? 0L : annotation.timeout();
    }

    public boolean shouldRunOnUiThread(FrameworkMethod method) {
        if (mKlass.getAnnotation(UiThreadTest.class) != null) {
            return true;
        } else {
            return UiThreadStatement.shouldRunOnUiThread(method);
        }
    }
}
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * When applied to a class, all tests, befores, and afters will behave as if
 * they have @UiThreadTest applied to them.
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UiThreadTest {
}
+4 −22
Original line number Diff line number Diff line
@@ -37,11 +37,12 @@ import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.support.test.annotation.UiThreadTest;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.view.Display;

import com.android.systemui.SysUIRunner;
import com.android.systemui.UiThreadTest;
import com.android.systemui.statusbar.phone.DozeParameters;

import org.junit.Before;
@@ -49,7 +50,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(SysUIRunner.class)
@UiThreadTest
public class DozeMachineTest {

    DozeMachine mMachine;
@@ -72,7 +74,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testInitialize_initializesParts() {
        mMachine.requestState(INITIALIZED);

@@ -80,7 +81,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testInitialize_goesToDoze() {
        when(mParamsMock.getAlwaysOn()).thenReturn(false);

@@ -91,7 +91,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testInitialize_goesToAod() {
        when(mParamsMock.getAlwaysOn()).thenReturn(true);

@@ -102,7 +101,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testPulseDone_goesToDoze() {
        when(mParamsMock.getAlwaysOn()).thenReturn(false);
        mMachine.requestState(INITIALIZED);
@@ -116,7 +114,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testPulseDone_goesToAoD() {
        when(mParamsMock.getAlwaysOn()).thenReturn(true);
        mMachine.requestState(INITIALIZED);
@@ -130,7 +127,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testFinished_staysFinished() {
        mMachine.requestState(INITIALIZED);
        mMachine.requestState(FINISH);
@@ -143,7 +139,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testFinish_finishesService() {
        mMachine.requestState(INITIALIZED);

@@ -153,7 +148,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testWakeLock_heldInTransition() {
        doAnswer((inv) -> {
            assertTrue(mWakeLockFake.isHeld());
@@ -164,7 +158,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testWakeLock_heldInPulseStates() {
        mMachine.requestState(INITIALIZED);

@@ -176,7 +169,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testWakeLock_notHeldInDozeStates() {
        mMachine.requestState(INITIALIZED);

@@ -188,7 +180,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testWakeLock_releasedAfterPulse() {
        mMachine.requestState(INITIALIZED);

@@ -201,7 +192,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testPulseDuringPulse_doesntCrash() {
        mMachine.requestState(INITIALIZED);

@@ -213,7 +203,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testSuppressingPulse_doesntCrash() {
        mMachine.requestState(INITIALIZED);

@@ -223,7 +212,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testScreen_offInDoze() {
        mMachine.requestState(INITIALIZED);

@@ -233,7 +221,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testScreen_onInAod() {
        mMachine.requestState(INITIALIZED);

@@ -243,7 +230,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testScreen_onInPulse() {
        mMachine.requestState(INITIALIZED);

@@ -254,7 +240,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testScreen_offInRequestPulseWithoutAoD() {
        mMachine.requestState(INITIALIZED);

@@ -265,7 +250,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testScreen_onInRequestPulseWithoutAoD() {
        mMachine.requestState(INITIALIZED);

@@ -276,7 +260,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testTransitions_canRequestTransitions() {
        mMachine.requestState(INITIALIZED);
        mMachine.requestState(DOZE);
@@ -291,7 +274,6 @@ public class DozeMachineTest {
    }

    @Test
    @UiThreadTest
    public void testWakeUp_wakesUp() {
        mMachine.wakeUp();

+4 −15
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@ package com.android.systemui.notification;

import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.support.test.annotation.UiThreadTest;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

@@ -28,7 +27,9 @@ import android.util.Property;
import android.view.View;
import android.view.animation.Interpolator;

import com.android.systemui.SysUIRunner;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.UiThreadTest;
import com.android.systemui.statusbar.notification.PropertyAnimator;
import com.android.systemui.statusbar.stack.AnimationFilter;
import com.android.systemui.statusbar.stack.AnimationProperties;
@@ -49,7 +50,8 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(SysUIRunner.class)
@UiThreadTest
public class PropertyAnimatorTest extends SysuiTestCase {

    private View mView;
@@ -106,13 +108,11 @@ public class PropertyAnimatorTest extends SysuiTestCase {


    @Before
    @UiThreadTest
    public void setUp() {
        mView = new View(getContext());
    }

    @Test
    @UiThreadTest
    public void testAnimationStarted() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -121,7 +121,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testNoAnimationStarted() {
        mAnimationFilter.reset();
        PropertyAnimator.startAnimation(mView, mProperty, 200, mAnimationProperties);
@@ -129,7 +128,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testEndValueUpdated() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -139,7 +137,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testStartTagUpdated() {
        mEffectiveProperty.set(mView, 100f);
        mAnimationFilter.reset();
@@ -150,7 +147,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testValueIsSetUnAnimated() {
        mAnimationFilter.reset();
        PropertyAnimator.startAnimation(mView, mProperty, 200f, mAnimationProperties);
@@ -158,7 +154,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testAnimationToRightValueUpdated() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -171,7 +166,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testAnimationToRightValueUpdateAnimated() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -185,7 +179,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testStartTagShiftedWhenChanging() {
        mEffectiveProperty.set(mView, 100f);
        mAnimationFilter.reset();
@@ -198,7 +191,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testUsingDuration() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -210,7 +202,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testUsingDelay() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -222,7 +213,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testUsingInterpolator() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
@@ -234,7 +224,6 @@ public class PropertyAnimatorTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testUsingListener() {
        mAnimationFilter.reset();
        mAnimationFilter.animate(mProperty.getProperty());
+5 −31
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import android.content.pm.ParceledListSlice;
import android.graphics.drawable.Drawable;
import android.service.notification.StatusBarNotification;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
@@ -55,7 +54,10 @@ import android.widget.Switch;
import android.widget.TextView;
import com.android.internal.util.CharSequences;
import com.android.systemui.R;
import com.android.systemui.SysUIRunner;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.UiThreadTest;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.Test;
@@ -65,7 +67,8 @@ import java.util.Collections;
import java.util.concurrent.CountDownLatch;

@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(SysUIRunner.class)
@UiThreadTest
public class NotificationInfoTest extends SysuiTestCase {
    private static final String TEST_PACKAGE_NAME = "test_package";
    private static final String TEST_CHANNEL = "test_channel";
@@ -79,7 +82,6 @@ public class NotificationInfoTest extends SysuiTestCase {
            mock(StatusBarNotification.class);

    @Before
    @UiThreadTest
    public void setUp() throws Exception {
        // Inflate the layout
        final LayoutInflater layoutInflater =
@@ -116,7 +118,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsTextApplicationName() throws Exception {
        when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name");
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -126,7 +127,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsPackageIcon() throws Exception {
        final Drawable iconDrawable = mock(Drawable.class);
        when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class)))
@@ -138,7 +138,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_GroupNameHiddenIfNoGroup() throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                mMockStatusBarNotification, mNotificationChannel, null, null, null);
@@ -150,7 +149,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsGroupNameIfNonNull() throws Exception {
        mNotificationChannel.setGroup("test_group_id");
        final NotificationChannelGroup notificationChannelGroup =
@@ -169,7 +167,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsGroupName_resId() throws Exception {
        when(mMockPackageManager.getText(eq(TEST_PACKAGE_NAME),
                eq(R.string.legacy_vpn_name), anyObject())).thenReturn(
@@ -191,7 +188,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsTextChannelName() throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                mMockStatusBarNotification, mNotificationChannel, null, null, null);
@@ -200,7 +196,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsTextChannelName_resId() throws Exception {
        when(mMockPackageManager.getText(eq(TEST_PACKAGE_NAME),
                eq(R.string.notification_menu_accessibility), anyObject())).thenReturn(
@@ -216,7 +211,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -231,7 +225,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SettingsTextWithOneChannel() throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                mMockStatusBarNotification, mNotificationChannel, (View v, int appUid) -> {}, null,
@@ -242,7 +235,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SettingsTextWithMultipleChannels() throws Exception {
        when(mMockINotificationManager.getNumNotificationChannelsForPackage(
                eq(TEST_PACKAGE_NAME), anyInt(), anyBoolean())).thenReturn(2);
@@ -255,7 +247,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_SetsOnClickListenerForDone() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -270,7 +261,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_NumChannelsTextHiddenWhenDefaultChannel() throws Exception {
        final NotificationChannel defaultChannel = new NotificationChannel(
                NotificationChannel.DEFAULT_CHANNEL_ID, TEST_CHANNEL_NAME,
@@ -283,7 +273,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_NumChannelsTextDisplaysWhenNotDefaultChannel()
            throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -295,7 +284,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_NumChannelsTextScalesWithNumberOfChannels()
            throws Exception {
        when(mMockINotificationManager.getNumNotificationChannelsForPackage(
@@ -308,7 +296,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testbindNotification_ChannelDisabledTextGoneWhenNotDisabled() throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                mMockStatusBarNotification, mNotificationChannel, null, null, null);
@@ -318,7 +305,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testbindNotification_ChannelDisabledTextVisibleWhenDisabled() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -333,7 +319,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testHasImportanceChanged_DefaultsToFalse() throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                mMockStatusBarNotification, mNotificationChannel, null, null, null);
@@ -341,7 +326,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testHasImportanceChanged_ReturnsTrueAfterChannelDisabled() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -353,7 +337,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                mMockStatusBarNotification, mNotificationChannel, null, null, null);
@@ -362,7 +345,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -375,7 +357,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
            throws Exception {
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -387,7 +368,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
            throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
@@ -400,7 +380,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testEnabledSwitchOnByDefault() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -411,7 +390,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testEnabledButtonOffWhenAlreadyBanned() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -422,7 +400,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testEnabledSwitchVisibleByDefault() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -433,7 +410,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testEnabledSwitchInvisibleIfNonBlockable() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -445,7 +421,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testEnabledSwitchChangedCallsUpdateNotificationChannel() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
@@ -460,7 +435,6 @@ public class NotificationInfoTest extends SysuiTestCase {
    }

    @Test
    @UiThreadTest
    public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
        mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,