Search in sources :

Example 46 with ServiceProvider

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

the class ScopingBackEventDispatchTest method onBackDispatchExecutesEvenIfNoStateChanger.

@Test
public void onBackDispatchExecutesEvenIfNoStateChanger() {
    final HandlesBackOnce service = new HandlesBackOnce();
    Object key = new TestKeyWithScope("key") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service", service);
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(key));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.getHistory()).containsExactly(key);
    backstack.detachStateChanger();
    boolean handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(service.handledBackOnce).isTrue();
    // returns `false` even if no state changer
    handled = backstack.goBack();
    assertThat(handled).isFalse();
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) Nonnull(javax.annotation.Nonnull) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 47 with ServiceProvider

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

the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackHandlesInActiveParentScopeEvenIfTopIsNotScopeKeyAndExplicitParentIsSharedFromPreviousKey.

@Test
public void onBackDispatchHandlesBackHandlesInActiveParentScopeEvenIfTopIsNotScopeKeyAndExplicitParentIsSharedFromPreviousKey() {
    final HandlesBackOnce parentService = new HandlesBackOnce();
    Object key1 = new TestKeyWithOnlyParentServices("key1", History.of(HandlesBackOnce.class.getName())) {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if (HandlesBackOnce.class.getName().equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService(HandlesBackOnce.class.getName(), parentService);
            }
        }
    };
    Object key2 = new TestKeyWithOnlyParentServices("key2", History.of(HandlesBackOnce.class.getName())) {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if (HandlesBackOnce.class.getName().equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService(HandlesBackOnce.class.getName(), parentService);
            }
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(key1, key2));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.lookupService(HandlesBackOnce.class.getName())).isSameAs(parentService);
    assertThat(backstack.getHistory()).containsExactly(key1, key2);
    assertThat(parentService.handledBackOnce).isFalse();
    boolean handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(parentService.handledBackOnce).isTrue();
    assertThat(backstack.getHistory()).containsExactly(key1, key2);
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(backstack.getHistory()).containsExactly(key1);
    handled = backstack.goBack();
    assertThat(handled).isFalse();
}
Also used : TestKeyWithOnlyParentServices(com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 48 with ServiceProvider

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

the class ScopingBackEventDispatchTest method onBackDispatchDoesNotDispatchDuringStateChange.

@Test
public void onBackDispatchDoesNotDispatchDuringStateChange() {
    final HandlesBackOnce service1 = new HandlesBackOnce();
    final HandlesBackOnce service2 = new HandlesBackOnce();
    Object key1 = new TestKeyWithScope("key1") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service1", service1);
        }
    };
    Object key2 = new TestKeyWithScope("key2") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service2", service2);
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(key1));
    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.getHistory()).containsExactly(key1);
    backstack.goTo(key2);
    assertThat(service1.handledBackOnce).isEqualTo(false);
    assertThat(service2.handledBackOnce).isEqualTo(false);
    // ignored!
    boolean handled = backstack.goBack();
    assertThat(handled).isEqualTo(true);
    assertThat(service1.handledBackOnce).isEqualTo(false);
    assertThat(service2.handledBackOnce).isEqualTo(false);
    // goTo(key2)
    callbackRef.get().stateChangeComplete();
    assertThat(backstack.getHistory()).containsExactly(key1, key2);
    handled = backstack.goBack();
    assertThat(handled).isEqualTo(true);
    assertThat(service1.handledBackOnce).isEqualTo(false);
    assertThat(service2.handledBackOnce).isEqualTo(true);
    handled = backstack.goBack();
    callbackRef.get().stateChangeComplete();
    assertThat(handled).isEqualTo(true);
    assertThat(service1.handledBackOnce).isEqualTo(false);
    assertThat(service2.handledBackOnce).isEqualTo(true);
    assertThat(backstack.getHistory()).containsExactly(key1);
    handled = backstack.goBack();
    assertThat(handled).isEqualTo(true);
    assertThat(service1.handledBackOnce).isEqualTo(true);
    assertThat(service2.handledBackOnce).isEqualTo(true);
    assertThat(backstack.getHistory()).containsExactly(key1);
    handled = backstack.goBack();
    assertThat(handled).isEqualTo(false);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 49 with ServiceProvider

use of com.zhuinden.simplestack.helpers.ServiceProvider 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 50 with ServiceProvider

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

the class ScopingGlobalScopeTest method newHistoryShouldReinitializeScopes.

@Test
public void newHistoryShouldReinitializeScopes() {
    final List<Pair<Object, ServiceEvent>> events = new ArrayList<>();
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    class MyService implements ScopedServices.Activated, ScopedServices.Registered {

        private int id = 0;

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

        @Override
        public void onServiceActive() {
            events.add(Pair.of((Object) this, ServiceEvent.ACTIVE));
        }

        @Override
        public void onServiceInactive() {
            events.add(Pair.of((Object) this, ServiceEvent.INACTIVE));
        }

        @Override
        public String toString() {
            return "MyService{" + "id=" + id + '}';
        }

        @Override
        public void onServiceRegistered() {
            events.add(Pair.of((Object) this, ServiceEvent.CREATE));
        }

        @Override
        public void onServiceUnregistered() {
            events.add(Pair.of((Object) this, ServiceEvent.DESTROY));
        }
    }
    final MyService globalService = new MyService(0);
    final MyService explicitParentService = new MyService(1);
    final MyService implicitParentService = new MyService(2);
    final MyService currentScopeService = new MyService(3);
    abstract class TestKeyWithExplicitParent extends TestKeyWithScope implements HasParentServices {

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

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

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

        @Override
        public final void bindServices(ServiceBinder serviceBinder) {
            if ("explicitParentScope".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("explicitParentService", explicitParentService);
            }
            if (name.equals(serviceBinder.getScopeTag())) {
                bindOwnServices(serviceBinder);
            }
        }

        abstract void bindOwnServices(ServiceBinder serviceBinder);
    }
    TestKeyWithScope beep = new TestKeyWithExplicitParent("beep") {

        @Override
        void bindOwnServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("implicitParentService", implicitParentService);
        }
    };
    TestKeyWithScope boop = new TestKeyWithExplicitParent("boop") {

        @Override
        void bindOwnServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("currentScopeService", currentScopeService);
        }
    };
    backstack.setGlobalServices(GlobalServices.builder().addService("globalService", globalService).build());
    backstack.setup(History.of(beep, boop));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.canFindService("globalService")).isTrue();
    assertThat(backstack.canFindService("currentScopeService")).isTrue();
    assertThat(backstack.canFindService("implicitParentService")).isTrue();
    assertThat(backstack.canFindService("explicitParentService")).isTrue();
    backstack.finalizeScopes();
    assertThat(backstack.canFindService("currentScopeService")).isFalse();
    assertThat(backstack.canFindService("implicitParentService")).isFalse();
    assertThat(backstack.canFindService("explicitParentService")).isFalse();
    assertThat(backstack.canFindService("globalService")).isFalse();
    backstack.setHistory(History.of(beep, boop), StateChange.REPLACE);
    assertThat(backstack.canFindService("globalService")).isTrue();
    assertThat(backstack.canFindService("currentScopeService")).isTrue();
    assertThat(backstack.canFindService("implicitParentService")).isTrue();
    assertThat(backstack.canFindService("explicitParentService")).isTrue();
    assertThat(backstack.hasService("explicitParentScope", "explicitParentService")).isTrue();
    assertThat(events).containsExactly(Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.INACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.INACTIVE), Pair.of((Object) globalService, ServiceEvent.INACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.DESTROY), Pair.of((Object) implicitParentService, ServiceEvent.DESTROY), Pair.of((Object) explicitParentService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE));
    // this is just to check things, the test'simportant part is the one above
    backstack.goBack();
    assertThat(events).containsExactly(Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.INACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.INACTIVE), Pair.of((Object) globalService, ServiceEvent.INACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.DESTROY), Pair.of((Object) implicitParentService, ServiceEvent.DESTROY), Pair.of((Object) explicitParentService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE), Pair.of((Object) implicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.INACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.DESTROY));
}
Also used : HasParentServices(com.zhuinden.simplestack.helpers.HasParentServices) Parcel(android.os.Parcel) ArrayList(java.util.ArrayList) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Aggregations

ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)72 Test (org.junit.Test)72 TestKey (com.zhuinden.simplestack.helpers.TestKey)26 TestKeyWithScope (com.zhuinden.simplestack.helpers.TestKeyWithScope)25 ArrayList (java.util.ArrayList)13 Parcel (android.os.Parcel)9 Nonnull (javax.annotation.Nonnull)9 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)8 HasServices (com.zhuinden.simplestack.helpers.HasServices)7 TestKeyWithOnlyParentServices (com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices)6 TestKeyWithExplicitParent (com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent)4 StateBundle (com.zhuinden.statebundle.StateBundle)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Activity (android.app.Activity)2 Action (com.zhuinden.simplestack.helpers.Action)2 Nullable (javax.annotation.Nullable)1