Search in sources :

Example 1 with TestKey

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

the class ScopingTest method keyWithinNavigationButWithoutScopeStillAbleToFindScopes.

@Test
public void keyWithinNavigationButWithoutScopeStillAbleToFindScopes() {
    Backstack backstack = new Backstack();
    final Object helloService = new Object();
    final Object worldService = new Object();
    final Object kappaService = new Object();
    final Object parentService = new Object();
    backstack.setScopedServices(new ScopedServices() {

        @Override
        public void bindServices(@Nonnull ServiceBinder serviceBinder) {
            if ("hello".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("hello", helloService);
            } else if ("world".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("world", worldService);
            } else if ("kappa".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("kappa", kappaService);
            } else if ("parent".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parent", parentService);
            }
        }
    });
    class TestKeyWithExplicitParent extends TestKeyWithScope implements ScopeKey.Child {

        private String[] parentScopes;

        TestKeyWithExplicitParent(String name, String... parentScopes) {
            super(name);
            this.parentScopes = parentScopes;
        }

        protected TestKeyWithExplicitParent(Parcel in) {
            super(in);
        }

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.from(Arrays.asList(parentScopes));
        }
    }
    TestKeyWithScope scopeKey1 = new TestKeyWithScope("hello");
    TestKeyWithScope scopeKey2 = new TestKeyWithExplicitParent("world", "parent");
    TestKeyWithScope scopeKey3 = new TestKeyWithScope("kappa");
    TestKey key4 = new TestKey("360noscope");
    TestKey key5 = new TestKey("180noscope");
    backstack.setup(History.of(scopeKey1, scopeKey2, key4));
    final AtomicReference<StateChanger.Callback> callbackRef = new AtomicReference<>();
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            callbackRef.set(completionCallback);
        }
    });
    callbackRef.get().stateChangeComplete();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("world", "parent", "hello");
    backstack.setHistory(History.of(scopeKey1, scopeKey3, key5), StateChange.REPLACE);
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("world", "parent", "hello");
    assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.ALL)).containsExactly("kappa", "world", "parent", "hello");
    callbackRef.get().stateChangeComplete();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).isEmpty();
    assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(key5, ScopeLookupMode.ALL)).containsExactly("kappa", "hello");
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Parcel(android.os.Parcel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with TestKey

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

the class BackstackCoreTest method goUpChainWithFallbackOnExactMatchWorksAsDefaultBack.

@Test
public void goUpChainWithFallbackOnExactMatchWorksAsDefaultBack() {
    TestKey initial = new TestKey("hello");
    TestKey other = new TestKey("other");
    TestKey another = new TestKey("another");
    TestKey boop = new TestKey("boop");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial, other, another, boop));
    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(initial, other), true);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, other, another);
    backstack.goUpChain(History.of(initial, other), true);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, other);
    backstack.goUpChain(History.of(initial, other), true);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial, other);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 3 with TestKey

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

the class BackstackCoreTest method topWorks.

@Test
public void topWorks() {
    TestKey initial1 = new TestKey("hello1");
    TestKey initial2 = new TestKey("hello2");
    TestKey initial3 = new TestKey("hello3");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial1, initial2, initial3));
    try {
        backstack.top();
        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().top()).isSameAs(stateChange.topNewKey());
            } else {
                assertThat(stateChange.getBackstack().top()).isSameAs(stateChange.topPreviousKey());
            }
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.top()).isSameAs(initial3);
    backstack.setHistory(History.of(initial1, initial2), StateChange.REPLACE);
    assertThat(backstack.top()).isSameAs(initial2);
    backstack.removeStateChanger();
    backstack.setStateChanger(stateChanger);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 4 with TestKey

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

the class BackstackCoreTest method completionListenerShouldBeCalled.

@Test
public void completionListenerShouldBeCalled() {
    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.setStateChanger(stateChanger);
    callback.stateChangeComplete();
    assertThat(backstack.isStateChangePending()).isFalse();
    Mockito.verify(completionListener, Mockito.only()).stateChangeCompleted(stateChange);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 5 with TestKey

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

the class BackstackCoreTest method goUpChainWithSingleElementWhenPreviousDoesNotExist.

@Test
public void goUpChainWithSingleElementWhenPreviousDoesNotExist() {
    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.goUpChain(History.single(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