Search in sources :

Example 6 with HasServices

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

the class ScopingGlobalScopeTest method globalScopeLookupWorks.

@Test
public void globalScopeLookupWorks() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Object globalService = new Object();
    backstack.setGlobalServices(GlobalServices.builder().addService("parentService2", globalService).build());
    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")));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(parentService2);
    assertThat(backstack.lookupFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(globalService);
    assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isSameAs(parentService2);
    assertThat(backstack.lookupFromScope("beep", "parentService2", ScopeLookupMode.ALL)).isSameAs(globalService);
    assertThat(backstack.lookupService("parentService2")).isSameAs(parentService2);
    backstack.goBack();
    assertThat(backstack.lookupService("parentService2")).isSameAs(globalService);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) 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 7 with HasServices

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

the class ScopingTest method lookupServiceFromScopeWorks.

@Test
public void lookupServiceFromScopeWorks() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Object service1 = new Object();
    final Object service2 = new Object();
    final Object service0 = new Object();
    final Object service3 = new Object();
    class Key1 extends TestKey implements HasServices {

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

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.hasService("service0")).isFalse();
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service0")).isFalse();
            serviceBinder.addService("service0", service0);
            assertThat(serviceBinder.hasService("service0")).isTrue();
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service0")).isTrue();
            assertThat(serviceBinder.getService("service0")).isSameAs(service0);
            assertThat(serviceBinder.lookupService("service0")).isSameAs(service0);
            assertThat(serviceBinder.lookupFromScope(serviceBinder.getScopeTag(), "service0")).isSameAs(service0);
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isFalse();
            serviceBinder.addService("service", service1);
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isTrue();
            assertThat(serviceBinder.lookupFromScope(serviceBinder.getScopeTag(), "service")).isSameAs(service1);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    }
    class Key2 extends TestKey implements HasServices {

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

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.lookupService("service0")).isSameAs(service0);
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isTrue();
            assertThat(serviceBinder.lookupService("service")).isSameAs(service1);
            serviceBinder.addService("service", service2);
            // the mostimportant assertion here
            assertThat(serviceBinder.lookupService("service")).isSameAs(service2);
            assertThat(serviceBinder.lookupFromScope("beep", "service")).isSameAs(service1);
            serviceBinder.addService("service3", service3);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "boop";
        }
    }
    backstack.setup(History.of(new Key1("beep"), new Key2("boop")));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.lookupService("service")).isSameAs(service2);
    assertThat(backstack.canFindService("service3")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "service3")).isTrue();
    assertThat(backstack.lookupFromScope("boop", "service3")).isSameAs(service3);
    assertThat(backstack.lookupFromScope("beep", "service")).isSameAs(service1);
    assertThat(backstack.lookupFromScope("boop", "service")).isSameAs(service2);
    backstack.goBack();
    assertThat(backstack.canFindFromScope("boop", "service3")).isFalse();
    assertThat(backstack.lookupService("service")).isSameAs(service1);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Parcel(android.os.Parcel) HasServices(com.zhuinden.simplestack.helpers.HasServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Aggregations

Parcel (android.os.Parcel)7 HasServices (com.zhuinden.simplestack.helpers.HasServices)7 ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)7 TestKey (com.zhuinden.simplestack.helpers.TestKey)7 Test (org.junit.Test)7 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)6 Action (com.zhuinden.simplestack.helpers.Action)1 Nonnull (javax.annotation.Nonnull)1