Search in sources :

Example 51 with ServiceProvider

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

the class ScopingRegisteredCallbackTest method registeredWorks.

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

        private final String id;

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

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

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

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

        @Override
        public void onServiceUnregistered() {
            events.add(Pair.of((Object) this, new UnregisterEvent()));
        }

        @Override
        public String toString() {
            return "MyService{" + "id=" + id + '}';
        }
    }
    final Object service0 = new MyService("service0");
    final Object serviceShared0123P1P2P3 = new MyService("serviceShared0123P1P2P3");
    backstack.setGlobalServices(GlobalServices.builder().addService("service0", service0).addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3).build());
    final Object service1 = new MyService("service1");
    final Object service2 = new MyService("service2");
    final Object service3 = new MyService("service3");
    final Object serviceShared12 = new MyService("serviceShared12");
    final Object serviceShared13 = new MyService("serviceShared13");
    final Object serviceShared23 = new MyService("serviceShared23");
    final Object serviceShared123 = new MyService("serviceShared123");
    final Object serviceShared1P1 = new MyService("serviceShared1P1");
    final Object serviceShared1P2 = new MyService("serviceShared1P2");
    final Object serviceShared1P3 = new MyService("serviceShared1P3");
    final Object serviceShared2P1 = new MyService("serviceShared2P1");
    final Object serviceShared2P2 = new MyService("serviceShared2P2");
    final Object serviceShared2P3 = new MyService("serviceShared2P3");
    final Object serviceShared3P1 = new MyService("serviceShared3P1");
    final Object serviceShared3P2 = new MyService("serviceShared3P2");
    final Object serviceShared3P3 = new MyService("serviceShared3P3");
    final Object serviceP1 = new MyService("serviceP1");
    final Object serviceP2 = new MyService("serviceP2");
    final Object serviceP3 = new MyService("serviceP3");
    TestKeyWithScope beep = new TestKeyWithScope("scope1") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("service1", service1);
            serviceBinder.addService("serviceShared12", serviceShared12);
            serviceBinder.addService("serviceShared13", serviceShared13);
            serviceBinder.addService("serviceShared123", serviceShared123);
            serviceBinder.addService("serviceShared1P1", serviceShared1P1);
            serviceBinder.addService("serviceShared1P2", serviceShared1P2);
            serviceBinder.addService("serviceShared1P3", serviceShared1P3);
            serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
        }
    };
    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) {
            if ("parent1".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP1", serviceP1);
                serviceBinder.addService("serviceShared1P1", serviceShared1P1);
                serviceBinder.addService("serviceShared2P1", serviceShared2P1);
                serviceBinder.addService("serviceShared3P1", serviceShared3P1);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
            if ("parent2".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP2", serviceP2);
                serviceBinder.addService("serviceShared1P2", serviceShared1P2);
                serviceBinder.addService("serviceShared2P2", serviceShared2P2);
                serviceBinder.addService("serviceShared3P2", serviceShared3P2);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
        }

        @Override
        void bindOwnServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service2", service2);
            serviceBinder.addService("serviceShared12", serviceShared12);
            serviceBinder.addService("serviceShared23", serviceShared23);
            serviceBinder.addService("serviceShared123", serviceShared123);
            serviceBinder.addService("serviceShared2P1", serviceShared2P1);
            serviceBinder.addService("serviceShared2P2", serviceShared2P2);
            serviceBinder.addService("serviceShared2P3", serviceShared2P3);
            serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
        }
    };
    TestKeyWithExplicitParent braap = new TestKeyWithExplicitParent("scope3") {

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

        @Override
        void bindParentServices(ServiceBinder serviceBinder) {
            if ("parent1".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP1", serviceP1);
                serviceBinder.addService("serviceShared1P1", serviceShared1P1);
                serviceBinder.addService("serviceShared2P1", serviceShared2P1);
                serviceBinder.addService("serviceShared3P1", serviceShared3P1);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
            if ("parent3".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP3", serviceP3);
                serviceBinder.addService("serviceShared1P3", serviceShared1P3);
                serviceBinder.addService("serviceShared2P3", serviceShared2P3);
                serviceBinder.addService("serviceShared3P3", serviceShared3P3);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
        }

        @Override
        void bindOwnServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service3", service3);
            serviceBinder.addService("serviceShared13", serviceShared13);
            serviceBinder.addService("serviceShared23", serviceShared23);
            serviceBinder.addService("serviceShared123", serviceShared123);
            serviceBinder.addService("serviceShared3P1", serviceShared3P1);
            serviceBinder.addService("serviceShared3P2", serviceShared3P2);
            serviceBinder.addService("serviceShared3P3", serviceShared3P3);
            serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
        }
    };
    /*                      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);
    backstack.goBack();
    backstack.goBack();
    backstack.finalizeScopes();
    backstack.goTo(beep);
    assertThat(events).containsExactly(Pair.of(service0, new RegisterEvent()), Pair.of(serviceShared0123P1P2P3, new RegisterEvent()), Pair.of(service1, new RegisterEvent()), Pair.of(serviceShared12, new RegisterEvent()), Pair.of(serviceShared13, new RegisterEvent()), Pair.of(serviceShared123, new RegisterEvent()), Pair.of(serviceShared1P1, new RegisterEvent()), Pair.of(serviceShared1P2, new RegisterEvent()), Pair.of(serviceShared1P3, new RegisterEvent()), Pair.of(serviceP1, new RegisterEvent()), Pair.of(serviceShared2P1, new RegisterEvent()), Pair.of(serviceShared3P1, new RegisterEvent()), Pair.of(serviceP2, new RegisterEvent()), Pair.of(serviceShared2P2, new RegisterEvent()), Pair.of(serviceShared3P2, new RegisterEvent()), Pair.of(service2, new RegisterEvent()), Pair.of(serviceShared23, new RegisterEvent()), Pair.of(serviceShared2P3, new RegisterEvent()), Pair.of(serviceP3, new RegisterEvent()), Pair.of(serviceShared3P3, new RegisterEvent()), Pair.of(service3, new RegisterEvent()), Pair.of(service0, new ActiveEvent()), Pair.of(serviceShared0123P1P2P3, new ActiveEvent()), Pair.of(serviceP1, new ActiveEvent()), Pair.of(serviceShared1P1, new ActiveEvent()), Pair.of(serviceShared2P1, new ActiveEvent()), Pair.of(serviceShared3P1, new ActiveEvent()), Pair.of(serviceP3, new ActiveEvent()), Pair.of(serviceShared1P3, new ActiveEvent()), Pair.of(serviceShared2P3, new ActiveEvent()), Pair.of(serviceShared3P3, new ActiveEvent()), Pair.of(service3, new ActiveEvent()), Pair.of(serviceShared13, new ActiveEvent()), Pair.of(serviceShared23, new ActiveEvent()), Pair.of(serviceShared123, new ActiveEvent()), Pair.of(serviceShared3P2, new ActiveEvent()), Pair.of(serviceP2, new ActiveEvent()), Pair.of(serviceShared1P2, new ActiveEvent()), Pair.of(serviceShared2P2, new ActiveEvent()), Pair.of(service2, new ActiveEvent()), Pair.of(serviceShared12, new ActiveEvent()), Pair.of(serviceShared13, new InactiveEvent()), Pair.of(service3, new InactiveEvent()), Pair.of(serviceShared3P3, new InactiveEvent()), Pair.of(serviceShared1P3, new InactiveEvent()), Pair.of(serviceP3, new InactiveEvent()), Pair.of(service3, new UnregisterEvent()), Pair.of(serviceShared3P3, new UnregisterEvent()), Pair.of(serviceP3, new UnregisterEvent()), Pair.of(service1, new ActiveEvent()), Pair.of(serviceShared13, new ActiveEvent()), Pair.of(serviceShared1P3, new ActiveEvent()), Pair.of(serviceShared2P3, new InactiveEvent()), Pair.of(serviceShared23, new InactiveEvent()), Pair.of(service2, new InactiveEvent()), Pair.of(serviceShared3P2, new InactiveEvent()), Pair.of(serviceShared2P2, new InactiveEvent()), Pair.of(serviceP2, new InactiveEvent()), Pair.of(serviceShared3P1, new InactiveEvent()), Pair.of(serviceShared2P1, new InactiveEvent()), Pair.of(serviceP1, new InactiveEvent()), Pair.of(serviceShared2P3, new UnregisterEvent()), Pair.of(serviceShared23, new UnregisterEvent()), Pair.of(service2, new UnregisterEvent()), Pair.of(serviceShared3P2, new UnregisterEvent()), Pair.of(serviceShared2P2, new UnregisterEvent()), Pair.of(serviceP2, new UnregisterEvent()), Pair.of(serviceShared3P1, new UnregisterEvent()), Pair.of(serviceShared2P1, new UnregisterEvent()), Pair.of(serviceP1, new UnregisterEvent()), Pair.of(serviceShared1P3, new InactiveEvent()), Pair.of(serviceShared1P2, new InactiveEvent()), Pair.of(serviceShared1P1, new InactiveEvent()), Pair.of(serviceShared123, new InactiveEvent()), Pair.of(serviceShared13, new InactiveEvent()), Pair.of(serviceShared12, new InactiveEvent()), Pair.of(service1, new InactiveEvent()), Pair.of(serviceShared0123P1P2P3, new InactiveEvent()), Pair.of(service0, new InactiveEvent()), Pair.of(serviceShared1P3, new UnregisterEvent()), Pair.of(serviceShared1P2, new UnregisterEvent()), Pair.of(serviceShared1P1, new UnregisterEvent()), Pair.of(serviceShared123, new UnregisterEvent()), Pair.of(serviceShared13, new UnregisterEvent()), Pair.of(serviceShared12, new UnregisterEvent()), Pair.of(service1, new UnregisterEvent()), Pair.of(serviceShared0123P1P2P3, new UnregisterEvent()), Pair.of(service0, new UnregisterEvent()), // restoration to 'beep'
    Pair.of(service0, new RegisterEvent()), Pair.of(serviceShared0123P1P2P3, new RegisterEvent()), Pair.of(service1, new RegisterEvent()), Pair.of(serviceShared12, new RegisterEvent()), Pair.of(serviceShared13, new RegisterEvent()), Pair.of(serviceShared123, new RegisterEvent()), Pair.of(serviceShared1P1, new RegisterEvent()), Pair.of(serviceShared1P2, new RegisterEvent()), Pair.of(serviceShared1P3, new RegisterEvent()), Pair.of(service0, new ActiveEvent()), Pair.of(serviceShared0123P1P2P3, new ActiveEvent()), Pair.of(service1, new ActiveEvent()), Pair.of(serviceShared12, new ActiveEvent()), Pair.of(serviceShared13, new ActiveEvent()), Pair.of(serviceShared123, new ActiveEvent()), Pair.of(serviceShared1P1, new ActiveEvent()), Pair.of(serviceShared1P2, new ActiveEvent()), Pair.of(serviceShared1P3, new ActiveEvent()));
}
Also used : HasParentServices(com.zhuinden.simplestack.helpers.HasParentServices) Nonnull(javax.annotation.Nonnull) Parcel(android.os.Parcel) ArrayList(java.util.ArrayList) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 52 with ServiceProvider

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

the class ScopingTest method serviceBinderThrowsIfRootBackstackIsAService.

@Test
public void serviceBinderThrowsIfRootBackstackIsAService() {
    final String serviceTag = "backstack";
    final Backstack backstack = new Backstack();
    TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService(serviceTag, backstack);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(testKeyWithScope));
    try {
        backstack.setStateChanger(stateChanger);
        Assert.fail("This would cause a save-state loop in toBundle()");
    } catch (IllegalArgumentException e) {
    // OK!
    }
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 53 with ServiceProvider

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

the class ScopingTest method activatedIsCalledInRightOrder.

@Test
public void activatedIsCalledInRightOrder() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final List<Object> activatedServices = new ArrayList<>();
    final List<Object> deactivatedServices = new ArrayList<>();
    class MyService implements ScopedServices.Activated {

        @Override
        public void onServiceActive() {
            activatedServices.add(this);
        }

        @Override
        public void onServiceInactive() {
            deactivatedServices.add(this);
        }
    }
    final MyService service1 = new MyService();
    final MyService service2 = new MyService();
    final MyService service3 = new MyService();
    final MyService service4 = new MyService();
    final MyService service5 = new MyService();
    final MyService service6 = new MyService();
    TestKeyWithScope beep = new TestKeyWithScope("beep") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("SERVICE1", service1);
            serviceBinder.addService("SERVICE2", service2);
            serviceBinder.addService("SERVICE3", service3);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    TestKeyWithScope boop = new TestKeyWithScope("beep") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("SERVICE4", service4);
            serviceBinder.addService("SERVICE5", service5);
            serviceBinder.addService("SERVICE6", service6);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "boop";
        }
    };
    backstack.setup(History.of(beep));
    assertThat(activatedServices).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(activatedServices).containsExactly(service1, service2, service3);
    backstack.goTo(boop);
    assertThat(activatedServices).containsExactly(service1, service2, service3, service4, service5, service6);
    assertThat(deactivatedServices).containsExactly(service3, service2, service1);
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 54 with ServiceProvider

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

the class ScopingTest method persistedStateOfScopedServicesIsRestored.

@Test
public void persistedStateOfScopedServicesIsRestored() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(testKey2));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.hasService(testKey2.getScopeTag(), SERVICE_TAG)).isTrue();
    StateBundle stateBundle = backstack.toBundle();
    Backstack backstack2 = new Backstack();
    backstack2.setScopedServices(new ServiceProvider());
    StateChanger stateChanger2 = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack2.setup(History.of(testKey2));
    backstack2.fromBundle(stateBundle);
    backstack2.setStateChanger(stateChanger2);
    assertThat(backstack2.<Service>getService(testKey2.getScopeTag(), SERVICE_TAG).blah).isEqualTo(5);
}
Also used : Nonnull(javax.annotation.Nonnull) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 55 with ServiceProvider

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

the class ScopingTest method deactivationIsCalledOnlyOnce.

@Test
public void deactivationIsCalledOnlyOnce() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final List<Object> activatedServices = new ArrayList<>();
    final List<Object> deactivatedServices = new ArrayList<>();
    class MyService implements ScopedServices.Activated {

        @Override
        public void onServiceActive() {
            activatedServices.add(this);
        }

        @Override
        public void onServiceInactive() {
            deactivatedServices.add(this);
        }
    }
    final MyService service1 = new MyService();
    TestKeyWithScope beep = new TestKeyWithScope("beep") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("SERVICE1", service1);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    TestKey bye = new TestKey("bye");
    backstack.setup(History.of(beep));
    assertThat(activatedServices).isEmpty();
    assertThat(deactivatedServices).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(activatedServices).containsExactly(service1);
    assertThat(deactivatedServices).isEmpty();
    backstack.removeStateChanger();
    assertThat(deactivatedServices).isEmpty();
    backstack.setHistory(History.of(bye), StateChange.REPLACE);
    assertThat(deactivatedServices).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(deactivatedServices).containsExactly(service1);
    backstack.removeStateChanger();
    backstack.setStateChanger(stateChanger);
    assertThat(deactivatedServices).containsExactly(service1);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) ArrayList(java.util.ArrayList) 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