Search in sources :

Example 66 with ServiceProvider

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

the class ScopingTest method registeredAndActivatedAreCalledInRightOrder.

@Test
public void registeredAndActivatedAreCalledInRightOrder() {
    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 void onServiceRegistered() {
            events.add(Pair.of((Object) this, ServiceEvent.CREATE));
        }

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

        @Override
        public String toString() {
            return "MyService{" + "id=" + id + '}';
        }
    }
    final Object service1 = new MyService(1);
    final Object service2 = new MyService(2);
    final Object service3 = new MyService(3);
    final Object service4 = new MyService(4);
    final Object service5 = new MyService(5);
    final Object service6 = new MyService(6);
    final Object service7 = new MyService(7);
    final Object service8 = new MyService(8);
    final Object service9 = new MyService(9);
    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("boop") {

        @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";
        }
    };
    TestKeyWithScope braap = new TestKeyWithScope("braap") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("SERVICE7", service7);
            serviceBinder.addService("SERVICE8", service8);
            serviceBinder.addService("SERVICE9", service9);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "braap";
        }
    };
    backstack.setup(History.of(beep, boop));
    backstack.setStateChanger(stateChanger);
    backstack.goTo(braap);
    // just to make sure
    backstack.removeStateChanger();
    // just to make sure
    backstack.setStateChanger(stateChanger);
    backstack.goBack();
    TestKey bye = new TestKey("bye");
    backstack.setHistory(History.of(bye), StateChange.REPLACE);
    assertThat(events).containsExactly(Pair.of(service1, ServiceEvent.CREATE), Pair.of(service2, ServiceEvent.CREATE), Pair.of(service3, ServiceEvent.CREATE), Pair.of(service4, ServiceEvent.CREATE), Pair.of(service5, ServiceEvent.CREATE), Pair.of(service6, ServiceEvent.CREATE), Pair.of(service4, ServiceEvent.ACTIVE), Pair.of(service5, ServiceEvent.ACTIVE), Pair.of(service6, ServiceEvent.ACTIVE), Pair.of(service7, ServiceEvent.CREATE), Pair.of(service8, ServiceEvent.CREATE), Pair.of(service9, ServiceEvent.CREATE), Pair.of(service7, ServiceEvent.ACTIVE), Pair.of(service8, ServiceEvent.ACTIVE), Pair.of(service9, ServiceEvent.ACTIVE), Pair.of(service6, ServiceEvent.INACTIVE), Pair.of(service5, ServiceEvent.INACTIVE), Pair.of(service4, ServiceEvent.INACTIVE), Pair.of(service4, ServiceEvent.ACTIVE), Pair.of(service5, ServiceEvent.ACTIVE), Pair.of(service6, ServiceEvent.ACTIVE), Pair.of(service9, ServiceEvent.INACTIVE), Pair.of(service8, ServiceEvent.INACTIVE), Pair.of(service7, ServiceEvent.INACTIVE), Pair.of(service9, ServiceEvent.DESTROY), Pair.of(service8, ServiceEvent.DESTROY), Pair.of(service7, ServiceEvent.DESTROY), Pair.of(service6, ServiceEvent.INACTIVE), Pair.of(service5, ServiceEvent.INACTIVE), Pair.of(service4, ServiceEvent.INACTIVE), Pair.of(service6, ServiceEvent.DESTROY), Pair.of(service5, ServiceEvent.DESTROY), Pair.of(service4, ServiceEvent.DESTROY), Pair.of(service3, ServiceEvent.DESTROY), Pair.of(service2, ServiceEvent.DESTROY), Pair.of(service1, ServiceEvent.DESTROY));
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 67 with ServiceProvider

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

the class ScopingTest method serviceCreationAndDestructionHappensInForwardAndReverseOrder.

@Test
public void serviceCreationAndDestructionHappensInForwardAndReverseOrder() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final List<Object> serviceRegistered = new ArrayList<>();
    final List<Object> serviceUnregistered = new ArrayList<>();
    class MyService implements ScopedServices.Registered {

        @Override
        public void onServiceRegistered() {
            serviceRegistered.add(this);
        }

        @Override
        public void onServiceUnregistered() {
            serviceUnregistered.add(this);
        }
    }
    final MyService service1 = new MyService();
    final MyService service2 = 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);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    TestKey bye = new TestKey("bye");
    backstack.setup(History.of(beep));
    assertThat(serviceRegistered).isEmpty();
    assertThat(serviceUnregistered).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(serviceRegistered).containsExactly(service1, service2);
    assertThat(serviceUnregistered).isEmpty();
    backstack.setHistory(History.of(bye), StateChange.REPLACE);
    assertThat(serviceRegistered).containsExactly(service1, service2);
    assertThat(serviceUnregistered).containsExactly(service2, service1);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 68 with ServiceProvider

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

the class ScopingTest method activeScopeIsDeactivatedWhenScopesAreFinalized.

@Test
public void activeScopeIsDeactivatedWhenScopesAreFinalized() {
    Activity activity = Mockito.mock(Activity.class);
    Mockito.when(activity.isFinishing()).thenReturn(true);
    class MyService implements ScopedServices.Activated {

        boolean didServiceActivate = false;

        boolean didScopeDeactivate = false;

        @Override
        public void onServiceActive() {
            didServiceActivate = true;
        }

        @Override
        public void onServiceInactive() {
            didScopeDeactivate = true;
        }
    }
    final MyService service = new MyService();
    TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {

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

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

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(testKeyWithScope));
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.canFindService(SERVICE_TAG)).isTrue();
    backstack.reattachStateChanger();
    backstack.detachStateChanger();
    assertThat(service.didServiceActivate).isTrue();
    assertThat(service.didScopeDeactivate).isFalse();
    assertThat(backstack.hasScope("beep")).isTrue();
    assertThat(backstack.hasService(testKeyWithScope, SERVICE_TAG)).isTrue();
    backstack.finalizeScopes();
    assertThat(backstack.hasScope("beep")).isFalse();
    assertThat(backstack.hasService(testKeyWithScope, SERVICE_TAG)).isFalse();
    assertThat(service.didServiceActivate).isTrue();
    assertThat(service.didScopeDeactivate).isTrue();
}
Also used : Nonnull(javax.annotation.Nonnull) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Activity(android.app.Activity) Test(org.junit.Test)

Example 69 with ServiceProvider

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

the class ScopingTest method sameServiceRegisteredInScopeMultipleTimesReceivesCallbackOnlyOnce.

@Test
public void sameServiceRegisteredInScopeMultipleTimesReceivesCallbackOnlyOnce() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final List<Object> activated = new ArrayList<>();
    final List<Object> inactivated = new ArrayList<>();
    final List<Object> registered = new ArrayList<>();
    final List<Object> unregistered = new ArrayList<>();
    class MyService implements ScopedServices.Activated, ScopedServices.Registered {

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

        @Override
        public void onServiceInactive() {
            inactivated.add(this);
        }

        @Override
        public void onServiceRegistered() {
            registered.add(this);
        }

        @Override
        public void onServiceUnregistered() {
            unregistered.add(this);
        }
    }
    final MyService service = new MyService();
    final String serviceTag1 = "service1";
    final String serviceTag2 = "service2";
    TestKeyWithScope beep = new TestKeyWithScope("beep") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService(serviceTag1, service);
            serviceBinder.addService(serviceTag2, service);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    TestKey clear = new TestKey("clear");
    backstack.setup(History.of(beep));
    assertThat(activated).isEmpty();
    assertThat(inactivated).isEmpty();
    assertThat(registered).isEmpty();
    assertThat(unregistered).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(activated).isNotEmpty();
    assertThat(inactivated).isEmpty();
    assertThat(registered).isNotEmpty();
    assertThat(unregistered).isEmpty();
    assertThat(activated).containsOnlyOnce(service);
    assertThat(registered).containsOnlyOnce(service);
    backstack.setHistory(History.of(clear), StateChange.REPLACE);
    assertThat(activated).isNotEmpty();
    assertThat(inactivated).isNotEmpty();
    assertThat(registered).isNotEmpty();
    assertThat(unregistered).isNotEmpty();
    assertThat(inactivated).containsOnlyOnce(service);
    assertThat(unregistered).containsOnlyOnce(service);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 70 with ServiceProvider

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

the class ScopingTest method canFindServiceWithOverlapsWorks.

@Test
public void canFindServiceWithOverlapsWorks() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Service service1 = new Service();
    final Service service2 = new Service();
    TestKeyWithScope beep = new TestKeyWithScope("beep") {

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

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

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

        @Nonnull
        @Override
        public String getScopeTag() {
            return "boop";
        }
    };
    backstack.setup(History.of(beep, boop));
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isTrue();
    assertThat(backstack.<Object>canFindService(SERVICE_TAG)).isTrue();
    backstack.goBack();
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
    assertThat(backstack.<Object>canFindService(SERVICE_TAG)).isTrue();
    backstack.setHistory(History.single(testKey1), StateChange.REPLACE);
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
    assertThat(backstack.canFindService(SERVICE_TAG)).isFalse();
}
Also used : 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