use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method afterClearAndRestorationTheInitialKeysShouldBeRestoredAndNotOverwrittenByRestoredState.
@Test
public void afterClearAndRestorationTheInitialKeysShouldBeRestoredAndNotOverwrittenByRestoredState() {
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);
if (!backstack.goBack()) {
backstack.forceClear();
}
assertThat(backstack.getHistory()).isEmpty();
backstack.setStateChanger(stateChanger);
assertThat(backstack.getHistory()).doesNotContain(restored);
assertThat(backstack.getHistory()).containsExactly(initial);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method savedStateWorksForResultPassing.
@Test
public void savedStateWorksForResultPassing() {
TestKey first = new TestKey("first");
View view = Mockito.mock(View.class);
Context context = Mockito.mock(Context.class);
Mockito.when(view.getContext()).thenReturn(context);
Mockito.when(context.getSystemService(KeyContextWrapper.TAG)).thenReturn(first);
TestKey second = new TestKey("second");
Backstack backstack = new Backstack();
backstack.setup(History.of(first));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.goTo(second);
StateBundle firstBundle = backstack.getSavedState(first).getBundle();
firstBundle.putString("result", "Success!");
backstack.persistViewToState(view);
StateBundle persistedBundle = backstack.toBundle();
Backstack backstack2 = new Backstack();
backstack2.setup(History.of(second));
backstack2.fromBundle(persistedBundle);
backstack2.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack2.getSavedState(first).getBundle().getString("result")).isEqualTo("Success!");
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeExitsExplicitScopesCorrectly.
@Test
public void exitScopeExitsExplicitScopesCorrectly() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object firstKey = new TestKey("firstKey");
Object secondKey = new TestKey("secondKey");
Object lastKey = new TestKey("lastKey");
Object key1 = new TestKeyWithExplicitParent("key1") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("blah", "parentKey1");
}
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
}
};
Object key2 = new TestKeyWithExplicitParent("key2") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("blah", "parentKey2");
}
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(firstKey, secondKey, key1, key2, lastKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.exitScope("blah");
assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeToExitsImplicitScopeCorrectlyAndGoesBackIfFound.
@Test
public void exitScopeToExitsImplicitScopeCorrectlyAndGoesBackIfFound() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object firstKey = new TestKey("firstKey");
Object secondKey = new TestKey("secondKey");
Object thirdKey = new TestKey("thirdKey");
Object lastKey = new TestKey("lastKey");
Object key = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(firstKey, secondKey, thirdKey, key, lastKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.exitScopeTo("blah", secondKey, StateChange.BACKWARD);
assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeExitsExplicitScopeCorrectly.
@Test
public void exitScopeExitsExplicitScopeCorrectly() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object firstKey = new TestKey("firstKey");
Object lastKey = new TestKey("lastKey");
Object key = new TestKeyWithOnlyParentServices("key", History.of("blah")) {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(firstKey, key, lastKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.exitScope("blah");
assertThat(backstack.getHistory()).containsExactly(firstKey);
}
Aggregations