use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method replaceTopShouldWorkAsIntended.
@Test
public void replaceTopShouldWorkAsIntended() {
TestKey initial = new TestKey("hello");
TestKey other = new TestKey("other");
TestKey another = new TestKey("another");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial));
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
stateChange = _stateChange;
callback = completionCallback;
}
};
backstack.setStateChanger(stateChanger);
callback.stateChangeComplete();
backstack.goTo(other);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial, other);
backstack.replaceTop(another, StateChange.FORWARD);
assertThat(stateChange.getDirection()).isEqualTo(StateChange.FORWARD);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial, another);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method goUpChainWithMultipleElementWhenNoneExists.
@Test
public void goUpChainWithMultipleElementWhenNoneExists() {
TestKey initial1 = new TestKey("hello1");
TestKey initial2 = new TestKey("hello2");
TestKey initial3 = new TestKey("hello3");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial3));
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
stateChange = _stateChange;
callback = completionCallback;
}
};
backstack.setStateChanger(stateChanger);
callback.stateChangeComplete();
backstack.goUpChain(History.of(initial1, initial2));
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial1, initial2);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method afterClearAndRestorationTheFilteredAreNotRestored.
@Test
public void afterClearAndRestorationTheFilteredAreNotRestored() {
final TestKey initial = new TestKey("initial");
final TestKey restored = new TestKey("restored");
final TestKey filtered = new TestKey("filtered");
ArrayList<Parcelable> history = new ArrayList<>();
history.add(restored);
history.add(filtered);
StateBundle stateBundle = new StateBundle();
stateBundle.putParcelableArrayList(Backstack.getHistoryTag(), history);
Backstack backstack = new Backstack();
backstack.setKeyFilter(new KeyFilter() {
@Nonnull
@Override
public List<Object> filterHistory(@Nonnull List<Object> restoredKeys) {
restoredKeys.remove(filtered);
return restoredKeys;
}
});
backstack.setup(History.single(initial));
backstack.fromBundle(stateBundle);
backstack.setStateChanger(stateChanger);
assertThat(backstack.getHistory()).contains(restored);
assertThat(backstack.getHistory()).doesNotContain(filtered);
// // would restore properly
backstack = new Backstack();
backstack.setup(History.single(initial));
backstack.fromBundle(stateBundle);
backstack.setStateChanger(stateChanger);
assertThat(backstack.getHistory()).contains(restored, filtered);
// // if both are filtered, restore initial
backstack = new Backstack();
backstack.setKeyFilter(new KeyFilter() {
@Nonnull
@Override
public List<Object> filterHistory(@Nonnull List<Object> restoredKeys) {
restoredKeys.remove(restored);
restoredKeys.remove(filtered);
return restoredKeys;
}
});
backstack.setup(History.single(initial));
backstack.fromBundle(stateBundle);
backstack.setStateChanger(stateChanger);
assertThat(backstack.getHistory()).doesNotContain(restored, filtered);
assertThat(backstack.getHistory()).contains(initial);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeToThrowsWhenBackstackIsEmpty.
@Test
public void exitScopeToThrowsWhenBackstackIsEmpty() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object key = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(key));
Object targetKey = new TestKey("target");
try {
backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method stateChangeCompletionListenerIsCalledCorrectly.
@Test
public void stateChangeCompletionListenerIsCalledCorrectly() {
final TestKey initial = new TestKey("initial");
final TestKey other = new TestKey("other");
final AtomicInteger atomicInteger = new AtomicInteger(0);
final List<Integer> integers = new ArrayList<>();
Backstack backstack = new Backstack();
backstack.setup(History.single(initial));
Backstack.CompletionListener stateChangeCompletionListener = new Backstack.CompletionListener() {
@Override
public void stateChangeCompleted(@Nonnull StateChange stateChange) {
integers.add(atomicInteger.getAndIncrement());
}
};
backstack.addStateChangeCompletionListener(stateChangeCompletionListener);
backstack.setStateChanger(stateChanger);
backstack.goTo(other);
backstack.goBack();
assertThat(integers).containsExactly(0, 1, 2);
backstack.removeStateChangeCompletionListener(stateChangeCompletionListener);
backstack.setHistory(History.single(other), StateChange.REPLACE);
assertThat(integers).containsExactly(0, 1, 2);
backstack.addStateChangeCompletionListener(stateChangeCompletionListener);
backstack.setHistory(History.single(initial), StateChange.REPLACE);
assertThat(integers).containsExactly(0, 1, 2, 3);
backstack.removeAllStateChangeCompletionListeners();
backstack.goTo(other);
assertThat(integers).containsExactly(0, 1, 2, 3);
}
Aggregations