Search in sources :

Example 11 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method handlesReregistrationAfterCreate.

@Test
public void handlesReregistrationAfterCreate() {
    Bundle b = new Bundle();
    getBundleServiceRunner(activityScope).onCreate(b);
    final AtomicInteger i = new AtomicInteger(0);
    final BundleService bundleService = getBundleService(activityScope);
    bundleService.register(new Bundler() {

        @Override
        public String getMortarBundleKey() {
            return "key";
        }

        @Override
        public void onEnterScope(MortarScope scope) {
        }

        @Override
        public void onLoad(Bundle savedInstanceState) {
            if (i.incrementAndGet() < 1)
                bundleService.register(this);
        }

        @Override
        public void onSave(Bundle outState) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void onExitScope() {
            throw new UnsupportedOperationException();
        }
    });
    assertThat(i.get()).isEqualTo(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) Matchers.anyString(org.mockito.Matchers.anyString) BundleService.getBundleService(mortar.bundler.BundleService.getBundleService) Test(org.junit.Test)

Example 12 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method registerWithDescendantScopesCreatedDuringParentOnCreateGetOnlyOneOnLoadCall.

/** <a href="https://github.com/square/mortar/issues/46">Issue 46</a> */
@Test
public void registerWithDescendantScopesCreatedDuringParentOnCreateGetOnlyOneOnLoadCall() {
    final MyBundler childBundler = new MyBundler("child");
    final MyBundler grandChildBundler = new MyBundler("grandChild");
    final AtomicBoolean spawnSubScope = new AtomicBoolean(false);
    getBundleService(activityScope).register(new MyBundler("outer") {

        @Override
        public void onLoad(Bundle savedInstanceState) {
            if (spawnSubScope.get()) {
                MortarScope childScope = activityScope.buildChild().build("child scope");
                getBundleService(childScope).register(childBundler);
                // 1. We're in the middle of loading, so the usual register > load call doesn't happen.
                assertThat(childBundler.loaded).isFalse();
                MortarScope grandchildScope = childScope.buildChild().build("grandchild scope");
                getBundleService(grandchildScope).register(grandChildBundler);
                assertThat(grandChildBundler.loaded).isFalse();
            }
        }
    });
    spawnSubScope.set(true);
    getBundleServiceRunner(activityScope).onCreate(null);
    // 2. But load is called before the onCreate chain ends.
    assertThat(childBundler.loaded).isTrue();
    assertThat(grandChildBundler.loaded).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) Test(org.junit.Test)

Example 13 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method cannotGetBundleServiceForDestroyed.

@Test
public void cannotGetBundleServiceForDestroyed() {
    MortarScope child = activityScope.buildChild().build("child");
    child.destroy();
    IllegalStateException caught = null;
    try {
        getBundleService(child);
    } catch (IllegalStateException e) {
        caught = e;
    }
    assertThat(caught).isNotNull();
}
Also used : MortarScope(mortar.MortarScope) Test(org.junit.Test)

Example 14 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method newProcess.

private void newProcess(String activityScopeName) {
    MortarScope root = MortarScope.buildRootScope().build(activityScopeName);
    activityScope = root.buildChild().withService(BundleServiceRunner.SERVICE_NAME, new BundleServiceRunner()).build("activity");
}
Also used : BundleServiceRunner.getBundleServiceRunner(mortar.bundler.BundleServiceRunner.getBundleServiceRunner) MortarScope(mortar.MortarScope)

Example 15 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method deliversStateToBundlerWhenRegisterAfterOnCreate.

@Test
public void deliversStateToBundlerWhenRegisterAfterOnCreate() {
    class SavesAndRestores extends MyBundler {

        SavesAndRestores() {
            super("sNr");
        }

        boolean restored;

        @Override
        public void onLoad(Bundle savedInstanceState) {
            super.onLoad(savedInstanceState);
            restored = savedInstanceState != null && savedInstanceState.getBoolean("fred");
        }

        @Override
        public void onSave(Bundle outState) {
            super.onSave(outState);
            outState.putBoolean("fred", true);
        }
    }
    class Top extends MyBundler {

        Top() {
            super("top");
        }

        final SavesAndRestores child = new SavesAndRestores();

        @Override
        public void onLoad(Bundle savedInstanceState) {
            super.onLoad(savedInstanceState);
            MortarScope childScope = activityScope.buildChild().build("child");
            getBundleService(childScope).register(child);
        }
    }
    Top originalTop = new Top();
    getBundleService(activityScope).register(originalTop);
    assertThat(originalTop.child.restored).isFalse();
    Bundle bundle = new Bundle();
    getBundleServiceRunner(activityScope).onSaveInstanceState(bundle);
    newProcess();
    getBundleServiceRunner(activityScope).onCreate(bundle);
    Top newTop = new Top();
    getBundleService(activityScope).register(newTop);
    assertThat(newTop.child.restored).isTrue();
}
Also used : Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) Test(org.junit.Test)

Aggregations

MortarScope (mortar.MortarScope)16 Test (org.junit.Test)12 Bundle (android.os.Bundle)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BundleService.getBundleService (mortar.bundler.BundleService.getBundleService)4 BundleServiceRunner.getBundleServiceRunner (mortar.bundler.BundleServiceRunner.getBundleServiceRunner)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Context (android.content.Context)2 ChatListScreen (com.example.mortar.screen.ChatListScreen)1 GsonParceler (com.example.mortar.screen.GsonParceler)1 Gson (com.google.gson.Gson)1 FlowDelegate (flow.FlowDelegate)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BundleServiceRunner (mortar.bundler.BundleServiceRunner)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1