Search in sources :

Example 11 with TestKey

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);
}
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 12 with TestKey

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!");
}
Also used : Context(android.content.Context) TestKey(com.zhuinden.simplestack.helpers.TestKey) View(android.view.View) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 13 with TestKey

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

Example 14 with TestKey

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

Example 15 with TestKey

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

Aggregations

TestKey (com.zhuinden.simplestack.helpers.TestKey)148 Test (org.junit.Test)148 Nonnull (javax.annotation.Nonnull)43 ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)26 ArrayList (java.util.ArrayList)22 StateBundle (com.zhuinden.statebundle.StateBundle)11 Parcel (android.os.Parcel)10 HasServices (com.zhuinden.simplestack.helpers.HasServices)7 TestKeyWithScope (com.zhuinden.simplestack.helpers.TestKeyWithScope)7 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)6 Parcelable (android.os.Parcelable)5 TestKeyWithOnlyParentServices (com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 Activity (android.app.Activity)2 TestKeyWithExplicitParent (com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent)2 Application (android.app.Application)1 Context (android.content.Context)1 View (android.view.View)1