use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class ScopingTest method keyWithinNavigationButWithoutScopeStillAbleToFindScopes.
@Test
public void keyWithinNavigationButWithoutScopeStillAbleToFindScopes() {
Backstack backstack = new Backstack();
final Object helloService = new Object();
final Object worldService = new Object();
final Object kappaService = new Object();
final Object parentService = new Object();
backstack.setScopedServices(new ScopedServices() {
@Override
public void bindServices(@Nonnull ServiceBinder serviceBinder) {
if ("hello".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("hello", helloService);
} else if ("world".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("world", worldService);
} else if ("kappa".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("kappa", kappaService);
} else if ("parent".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("parent", parentService);
}
}
});
class TestKeyWithExplicitParent extends TestKeyWithScope implements ScopeKey.Child {
private String[] parentScopes;
TestKeyWithExplicitParent(String name, String... parentScopes) {
super(name);
this.parentScopes = parentScopes;
}
protected TestKeyWithExplicitParent(Parcel in) {
super(in);
}
@Nonnull
@Override
public List<String> getParentScopes() {
return History.from(Arrays.asList(parentScopes));
}
}
TestKeyWithScope scopeKey1 = new TestKeyWithScope("hello");
TestKeyWithScope scopeKey2 = new TestKeyWithExplicitParent("world", "parent");
TestKeyWithScope scopeKey3 = new TestKeyWithScope("kappa");
TestKey key4 = new TestKey("360noscope");
TestKey key5 = new TestKey("180noscope");
backstack.setup(History.of(scopeKey1, scopeKey2, key4));
final AtomicReference<StateChanger.Callback> callbackRef = new AtomicReference<>();
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
callbackRef.set(completionCallback);
}
});
callbackRef.get().stateChangeComplete();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("world", "parent", "hello");
backstack.setHistory(History.of(scopeKey1, scopeKey3, key5), StateChange.REPLACE);
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("world", "parent", "hello");
assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.ALL)).containsExactly("kappa", "world", "parent", "hello");
callbackRef.get().stateChangeComplete();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).isEmpty();
assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.ALL)).containsExactly("kappa", "hello");
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method goUpChainWithFallbackOnExactMatchWorksAsDefaultBack.
@Test
public void goUpChainWithFallbackOnExactMatchWorksAsDefaultBack() {
TestKey initial = new TestKey("hello");
TestKey other = new TestKey("other");
TestKey another = new TestKey("another");
TestKey boop = new TestKey("boop");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial, other, another, boop));
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(initial, other), true);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial, other, another);
backstack.goUpChain(History.of(initial, other), true);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial, other);
backstack.goUpChain(History.of(initial, other), true);
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial, other);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method topWorks.
@Test
public void topWorks() {
TestKey initial1 = new TestKey("hello1");
TestKey initial2 = new TestKey("hello2");
TestKey initial3 = new TestKey("hello3");
Backstack backstack = new Backstack();
backstack.setup(History.of(initial1, initial2, initial3));
try {
backstack.top();
org.junit.Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
if (stateChange.getPreviousKeys().isEmpty()) {
assertThat(stateChange.getBackstack().top()).isSameAs(stateChange.topNewKey());
} else {
assertThat(stateChange.getBackstack().top()).isSameAs(stateChange.topPreviousKey());
}
completionCallback.stateChangeComplete();
}
};
backstack.setStateChanger(stateChanger);
assertThat(backstack.top()).isSameAs(initial3);
backstack.setHistory(History.of(initial1, initial2), StateChange.REPLACE);
assertThat(backstack.top()).isSameAs(initial2);
backstack.removeStateChanger();
backstack.setStateChanger(stateChanger);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method completionListenerShouldBeCalled.
@Test
public void completionListenerShouldBeCalled() {
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.setStateChanger(stateChanger);
callback.stateChangeComplete();
assertThat(backstack.isStateChangePending()).isFalse();
Mockito.verify(completionListener, Mockito.only()).stateChangeCompleted(stateChange);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackCoreTest method goUpChainWithSingleElementWhenPreviousDoesNotExist.
@Test
public void goUpChainWithSingleElementWhenPreviousDoesNotExist() {
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();
backstack.goUpChain(History.single(another));
callback.stateChangeComplete();
assertThat(backstack.getHistory()).containsExactly(initial, another);
}
Aggregations