Search in sources :

Example 61 with ServiceProvider

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

the class ScopingTest method scopeServicesArePersistedToStateBundle.

@Test
public void scopeServicesArePersistedToStateBundle() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Service service = new Service();
    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";
        }
    };
    backstack.setup(History.of(testKeyWithScope));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.hasService(testKeyWithScope.getScopeTag(), SERVICE_TAG)).isTrue();
    StateBundle stateBundle = backstack.toBundle();
    // noinspection ConstantConditions
    // backstack.getScopesTag() is internal
    assertThat(stateBundle.getBundle(Backstack.getScopesTag()).getBundle(testKeyWithScope.getScopeTag()).getBundle(SERVICE_TAG).getInt("blah")).isEqualTo(5);
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 62 with ServiceProvider

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

the class ScopingTest method gettingNonExistentServiceThrows.

@Test
public void gettingNonExistentServiceThrows() {
    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();
    try {
        backstack.getService(testKey2, "d'oh");
        Assert.fail();
    } catch (IllegalArgumentException e) {
    // OK!
    }
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 63 with ServiceProvider

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

the class ScopingTest method lookupServiceWithOverlapsWorks.

@Test
public void lookupServiceWithOverlapsWorks() {
    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.hasScope("beep")).isFalse();
    assertThat(backstack.hasScope("boop")).isFalse();
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.hasScope("beep")).isTrue();
    assertThat(backstack.hasScope("boop")).isTrue();
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isTrue();
    assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service2);
    backstack.goBack();
    assertThat(backstack.hasScope("beep")).isTrue();
    assertThat(backstack.hasScope("boop")).isFalse();
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
    assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
    assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service1);
    backstack.setHistory(History.single(testKey1), StateChange.REPLACE);
    assertThat(backstack.hasScope("beep")).isFalse();
    assertThat(backstack.hasScope("boop")).isFalse();
    assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
    assertThat(backstack.hasService("boop", 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 64 with ServiceProvider

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

the class ScopingTest method scopedServicesCanBeSetBeforeInitialStateChange.

@Test
public void scopedServicesCanBeSetBeforeInitialStateChange() {
    Backstack backstack = new Backstack();
    backstack.setup(History.of(testKey1));
    backstack.setScopedServices(new ServiceProvider());
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    try {
        backstack.setScopedServices(new ServiceProvider());
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 65 with ServiceProvider

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

the class ScopingTest method activationIsCalledOnlyOnce.

@Test
public void activationIsCalledOnlyOnce() {
    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";
        }
    };
    backstack.setup(History.of(beep));
    assertThat(activatedServices).isEmpty();
    assertThat(deactivatedServices).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(activatedServices).containsExactly(service1);
    backstack.removeStateChanger();
    backstack.setStateChanger(stateChanger);
    assertThat(activatedServices).containsExactly(service1);
}
Also used : 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