Search in sources :

Example 56 with TestKey

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

the class BackstackCoreTest method forceClearShouldClearStackIfStateChangeIsComplete.

@Test
public void forceClearShouldClearStackIfStateChangeIsComplete() {
    TestKey initial = new TestKey("hello");
    TestKey other = new TestKey("other");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
            stateChange = _stateChange;
            callback = completionCallback;
        }
    };
    backstack.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    backstack.goTo(other);
    callback.stateChangeComplete();
    backstack.forceClear();
    assertThat(backstack.getHistory()).isEmpty();
    backstack.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 57 with TestKey

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

the class BackstackCoreTest method goUpWithMoreElementsFoundParentGoesToParent.

@Test
public void goUpWithMoreElementsFoundParentGoesToParent() {
    TestKey initial = new TestKey("hello");
    TestKey other = new TestKey("other");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
            stateChange = _stateChange;
            callback = completionCallback;
        }
    };
    backstack.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    backstack.goTo(other);
    callback.stateChangeComplete();
    backstack.goUp(initial);
    callback.stateChangeComplete();
    ;
    assertThat(backstack.getHistory()).containsExactly(initial);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 58 with TestKey

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

the class BackstackCoreTest method replaceTopReentrantShouldWorkAsIntended.

@Test
public void replaceTopReentrantShouldWorkAsIntended() {
    TestKey initial = new TestKey("hello");
    TestKey other = new TestKey("other");
    TestKey another = new TestKey("another");
    TestKey yetAnother = new TestKey("yetAnother");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
            stateChange = _stateChange;
            callback = completionCallback;
        }
    };
    backstack.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    backstack.goTo(other);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, other);
    backstack.replaceTop(another, StateChange.BACKWARD);
    // replaceTop is terminal, so this is ignored
    backstack.replaceTop(yetAnother, StateChange.REPLACE);
    assertThat(stateChange.getDirection()).isEqualTo(StateChange.BACKWARD);
    assertThat(stateChange.getNewKeys()).containsExactly(initial, another);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, another);
    try {
        callback.stateChangeComplete();
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK
    }
    assertThat(backstack.getHistory()).containsExactly(initial, another);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 59 with TestKey

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

the class BackstackCoreTest method rootWorks.

@Test
public void rootWorks() {
    TestKey initial1 = new TestKey("hello1");
    TestKey initial2 = new TestKey("hello2");
    TestKey initial3 = new TestKey("hello3");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial1));
    try {
        backstack.root();
        org.junit.Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            if (stateChange.getPreviousKeys().isEmpty()) {
                assertThat(stateChange.getBackstack().root()).isSameAs(stateChange.getNewKeys().get(0));
            } else {
                assertThat(stateChange.getBackstack().root()).isSameAs(stateChange.getPreviousKeys().get(0));
            }
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.root()).isSameAs(initial1);
    backstack.setHistory(History.of(initial2, initial3), StateChange.REPLACE);
    assertThat(backstack.root()).isSameAs(initial2);
    backstack.removeStateChanger();
    backstack.setStateChanger(stateChanger);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 60 with TestKey

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

the class BackstackCoreTest method goUpWithMoreElementsNotFoundParentReplacesCurrentTop.

@Test
public void goUpWithMoreElementsNotFoundParentReplacesCurrentTop() {
    TestKey initial = new TestKey("hello");
    TestKey other = new TestKey("other");
    TestKey another = new TestKey("another");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
            stateChange = _stateChange;
            callback = completionCallback;
        }
    };
    backstack.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    backstack.goTo(other);
    callback.stateChangeComplete();
    backstack.goUp(another);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, another);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) 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