Merge "Prevent leaked receivers from MediaOutputDialogTest" into udc-dev
This commit is contained in:
@@ -39,6 +39,7 @@ import android.testing.TestableLooper;
|
|||||||
import android.util.FeatureFlagUtils;
|
import android.util.FeatureFlagUtils;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.test.filters.MediumTest;
|
import androidx.test.filters.MediumTest;
|
||||||
|
|
||||||
import com.android.internal.logging.UiEventLogger;
|
import com.android.internal.logging.UiEventLogger;
|
||||||
@@ -64,6 +65,7 @@ import org.junit.runner.RunWith;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
@RunWith(AndroidTestingRunner.class)
|
@RunWith(AndroidTestingRunner.class)
|
||||||
@@ -102,6 +104,11 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
private MediaOutputController mMediaOutputController;
|
private MediaOutputController mMediaOutputController;
|
||||||
private final List<String> mFeatures = new ArrayList<>();
|
private final List<String> mFeatures = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldFailOnLeakedReceiver() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
|
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
|
||||||
@@ -120,8 +127,7 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
Optional.of(mNearbyMediaDevicesManager), mAudioManager, mPowerExemptionManager,
|
Optional.of(mNearbyMediaDevicesManager), mAudioManager, mPowerExemptionManager,
|
||||||
mKeyguardManager, mFlags);
|
mKeyguardManager, mFlags);
|
||||||
mMediaOutputController.mLocalMediaManager = mLocalMediaManager;
|
mMediaOutputController.mLocalMediaManager = mLocalMediaManager;
|
||||||
mMediaOutputDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
|
mMediaOutputDialog = makeTestDialog(mMediaOutputController);
|
||||||
mMediaOutputController, mUiEventLogger);
|
|
||||||
mMediaOutputDialog.show();
|
mMediaOutputDialog.show();
|
||||||
|
|
||||||
when(mLocalMediaManager.getCurrentConnectedDevice()).thenReturn(mMediaDevice);
|
when(mLocalMediaManager.getCurrentConnectedDevice()).thenReturn(mMediaDevice);
|
||||||
@@ -130,7 +136,7 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
mMediaOutputDialog.dismissDialog();
|
mMediaOutputDialog.dismiss();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -311,11 +317,9 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
MediaOutputController mockMediaOutputController = mock(MediaOutputController.class);
|
MediaOutputController mockMediaOutputController = mock(MediaOutputController.class);
|
||||||
when(mockMediaOutputController.isBroadcastSupported()).thenReturn(false);
|
when(mockMediaOutputController.isBroadcastSupported()).thenReturn(false);
|
||||||
|
|
||||||
MediaOutputDialog testDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
|
withTestDialog(mockMediaOutputController, testDialog -> {
|
||||||
mockMediaOutputController, mUiEventLogger);
|
|
||||||
testDialog.show();
|
|
||||||
|
|
||||||
assertThat(testDialog.getStopButtonText().toString()).isEqualTo(stopText);
|
assertThat(testDialog.getStopButtonText().toString()).isEqualTo(stopText);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -328,11 +332,9 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
when(mockMediaOutputController.isBluetoothLeDevice(any())).thenReturn(true);
|
when(mockMediaOutputController.isBluetoothLeDevice(any())).thenReturn(true);
|
||||||
when(mockMediaOutputController.isPlaying()).thenReturn(true);
|
when(mockMediaOutputController.isPlaying()).thenReturn(true);
|
||||||
when(mockMediaOutputController.isBluetoothLeBroadcastEnabled()).thenReturn(false);
|
when(mockMediaOutputController.isBluetoothLeBroadcastEnabled()).thenReturn(false);
|
||||||
MediaOutputDialog testDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
|
withTestDialog(mockMediaOutputController, testDialog -> {
|
||||||
mockMediaOutputController, mUiEventLogger);
|
|
||||||
testDialog.show();
|
|
||||||
|
|
||||||
assertThat(testDialog.getStopButtonText().toString()).isEqualTo(stopText);
|
assertThat(testDialog.getStopButtonText().toString()).isEqualTo(stopText);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -341,11 +343,9 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
when(mockMediaOutputController.isBroadcastSupported()).thenReturn(false);
|
when(mockMediaOutputController.isBroadcastSupported()).thenReturn(false);
|
||||||
when(mockMediaOutputController.getCurrentConnectedMediaDevice()).thenReturn(null);
|
when(mockMediaOutputController.getCurrentConnectedMediaDevice()).thenReturn(null);
|
||||||
when(mockMediaOutputController.isPlaying()).thenReturn(false);
|
when(mockMediaOutputController.isPlaying()).thenReturn(false);
|
||||||
MediaOutputDialog testDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
|
withTestDialog(mockMediaOutputController, testDialog -> {
|
||||||
mockMediaOutputController, mUiEventLogger);
|
|
||||||
testDialog.show();
|
|
||||||
|
|
||||||
testDialog.onStopButtonClick();
|
testDialog.onStopButtonClick();
|
||||||
|
});
|
||||||
|
|
||||||
verify(mockMediaOutputController).releaseSession();
|
verify(mockMediaOutputController).releaseSession();
|
||||||
}
|
}
|
||||||
@@ -354,13 +354,22 @@ public class MediaOutputDialogTest extends SysuiTestCase {
|
|||||||
// Check the visibility metric logging by creating a new MediaOutput dialog,
|
// Check the visibility metric logging by creating a new MediaOutput dialog,
|
||||||
// and verify if the calling times increases.
|
// and verify if the calling times increases.
|
||||||
public void onCreate_ShouldLogVisibility() {
|
public void onCreate_ShouldLogVisibility() {
|
||||||
MediaOutputDialog testDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
|
withTestDialog(mMediaOutputController, testDialog -> {});
|
||||||
mMediaOutputController, mUiEventLogger);
|
|
||||||
testDialog.show();
|
|
||||||
|
|
||||||
testDialog.dismissDialog();
|
|
||||||
|
|
||||||
verify(mUiEventLogger, times(2))
|
verify(mUiEventLogger, times(2))
|
||||||
.log(MediaOutputDialog.MediaOutputEvent.MEDIA_OUTPUT_DIALOG_SHOW);
|
.log(MediaOutputDialog.MediaOutputEvent.MEDIA_OUTPUT_DIALOG_SHOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private MediaOutputDialog makeTestDialog(MediaOutputController controller) {
|
||||||
|
return new MediaOutputDialog(mContext, false, mBroadcastSender,
|
||||||
|
controller, mUiEventLogger);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void withTestDialog(MediaOutputController controller, Consumer<MediaOutputDialog> c) {
|
||||||
|
MediaOutputDialog testDialog = makeTestDialog(controller);
|
||||||
|
testDialog.show();
|
||||||
|
c.accept(testDialog);
|
||||||
|
testDialog.dismiss();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,8 @@ public abstract class SysuiTestCase {
|
|||||||
mock(Executor.class),
|
mock(Executor.class),
|
||||||
mock(DumpManager.class),
|
mock(DumpManager.class),
|
||||||
mock(BroadcastDispatcherLogger.class),
|
mock(BroadcastDispatcherLogger.class),
|
||||||
mock(UserTracker.class));
|
mock(UserTracker.class),
|
||||||
|
shouldFailOnLeakedReceiver());
|
||||||
|
|
||||||
mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
|
mRealInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||||
Instrumentation inst = spy(mRealInstrumentation);
|
Instrumentation inst = spy(mRealInstrumentation);
|
||||||
@@ -141,6 +142,10 @@ public abstract class SysuiTestCase {
|
|||||||
mDependency.injectTestDependency(DialogLaunchAnimator.class, fakeDialogLaunchAnimator());
|
mDependency.injectTestDependency(DialogLaunchAnimator.class, fakeDialogLaunchAnimator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean shouldFailOnLeakedReceiver() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void SysuiTeardown() {
|
public void SysuiTeardown() {
|
||||||
if (mRealInstrumentation != null) {
|
if (mRealInstrumentation != null) {
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import com.android.systemui.SysuiTestableContext
|
|||||||
import com.android.systemui.broadcast.logging.BroadcastDispatcherLogger
|
import com.android.systemui.broadcast.logging.BroadcastDispatcherLogger
|
||||||
import com.android.systemui.dump.DumpManager
|
import com.android.systemui.dump.DumpManager
|
||||||
import com.android.systemui.settings.UserTracker
|
import com.android.systemui.settings.UserTracker
|
||||||
|
import java.lang.IllegalStateException
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.Executor
|
import java.util.concurrent.Executor
|
||||||
|
|
||||||
@@ -37,7 +38,8 @@ class FakeBroadcastDispatcher(
|
|||||||
broadcastRunningExecutor: Executor,
|
broadcastRunningExecutor: Executor,
|
||||||
dumpManager: DumpManager,
|
dumpManager: DumpManager,
|
||||||
logger: BroadcastDispatcherLogger,
|
logger: BroadcastDispatcherLogger,
|
||||||
userTracker: UserTracker
|
userTracker: UserTracker,
|
||||||
|
private val shouldFailOnLeakedReceiver: Boolean
|
||||||
) :
|
) :
|
||||||
BroadcastDispatcher(
|
BroadcastDispatcher(
|
||||||
context,
|
context,
|
||||||
@@ -85,6 +87,9 @@ class FakeBroadcastDispatcher(
|
|||||||
fun cleanUpReceivers(testName: String) {
|
fun cleanUpReceivers(testName: String) {
|
||||||
registeredReceivers.forEach {
|
registeredReceivers.forEach {
|
||||||
Log.i(testName, "Receiver not unregistered from dispatcher: $it")
|
Log.i(testName, "Receiver not unregistered from dispatcher: $it")
|
||||||
|
if (shouldFailOnLeakedReceiver) {
|
||||||
|
throw IllegalStateException("Receiver not unregistered from dispatcher: $it")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
registeredReceivers.clear()
|
registeredReceivers.clear()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user