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

Commit 21fb3504 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Revert "Revert "Integrate new looper apis into testables""" into oc-dev

parents 1c35554c 745d0a8b
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothProfile;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.testing.TestableLooper.RunWithLooper;

import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -31,10 +33,13 @@ import com.android.systemui.SysuiTestCase;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

@RunWith(AndroidTestingRunner.class)
@RunWithLooper
public class BluetoothControllerImplTest extends SysuiTestCase {

    private LocalBluetoothManager mMockBluetoothManager;
@@ -47,7 +52,7 @@ public class BluetoothControllerImplTest extends SysuiTestCase {

    @Before
    public void setup() throws Exception {
        mTestableLooper = new TestableLooper();
        mTestableLooper = TestableLooper.get(this);
        mMockBluetoothManager = mDependency.injectMockDependency(LocalBluetoothManager.class);
        mDevices = new ArrayList<>();
        mMockDeviceManager = mock(CachedBluetoothDeviceManager.class);
+6 −39
Original line number Diff line number Diff line
@@ -53,9 +53,10 @@ import android.os.Process;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.test.annotation.UiThreadTest;
import android.support.test.InstrumentationRegistry;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.testing.TestableLooper.RunWithLooper;

import java.util.ArrayList;
import java.util.Arrays;
@@ -63,6 +64,7 @@ import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
@@ -70,6 +72,8 @@ import org.mockito.MockitoAnnotations;
import com.android.server.lights.Light;
import com.android.server.lights.LightsManager;

@RunWith(AndroidTestingRunner.class)
@RunWithLooper
public class NotificationManagerServiceTest {
    private static final long WAIT_FOR_IDLE_TIMEOUT = 2;
    private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
@@ -109,7 +113,6 @@ public class NotificationManagerServiceTest {
    }

    @Before
    @UiThreadTest
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mNotificationManagerService = new TestableNotificationManagerService(mContext);
@@ -124,7 +127,7 @@ public class NotificationManagerServiceTest {
        final LightsManager mockLightsManager = mock(LightsManager.class);
        when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class));
        // Use this testable looper.
        mTestableLooper = new TestableLooper(false);
        mTestableLooper = TestableLooper.get(this);

        mListener = mNotificationListeners.new ManagedServiceInfo(
                null, new ComponentName(PKG, "test_class"), uid, true, null, 0);
@@ -165,7 +168,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateNotificationChannels_SingleChannel() throws Exception {
        final NotificationChannel channel =
                new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
@@ -177,7 +179,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
        try {
            mBinderService.createNotificationChannels("test_pkg",
@@ -189,7 +190,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateNotificationChannels_TwoChannels() throws Exception {
        final NotificationChannel channel1 =
                new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT);
@@ -202,7 +202,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance()
            throws Exception {
        final NotificationChannel channel =
@@ -221,7 +220,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
            throws Exception {
        final NotificationChannel channel1 =
@@ -236,7 +234,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testBlockedNotifications_suspended() throws Exception {
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true);
@@ -249,7 +246,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testBlockedNotifications_blockedChannel() throws Exception {
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
@@ -263,7 +259,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testBlockedNotifications_blockedApp() throws Exception {
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
@@ -277,7 +272,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
                generateNotificationRecord(null).getNotification(), 0);
@@ -288,7 +282,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception {
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
                generateNotificationRecord(null).getNotification(), 0);
@@ -300,7 +293,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelNotificationWhilePostedAndEnqueued() throws Exception {
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
                generateNotificationRecord(null).getNotification(), 0);
@@ -315,7 +307,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
@@ -328,7 +319,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
@@ -341,7 +331,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelAllNotifications_IgnoreForegroundService() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
@@ -355,7 +344,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
@@ -369,7 +357,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
@@ -382,7 +369,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
@@ -396,7 +382,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
@@ -411,7 +396,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testTvExtenderChannelOverride_onTv() throws Exception {
        mNotificationManagerService.setIsTelevision(true);
        mNotificationManagerService.setRankingHelper(mRankingHelper);
@@ -427,7 +411,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testTvExtenderChannelOverride_notOnTv() throws Exception {
        mNotificationManagerService.setIsTelevision(false);
        mNotificationManagerService.setRankingHelper(mRankingHelper);
@@ -443,7 +426,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateChannelNotifyListener() throws Exception {
        List<String> associations = new ArrayList<>();
        associations.add("a");
@@ -469,7 +451,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testCreateChannelGroupNotifyListener() throws Exception {
        List<String> associations = new ArrayList<>();
        associations.add("a");
@@ -490,7 +471,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testUpdateChannelNotifyListener() throws Exception {
        List<String> associations = new ArrayList<>();
        associations.add("a");
@@ -509,7 +489,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testDeleteChannelNotifyListener() throws Exception {
        List<String> associations = new ArrayList<>();
        associations.add("a");
@@ -526,7 +505,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testDeleteChannelGroupNotifyListener() throws Exception {
        List<String> associations = new ArrayList<>();
        associations.add("a");
@@ -543,7 +521,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -561,7 +538,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -583,7 +559,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -609,7 +584,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -624,7 +598,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -643,7 +616,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -666,7 +638,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -680,7 +651,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -698,7 +668,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        List<String> associations = new ArrayList<>();
@@ -719,7 +688,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testHasCompanionDevice_failure() throws Exception {
        when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow(
                new IllegalArgumentException());
@@ -727,7 +695,6 @@ public class NotificationManagerServiceTest {
    }

    @Test
    @UiThreadTest
    public void testHasCompanionDevice_noService() throws Exception {
        mNotificationManagerService = new TestableNotificationManagerService(mContext);

+33 −15
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ import android.support.test.internal.runner.junit4.statement.RunAfters;
import android.support.test.internal.runner.junit4.statement.RunBefores;
import android.support.test.internal.runner.junit4.statement.UiThreadStatement;

import android.testing.TestableLooper.LooperStatement;
import android.testing.TestableLooper.LooperFrameworkMethod;
import android.testing.TestableLooper.RunWithLooper;

import org.junit.After;
@@ -30,6 +30,7 @@ import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.List;

/**
@@ -49,28 +50,21 @@ public class AndroidTestingRunner extends BlockJUnit4ClassRunner {

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

    protected Statement methodInvokerInt(FrameworkMethod method, Object test) {
        RunWithLooper annotation = method.getAnnotation(RunWithLooper.class);
        if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class);
        if (annotation != null) {
            return new LooperStatement(super.methodInvoker(method, test),
                    annotation.setAsMainLooper(), test);
        }
        return super.methodInvoker(method, test);
        method = looperWrap(method, test, method);
        final Statement statement = super.methodInvoker(method, test);
        return shouldRunOnUiThread(method) ? new UiThreadStatement(statement, true) : statement;
    }

    protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
        List befores = this.getTestClass().getAnnotatedMethods(Before.class);
        List befores = looperWrap(method, target,
                this.getTestClass().getAnnotatedMethods(Before.class));
        return befores.isEmpty() ? statement : new RunBefores(method, statement,
                befores, target);
    }

    protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
        List afters = this.getTestClass().getAnnotatedMethods(After.class);
        List afters = looperWrap(method, target,
                this.getTestClass().getAnnotatedMethods(After.class));
        return afters.isEmpty() ? statement : new RunAfters(method, statement, afters,
                target);
    }
@@ -88,6 +82,30 @@ public class AndroidTestingRunner extends BlockJUnit4ClassRunner {
        return annotation == null ? 0L : annotation.timeout();
    }

    protected List<FrameworkMethod> looperWrap(FrameworkMethod method, Object test,
            List<FrameworkMethod> methods) {
        RunWithLooper annotation = method.getAnnotation(RunWithLooper.class);
        if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class);
        if (annotation != null) {
            methods = new ArrayList<>(methods);
            for (int i = 0; i < methods.size(); i++) {
                methods.set(i, LooperFrameworkMethod.get(methods.get(i),
                        annotation.setAsMainLooper(), test));
            }
        }
        return methods;
    }

    protected FrameworkMethod looperWrap(FrameworkMethod method, Object test,
            FrameworkMethod base) {
        RunWithLooper annotation = method.getAnnotation(RunWithLooper.class);
        if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class);
        if (annotation != null) {
            return LooperFrameworkMethod.get(base, annotation.setAsMainLooper(), test);
        }
        return base;
    }

    public boolean shouldRunOnUiThread(FrameworkMethod method) {
        if (mKlass.getAnnotation(UiThreadTest.class) != null) {
            return true;
+143 −76

File changed.

Preview size limit exceeded, changes collapsed.

+19 −41
Original line number Diff line number Diff line
@@ -24,17 +24,16 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.testing.TestableLooper.MessageHandler;
import android.testing.TestableLooper.RunWithLooper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidTestingRunner.class)
@RunWithLooper
public class TestableLooperTest {
@@ -46,11 +45,6 @@ public class TestableLooperTest {
        mTestableLooper = TestableLooper.get(this);
    }

    @After
    public void tearDown() throws Exception {
        mTestableLooper.destroy();
    }

    @Test
    public void testMessageExecuted() throws Exception {
        Handler h = new Handler();
@@ -133,39 +127,23 @@ public class TestableLooperTest {
    @Test
    public void testMainLooper() throws Exception {
        assertNotEquals(Looper.myLooper(), Looper.getMainLooper());

        Looper originalMain = Looper.getMainLooper();
        mTestableLooper.setAsMainLooper();
        assertEquals(Looper.myLooper(), Looper.getMainLooper());
        Runnable r = mock(Runnable.class);
        Runnable r2 = mock(Runnable.class);
        TestableLooper testableLooper = new TestableLooper(Looper.getMainLooper());

        try {
            testableLooper.setMessageHandler(m -> {
                if (m.getCallback() == r) return true;
                return false;
            });
            new Handler(Looper.getMainLooper()).post(r);
        mTestableLooper.processAllMessages();
            testableLooper.processAllMessages();

            verify(r).run();
        mTestableLooper.destroy();

        assertEquals(originalMain, Looper.getMainLooper());
            verify(r2, never()).run();
        } finally {
            testableLooper.destroy();
        }

    @Test
    public void testNotMyLooper() throws Exception {
        TestableLooper looper = new TestableLooper(false);

        assertEquals(Looper.myLooper(), mTestableLooper.getLooper());
        assertNotEquals(Looper.myLooper(), looper.getLooper());

        Runnable r = mock(Runnable.class);
        Runnable r2 = mock(Runnable.class);
        new Handler().post(r);
        new Handler(looper.getLooper()).post(r2);

        looper.processAllMessages();
        verify(r2).run();
        verify(r, never()).run();

        mTestableLooper.processAllMessages();
        verify(r).run();
    }

    @Test