恢复状态后,Robolectric视图不会更新

时间:2015-04-16 09:10:46

标签: android robolectric

我有一个包含片段的活动。 这是片段的一部分:

public class NotificationFragment extends DialogFragment {
    private static final String EDIT_TEXT_KEY = "EDIT_TEXT_KEY";
    private static final String SPINNER_KEY = "SPINNER_KEY";
    private static final String IMAGE_VIEW_KEY = "IMAGE_VIEW_KEY";

    private static final List<String> arraySpinner = Arrays.asList("item 1", "item 2", "item 3", "item 4", "item 5");

    //    private String text;
    private String spinnerValue;
    private Bitmap bitmap;

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("NotificationFragment#onActivityCreated");
        if (savedInstanceState != null) {
            final EditText editText = (EditText) getView().findViewById(R.id.edit_text);
            editText.setText(savedInstanceState.getString(EDIT_TEXT_KEY));
            editText.setText("gebroken");
            final Spinner spinner = (Spinner) getView().findViewById(R.id.spinner);
            spinner.setSelection(savedInstanceState.getInt(SPINNER_KEY));
            final ImageView imageView = (ImageView) getView().findViewById(R.id.image_view);
            imageView.setImageBitmap((Bitmap) savedInstanceState.getParcelable(IMAGE_VIEW_KEY));
            System.out.println("1: " + ((EditText) getView().findViewById(R.id.edit_text)).getText().toString());
            System.out.println(getView().hashCode());
        }
    }

在我的测试中,我想在恢复活动/片段后检查视图是否恢复。

我的测试(调试状态)如下所示:

@Config(manifest = IConfig.MANIFEST_PATH, emulateSdk = IConfig.SDK_VERSION, reportSdk = IConfig.SDK_VERSION)
@RunWith(RobolectricTestRunner.class)
public class AbstractFragmentTest {

    @Test
    public void test() {
        final ActivityController<NotificationActivity> controller = Robolectric.buildActivity(NotificationActivity.class).setup().visible()
                .start().resume();
        final NotificationActivity activity = controller.get();
        final ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        shadowActivity.recreate();
        assertEquals("gebroken", ((EditText) shadowActivity.findViewById(R.id.edit_text)).getText().toString());
    }

我希望有#34; gebroken&#34;结果,因为这是测试的输出:

NotificationActivity#onCreate
NotificationFragment#onCreateView
NotificationFragment#onViewCreated
2: 
NotificationFragment#onActivityCreated
NotificationFragment#onSaveInstanceState
NotificationActivity#onSaveInstanceState
NotificationActivity#onCreate
NotificationFragment#onCreateView
NotificationFragment#onViewCreated
2: 
NotificationFragment#onActivityCreated
1: gebroken
NotificationActivity#onRestoreInstanceState

但不是&#34; gebroken&#34;,结果是&#34;&#34;。

我错过了这里的刷新吗?

1 个答案:

答案 0 :(得分:1)

将测试更改为:

@Config(manifest = IConfig.MANIFEST_PATH, emulateSdk = IConfig.SDK_VERSION, reportSdk = IConfig.SDK_VERSION)
@RunWith(RobolectricTestRunner.class)
public class AbstractFragmentTest {
    private static final int EDIT_TEXT_ID = R.id.edit_text;
    private static final String EDIT_TEXT_VALUE = "this text value should be restored";

    private ActivityController<NotificationActivity> controller;

    @Before
    public void setupFreshFixture() {
        controller = Robolectric.buildActivity(NotificationActivity.class).setup();
    }

    @Test
    public void test() {
        setEditText(controller.get(), EDIT_TEXT_ID, EDIT_TEXT_VALUE);
        final Bundle state = restartActivity();
        assertEquals(EDIT_TEXT_VALUE, state.getString(NotificationFragment.EDIT_TEXT_KEY));
        assertEquals(EDIT_TEXT_VALUE, getEditText(controller.get(), EDIT_TEXT_ID));
    }

    private Bundle restartActivity() {
        final Bundle state = new Bundle();
        controller.saveInstanceState(state).stop().destroy();
        controller = Robolectric.buildActivity(NotificationActivity.class).setup(state);
        return state;
    }

    //TODO move to utilities
    public static String getEditText(final Activity activity, final int id) {
        return ((EditText) activity.findViewById(id)).getText().toString();
    }

    public static void setEditText(final Activity activity, final int id, final String value) {
        ((EditText) activity.findViewById(id)).setText(value);
    }
}

我也必须修改我的活动:

public class NotificationActivity extends FragmentActivity {

    private NotificationFragment fragment;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("NotificationActivity#onCreate");
        setContentView(R.layout.activity_notification);
        if (savedInstanceState == null) {
            fragment = new NotificationFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.notification_container, fragment).commit();
        }
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        System.out.println("NotificationActivity#onSaveInstanceState");
        fragment.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(final Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        System.out.println("NotificationActivity#onRestoreInstanceState");
        getSupportFragmentManager().findFragmentById(R.id.notification_container).onActivityCreated(savedInstanceState);
    }
}

所有这些一起确保您可以使用robolectric测试片段中状态的保存和恢复