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

Commit 24483c37 authored by Mady Mellor's avatar Mady Mellor
Browse files

Move some tests out of BubblesTest

These are all related to bubble controller / bubble bar logging
so moving into the BubbleControllerBubbleBarTest file.

Includes a fix for b/415825436 ... which was to wait for an idle sync
in the teardown

Flag: EXEMPT tests only
Test: atest BubbleControllerBubbleBarTest
Test: atest WMShellRobolectricTests
Bug: 216523800
Change-Id: I69550d7a8865cec45c80fd126cdeac30100a3ff8
parent fecb36ff
Loading
Loading
Loading
Loading
+200 −4
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.view.WindowManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.android.internal.logging.testing.UiEventLoggerFake
import com.android.internal.protolog.ProtoLog
import com.android.internal.statusbar.IStatusBarService
@@ -147,6 +148,7 @@ class BubbleControllerBubbleBarTest {
    fun tearDown() {
        mainExecutor.flushAll()
        bgExecutor.flushAll()
        getInstrumentation().waitForIdleSync()
    }

    @Test
@@ -217,13 +219,207 @@ class BubbleControllerBubbleBarTest {
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_MOVED_RIGHT_DRAG_BUBBLE.id)
    }

    private fun addBubble(): Bubble {
        val bubble = FakeBubbleFactory.createChatBubble(context)
    @Test
    fun testEventLogging_bubbleBar_addBubble() {
        addBubble()

        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_POSTED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_updateBubble() {
        val bubble = addBubble()
        uiEventLoggerFake.logs.clear()

        bubble.setTextChangedForTest(true)
        bubbleController.inflateAndAdd(bubble, false, true)

        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_UPDATED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_dragSelectedBubbleToDismiss() {
        addBubble("key1")
        addBubble("key2")
        expandAndSelectBubble("key2")
        uiEventLoggerFake.logs.clear()

        // Dismiss selected bubble
        assertThat(bubbleData.selectedBubbleKey).isEqualTo("key2")
        getInstrumentation().runOnMainSync {
            bubbleController.startBubbleDrag("key2")
            bubbleController.dragBubbleToDismiss("key2", System.currentTimeMillis())
        }
        // Log bubble dismissed via drag and there's a switch event
        assertThat(bubbleData.selectedBubbleKey).isEqualTo("key1")
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(2)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_DISMISSED_DRAG_BUBBLE.id)
        assertThat(uiEventLoggerFake.eventId(1))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_SWITCHED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_dragOtherBubbleToDismiss() {
        addBubble("key1")
        addBubble("key2")
        expandAndSelectBubble("key2")

        uiEventLoggerFake.logs.clear()

        // Dismiss the non selected bubble
        assertThat(bubbleData.selectedBubbleKey).isEqualTo("key2")
        getInstrumentation().runOnMainSync {
            bubbleController.startBubbleDrag("key1")
            bubbleController.dragBubbleToDismiss("key1", System.currentTimeMillis())
        }

        // Log bubble dismissed via drag, but no switch event
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_DISMISSED_DRAG_BUBBLE.id)
    }

    @Test
    fun testEventLogging_bubbleBar_dragBarToDismiss() {
        addBubble()
        uiEventLoggerFake.logs.clear()

        bubbleController.removeAllBubbles(Bubbles.DISMISS_USER_GESTURE)
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_DISMISSED_DRAG_BAR.id)
    }

    @Test
    fun testEventLogging_bubbleBar_blocked() {
        val bubble = addBubble()
        uiEventLoggerFake.logs.clear()

        bubbleController.removeBubble(bubble.key, Bubbles.DISMISS_NO_LONGER_BUBBLE)
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_REMOVED_BLOCKED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_notifCanceled() {
        val bubble = addBubble()
        uiEventLoggerFake.logs.clear()

        bubbleController.removeBubble(bubble.key, Bubbles.DISMISS_NOTIF_CANCEL)
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_REMOVED_CANCELED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_taskFinished() {
        val bubble = addBubble()
        uiEventLoggerFake.logs.clear()

        bubbleController.removeBubble(bubble.key, Bubbles.DISMISS_TASK_FINISHED)
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_ACTIVITY_FINISH.id)
    }

    @Test
    fun testEventLogging_bubbleBar_expandAndCollapse() {
        addBubble("key")
        uiEventLoggerFake.logs.clear()

        expandAndSelectBubble("key")

        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_EXPANDED.id)
        uiEventLoggerFake.logs.clear()

        getInstrumentation().runOnMainSync {
            bubbleController.collapseStack()
        }

        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_COLLAPSED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_autoExpandingBubble() {
        addBubble("key", autoExpand = true)

        // 2 events: add bubble + expand
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(2)
        assertThat(uiEventLoggerFake.eventId(1))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_EXPANDED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_switchBubble() {
        addBubble("key1")
        addBubble("key2")
        expandAndSelectBubble("key2")
        assertThat(bubbleData.selectedBubbleKey).isEqualTo("key2")
        uiEventLoggerFake.logs.clear()

        // Select the next bubble
        expandAndSelectBubble("key1")

        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_SWITCHED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_openOverflow() {
        addBubble("key")
        expandAndSelectBubble("key")
        uiEventLoggerFake.logs.clear()

        expandAndSelectBubble(BubbleOverflow.KEY)

        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(1)
        assertThat(uiEventLoggerFake.eventId(0))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_OVERFLOW_SELECTED.id)
    }

    @Test
    fun testEventLogging_bubbleBar_fromOverflowToBar() {
        val bubble = addBubble()

        // Dismiss the bubble so it's in the overflow
        bubbleController.removeBubble(bubble.key, Bubbles.DISMISS_USER_GESTURE)
        val overflowBubble = bubbleData.getOverflowBubbleWithKey(bubble.key)
        assertThat(overflowBubble).isNotNull()

        // Promote overflow bubble and check that it is logged
        bubbleController.promoteBubbleFromOverflow(overflowBubble)

        // 2 events: add + remove
        assertThat(uiEventLoggerFake.numLogs()).isEqualTo(2)
        assertThat(uiEventLoggerFake.eventId(1))
            .isEqualTo(BubbleLogger.Event.BUBBLE_BAR_OVERFLOW_REMOVE_BACK_TO_BAR.id)
    }

    private fun expandAndSelectBubble(key: String) {
        getInstrumentation().runOnMainSync {
            bubbleController.expandStackAndSelectBubbleFromLauncher(key, 0)
        }
    }

    private fun addBubble(key: String = "key", autoExpand: Boolean = false): Bubble {
        val bubble = FakeBubbleFactory.createChatBubble(context, key)
        bubble.setInflateSynchronously(true)
        bubbleData.notificationEntryUpdated(
            bubble,
        bubble.setShouldAutoExpand(autoExpand)
        bubbleController.inflateAndAdd(bubble,
            /* suppressFlyout= */ true,
            /* showInShade= */ true,
            /* bubbleBarLocation = */ null,
        )
        return bubble
    }
+5 −181
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -2752,20 +2751,11 @@ public class BubblesTest extends SysuiTestCase {
        verify(stackView, never()).showOverflow(anyBoolean());
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_addBubble() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);

        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_POSTED));
    }

    // TODO (b/216523800): There's a test in BubbleControllerBubbleBarTest verifying this logging,
    //  however, it doesn't do it via the notification entry listener, it calls the method within
    //  BubbleController that would eventually be called. We can remove this test from BubblesTest
    //  once we have something that validates entryListener#onEntryUpdated -> calls what's needed
    //  in BubbleController.
    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_updateBubble() {
@@ -2783,172 +2773,6 @@ public class BubblesTest extends SysuiTestCase {
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_UPDATED));
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_dragSelectedBubbleToDismiss() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);
        mEntryListener.onEntryAdded(mEntry2);
        mBubbleController.expandStackAndSelectBubbleFromLauncher(mEntry2.getKey(), 0);

        clearInvocations(mBubbleLogger);

        // Dismiss selected bubble
        mBubbleController.startBubbleDrag(mEntry2.getKey());
        mBubbleController.dragBubbleToDismiss(mEntry2.getKey(), System.currentTimeMillis());

        // Log bubble dismissed via drag and new bubble selected
        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry2.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_DISMISSED_DRAG_BUBBLE));
        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_SWITCHED));

        verifyNoMoreInteractions(mBubbleLogger);
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_dragOtherBubbleToDismiss() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);
        mEntryListener.onEntryAdded(mEntry2);
        mBubbleController.expandStackAndSelectBubbleFromLauncher(mEntry2.getKey(), 0);

        clearInvocations(mBubbleLogger);

        // Dismiss other bubble
        mBubbleController.startBubbleDrag(mEntry.getKey());
        mBubbleController.dragBubbleToDismiss(mEntry.getKey(), System.currentTimeMillis());

        // Log bubble dismissed via drag, but no switch event
        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_DISMISSED_DRAG_BUBBLE));

        verifyNoMoreInteractions(mBubbleLogger);
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_dragBarToDismiss() {
        mPositioner.setIsLargeScreen(true);

        // Not a user gesture, should not log an event
        mBubbleController.removeAllBubbles(Bubbles.DISMISS_NO_LONGER_BUBBLE);
        verify(mBubbleLogger, never()).log(BubbleLogger.Event.BUBBLE_BAR_DISMISSED_DRAG_BAR);

        // Dismiss via user gesture, log an event
        mBubbleController.removeAllBubbles(Bubbles.DISMISS_USER_GESTURE);
        verify(mBubbleLogger).log(BubbleLogger.Event.BUBBLE_BAR_DISMISSED_DRAG_BAR);
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_expandAndCollapse() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);
        mBubbleController.expandStackAndSelectBubbleFromLauncher(mEntry.getKey(), 0);

        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_EXPANDED));

        mBubbleController.collapseStack();

        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_COLLAPSED));
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_autoExpandingBubble() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        setMetadataFlags(mEntry,
                Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE, true /* enableFlag */);
        mEntryListener.onEntryAdded(mEntry);

        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_EXPANDED));
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_switchBubble() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);
        mEntryListener.onEntryAdded(mEntry2);
        mBubbleController.expandStackAndSelectBubbleFromLauncher(mEntry.getKey(), 0);

        // First select is expand
        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_EXPANDED));
        verify(mBubbleLogger, never()).log(eqBubbleWithKey(mEntry.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_SWITCHED));

        // Second select is switch
        mBubbleController.expandStackAndSelectBubbleFromLauncher(mEntry2.getKey(), 0);
        verify(mBubbleLogger).log(eqBubbleWithKey(mEntry2.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_BUBBLE_SWITCHED));
        verify(mBubbleLogger, never()).log(eqBubbleWithKey(mEntry2.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_EXPANDED));
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_openOverflow() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);

        clearInvocations(mBubbleLogger);
        mBubbleController.expandStackAndSelectBubbleFromLauncher(BubbleOverflow.KEY, 0);
        verify(mBubbleLogger).log(BubbleLogger.Event.BUBBLE_BAR_OVERFLOW_SELECTED);
        verifyNoMoreInteractions(mBubbleLogger);
    }

    @EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
    @Test
    public void testEventLogging_bubbleBar_fromOverflowToBar() {
        mPositioner.setIsLargeScreen(true);
        mBubbleController.setLauncherHasBubbleBar(true);
        FakeBubbleStateListener bubbleStateListener = new FakeBubbleStateListener();
        mBubbleController.registerBubbleStateListener(bubbleStateListener);

        mEntryListener.onEntryAdded(mEntry);

        // Dismiss the bubble so it's in the overflow
        mBubbleController.removeBubble(
                mEntry.getKey(), Bubbles.DISMISS_USER_GESTURE);
        Bubble overflowBubble = mBubbleData.getOverflowBubbleWithKey(mEntry.getKey());
        assertThat(overflowBubble).isNotNull();

        // Promote overflow bubble and check that it is logged
        mBubbleController.promoteBubbleFromOverflow(overflowBubble);
        verify(mBubbleLogger).log(eqBubbleWithKey(overflowBubble.getKey()),
                eq(BubbleLogger.Event.BUBBLE_BAR_OVERFLOW_REMOVE_BACK_TO_BAR));
    }

    /** Creates a bubble using the userId and package. */
    private Bubble createBubble(int userId, String pkg) {
        final UserHandle userHandle = new UserHandle(userId);