Inject mocks in RemoteVolumeControllerTest via constructor

This fix avoids the test wrongfully initializing real components before
the tests can modify mockable fields, which causes NPEs.

Bug: 318078730
Test: atest RemoteVolumeGroupControllerTest
Change-Id: I350e3e2e45eadb0f9737ba5d4b45c9f80e3355fb
This commit is contained in:
Iván Budnik
2023-12-29 17:05:05 +00:00
parent 946f52b2a1
commit b02f9f38e4
2 changed files with 20 additions and 3 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settings.notification;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRouter2Manager;
@@ -53,6 +55,7 @@ public class RemoteVolumeGroupController extends BasePreferenceController implem
@VisibleForTesting
static final String SWITCHER_PREFIX = "OUTPUT_SWITCHER";
@Nullable
private PreferenceCategory mPreferenceCategory;
private final List<RoutingSessionInfo> mRoutingSessionInfos = new ArrayList<>();
@@ -61,6 +64,7 @@ public class RemoteVolumeGroupController extends BasePreferenceController implem
@VisibleForTesting
MediaRouter2Manager mRouterManager;
// Called via reflection from BasePreferenceController#createInstance().
public RemoteVolumeGroupController(Context context, String preferenceKey) {
super(context, preferenceKey);
if (mLocalMediaManager == null) {
@@ -71,6 +75,19 @@ public class RemoteVolumeGroupController extends BasePreferenceController implem
mRouterManager = MediaRouter2Manager.getInstance(context);
}
@VisibleForTesting
/* package */ RemoteVolumeGroupController(
@NonNull Context context,
@NonNull String preferenceKey,
@NonNull LocalMediaManager localMediaManager,
@NonNull MediaRouter2Manager mediaRouter2Manager) {
super(context, preferenceKey);
mLocalMediaManager = localMediaManager;
mRouterManager = mediaRouter2Manager;
mLocalMediaManager.registerCallback(this);
mLocalMediaManager.startScan();
}
@Override
public int getAvailabilityStatus() {
if (mRoutingSessionInfos.isEmpty()) {