Search in sources :

Example 6 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method handlesReregistrationBeforeCreate.

@Test
public void handlesReregistrationBeforeCreate() {
    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();
        }
    });
    Bundle b = new Bundle();
    getBundleServiceRunner(activityScope).onCreate(b);
    assertThat(i.get()).isEqualTo(2);
}
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 7 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method handleDestroyFromEarlyLoad.

@Test
public void handleDestroyFromEarlyLoad() {
    final AtomicInteger loads = new AtomicInteger(0);
    final AtomicInteger destroys = new AtomicInteger(0);
    class Destroyer implements Bundler {

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

        @Override
        public void onEnterScope(MortarScope scope) {
        }

        @Override
        public void onLoad(Bundle savedInstanceState) {
            if (loads.incrementAndGet() > 2) {
                activityScope.destroy();
            }
        }

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

        @Override
        public void onExitScope() {
            destroys.incrementAndGet();
        }
    }
    BundleService bundleService = getBundleService(activityScope);
    bundleService.register(new Destroyer());
    bundleService.register(new Destroyer());
    Bundle b = new Bundle();
    getBundleServiceRunner(activityScope).onCreate(b);
    assertThat(loads.get()).isEqualTo(3);
    assertThat(destroys.get()).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) BundleService.getBundleService(mortar.bundler.BundleService.getBundleService) Test(org.junit.Test)

Example 8 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method descendantScopesCreatedDuringParentOnLoadAreNotStuckInLoadingMode.

/**
   * Happened during first naive fix of
   * <a href="https://github.com/square/mortar/issues/46">Issue 46</a>.
   */
@Test
public void descendantScopesCreatedDuringParentOnLoadAreNotStuckInLoadingMode() {
    getBundleService(activityScope).register(new MyBundler("outer") {

        @Override
        public void onLoad(Bundle savedInstanceState) {
            MortarScope child = activityScope.buildChild().build("subscope");
            child.buildChild().build("subsubscope");
        }
    });
    getBundleServiceRunner(activityScope).onSaveInstanceState(new Bundle());
// No crash? Victoire!
}
Also used : Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) Test(org.junit.Test)

Example 9 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method mockContext.

private static Context mockContext(MortarScope root) {
    final MortarScope scope = root;
    Context appContext = mock(Context.class);
    when(appContext.getSystemService(anyString())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String name = (String) invocation.getArguments()[0];
            return scope.hasService(name) ? scope.getService(name) : null;
        }
    });
    return appContext;
}
Also used : Context(android.content.Context) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MortarScope(mortar.MortarScope) Matchers.anyString(org.mockito.Matchers.anyString)

Example 10 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method bundlersInChildScopesLoadAfterBundlersOnParent.

/**
   * https://github.com/square/mortar/issues/77
   */
@Test
public void bundlersInChildScopesLoadAfterBundlersOnParent() {
    final List<Bundler> loadingOrder = new ArrayList<>();
    // rootBundler#onLoad creates a child scope and registers childBundler on it,
    // and after that registers a serviceBundler on the higher level
    // activity scope. The service must receive onLoad before the child does.
    getBundleServiceRunner(activityScope).onCreate(null);
    final MyBundler serviceOnActivityScope = new MyBundler("service") {

        @Override
        public void onLoad(Bundle savedInstanceState) {
            super.onLoad(savedInstanceState);
            loadingOrder.add(this);
        }
    };
    final MyBundler childBundler = new MyBundler("childBundler") {

        @Override
        public void onLoad(Bundle savedInstanceState) {
            super.onLoad(savedInstanceState);
            loadingOrder.add(this);
        }
    };
    MyBundler rootBundler = new MyBundler("root") {

        @Override
        public void onLoad(Bundle savedInstanceState) {
            loadingOrder.add(this);
            MortarScope childScope = activityScope.buildChild().build("childScope");
            getBundleService(childScope).register(childBundler);
            getBundleService(activityScope).register(serviceOnActivityScope);
        }
    };
    getBundleService(activityScope).register(rootBundler);
    assertThat(loadingOrder.size()).isEqualTo(3);
    assertThat(loadingOrder.get(0)).isSameAs(rootBundler);
    assertThat(loadingOrder.get(1)).isSameAs(serviceOnActivityScope);
    assertThat(loadingOrder.get(2)).isSameAs(childBundler);
}
Also used : Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) ArrayList(java.util.ArrayList) 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