use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class BackstackDelegateTest method testRestoreViewFromState.
@Test
public void testRestoreViewFromState() {
BackstackDelegate backstackDelegate = new BackstackDelegate();
TestKey key = new TestKey("hello");
backstackDelegate.onCreate(null, null, History.single(key));
backstackDelegate.setStateChanger(stateChanger);
Mockito.when(view.getContext()).thenReturn(context);
StateBundle stateBundle = new StateBundle();
Mockito.when(((Bundleable) view).toBundle()).thenReturn(stateBundle);
// noinspection ResourceType
Mockito.when(context.getSystemService(KeyContextWrapper.TAG)).thenReturn(key);
backstackDelegate.persistViewToState(view);
backstackDelegate.restoreViewFromState(view);
((Bundleable) Mockito.verify(view, Mockito.times(1))).fromBundle(stateBundle);
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class BackstackTest method getInitialKeysReturnsExpectedValues.
@Test
public void getInitialKeysReturnsExpectedValues() {
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();
backstack.setStateChanger(stateChanger);
assertThat(backstack.getInitialKeys()).containsExactly(initial);
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class ScopeManager method saveStates.
StateBundle saveStates() {
StateBundle rootBundle = new StateBundle();
for (Map.Entry<String, ScopeNode> scopeSet : scopes.entrySet()) {
String scopeKey = scopeSet.getKey();
ScopeNode services = scopeSet.getValue();
StateBundle scopeBundle = new StateBundle();
for (Map.Entry<String, Object> serviceEntry : services.services()) {
String serviceTag = serviceEntry.getKey();
Object service = serviceEntry.getValue();
if (service instanceof Bundleable) {
scopeBundle.putBundle(serviceTag, ((Bundleable) service).toBundle());
}
}
rootBundle.putBundle(scopeKey, scopeBundle);
}
return rootBundle;
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class ScopingTest method persistedStateOfScopedServicesIsRestored.
@Test
public void persistedStateOfScopedServicesIsRestored() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(testKey2));
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
};
backstack.setStateChanger(stateChanger);
assertThat(backstack.hasService(testKey2.getScopeTag(), SERVICE_TAG)).isTrue();
StateBundle stateBundle = backstack.toBundle();
Backstack backstack2 = new Backstack();
backstack2.setScopedServices(new ServiceProvider());
StateChanger stateChanger2 = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
};
backstack2.setup(History.of(testKey2));
backstack2.fromBundle(stateBundle);
backstack2.setStateChanger(stateChanger2);
assertThat(backstack2.<Service>getService(testKey2.getScopeTag(), SERVICE_TAG).blah).isEqualTo(5);
}
use of com.zhuinden.statebundle.StateBundle in project simple-stack by Zhuinden.
the class ScopingTest method scopeServicesArePersistedToStateBundle.
@Test
public void scopeServicesArePersistedToStateBundle() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Service service = new Service();
TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
backstack.setup(History.of(testKeyWithScope));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.hasService(testKeyWithScope.getScopeTag(), SERVICE_TAG)).isTrue();
StateBundle stateBundle = backstack.toBundle();
// noinspection ConstantConditions
// backstack.getScopesTag() is internal
assertThat(stateBundle.getBundle(Backstack.getScopesTag()).getBundle(testKeyWithScope.getScopeTag()).getBundle(SERVICE_TAG).getInt("blah")).isEqualTo(5);
}
Aggregations