Add an automated test for the null bundle crash.
Bug: 34633464 Test: Settings robotest . Change-Id: I244e58c3660ea3376dbeb6293da54c99a98e3157
This commit is contained in:
@@ -21,6 +21,7 @@ import android.os.Bundle;
|
||||
import android.os.storage.StorageManager;
|
||||
import android.os.storage.VolumeInfo;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
@@ -55,14 +56,11 @@ public class StorageDashboardFragment extends DashboardFragment {
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
final Context context = getActivity();
|
||||
|
||||
// Initialize the storage sizes that we can quickly calc.
|
||||
final Context context = getActivity();
|
||||
StorageManager sm = context.getSystemService(StorageManager.class);
|
||||
String volumeId = getArguments().getString(VolumeInfo.EXTRA_VOLUME_ID,
|
||||
VolumeInfo.ID_PRIVATE_INTERNAL);
|
||||
mVolume = sm.findVolumeById(volumeId);
|
||||
if (!isVolumeValid()) {
|
||||
if (!initializeVolume(sm, getArguments())) {
|
||||
getActivity().finish();
|
||||
return;
|
||||
}
|
||||
@@ -119,6 +117,17 @@ public class StorageDashboardFragment extends DashboardFragment {
|
||||
return controllers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the volume with a given bundle and returns if the volume is valid.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
boolean initializeVolume(StorageManager sm, Bundle bundle) {
|
||||
String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID,
|
||||
VolumeInfo.ID_PRIVATE_INTERNAL);
|
||||
mVolume = sm.findVolumeById(volumeId);
|
||||
return isVolumeValid();
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
*/
|
||||
|
@@ -16,6 +16,10 @@
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.storage.DiskInfo;
|
||||
import android.os.storage.StorageManager;
|
||||
import android.os.storage.VolumeInfo;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
@@ -35,15 +39,19 @@ import org.robolectric.shadows.ShadowApplication;
|
||||
import java.util.List;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class StorageDashboardFragmentTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private StorageManager mStorageManager;
|
||||
|
||||
private StorageDashboardFragment mFragment;
|
||||
|
||||
@Before
|
||||
@@ -71,4 +79,11 @@ public class StorageDashboardFragmentTest {
|
||||
assertThat(indexRes).isNotNull();
|
||||
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitializeVolumeDoesntBreakOnNullVolume() {
|
||||
VolumeInfo info = new VolumeInfo("id", 0, new DiskInfo("id", 0), "");
|
||||
when(mStorageManager.findVolumeById(anyString())).thenReturn(info);
|
||||
mFragment.initializeVolume(mStorageManager, new Bundle());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user