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

Commit 99206ee2 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Prevent NPE from onQueueChanged with a null queue

Bug: 78768610
Test: runtest bluetooth -c
com.android.bluetooth.avrcp.MediaPlayerWrapperTest
Change-Id: Ie8b8f96017bff20d54dbb06a620c235e8b8db1ae
parent 5a6b8d18
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.support.annotation.VisibleForTesting;
import android.util.Log;

import java.util.List;
import java.util.Objects;

/*
 * A class to synchronize Media Controller Callbacks and only pass through
@@ -423,17 +424,19 @@ class MediaPlayerWrapper {
                return;
            }

            if (!queue.equals(getQueue())) {
            if (!Objects.equals(queue, getQueue())) {
                e("The callback queue isn't the current queue");
            }
            if (queue.equals(mCurrentData.queue)) {

            List<Metadata> current_queue = Util.toMetadataList(queue);
            if (current_queue.equals(mCurrentData.queue)) {
                Log.w(TAG, "onQueueChanged(): " + mPackageName
                        + " tried to update with no new data");
                return;
            }

            if (DEBUG) {
                for (int i = 0; i < queue.size(); i++) {
                for (int i = 0; i < current_queue.size(); i++) {
                    Log.d(TAG, "  └ QueueItem(" + i + "): " + queue.get(i));
                }
            }
+25 −1
Original line number Diff line number Diff line
@@ -282,7 +282,7 @@ public class MediaPlayerWrapperTest {
        verify(mMockController).registerCallback(mControllerCbs.capture(), any());
        MediaController.Callback controllerCallbacks = mControllerCbs.getValue();

        // Update Metdata returned by controller
        // Update Metadata returned by controller
        mTestMetadata.putString(MediaMetadata.METADATA_KEY_TITLE, "New Title");
        doReturn(mTestMetadata.build()).when(mMockController).getMetadata();

@@ -311,6 +311,30 @@ public class MediaPlayerWrapperTest {
        verify(mFailHandler, never()).onTerribleFailure(any(), any(), anyBoolean());
    }

    @Test
    public void testNullQueue() {
        // Create the wrapper object and register the looper with the timeout handler
        TestLooperManager looperManager = new TestLooperManager(mThread.getLooper());
        MediaPlayerWrapper wrapper =
                MediaPlayerWrapper.wrap(mMockController, mThread.getLooper());
        wrapper.registerCallback(mTestCbs);

        // Return null when getting the queue
        doReturn(null).when(mMockController).getQueue();

        // Grab the callbacks the wrapper registered with the controller
        verify(mMockController).registerCallback(mControllerCbs.capture(), any());
        MediaController.Callback controllerCallbacks = mControllerCbs.getValue();

        // Call the callback
        controllerCallbacks.onQueueChanged(null);

        // Assert that both metadata and playback state are there.
        verify(mTestCbs, times(1)).mediaUpdatedCallback(mMediaUpdateData.capture());
        MediaData data = mMediaUpdateData.getValue();
        Assert.assertEquals("Returned Queue isn't null", data.queue.size(), 0);
    }

    /*
     * This test checks to see if the now playing queue data is cached.
     */