Search in sources :

Example 16 with ServiceProvider

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

the class ScopingTest method activationHappensEvenWithForceExecutedStateChangeAndInitializeStateChange.

@Test
public void activationHappensEvenWithForceExecutedStateChangeAndInitializeStateChange() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final List<Object> activatedServices = new ArrayList<>();
    final List<Object> deactivatedServices = new ArrayList<>();
    final AtomicReference<StateChanger.Callback> callback = new AtomicReference<>();
    StateChanger pendingStateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            callback.set(completionCallback);
        }
    };
    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();
    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(activatedServices).isEmpty();
    backstack.setStateChanger(pendingStateChanger);
    callback.get().stateChangeComplete();
    assertThat(activatedServices).containsExactly(service1, service2, service3);
    backstack.setHistory(History.of(boop), StateChange.BACKWARD);
    backstack.removeStateChanger();
    backstack.executePendingStateChange();
    backstack.setStateChanger(pendingStateChanger);
    callback.get().stateChangeComplete();
    assertThat(activatedServices).containsExactly(service1, service2, service3, service4, service5, service6);
    assertThat(deactivatedServices).containsExactly(service3, service2, service1);
}
Also used : Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 17 with ServiceProvider

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

the class ScopingTest method scopesAreFinalizedWhenActivityIsFinishing.

@Test
public void scopesAreFinalizedWhenActivityIsFinishing() {
    Activity activity = Mockito.mock(Activity.class);
    Mockito.when(activity.isFinishing()).thenReturn(true);
    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";
        }
    };
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    BackstackDelegate backstackDelegate = new BackstackDelegate();
    backstackDelegate.setScopedServices(activity, new ServiceProvider());
    backstackDelegate.onCreate(null, null, History.of(testKeyWithScope));
    backstackDelegate.setStateChanger(stateChanger);
    backstackDelegate.onPostResume();
    backstackDelegate.onPause();
    assertThat(backstackDelegate.hasScope("beep")).isTrue();
    assertThat(backstackDelegate.hasService(testKeyWithScope, SERVICE_TAG)).isTrue();
    assertThat(service.didServiceRegister).isTrue();
    assertThat(service.didServiceUnregister).isFalse();
    backstackDelegate.onDestroy();
    assertThat(backstackDelegate.hasScope("beep")).isFalse();
    assertThat(backstackDelegate.hasService(testKeyWithScope, SERVICE_TAG)).isFalse();
    assertThat(service.didServiceRegister).isTrue();
    assertThat(service.didServiceUnregister).isTrue();
}
Also used : Nonnull(javax.annotation.Nonnull) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Activity(android.app.Activity) Test(org.junit.Test)

Example 18 with ServiceProvider

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

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

the class ScopingTest method scopedServicesCanRetrieveBackstackFromServiceBinder.

@Test
public void scopedServicesCanRetrieveBackstackFromServiceBinder() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final AtomicReference<Backstack> ref = new AtomicReference<>();
    class MyService {
    }
    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);
            ref.set(serviceBinder.getBackstack());
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    backstack.setup(History.of(beep));
    backstack.setStateChanger(stateChanger);
    assertThat(backstack).isSameAs(ref.get());
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 20 with ServiceProvider

use of com.zhuinden.simplestack.helpers.ServiceProvider 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)

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