Skip to content

Commit 9c06914

Browse files
committed
Add test case to ensure that super.onRestoreInstanceState() is called correctly
1 parent 707b933 commit 9c06914

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2016-2017 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.instancestate;
17+
18+
import org.androidannotations.annotations.EView;
19+
import org.androidannotations.annotations.InstanceState;
20+
21+
import android.content.Context;
22+
import android.os.Parcelable;
23+
import android.view.View;
24+
25+
@EView
26+
public class InstanceStateView extends View {
27+
28+
@InstanceState
29+
int instanceField = -1;
30+
31+
int restoredInRestoreInstanceState = -2;
32+
33+
public InstanceStateView(Context context) {
34+
super(context);
35+
}
36+
37+
@Override
38+
protected void onRestoreInstanceState(Parcelable state) {
39+
super.onRestoreInstanceState(state);
40+
restoredInRestoreInstanceState = instanceField;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (C) 2016-2017 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.instancestate;
17+
18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
20+
import org.androidannotations.test.EmptyActivityWithoutLayout_;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.robolectric.Robolectric;
24+
import org.robolectric.RobolectricTestRunner;
25+
26+
import android.content.Context;
27+
import android.os.Bundle;
28+
29+
@RunWith(RobolectricTestRunner.class)
30+
public class InstanceStateViewTest {
31+
32+
@Test
33+
public void testInstanceState() {
34+
int restoredValue = 42;
35+
36+
Bundle bundle = new Bundle();
37+
bundle.putInt("instanceField", restoredValue);
38+
39+
Context context = Robolectric.buildActivity(EmptyActivityWithoutLayout_.class).create().get();
40+
InstanceStateView stateView = InstanceStateView_.build(context);
41+
42+
assertThat(stateView.instanceField).isEqualTo(-1);
43+
assertThat(stateView.restoredInRestoreInstanceState).isEqualTo(-2);
44+
45+
stateView.onRestoreInstanceState(bundle);
46+
47+
assertThat(stateView.instanceField).isEqualTo(restoredValue);
48+
assertThat(stateView.restoredInRestoreInstanceState).isEqualTo(restoredValue);
49+
}
50+
}

0 commit comments

Comments
 (0)