From a65f509f4d5913ccfec8fb21c3a12a573b947c10 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Wed, 10 May 2023 14:25:30 +0000 Subject: [PATCH] Avoid onDestroy content in test Even though `Activity.finish` is called by the @Rule before the teardown happens, `onDestroy` may not be called synchronously and therefore may happen after mocks have been nulled. To prevent NPE, do not call `BroadcastDispatcher.unregisterReceiver` in the test. Test: atest com.android.systemui.controls Fixes: 280020640 Change-Id: I992befeac6588116da03ed7869730f5b14b4e091 --- .../com/android/systemui/controls/ui/ControlsActivity.kt | 4 ++++ .../systemui/controls/ui/TestableControlsActivity.kt | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt index c964b9654955f..81004ef8dc372 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt @@ -158,6 +158,10 @@ open class ControlsActivity @Inject constructor( override fun onDestroy() { super.onDestroy() + unregisterReceiver() + } + + protected open fun unregisterReceiver() { broadcastDispatcher.unregisterReceiver(broadcastReceiver) } diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt index f0b473210630a..d2fe68ad8e1af 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt @@ -22,6 +22,8 @@ import com.android.systemui.controls.settings.ControlsSettingsDialogManager import com.android.systemui.flags.FeatureFlags import com.android.systemui.statusbar.policy.KeyguardStateController +// IMPORTANT: onDestroy may be called outside of bounds of the test. That means that the mocks +// may have been nulled before onDestroy happens. class TestableControlsActivity( uiController: ControlsUiController, broadcastDispatcher: BroadcastDispatcher, @@ -37,4 +39,8 @@ class TestableControlsActivity( featureFlags, controlsSettingsDialogManager, keyguardStateController - ) + ) { + override fun unregisterReceiver() { + // Do nothing. This will be called in `onDestroy` + } +}