use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method jumpToRootWorks.
@Test
public void jumpToRootWorks() {
TestKey initial1 = new TestKey("hello1");
TestKey initial2 = new TestKey("hello2");
TestKey initial3 = new TestKey("hello3");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial1));
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
stateChange = _stateChange;
callback = completionCallback;
}
};
backstack.setHistory(History.of(initial1, initial2, initial3), StateChange.REPLACE);
backstack.setStateChanger(stateChanger);
// initial state change
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial1, initial2, initial3);
backstack.jumpToRoot();
assertThat(stateChange.getDirection()).isSameAs(StateChange.BACKWARD);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial1);
backstack.jumpToRoot(StateChange.REPLACE);
assertThat(stateChange.getDirection()).isSameAs(StateChange.REPLACE);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial1);
backstack.jumpToRoot(StateChange.FORWARD);
assertThat(stateChange.getDirection()).isSameAs(StateChange.FORWARD);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial1);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method removedCompletionListenerShouldNotBeCalled.
@Test
public void removedCompletionListenerShouldNotBeCalled() {
TestKey initial = new TestKey("hello");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial));
Backstack.CompletionListener completionListener = Mockito.mock(Backstack.CompletionListener.class);
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
stateChange = _stateChange;
callback = completionCallback;
}
};
backstack.addCompletionListener(completionListener);
backstack.removeCompletionListener(completionListener);
backstack.setStateChanger(stateChanger);
callback.stateChangeComplete();
assertThat(backstack.isStateChangePending()).isFalse();
Mockito.verify(completionListener, Mockito.never()).stateChangeCompleted(stateChange);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method goUpChainWithSingleElementWhenPreviousDoesNotExistWithBefore.
@Test
public void goUpChainWithSingleElementWhenPreviousDoesNotExistWithBefore() {
TestKey initial1 = new TestKey("hello1");
TestKey initial2 = new TestKey("hello2");
TestKey initial3 = new TestKey("hello3");
TestKey initial4 = new TestKey("hello4");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial1, initial2, 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(initial4));
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial1, initial2, initial4);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method goBackShouldReturnTrueDuringActiveStateChange.
@Test
public void goBackShouldReturnTrueDuringActiveStateChange() {
TestKey hi = new TestKey("hi");
Backstack backstack = new Backstack();
backstack.setup(History.of(hi, new TestKey("bye")));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
callback = completionCallback;
}
});
callback.stateChangeComplete();
backstack.goTo(hi);
assertThat(backstack.goBack()).isTrue();
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method illegalThreadAccessThrowsException.
@Test
public void illegalThreadAccessThrowsException() throws InterruptedException {
TestKey testKey = new TestKey("a");
final Backstack backstack = new Backstack();
backstack.setup(History.of(testKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> ref = new AtomicReference<>();
new Thread(new Runnable() {
@Override
public void run() {
try {
backstack.setHistory(History.of(new TestKey("b")), StateChange.REPLACE);
} catch (Exception e) {
ref.set(e);
} finally {
latch.countDown();
}
}
}).start();
latch.await();
assertThat(ref.get()).isInstanceOf(IllegalStateException.class);
assertThat(ref.get().getMessage()).contains("backstack is not thread-safe");
}
Aggregations