Search in sources :

Example 61 with TestKey

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

the class BackstackCoreTest method goUpThrowsForNull.

@Test
public void goUpThrowsForNull() {
    TestKey initial = new TestKey("hello");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initial));
    try {
        backstack.goUp(null);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    // OK!
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Test(org.junit.Test)

Example 62 with TestKey

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

the class BackstackTest method retainedObjectsDoRestorePendingState.

@Test
public void retainedObjectsDoRestorePendingState() {
    TestKey initialKey = new TestKey("initialKey");
    TestKey newKey = new TestKey("newKey");
    Backstack backstack = new Backstack();
    backstack.setup(History.of(initialKey));
    class TestObject implements Bundleable {

        private int currentState = 3;

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

        @Override
        public void fromBundle(@Nullable StateBundle bundle) {
            if (bundle != null) {
                currentState = bundle.getInt("currentState", 3);
            }
        }
    }
    class InvalidObject {
    }
    TestObject testObject = new TestObject();
    TestObject testPendingObject = new TestObject();
    TestObject testRemovedObject = new TestObject();
    Object testNormalObject = new Object();
    backstack.addRetainedObject("testObject", testObject);
    backstack.addRetainedObject("testPendingObject", testPendingObject);
    backstack.addRetainedObject("testRemovedObject", testRemovedObject);
    backstack.addRetainedObject("testNormalObject", testNormalObject);
    assertThat(testObject.currentState).isEqualTo(3);
    assertThat(testPendingObject.currentState).isEqualTo(3);
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull StateChanger.Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    backstack.setHistory(History.of(newKey), StateChange.REPLACE);
    StateBundle bundle = backstack.toBundle();
    Backstack backstack2 = new Backstack();
    backstack2.setup(History.of(initialKey));
    assertThat(testObject.currentState).isEqualTo(3);
    backstack2.addRetainedObject("testObject", testObject);
    assertThat(testObject.currentState).isEqualTo(3);
    backstack2.fromBundle(bundle);
    assertThat(testObject.currentState).isEqualTo(5);
    backstack2.addRetainedObject("testNormalObject", testNormalObject);
    try {
        backstack2.addRetainedObject("testPendingObject", new InvalidObject());
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
    assertThat(testPendingObject.currentState).isEqualTo(3);
    backstack2.addRetainedObject("testPendingObject", testPendingObject);
    assertThat(testPendingObject.currentState).isEqualTo(5);
    backstack2.removeRetainedObject("testRemovedObject");
    backstack2.addRetainedObject("testRemovedObject", testRemovedObject);
    assertThat(testRemovedObject.currentState).isEqualTo(3);
    backstack2.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull StateChanger.Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack2.getHistory()).containsExactly(newKey);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) StateBundle(com.zhuinden.statebundle.StateBundle) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 63 with TestKey

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

the class HistoryBuilderTest method removeUntilRemovesUntil.

@Test
public void removeUntilRemovesUntil() {
    TestKey hi = new TestKey("hi");
    TestKey hello = new TestKey("hello");
    TestKey bye = new TestKey("bye");
    List<Object> history = History.newBuilder().add(hi).add(hello).add(bye).removeUntil(hi).build();
    assertThat(history).containsExactly(hi);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Test(org.junit.Test)

Example 64 with TestKey

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

the class BackstackDelegateTest method addNullStateChangeListenerThrows.

@Test
public void addNullStateChangeListenerThrows() {
    TestKey testKey = new TestKey("hello");
    BackstackDelegate backstackDelegate = new BackstackDelegate();
    try {
        backstackDelegate.addStateChangeCompletionListener(null);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    // OK
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Test(org.junit.Test)

Example 65 with TestKey

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

the class BackstackDelegateTest method registerLifecycleCallbacksShouldBeCalledForActivity.

@Test
public void registerLifecycleCallbacksShouldBeCalledForActivity() {
    Activity activity = Mockito.mock(Activity.class);
    Application application = Mockito.mock(Application.class);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Object key = new TestKey("hello");
    BackstackDelegate backstackDelegate = new BackstackDelegate();
    backstackDelegate.onCreate(null, null, History.single(key));
    // THEN
    backstackDelegate.registerForLifecycleCallbacks(activity);
    Mockito.verify(activity, Mockito.times(1)).getApplication();
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Activity(android.app.Activity) Application(android.app.Application) 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