Search in sources :

Example 21 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey 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 22 with TestKey

use of com.zhuinden.simplestack.helpers.TestKey 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 23 with TestKey

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

the class ScopingTest method navigationIsPossibleAndEnqueuedDuringActivationDispatch.

@Test
public void navigationIsPossibleAndEnqueuedDuringActivationDispatch() {
    final TestKey destination = new TestKey("destination");
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    class MyService implements ScopedServices.Activated {

        private final Backstack backstack;

        public MyService(Backstack backstack) {
            this.backstack = backstack;
        }

        @Override
        public void onServiceActive() {
            backstack.setHistory(History.of(destination), StateChange.REPLACE);
        }

        @Override
        public void onServiceInactive() {
        }
    }
    final MyService service = new MyService(backstack);
    TestKeyWithOnlyParentServices beep = new TestKeyWithOnlyParentServices("beep", History.of("registration")) {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if (serviceBinder.getScopeTag().equals("registration")) {
                serviceBinder.addService("SERVICE", service);
            }
        }
    };
    TestKeyWithScope boop = new TestKeyWithScope("boop") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.setup(History.of(boop));
    backstack.setStateChanger(stateChanger);
    backstack.setHistory(History.of(beep), StateChange.REPLACE);
    assertThat(backstack.getHistory()).containsExactly(destination);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) TestKeyWithOnlyParentServices(com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices) Test(org.junit.Test)

Example 24 with TestKey

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

the class ScopingTest method activatedWorks.

@Test
public void activatedWorks() {
    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();
    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(activatedServices).isEmpty();
    assertThat(deactivatedServices).isEmpty();
    backstack.setStateChanger(stateChanger);
    assertThat(activatedServices).containsExactly(service2);
    assertThat(deactivatedServices).isEmpty();
    backstack.goBack();
    assertThat(activatedServices).containsExactly(service2, service1);
    assertThat(deactivatedServices).containsExactly(service2);
    backstack.setHistory(History.of(bye), StateChange.REPLACE);
    assertThat(activatedServices).containsExactly(service2, service1);
    assertThat(deactivatedServices).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 25 with TestKey

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

the class ScopingGlobalScopeTest method globalScopeLookupPrefersImplicitsToGlobal.

@Test
public void globalScopeLookupPrefersImplicitsToGlobal() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Object globalService = new Object();
    backstack.setGlobalServices(GlobalServices.builder().addService("parentService1", globalService).build());
    final Object parentService1 = new Object();
    final Object parentService2 = new Object();
    final Object service1 = new Object();
    final Object service2 = new Object();
    class Key1 extends TestKey implements HasServices, HasParentServices {

        Key1(String name) {
            super(name);
        }

        protected Key1(Parcel in) {
            super(in);
        }

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if ("parent1".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parentService1", parentService1);
            } else if (name.equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("service1", service1);
            }
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return name;
        }

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent1");
        }
    }
    class Key2 extends TestKey implements HasServices, HasParentServices {

        Key2(String name) {
            super(name);
        }

        protected Key2(Parcel in) {
            super(in);
        }

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if ("parent2".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parentService2", parentService2);
            } else if (name.equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("service2", service2);
            }
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return name;
        }

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent2");
        }
    }
    backstack.setup(History.of(new Key1("beep"), new Key2("boop")));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(globalService);
    assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(parentService1);
    assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThat(backstack.lookupService("parentService1")).isSameAs(parentService1);
    backstack.setHistory(History.of(new Key2("boop")), StateChange.REPLACE);
    assertThat(backstack.lookupService("parentService1")).isSameAs(globalService);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) HasParentServices(com.zhuinden.simplestack.helpers.HasParentServices) Parcel(android.os.Parcel) HasServices(com.zhuinden.simplestack.helpers.HasServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Aggregations

TestKey (com.zhuinden.simplestack.helpers.TestKey)148 Test (org.junit.Test)148 Nonnull (javax.annotation.Nonnull)43 ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)26 ArrayList (java.util.ArrayList)22 StateBundle (com.zhuinden.statebundle.StateBundle)11 Parcel (android.os.Parcel)10 HasServices (com.zhuinden.simplestack.helpers.HasServices)7 TestKeyWithScope (com.zhuinden.simplestack.helpers.TestKeyWithScope)7 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)6 Parcelable (android.os.Parcelable)5 TestKeyWithOnlyParentServices (com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 Activity (android.app.Activity)2 TestKeyWithExplicitParent (com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent)2 Application (android.app.Application)1 Context (android.content.Context)1 View (android.view.View)1