use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method onGoBackKeysShouldNotBeClearedAndShouldRestoreRestoredKey.
@Test
public void onGoBackKeysShouldNotBeClearedAndShouldRestoreRestoredKey() {
TestKey initial = new TestKey("initial");
TestKey restored = new TestKey("restored");
ArrayList<Parcelable> history = new ArrayList<>();
history.add(restored);
StateBundle stateBundle = new StateBundle();
stateBundle.putParcelableArrayList(Backstack.getHistoryTag(), history);
Backstack backstack = new Backstack();
backstack.setup(History.single(initial));
backstack.fromBundle(stateBundle);
backstack.setStateChanger(stateChanger);
backstack.goBack();
assertThat(backstack.getHistory()).isNotEmpty();
assertThat(backstack.getHistory()).containsExactly(restored);
backstack.setStateChanger(stateChanger);
assertThat(backstack.getHistory()).containsExactly(restored);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeToWorks.
@Test
public void exitScopeToWorks() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object key = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
Object targetKey = new TestKey("targetKey");
backstack.setup(History.of(key));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
assertThat(backstack.getHistory()).containsExactly(targetKey);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method retainedObjectRestoringInvalidObjectFails.
@Test
public void retainedObjectRestoringInvalidObjectFails() {
TestKey initialKey = new TestKey("initialKey");
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();
InvalidObject invalidObject = new InvalidObject();
backstack.addRetainedObject("testObject", testObject);
assertThat(testObject.currentState).isEqualTo(3);
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull StateChanger.Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
StateBundle bundle = backstack.toBundle();
Backstack backstack2 = new Backstack();
backstack2.setup(History.of(initialKey));
backstack2.addRetainedObject("testObject", invalidObject);
try {
backstack2.fromBundle(bundle);
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method keyFilterSetAfterSetupShouldThrow.
@Test
public void keyFilterSetAfterSetupShouldThrow() {
final TestKey initial = new TestKey("initial");
Backstack backstack = new Backstack();
backstack.setup(History.single(initial));
try {
backstack.setKeyFilter(new KeyFilter() {
@Nonnull
@Override
public List<Object> filterHistory(@Nonnull List<Object> restoredKeys) {
return restoredKeys;
}
});
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class ScopingTest method keyWithinNavigationWithOnlyExplicitScopeStillAbleToFindScopes.
@Test
public void keyWithinNavigationWithOnlyExplicitScopeStillAbleToFindScopes() {
Backstack backstack = new Backstack();
final Object helloService = new Object();
final Object worldService = new Object();
final Object kappaService = new Object();
final Object parentService = new Object();
final Object parent2Service = 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);
} else if ("parent2".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("parent2", parent2Service);
}
}
});
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));
}
}
class TestKeyWithOnlyExplicitParent extends TestKey implements ScopeKey.Child {
private String[] parentScopes;
TestKeyWithOnlyExplicitParent(String name, String... parentScopes) {
super(name);
this.parentScopes = parentScopes;
}
protected TestKeyWithOnlyExplicitParent(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 TestKeyWithOnlyExplicitParent("parentpls", "parent2");
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)).containsExactly("parent2");
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("parent2", "world", "parent", "hello");
backstack.setHistory(History.of(scopeKey1, scopeKey3), StateChange.REPLACE);
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).containsExactly("parent2");
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("parent2", "world", "parent", "hello");
callbackRef.get().stateChangeComplete();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).isEmpty();
}
Aggregations