Search in sources :

Example 26 with TestKey

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

the class ScopingGlobalScopeTest method globalServicesFactoryFailsIfBackstackIsAddedAsService.

@Test
public void globalServicesFactoryFailsIfBackstackIsAddedAsService() {
    Backstack backstack = new Backstack();
    backstack.setGlobalServices(new GlobalServices.Factory() {

        @Nonnull
        @Override
        public GlobalServices create(@Nonnull Backstack backstack) {
            return GlobalServices.builder().addService("backstack", backstack).build();
        }
    });
    backstack.setup(History.of(new TestKey("hello!")));
    try {
        backstack.setStateChanger(new StateChanger() {

            @Override
            public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
                completionCallback.stateChangeComplete();
            }
        });
        StateBundle bundle = backstack.toBundle();
        Assert.fail("This would fail on `toBundle()`");
    } catch (IllegalArgumentException e) {
    // OK!
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 27 with TestKey

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

the class ScopingGlobalScopeTest method globalServiceStateIsRestored.

@Test
public void globalServiceStateIsRestored() {
    class Service implements Bundleable {

        int blah = 2;

        @Nonnull
        @Override
        public StateBundle toBundle() {
            StateBundle stateBundle = new StateBundle();
            stateBundle.putInt("blah", 5);
            return stateBundle;
        }

        @Override
        public void fromBundle(@Nullable StateBundle bundle) {
            if (bundle != null) {
                blah = bundle.getInt("blah");
            }
        }
    }
    TestKey testKey = new TestKey("world");
    final Service service = new Service();
    final Backstack backstack = new Backstack();
    backstack.setGlobalServices(GlobalServices.builder().addService("service", service).build());
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setup(History.of(testKey));
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.canFindService("service")).isTrue();
    StateBundle stateBundle = backstack.toBundle();
    final Backstack backstack2 = new Backstack();
    backstack2.setGlobalServices(GlobalServices.builder().addService("service", new Service()).build());
    StateChanger stateChanger2 = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack2.setup(History.of(testKey));
    backstack2.fromBundle(stateBundle);
    backstack2.setStateChanger(stateChanger2);
    assertThat(backstack2.lookupService("service")).isNotSameAs(service);
    assertThat(backstack.<Service>lookupService("service").blah).isEqualTo(2);
    assertThat(backstack2.<Service>lookupService("service").blah).isEqualTo(5);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) StateBundle(com.zhuinden.statebundle.StateBundle) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 28 with TestKey

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

the class HistoryTest method builderFromBackstackDelegate.

@Test
public void builderFromBackstackDelegate() throws Exception {
    TestKey testKey1 = new TestKey("Hello");
    TestKey testKey2 = new TestKey("World");
    BackstackDelegate backstackDelegate = new BackstackDelegate();
    backstackDelegate.onCreate(null, null, History.of(testKey1, testKey2));
    backstackDelegate.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(History.builderFrom(backstackDelegate)).containsExactly(testKey1, testKey2);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Test(org.junit.Test)

Example 29 with TestKey

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

the class HistoryTest method fromList.

@Test
public void fromList() throws Exception {
    TestKey testKey1 = new TestKey("Hello");
    TestKey testKey2 = new TestKey("World");
    List<TestKey> list = new ArrayList<>(2);
    list.add(testKey1);
    list.add(testKey2);
    assertThat(History.from(list)).containsExactly(testKey1, testKey2);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 30 with TestKey

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

the class HistoryTest method clear.

@Test
public void clear() throws Exception {
    TestKey testKey1 = new TestKey("Hello");
    TestKey testKey2 = new TestKey("World");
    History history = History.of(testKey1, testKey2);
    try {
        history.clear();
        fail();
    } catch (UnsupportedOperationException e) {
    // OK!
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) 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