Search in sources :

Example 51 with TestKey

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

the class BackstackCoreTest method jumpToRootWorks.

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

        @Override
        public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
            stateChange = _stateChange;
            callback = completionCallback;
        }
    };
    backstack.setHistory(History.of(initial1, initial2, initial3), StateChange.REPLACE);
    backstack.setStateChanger(stateChanger);
    // initial state change
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial1, initial2, initial3);
    backstack.jumpToRoot();
    assertThat(stateChange.getDirection()).isSameAs(StateChange.BACKWARD);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial1);
    backstack.jumpToRoot(StateChange.REPLACE);
    assertThat(stateChange.getDirection()).isSameAs(StateChange.REPLACE);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial1);
    backstack.jumpToRoot(StateChange.FORWARD);
    assertThat(stateChange.getDirection()).isSameAs(StateChange.FORWARD);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial1);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 52 with TestKey

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

the class BackstackCoreTest method removedCompletionListenerShouldNotBeCalled.

@Test
public void removedCompletionListenerShouldNotBeCalled() {
    TestKey initial = new TestKey("hello");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial));
    Backstack.CompletionListener completionListener = Mockito.mock(Backstack.CompletionListener.class);
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange _stateChange, @Nonnull Callback completionCallback) {
            stateChange = _stateChange;
            callback = completionCallback;
        }
    };
    backstack.addCompletionListener(completionListener);
    backstack.removeCompletionListener(completionListener);
    backstack.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    assertThat(backstack.isStateChangePending()).isFalse();
    Mockito.verify(completionListener, Mockito.never()).stateChangeCompleted(stateChange);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 53 with TestKey

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

the class BackstackCoreTest method goUpChainWithSingleElementWhenPreviousDoesNotExistWithBefore.

@Test
public void goUpChainWithSingleElementWhenPreviousDoesNotExistWithBefore() {
    TestKey initial1 = new TestKey("hello1");
    TestKey initial2 = new TestKey("hello2");
    TestKey initial3 = new TestKey("hello3");
    TestKey initial4 = new TestKey("hello4");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial1, initial2, 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(initial4));
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial1, initial2, initial4);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 54 with TestKey

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

the class BackstackCoreTest method goBackShouldReturnTrueDuringActiveStateChange.

@Test
public void goBackShouldReturnTrueDuringActiveStateChange() {
    TestKey hi = new TestKey("hi");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(hi, new TestKey("bye")));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            callback = completionCallback;
        }
    });
    callback.stateChangeComplete();
    backstack.goTo(hi);
    assertThat(backstack.goBack()).isTrue();
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Test(org.junit.Test)

Example 55 with TestKey

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

the class BackstackCoreTest method illegalThreadAccessThrowsException.

@Test
public void illegalThreadAccessThrowsException() throws InterruptedException {
    TestKey testKey = new TestKey("a");
    final Backstack backstack = new Backstack();
    backstack.setup(History.of(testKey));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                backstack.setHistory(History.of(new TestKey("b")), StateChange.REPLACE);
            } catch (Exception e) {
                ref.set(e);
            } finally {
                latch.countDown();
            }
        }
    }).start();
    latch.await();
    assertThat(ref.get()).isInstanceOf(IllegalStateException.class);
    assertThat(ref.get().getMessage()).contains("backstack is not thread-safe");
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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