Makes it possible to robo-test Settings app fragments.
This adds bunch of shadow/placeholder classes and logic to handle references to Android internal resources or newly added classes/methods that Robolectric hasn't yet picked up. Developers can follow ManageApplicationsTest example to use the shadow classes and the utility method to start ther fragment in their robolectric tests. Bug: 33431346 Test: This is a test improvement CL. RunSettingsRoboTests still passes. Change-Id: I943ab871631cb8c368d9f9db481c00558c5c4d1f
This commit is contained in:
@@ -15,15 +15,23 @@
|
||||
*/
|
||||
package com.android.settings;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.content.Intent;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.manifest.AndroidManifest;
|
||||
import org.robolectric.res.Fs;
|
||||
import org.robolectric.res.ResourcePath;
|
||||
import org.robolectric.util.ActivityController;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT;
|
||||
import static org.robolectric.Robolectric.getShadowsAdapter;
|
||||
|
||||
/**
|
||||
* Custom test runner for the testing of BluetoothPairingDialogs. This is needed because the
|
||||
* default behavior for robolectric is just to grab the resource directory in the target package.
|
||||
@@ -77,4 +85,15 @@ public class SettingsRobolectricTestRunner extends RobolectricTestRunner {
|
||||
manifest.setPackageName("com.android.settings");
|
||||
return manifest;
|
||||
}
|
||||
}
|
||||
|
||||
// A simple utility class to start a Settings fragment with an intent. The code here is almost
|
||||
// the same as FragmentTestUtil.startFragment except that it starts an activity with an intent.
|
||||
public static void startSettingsFragment(
|
||||
Fragment fragment, Class<? extends SettingsActivity> activityClass) {
|
||||
Intent intent = new Intent().putExtra(EXTRA_SHOW_FRAGMENT, fragment.getClass().getName());
|
||||
SettingsActivity activity = ActivityController.of(
|
||||
getShadowsAdapter(), ReflectionHelpers.callConstructor(activityClass), intent)
|
||||
.setup().get();
|
||||
activity.getFragmentManager().beginTransaction().add(fragment, null).commit();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user