Search in sources :

Example 36 with StateBundle

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);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 37 with 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);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 38 with StateBundle

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;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) StateBundle(com.zhuinden.statebundle.StateBundle)

Example 39 with StateBundle

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);
}
Also used : Nonnull(javax.annotation.Nonnull) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 40 with StateBundle

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);
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Aggregations

StateBundle (com.zhuinden.statebundle.StateBundle)44 Test (org.junit.Test)17 Parcelable (android.os.Parcelable)12 TestKey (com.zhuinden.simplestack.helpers.TestKey)11 ArrayList (java.util.ArrayList)10 ServiceTree (com.zhuinden.servicetree.ServiceTree)8 Nonnull (javax.annotation.Nonnull)6 NonNull (android.support.annotation.NonNull)5 Bundleable (com.zhuinden.simplestack.Bundleable)4 ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 Nullable (javax.annotation.Nullable)4 SparseArray (android.util.SparseArray)2 AbstractMap (java.util.AbstractMap)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 List (java.util.List)2 Context (android.content.Context)1 View (android.view.View)1