Search in sources :

Example 41 with TestKey

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

the class ScopeLookupModeTest method findScopesForKeyOtherSetup.

@Test
public void findScopesForKeyOtherSetup() {
    abstract class TestKeyWithScope extends TestKey implements HasServices {

        TestKeyWithScope(String name) {
            super(name);
        }

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

        @Nonnull
        @Override
        public String getScopeTag() {
            return name;
        }
    }
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    class MyService {

        private final String id;

        MyService(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "MyService{" + "id=" + id + '}';
        }
    }
    final Object service0 = new MyService("service0");
    backstack.setGlobalServices(GlobalServices.builder().addService("service0", service0).build());
    TestKeyWithScope beep = new TestKeyWithScope("scope1") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    abstract class TestKeyWithExplicitParent extends TestKeyWithScope implements HasParentServices {

        TestKeyWithExplicitParent(String name) {
            super(name);
        }

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

        @Override
        public final void bindServices(ServiceBinder serviceBinder) {
            if (name.equals(serviceBinder.getScopeTag())) {
                bindOwnServices(serviceBinder);
            } else {
                bindParentServices(serviceBinder);
            }
        }

        abstract void bindParentServices(ServiceBinder serviceBinder);

        abstract void bindOwnServices(ServiceBinder serviceBinder);
    }
    TestKeyWithExplicitParent boop = new TestKeyWithExplicitParent("scope2") {

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent1", "parent2");
        }

        @Override
        void bindParentServices(ServiceBinder serviceBinder) {
        }

        @Override
        void bindOwnServices(ServiceBinder serviceBinder) {
        }
    };
    TestKeyWithExplicitParent braap = new TestKeyWithExplicitParent("scope3") {

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent1", "parent3");
        }

        @Override
        void bindParentServices(ServiceBinder serviceBinder) {
        }

        @Override
        void bindOwnServices(ServiceBinder serviceBinder) {
        }
    };
    /*                      GLOBAL
         *                                PARENT1
         *                        PARENT2        PARENT3
         *   BEEP               BOOP                 BRAAP
         */
    backstack.setup(History.of(beep, boop, braap));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.ALL)).containsExactly("scope1", GlobalServices.SCOPE_TAG);
    assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.EXPLICIT)).containsExactly("scope1", GlobalServices.SCOPE_TAG);
    assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.ALL)).containsExactly("scope2", "parent2", "parent1", "scope1", GlobalServices.SCOPE_TAG);
    assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.EXPLICIT)).containsExactly("scope2", "parent2", "parent1", GlobalServices.SCOPE_TAG);
    assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.ALL)).containsExactly("scope3", "parent3", "parent1", "scope2", "parent2", "scope1", GlobalServices.SCOPE_TAG);
    assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.EXPLICIT)).containsExactly("scope3", "parent3", "parent1", GlobalServices.SCOPE_TAG);
    backstack.finalizeScopes();
    assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.ALL)).isEmpty();
    assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.ALL)).isEmpty();
    assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.ALL)).isEmpty();
    assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.EXPLICIT)).isEmpty();
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) HasParentServices(com.zhuinden.simplestack.helpers.HasParentServices) Nonnull(javax.annotation.Nonnull) Parcel(android.os.Parcel) HasServices(com.zhuinden.simplestack.helpers.HasServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 42 with TestKey

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

the class ScopeLookupModeTest method lookupModesWork.

@Test
public void lookupModesWork() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Object parentService1 = new Object();
    final Object parentService2 = new Object();
    final Object service1 = new Object();
    final Object service2 = new Object();
    class Key1 extends TestKey implements HasServices, HasParentServices {

        Key1(String name) {
            super(name);
        }

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if ("parent1".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parentService1", parentService1);
            } else if (name.equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("service1", service1);
            }
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return name;
        }

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent1");
        }
    }
    class Key2 extends TestKey implements HasServices, HasParentServices {

        Key2(String name) {
            super(name);
        }

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if ("parent2".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parentService2", parentService2);
            } else if (name.equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("service2", service2);
            }
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return name;
        }

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent2");
        }
    }
    backstack.setup(History.of(new Key1("beep"), new Key2("boop")));
    assertThat(backstack.canFindFromScope("boop", "service")).isFalse();
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    // default (ALL)
    assertThat(backstack.canFindFromScope("beep", "service1")).isTrue();
    assertThat(backstack.canFindFromScope("beep", "service2")).isFalse();
    assertThat(backstack.canFindFromScope("beep", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service1")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service2")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "parentService2")).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service1")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "service2")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService2")).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service1")).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service2")).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "parentService2")).isTrue();
    // ALL specified
    assertThat(backstack.canFindFromScope("beep", "service1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "service2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service1", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "parentService2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "service2", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "parentService2", ScopeLookupMode.ALL)).isTrue();
    // EXPLICIT specified
    assertThat(backstack.canFindFromScope("beep", "service1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "service2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "parentService2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service2", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "service2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
    // default (ALL)
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "service2");
        }
    });
    assertThat(backstack.lookupFromScope("beep", "service1")).isSameAs(service1);
    assertThat(backstack.lookupFromScope("beep", "parentService1")).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "parentService2");
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service1");
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service2");
        }
    });
    assertThat(backstack.lookupFromScope("parent1", "parentService1")).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "parentService2");
        }
    });
    assertThat(backstack.lookupFromScope("boop", "service1")).isSameAs(service1);
    assertThat(backstack.lookupFromScope("boop", "service2")).isSameAs(service2);
    assertThat(backstack.lookupFromScope("boop", "parentService1")).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("boop", "parentService2")).isSameAs(parentService2);
    assertThat(backstack.lookupFromScope("parent2", "service1")).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "service2");
        }
    });
    assertThat(backstack.lookupFromScope("parent2", "parentService1")).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("parent2", "parentService2")).isSameAs(parentService2);
    // ALL specified
    assertThat(backstack.lookupFromScope("beep", "service1", ScopeLookupMode.ALL)).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "service2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "parentService2", ScopeLookupMode.ALL);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service1", ScopeLookupMode.ALL);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("parent1", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "parentService2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("boop", "service1", ScopeLookupMode.ALL)).isSameAs(service1);
    assertThat(backstack.lookupFromScope("boop", "service2", ScopeLookupMode.ALL)).isSameAs(service2);
    assertThat(backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isSameAs(parentService2);
    assertThat(backstack.lookupFromScope("parent2", "service1", ScopeLookupMode.ALL)).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "service2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("parent2", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("parent2", "parentService2", ScopeLookupMode.ALL)).isSameAs(parentService2);
    // EXPLICIT specified
    assertThat(backstack.lookupFromScope("beep", "service1", ScopeLookupMode.EXPLICIT)).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "service2", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service2", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("parent1", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "parentService2", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("boop", "service1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("boop", "service2", ScopeLookupMode.EXPLICIT)).isSameAs(service2);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(parentService2);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "service1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "service2", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "parentService1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("parent2", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(parentService2);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Action(com.zhuinden.simplestack.helpers.Action) HasParentServices(com.zhuinden.simplestack.helpers.HasParentServices) Parcel(android.os.Parcel) HasServices(com.zhuinden.simplestack.helpers.HasServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 43 with TestKey

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

the class BackstackCoreTest method goUpChainWithMultipleElementWhenMorePreviousExists.

@Test
public void goUpChainWithMultipleElementWhenMorePreviousExists() {
    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, initial4));
    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 44 with TestKey

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

the class BackstackCoreTest method goUpChainWithMultipleElementWhenPreviousSomeExistsWithBefore.

@Test
public void goUpChainWithMultipleElementWhenPreviousSomeExistsWithBefore() {
    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, initial4));
    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(initial2, initial3));
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial1, initial2, initial3);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) Test(org.junit.Test)

Example 45 with TestKey

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

the class BackstackCoreTest method goUpWithMoreElementsFoundParentNoFallbackRemovesTopKeys.

@Test
public void goUpWithMoreElementsFoundParentNoFallbackRemovesTopKeys() {
    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.goUp(initial, false);
    callback.stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(initial);
}
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