Search in sources :

Example 81 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.

the class BackstackTest method exitScopeToExitsExplicitScopesCorrectly.

@Test
public void exitScopeToExitsExplicitScopesCorrectly() {
    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();
        }
    });
    Object targetKey = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
    assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey, targetKey);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) TestKeyWithExplicitParent(com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent) Test(org.junit.Test)

Example 82 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.

the class BackstackTest method exitScopeToThrowsWhenScopeIsNotFound.

@Test
public void exitScopeToThrowsWhenScopeIsNotFound() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    Object key = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.setup(History.of(key));
    Object targetKey = new TestKey("target");
    try {
        backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
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 83 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.

the class BackstackTest method exitScopeToExitsExplicitScopeCorrectlyAndGoesBackIfFound.

@Test
public void exitScopeToExitsExplicitScopeCorrectlyAndGoesBackIfFound() {
    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 TestKeyWithOnlyParentServices("key", History.of("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) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) TestKeyWithOnlyParentServices(com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices) Test(org.junit.Test)

Example 84 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.

the class BackstackTest method stateChangeCompletionListenerIsCalledInCorrectOrder.

@Test
public void stateChangeCompletionListenerIsCalledInCorrectOrder() {
    final TestKey initial = new TestKey("initial");
    final TestKey other = new TestKey("other");
    final List<TestKey> integers = new ArrayList<>();
    Backstack backstack = new Backstack();
    backstack.setup(History.single(initial));
    Backstack.CompletionListener stateChangeCompletionListener = new Backstack.CompletionListener() {

        @Override
        public void stateChangeCompleted(@Nonnull StateChange stateChange) {
            integers.add(stateChange.<TestKey>topNewKey());
        }
    };
    backstack.addStateChangeCompletionListener(stateChangeCompletionListener);
    backstack.setStateChanger(stateChanger);
    backstack.detachStateChanger();
    backstack.goTo(other);
    backstack.setHistory(History.single(initial), StateChange.REPLACE);
    backstack.setHistory(History.single(other), StateChange.REPLACE);
    backstack.setHistory(History.single(initial), StateChange.REPLACE);
    backstack.setHistory(History.single(initial), StateChange.REPLACE);
    backstack.goTo(other);
    backstack.reattachStateChanger();
    assertThat(integers).containsExactly(initial, other, initial, other, initial, initial, other);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 85 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.

the class HistoryBuilderTest method removeUntilThrowsIfKeyNotFound.

@Test
public void removeUntilThrowsIfKeyNotFound() {
    try {
        History.Builder builder = History.newBuilder().add(new TestKey("hello"));
        builder.removeUntil(new TestKey("bye"));
        Assert.fail();
    } catch (IllegalArgumentException e) {
    // Good!
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) 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