From c797cb1cac60831ddcc7c898cfd8f3ab33affd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Budnik?= Date: Thu, 27 Jul 2023 14:52:38 +0000 Subject: [PATCH] Remove fake package name from MediaOutputControllerTest The MediaRouter framework does not allow non-existent package names, which makes MediaOutputControllerTest run into undefined behaviour. Now, the tests use their own package name as a target. Bug: 192657812 Test: atest MediaOutputControllerTest Change-Id: I6097377f70c4145e4b841a78ed18c14fa26ee702 --- .../dialog/MediaOutputControllerTest.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java index 708bb5507f10e..19315803c7402 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java @@ -98,8 +98,6 @@ import java.util.List; @RunWith(AndroidTestingRunner.class) @TestableLooper.RunWithLooper(setAsMainLooper = true) public class MediaOutputControllerTest extends SysuiTestCase { - - private static final String TEST_PACKAGE_NAME = "com.test.package.name"; private static final String TEST_DEVICE_1_ID = "test_device_1_id"; private static final String TEST_DEVICE_2_ID = "test_device_2_id"; private static final String TEST_DEVICE_3_ID = "test_device_3_id"; @@ -167,6 +165,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { final Notification mNotification = mock(Notification.class); private Context mSpyContext; + private String mPackageName = null; private MediaOutputController mMediaOutputController; private LocalMediaManager mLocalMediaManager; private List mMediaControllers = new ArrayList<>(); @@ -177,12 +176,14 @@ public class MediaOutputControllerTest extends SysuiTestCase { @Before public void setUp() { + mPackageName = mContext.getPackageName(); + MockitoAnnotations.initMocks(this); mContext.setMockPackageManager(mPackageManager); mSpyContext = spy(mContext); final UserHandle userHandle = mock(UserHandle.class); when(mUserTracker.getUserHandle()).thenReturn(userHandle); - when(mSessionMediaController.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(mSessionMediaController.getPackageName()).thenReturn(mPackageName); when(mSessionMediaController.getPlaybackState()).thenReturn(mPlaybackState); mMediaControllers.add(mSessionMediaController); when(mMediaSessionManager.getActiveSessionsForUser(any(), @@ -193,7 +194,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn( mCachedBluetoothDeviceManager); - mMediaOutputController = new MediaOutputController(mSpyContext, TEST_PACKAGE_NAME, + mMediaOutputController = new MediaOutputController(mSpyContext, mPackageName, mMediaSessionManager, mLocalBluetoothManager, mStarter, mNotifCollection, mDialogLaunchAnimator, mNearbyMediaDevicesManager, mAudioManager, mPowerExemptionManager, @@ -231,7 +232,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mNotifCollection.getAllNotifs()).thenReturn(entryList); when(entry.getSbn()).thenReturn(sbn); when(sbn.getNotification()).thenReturn(mNotification); - when(sbn.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(sbn.getPackageName()).thenReturn(mPackageName); mNotification.extras = bundle; when(bundle.getParcelable(Notification.EXTRA_MEDIA_SESSION, MediaSession.Token.class)).thenReturn(token); @@ -339,8 +340,8 @@ public class MediaOutputControllerTest extends SysuiTestCase { public void tryToLaunchMediaApplication_intentNotNull_startActivity() { when(mDialogLaunchAnimator.createActivityLaunchController(any(View.class))).thenReturn( mController); - Intent intent = new Intent(TEST_PACKAGE_NAME); - doReturn(intent).when(mPackageManager).getLaunchIntentForPackage(TEST_PACKAGE_NAME); + Intent intent = new Intent(mPackageName); + doReturn(intent).when(mPackageManager).getLaunchIntentForPackage(mPackageName); mMediaOutputController.start(mCallback); mMediaOutputController.tryToLaunchMediaApplication(mDialogLaunchView); @@ -355,7 +356,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { mController); mMediaOutputController.start(mCallback); when(mLocalMediaManager.getLinkedItemComponentName()).thenReturn( - new ComponentName(TEST_PACKAGE_NAME, "")); + new ComponentName(mPackageName, "")); mMediaOutputController.tryToLaunchInAppRoutingIntent(TEST_DEVICE_1_ID, mDialogLaunchView); @@ -891,7 +892,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mNotifCollection.getAllNotifs()).thenReturn(entryList); when(entry.getSbn()).thenReturn(sbn); when(sbn.getNotification()).thenReturn(notification); - when(sbn.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(sbn.getPackageName()).thenReturn(mPackageName); when(notification.isMediaNotification()).thenReturn(true); when(notification.getLargeIcon()).thenReturn(null); @@ -910,7 +911,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mNotifCollection.getAllNotifs()).thenReturn(entryList); when(entry.getSbn()).thenReturn(sbn); when(sbn.getNotification()).thenReturn(notification); - when(sbn.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(sbn.getPackageName()).thenReturn(mPackageName); when(notification.isMediaNotification()).thenReturn(true); when(notification.getLargeIcon()).thenReturn(icon); @@ -929,7 +930,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mNotifCollection.getAllNotifs()).thenReturn(entryList); when(entry.getSbn()).thenReturn(sbn); when(sbn.getNotification()).thenReturn(notification); - when(sbn.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(sbn.getPackageName()).thenReturn(mPackageName); when(notification.isMediaNotification()).thenReturn(false); when(notification.getLargeIcon()).thenReturn(icon); @@ -947,7 +948,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mNotifCollection.getAllNotifs()).thenReturn(entryList); when(entry.getSbn()).thenReturn(sbn); when(sbn.getNotification()).thenReturn(notification); - when(sbn.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(sbn.getPackageName()).thenReturn(mPackageName); when(notification.isMediaNotification()).thenReturn(true); when(notification.getSmallIcon()).thenReturn(null); @@ -966,7 +967,7 @@ public class MediaOutputControllerTest extends SysuiTestCase { when(mNotifCollection.getAllNotifs()).thenReturn(entryList); when(entry.getSbn()).thenReturn(sbn); when(sbn.getNotification()).thenReturn(notification); - when(sbn.getPackageName()).thenReturn(TEST_PACKAGE_NAME); + when(sbn.getPackageName()).thenReturn(mPackageName); when(notification.isMediaNotification()).thenReturn(true); when(notification.getSmallIcon()).thenReturn(icon);