Search in sources :

Example 6 with TestKey

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

the class BackstackCoreTest method replaceTopShouldWorkAsIntended.

@Test
public void replaceTopShouldWorkAsIntended() {
    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();
    assertThat(backstack.getHistory()).containsExactly(initial, other);
    backstack.replaceTop(another, StateChange.FORWARD);
    assertThat(stateChange.getDirection()).isEqualTo(StateChange.FORWARD);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, another);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 7 with TestKey

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

the class BackstackCoreTest method goUpChainWithMultipleElementWhenNoneExists.

@Test
public void goUpChainWithMultipleElementWhenNoneExists() {
    TestKey initial1 = new TestKey("hello1");
    TestKey initial2 = new TestKey("hello2");
    TestKey initial3 = new TestKey("hello3");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial3));
    StateChanger stateChanger = new StateChanger() {

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

Example 8 with TestKey

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

the class BackstackTest method afterClearAndRestorationTheFilteredAreNotRestored.

@Test
public void afterClearAndRestorationTheFilteredAreNotRestored() {
    final TestKey initial = new TestKey("initial");
    final TestKey restored = new TestKey("restored");
    final TestKey filtered = new TestKey("filtered");
    ArrayList<Parcelable> history = new ArrayList<>();
    history.add(restored);
    history.add(filtered);
    StateBundle stateBundle = new StateBundle();
    stateBundle.putParcelableArrayList(Backstack.getHistoryTag(), history);
    Backstack backstack = new Backstack();
    backstack.setKeyFilter(new KeyFilter() {

        @Nonnull
        @Override
        public List<Object> filterHistory(@Nonnull List<Object> restoredKeys) {
            restoredKeys.remove(filtered);
            return restoredKeys;
        }
    });
    backstack.setup(History.single(initial));
    backstack.fromBundle(stateBundle);
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.getHistory()).contains(restored);
    assertThat(backstack.getHistory()).doesNotContain(filtered);
    // // would restore properly
    backstack = new Backstack();
    backstack.setup(History.single(initial));
    backstack.fromBundle(stateBundle);
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.getHistory()).contains(restored, filtered);
    // // if both are filtered, restore initial
    backstack = new Backstack();
    backstack.setKeyFilter(new KeyFilter() {

        @Nonnull
        @Override
        public List<Object> filterHistory(@Nonnull List<Object> restoredKeys) {
            restoredKeys.remove(restored);
            restoredKeys.remove(filtered);
            return restoredKeys;
        }
    });
    backstack.setup(History.single(initial));
    backstack.fromBundle(stateBundle);
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.getHistory()).doesNotContain(restored, filtered);
    assertThat(backstack.getHistory()).contains(initial);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) ArrayList(java.util.ArrayList) List(java.util.List) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 9 with TestKey

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

the class BackstackTest method exitScopeToThrowsWhenBackstackIsEmpty.

@Test
public void exitScopeToThrowsWhenBackstackIsEmpty() {
    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 10 with TestKey

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

the class BackstackTest method stateChangeCompletionListenerIsCalledCorrectly.

@Test
public void stateChangeCompletionListenerIsCalledCorrectly() {
    final TestKey initial = new TestKey("initial");
    final TestKey other = new TestKey("other");
    final AtomicInteger atomicInteger = new AtomicInteger(0);
    final List<Integer> 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(atomicInteger.getAndIncrement());
        }
    };
    backstack.addStateChangeCompletionListener(stateChangeCompletionListener);
    backstack.setStateChanger(stateChanger);
    backstack.goTo(other);
    backstack.goBack();
    assertThat(integers).containsExactly(0, 1, 2);
    backstack.removeStateChangeCompletionListener(stateChangeCompletionListener);
    backstack.setHistory(History.single(other), StateChange.REPLACE);
    assertThat(integers).containsExactly(0, 1, 2);
    backstack.addStateChangeCompletionListener(stateChangeCompletionListener);
    backstack.setHistory(History.single(initial), StateChange.REPLACE);
    assertThat(integers).containsExactly(0, 1, 2, 3);
    backstack.removeAllStateChangeCompletionListeners();
    backstack.goTo(other);
    assertThat(integers).containsExactly(0, 1, 2, 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestKey(com.zhuinden.simplestack.helpers.TestKey) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) 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