Search in sources :

Example 16 with TestKey

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

the class BackstackTest method onGoBackKeysShouldNotBeClearedAndShouldRestoreRestoredKey.

@Test
public void onGoBackKeysShouldNotBeClearedAndShouldRestoreRestoredKey() {
    TestKey initial = new TestKey("initial");
    TestKey restored = new TestKey("restored");
    ArrayList<Parcelable> history = new ArrayList<>();
    history.add(restored);
    StateBundle stateBundle = new StateBundle();
    stateBundle.putParcelableArrayList(Backstack.getHistoryTag(), history);
    Backstack backstack = new Backstack();
    backstack.setup(History.single(initial));
    backstack.fromBundle(stateBundle);
    backstack.setStateChanger(stateChanger);
    backstack.goBack();
    assertThat(backstack.getHistory()).isNotEmpty();
    assertThat(backstack.getHistory()).containsExactly(restored);
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.getHistory()).containsExactly(restored);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) StateBundle(com.zhuinden.statebundle.StateBundle) Test(org.junit.Test)

Example 17 with TestKey

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

the class BackstackTest method exitScopeToWorks.

@Test
public void exitScopeToWorks() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    Object key = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    Object targetKey = new TestKey("targetKey");
    backstack.setup(History.of(key));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
    assertThat(backstack.getHistory()).containsExactly(targetKey);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 18 with TestKey

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

the class BackstackTest method retainedObjectRestoringInvalidObjectFails.

@Test
public void retainedObjectRestoringInvalidObjectFails() {
    TestKey initialKey = new TestKey("initialKey");
    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();
    InvalidObject invalidObject = new InvalidObject();
    backstack.addRetainedObject("testObject", testObject);
    assertThat(testObject.currentState).isEqualTo(3);
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull StateChanger.Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    StateBundle bundle = backstack.toBundle();
    Backstack backstack2 = new Backstack();
    backstack2.setup(History.of(initialKey));
    backstack2.addRetainedObject("testObject", invalidObject);
    try {
        backstack2.fromBundle(bundle);
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) StateBundle(com.zhuinden.statebundle.StateBundle) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 19 with TestKey

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

the class BackstackTest method keyFilterSetAfterSetupShouldThrow.

@Test
public void keyFilterSetAfterSetupShouldThrow() {
    final TestKey initial = new TestKey("initial");
    Backstack backstack = new Backstack();
    backstack.setup(History.single(initial));
    try {
        backstack.setKeyFilter(new KeyFilter() {

            @Nonnull
            @Override
            public List<Object> filterHistory(@Nonnull List<Object> restoredKeys) {
                return restoredKeys;
            }
        });
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 20 with TestKey

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

the class ScopingTest method keyWithinNavigationWithOnlyExplicitScopeStillAbleToFindScopes.

@Test
public void keyWithinNavigationWithOnlyExplicitScopeStillAbleToFindScopes() {
    Backstack backstack = new Backstack();
    final Object helloService = new Object();
    final Object worldService = new Object();
    final Object kappaService = new Object();
    final Object parentService = new Object();
    final Object parent2Service = new Object();
    backstack.setScopedServices(new ScopedServices() {

        @Override
        public void bindServices(@Nonnull ServiceBinder serviceBinder) {
            if ("hello".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("hello", helloService);
            } else if ("world".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("world", worldService);
            } else if ("kappa".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("kappa", kappaService);
            } else if ("parent".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parent", parentService);
            } else if ("parent2".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parent2", parent2Service);
            }
        }
    });
    class TestKeyWithExplicitParent extends TestKeyWithScope implements ScopeKey.Child {

        private String[] parentScopes;

        TestKeyWithExplicitParent(String name, String... parentScopes) {
            super(name);
            this.parentScopes = parentScopes;
        }

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

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.from(Arrays.asList(parentScopes));
        }
    }
    class TestKeyWithOnlyExplicitParent extends TestKey implements ScopeKey.Child {

        private String[] parentScopes;

        TestKeyWithOnlyExplicitParent(String name, String... parentScopes) {
            super(name);
            this.parentScopes = parentScopes;
        }

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

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.from(Arrays.asList(parentScopes));
        }
    }
    TestKeyWithScope scopeKey1 = new TestKeyWithScope("hello");
    TestKeyWithScope scopeKey2 = new TestKeyWithExplicitParent("world", "parent");
    TestKeyWithScope scopeKey3 = new TestKeyWithScope("kappa");
    TestKey key4 = new TestKeyWithOnlyExplicitParent("parentpls", "parent2");
    backstack.setup(History.of(scopeKey1, scopeKey2, key4));
    final AtomicReference<StateChanger.Callback> callbackRef = new AtomicReference<>();
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            callbackRef.set(completionCallback);
        }
    });
    callbackRef.get().stateChangeComplete();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).containsExactly("parent2");
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("parent2", "world", "parent", "hello");
    backstack.setHistory(History.of(scopeKey1, scopeKey3), StateChange.REPLACE);
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).containsExactly("parent2");
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).containsExactly("parent2", "world", "parent", "hello");
    callbackRef.get().stateChangeComplete();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.EXPLICIT)).isEmpty();
    assertThat(backstack.findScopesForKey(key4, ScopeLookupMode.ALL)).isEmpty();
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Parcel(android.os.Parcel) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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