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

Commit 9deb242c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I0c728173,Iaef6ec1c into main

* changes:
  Initialize the player list after setting up factory injection pattern
  Check handler before trying to wait for looper
parents d1be6435 7721ec26
Loading
Loading
Loading
Loading
+10 −4
Original line number Original line Diff line number Diff line
@@ -77,6 +77,10 @@ public class MediaPlayerListTest {
        when(mMockContext.getSystemServiceName(AudioManager.class))
        when(mMockContext.getSystemServiceName(AudioManager.class))
                .thenReturn(Context.AUDIO_SERVICE);
                .thenReturn(Context.AUDIO_SERVICE);


        // MediaSessionManager is final and Bluetooth can't use extended Mockito to mock it. Thus,
        // using this as is risks leaking device state into the tests. To avoid this, the injected
        // controller and player below in the factory pattern will essentially replace each found
        // player with the *same* mock, giving us only one player in the end-- "testPlayer"
        mMediaSessionManager =
        mMediaSessionManager =
                InstrumentationRegistry.getTargetContext()
                InstrumentationRegistry.getTargetContext()
                        .getSystemService(MediaSessionManager.class);
                        .getSystemService(MediaSessionManager.class);
@@ -86,9 +90,6 @@ public class MediaPlayerListTest {
        when(mMockContext.getSystemServiceName(MediaSessionManager.class))
        when(mMockContext.getSystemServiceName(MediaSessionManager.class))
                .thenReturn(Context.MEDIA_SESSION_SERVICE);
                .thenReturn(Context.MEDIA_SESSION_SERVICE);


        mMediaPlayerList =
                new MediaPlayerList(Looper.myLooper(), InstrumentationRegistry.getTargetContext());

        when(mMockContext.registerReceiver(any(), any())).thenReturn(null);
        when(mMockContext.registerReceiver(any(), any())).thenReturn(null);
        when(mMockContext.getApplicationContext()).thenReturn(mMockContext);
        when(mMockContext.getApplicationContext()).thenReturn(mMockContext);
        when(mMockContext.getPackageManager()).thenReturn(mockPackageManager);
        when(mMockContext.getPackageManager()).thenReturn(mockPackageManager);
@@ -96,13 +97,18 @@ public class MediaPlayerListTest {


        BrowsablePlayerConnector mockConnector = mock(BrowsablePlayerConnector.class);
        BrowsablePlayerConnector mockConnector = mock(BrowsablePlayerConnector.class);
        BrowsablePlayerConnector.setInstanceForTesting(mockConnector);
        BrowsablePlayerConnector.setInstanceForTesting(mockConnector);
        mMediaPlayerList.init(mMediaUpdateCallback);


        MediaControllerFactory.inject(mMockController);
        MediaControllerFactory.inject(mMockController);
        MediaPlayerWrapperFactory.inject(mMockPlayerWrapper);
        MediaPlayerWrapperFactory.inject(mMockPlayerWrapper);


        doReturn("testPlayer").when(mMockController).getPackageName();
        doReturn("testPlayer").when(mMockController).getPackageName();
        when(mMockPlayerWrapper.isMetadataSynced()).thenReturn(false);
        when(mMockPlayerWrapper.isMetadataSynced()).thenReturn(false);

        // Be sure to do this setup last, after factor injections, or you risk leaking device state
        // into the tests
        mMediaPlayerList =
                new MediaPlayerList(Looper.myLooper(), InstrumentationRegistry.getTargetContext());
        mMediaPlayerList.init(mMediaUpdateCallback);
        mMediaPlayerList.setActivePlayer(mMediaPlayerList.addMediaPlayer(mMockController));
        mMediaPlayerList.setActivePlayer(mMediaPlayerList.addMediaPlayer(mMockController));


        verify(mMockPlayerWrapper).registerCallback(mPlayerWrapperCb.capture());
        verify(mMockPlayerWrapper).registerCallback(mPlayerWrapperCb.capture());
+4 −5
Original line number Original line Diff line number Diff line
@@ -283,7 +283,6 @@ public class MapClientStateMachineTest {
    /** Test that default state is STATE_CONNECTING */
    /** Test that default state is STATE_CONNECTING */
    @Test
    @Test
    public void testDefaultConnectingState() {
    public void testDefaultConnectingState() {
        Log.i(TAG, "in testDefaultConnectingState");
        Assert.assertEquals(BluetoothProfile.STATE_CONNECTING, mMceStateMachine.getState());
        Assert.assertEquals(BluetoothProfile.STATE_CONNECTING, mMceStateMachine.getState());
    }
    }


@@ -293,7 +292,6 @@ public class MapClientStateMachineTest {
     */
     */
    @Test
    @Test
    public void testStateTransitionFromConnectingToDisconnected() {
    public void testStateTransitionFromConnectingToDisconnected() {
        Log.i(TAG, "in testStateTransitionFromConnectingToDisconnected");
        setupSdpRecordReceipt();
        setupSdpRecordReceipt();
        Message msg = Message.obtain(mHandler, MceStateMachine.MSG_MAS_DISCONNECTED);
        Message msg = Message.obtain(mHandler, MceStateMachine.MSG_MAS_DISCONNECTED);
        mMceStateMachine.sendMessage(msg);
        mMceStateMachine.sendMessage(msg);
@@ -312,7 +310,6 @@ public class MapClientStateMachineTest {
    /** Test transition from STATE_CONNECTING --> (receive MSG_MAS_CONNECTED) --> STATE_CONNECTED */
    /** Test transition from STATE_CONNECTING --> (receive MSG_MAS_CONNECTED) --> STATE_CONNECTED */
    @Test
    @Test
    public void testStateTransitionFromConnectingToConnected() {
    public void testStateTransitionFromConnectingToConnected() {
        Log.i(TAG, "in testStateTransitionFromConnectingToConnected");
        setupSdpRecordReceipt();
        setupSdpRecordReceipt();


        int expectedFromState = BluetoothProfile.STATE_CONNECTING;
        int expectedFromState = BluetoothProfile.STATE_CONNECTING;
@@ -327,7 +324,6 @@ public class MapClientStateMachineTest {
     */
     */
    @Test
    @Test
    public void testStateTransitionFromConnectedToDisconnected() {
    public void testStateTransitionFromConnectedToDisconnected() {
        Log.i(TAG, "in testStateTransitionFromConnectedWithMasDisconnected");


        setupSdpRecordReceipt();
        setupSdpRecordReceipt();
        // transition to the connected state
        // transition to the connected state
@@ -1219,7 +1215,10 @@ public class MapClientStateMachineTest {
    }
    }


    private void assertCurrentStateAfterScheduledTask(int expectedState) {
    private void assertCurrentStateAfterScheduledTask(int expectedState) {
        TestUtils.waitForLooperToFinishScheduledTask(mMceStateMachine.getHandler().getLooper());
        Handler handler = mMceStateMachine.getHandler();
        if (handler != null) {
            TestUtils.waitForLooperToFinishScheduledTask(handler.getLooper());
        }
        assertThat(mMceStateMachine.getState()).isEqualTo(expectedState);
        assertThat(mMceStateMachine.getState()).isEqualTo(expectedState);
    }
    }


+3 −1
Original line number Original line Diff line number Diff line
@@ -533,8 +533,10 @@ public class PbapClientServiceTest {
        // Clean up and wait for it to complete
        // Clean up and wait for it to complete
        PbapClientStateMachine sm = mMockDeviceMap.get(mRemoteDevice);
        PbapClientStateMachine sm = mMockDeviceMap.get(mRemoteDevice);
        assertThat(sm).isNotNull();
        assertThat(sm).isNotNull();

        Looper looper = sm.getHandler().getLooper();
        sm.disconnect();
        sm.disconnect();
        TestUtils.waitForLooperToFinishScheduledTask(sm.getHandler().getLooper());
        TestUtils.waitForLooperToFinishScheduledTask(looper);
    }
    }


    // connect (device null) -> false
    // connect (device null) -> false