Search in sources :

Example 6 with TestKeyWithScope

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

the class ScopingAliasTest method bundleableNotCalledForAlias.

@Test
public void bundleableNotCalledForAlias() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    class Service implements Bundleable {

        private int saved = 0;

        private int restored = 0;

        @Nonnull
        @Override
        public StateBundle toBundle() {
            saved++;
            return new StateBundle();
        }

        @Override
        public void fromBundle(@Nullable StateBundle bundle) {
            if (bundle != null) {
                restored++;
            }
        }
    }
    final Service service = new Service();
    TestKeyWithScope boop = new TestKeyWithScope("boop") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service", service);
            serviceBinder.addAlias("alias", service);
        }
    };
    backstack.setup(History.of(boop));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.hasService("boop", "service")).isTrue();
    assertThat(backstack.hasService("boop", "alias")).isTrue();
    StateBundle bundle = backstack.toBundle();
    Backstack backstack2 = new Backstack();
    backstack2.setScopedServices(new ServiceProvider());
    backstack2.setup(History.of(boop));
    backstack2.fromBundle(bundle);
    backstack2.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(service.saved).isEqualTo(1);
    assertThat(service.restored).isEqualTo(1);
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) StateBundle(com.zhuinden.statebundle.StateBundle) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 7 with TestKeyWithScope

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

the class ScopingAliasTest method aliasesWork.

@Test
public void aliasesWork() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Object service = new Object();
    TestKeyWithScope boop = new TestKeyWithScope("boop") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service", service);
            assertThat(serviceBinder.hasService("alias")).isFalse();
            serviceBinder.addAlias("alias", service);
            assertThat(serviceBinder.hasService("alias")).isTrue();
        }
    };
    backstack.setup(History.of(boop));
    assertThat(backstack.hasService("boop", "service")).isFalse();
    assertThat(backstack.hasService("boop", "alias")).isFalse();
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    // then
    assertThat(backstack.hasService("boop", "service")).isTrue();
    assertThat(backstack.hasService("boop", "alias")).isTrue();
    assertThat(backstack.getService("boop", "service")).isSameAs(service);
    assertThat(backstack.getService("boop", "alias")).isSameAs(service);
    assertThat(backstack.canFindFromScope("boop", "alias")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "alias", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "alias", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindService("alias")).isTrue();
    assertThat(backstack.lookupFromScope("boop", "alias")).isSameAs(service);
    assertThat(backstack.lookupFromScope("boop", "alias", ScopeLookupMode.ALL)).isSameAs(service);
    assertThat(backstack.lookupFromScope("boop", "alias", ScopeLookupMode.EXPLICIT)).isSameAs(service);
    assertThat(backstack.lookupService("alias")).isSameAs(service);
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 8 with TestKeyWithScope

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

the class ScopingBackEventDispatchTest method onBackDispatchToHandlesBackCalledOnceForMultipleRegistrations.

@Test
public void onBackDispatchToHandlesBackCalledOnceForMultipleRegistrations() {
    class BackCounter implements ScopedServices.HandlesBack {

        private int count = 0;

        @Override
        public boolean onBackEvent() {
            count++;
            return false;
        }
    }
    final BackCounter service = new BackCounter();
    Object key = new TestKeyWithScope("key") {

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

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    boolean handled = backstack.goBack();
    assertThat(handled).isFalse();
    assertThat(service.count).isEqualTo(1);
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 9 with TestKeyWithScope

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

the class ScopingBackEventDispatchTest method onBackDispatchToHandlesBackNotCalledOnAlias.

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

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

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.getHistory()).containsExactly(key);
    assertThat(backstack.lookupService("service")).isSameAs(service);
    boolean handled = backstack.goBack();
    assertThat(handled).isEqualTo(false);
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 10 with TestKeyWithScope

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

the class ScopingBackEventDispatchTest method onBackDispatchToHandlesBackCalledInReverseOrder.

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

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

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.getHistory()).containsExactly(key);
    assertThat(backstack.lookupService("service1")).isSameAs(service1);
    assertThat(backstack.lookupService("service2")).isSameAs(service2);
    assertThat(service1.handledBackOnce).isEqualTo(false);
    assertThat(service2.handledBackOnce).isEqualTo(false);
    boolean handled = backstack.goBack();
    assertThat(handled).isEqualTo(true);
    assertThat(service1.handledBackOnce).isEqualTo(false);
    assertThat(service2.handledBackOnce).isEqualTo(true);
    handled = backstack.goBack();
    assertThat(handled).isEqualTo(true);
    assertThat(service1.handledBackOnce).isEqualTo(true);
    assertThat(service2.handledBackOnce).isEqualTo(true);
    handled = backstack.goBack();
    assertThat(handled).isEqualTo(false);
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Aggregations

ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)25 TestKeyWithScope (com.zhuinden.simplestack.helpers.TestKeyWithScope)25 Test (org.junit.Test)25 TestKey (com.zhuinden.simplestack.helpers.TestKey)7 Nonnull (javax.annotation.Nonnull)4 TestKeyWithExplicitParent (com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent)3 ArrayList (java.util.ArrayList)3 Parcel (android.os.Parcel)2 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)2 Action (com.zhuinden.simplestack.helpers.Action)1 StateBundle (com.zhuinden.statebundle.StateBundle)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (javax.annotation.Nullable)1