use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method goUpThrowsForNull.
@Test
public void goUpThrowsForNull() {
TestKey initial = new TestKey("hello");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial));
try {
backstack.goUp(null);
Assert.fail();
} catch (IllegalArgumentException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method retainedObjectsDoRestorePendingState.
@Test
public void retainedObjectsDoRestorePendingState() {
TestKey initialKey = new TestKey("initialKey");
TestKey newKey = new TestKey("newKey");
Backstack backstack = new Backstack();
backstack.setup(History.of(initialKey));
class TestObject implements Bundleable {
private int currentState = 3;
@Nonnull
@Override
public StateBundle toBundle() {
StateBundle stateBundle = new StateBundle();
stateBundle.putInt("currentState", 5);
return stateBundle;
}
@Override
public void fromBundle(@Nullable StateBundle bundle) {
if (bundle != null) {
currentState = bundle.getInt("currentState", 3);
}
}
}
class InvalidObject {
}
TestObject testObject = new TestObject();
TestObject testPendingObject = new TestObject();
TestObject testRemovedObject = new TestObject();
Object testNormalObject = new Object();
backstack.addRetainedObject("testObject", testObject);
backstack.addRetainedObject("testPendingObject", testPendingObject);
backstack.addRetainedObject("testRemovedObject", testRemovedObject);
backstack.addRetainedObject("testNormalObject", testNormalObject);
assertThat(testObject.currentState).isEqualTo(3);
assertThat(testPendingObject.currentState).isEqualTo(3);
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull StateChanger.Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.setHistory(History.of(newKey), StateChange.REPLACE);
StateBundle bundle = backstack.toBundle();
Backstack backstack2 = new Backstack();
backstack2.setup(History.of(initialKey));
assertThat(testObject.currentState).isEqualTo(3);
backstack2.addRetainedObject("testObject", testObject);
assertThat(testObject.currentState).isEqualTo(3);
backstack2.fromBundle(bundle);
assertThat(testObject.currentState).isEqualTo(5);
backstack2.addRetainedObject("testNormalObject", testNormalObject);
try {
backstack2.addRetainedObject("testPendingObject", new InvalidObject());
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
assertThat(testPendingObject.currentState).isEqualTo(3);
backstack2.addRetainedObject("testPendingObject", testPendingObject);
assertThat(testPendingObject.currentState).isEqualTo(5);
backstack2.removeRetainedObject("testRemovedObject");
backstack2.addRetainedObject("testRemovedObject", testRemovedObject);
assertThat(testRemovedObject.currentState).isEqualTo(3);
backstack2.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull StateChanger.Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack2.getHistory()).containsExactly(newKey);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class HistoryBuilderTest method removeUntilRemovesUntil.
@Test
public void removeUntilRemovesUntil() {
TestKey hi = new TestKey("hi");
TestKey hello = new TestKey("hello");
TestKey bye = new TestKey("bye");
List<Object> history = History.newBuilder().add(hi).add(hello).add(bye).removeUntil(hi).build();
assertThat(history).containsExactly(hi);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackDelegateTest method addNullStateChangeListenerThrows.
@Test
public void addNullStateChangeListenerThrows() {
TestKey testKey = new TestKey("hello");
BackstackDelegate backstackDelegate = new BackstackDelegate();
try {
backstackDelegate.addStateChangeCompletionListener(null);
Assert.fail();
} catch (IllegalArgumentException e) {
// OK
}
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackDelegateTest method registerLifecycleCallbacksShouldBeCalledForActivity.
@Test
public void registerLifecycleCallbacksShouldBeCalledForActivity() {
Activity activity = Mockito.mock(Activity.class);
Application application = Mockito.mock(Application.class);
Mockito.when(activity.getApplication()).thenReturn(application);
Object key = new TestKey("hello");
BackstackDelegate backstackDelegate = new BackstackDelegate();
backstackDelegate.onCreate(null, null, History.single(key));
// THEN
backstackDelegate.registerForLifecycleCallbacks(activity);
Mockito.verify(activity, Mockito.times(1)).getApplication();
}
Aggregations