Search in sources :

Example 11 with ServiceProvider

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

the class ScopingTest method deactivatedIsCalledInReverseOrder.

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

        @Override
        public void onServiceActive() {
        }

        @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(deactivatedServices).isEmpty();
    backstack.setStateChanger(stateChanger);
    backstack.goTo(boop);
    assertThat(deactivatedServices).containsExactly(service3, service2, service1);
    TestKey bye = new TestKey("bye");
    backstack.setHistory(History.of(bye), StateChange.REPLACE);
    assertThat(deactivatedServices).containsExactly(service3, service2, service1, service6, service5, service4);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with ServiceProvider

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

the class ScopingTest method lookupServiceNoOverlapsWorks.

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

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

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "boop";
        }
    };
    backstack.setup(History.of(beep, boop));
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
    assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service);
    backstack.goBack();
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
    assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service);
    backstack.setHistory(History.single(testKey1), StateChange.REPLACE);
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
    try {
        backstack.lookupService(SERVICE_TAG);
        Assert.fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).contains("does not exist in any scope");
    // OK!
    }
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 13 with ServiceProvider

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

the class ScopingTest method scopeIsCreatedForScopeKeys.

@Test
public void scopeIsCreatedForScopeKeys() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.single(testKey2));
    assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isTrue();
    Service service = backstack.getService(testKey2, SERVICE_TAG);
    assertThat(service).isSameAs(services.get(SERVICE_TAG));
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 14 with ServiceProvider

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

the class ScopingTest method scopeCreationAndDestructionHappensInForwardAndReverseOrder.

@Test
public void scopeCreationAndDestructionHappensInForwardAndReverseOrder() {
    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);
        }

        @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("SERVICE2", service2);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "boop";
        }
    };
    TestKey bye = new TestKey("bye");
    backstack.setup(History.of(beep, boop));
    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 15 with ServiceProvider

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

the class ScopingTest method serviceProviderCanBeSetWorksCorrectly.

@Test
public void serviceProviderCanBeSetWorksCorrectly() {
    Backstack backstack = new Backstack();
    assertThat(backstack.canSetScopeProviders()).isTrue();
    backstack.setup(History.of(testKey1));
    assertThat(backstack.canSetScopeProviders()).isTrue();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setGlobalServices(GlobalServices.builder().build());
    assertThat(backstack.canSetScopeProviders()).isTrue();
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.canSetScopeProviders()).isFalse();
    try {
        backstack.setScopedServices(new ServiceProvider());
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
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