Search in sources :

Example 1 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method cannotGetBundleServiceFromContextOfDestroyed.

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

Example 2 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method handlesDestroyFromOnSave.

@Test
public void handlesDestroyFromOnSave() {
    final AtomicInteger saves = 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) {
        }

        @Override
        public void onSave(Bundle outState) {
            saves.incrementAndGet();
            activityScope.destroy();
        }

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

Example 3 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class BundleServiceTest method destroyingWhileSaving.

/** https://github.com/square/mortar/issues/131 */
@Test
public void destroyingWhileSaving() {
    final MortarScope[] currentScreen = new MortarScope[] { null };
    MortarScope screenSwapperScope = activityScope.buildChild().build("screenOne");
    getBundleService(screenSwapperScope).register(new MyBundler("screenSwapper") {

        @Override
        public void onSave(Bundle outState) {
            currentScreen[0].destroy();
        }
    });
    final MortarScope screenOneScope = screenSwapperScope.buildChild().build("screenOne");
    getBundleService(screenOneScope).register(new MyBundler("bundlerOne"));
    currentScreen[0] = screenOneScope;
    final MortarScope screenTwoScope = screenSwapperScope.buildChild().build("screenTwo");
    getBundleService(screenTwoScope).register(new MyBundler("bundlerTwo"));
    getBundleServiceRunner(activityScope).onSaveInstanceState(new Bundle());
}
Also used : Bundle(android.os.Bundle) MortarScope(mortar.MortarScope) Test(org.junit.Test)

Example 4 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class MortarDemoActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GsonParceler parceler = new GsonParceler(new Gson());
    @SuppressWarnings("deprecation") FlowDelegate.NonConfigurationInstance nonConfig = (FlowDelegate.NonConfigurationInstance) getLastNonConfigurationInstance();
    MortarScope parentScope = MortarScope.getScope(getApplication());
    String scopeName = getLocalClassName() + "-task-" + getTaskId();
    activityScope = parentScope.findChild(scopeName);
    if (activityScope == null) {
        activityScope = parentScope.buildChild().withService(BundleServiceRunner.SERVICE_NAME, new BundleServiceRunner()).build(scopeName);
    }
    ObjectGraphService.inject(this, this);
    getBundleServiceRunner(activityScope).onCreate(savedInstanceState);
    actionBarOwner.takeView(this);
    setContentView(R.layout.root_layout);
    container = (PathContainerView) findViewById(R.id.container);
    containerAsHandlesBack = (HandlesBack) container;
    flowDelegate = FlowDelegate.onCreate(nonConfig, getIntent(), savedInstanceState, parceler, History.single(new ChatListScreen()), this);
}
Also used : BundleServiceRunner(mortar.bundler.BundleServiceRunner) BundleServiceRunner.getBundleServiceRunner(mortar.bundler.BundleServiceRunner.getBundleServiceRunner) FlowDelegate(flow.FlowDelegate) GsonParceler(com.example.mortar.screen.GsonParceler) MortarScope(mortar.MortarScope) ChatListScreen(com.example.mortar.screen.ChatListScreen) Gson(com.google.gson.Gson)

Example 5 with MortarScope

use of mortar.MortarScope in project mortar by square.

the class ScreenScoper method getScreenScope.

/**
   * Finds or creates the scope for the given screen, honoring its optional {@link
   * WithModuleFactory} or {@link WithModule} annotation. Note that scopes are also created
   * for unannotated screens.
   */
public MortarScope getScreenScope(Resources resources, MortarScope parentScope, final String name, final Object screen) {
    ModuleFactory moduleFactory = getModuleFactory(screen);
    Object[] childModule;
    if (moduleFactory != NO_FACTORY) {
        childModule = new Object[] { moduleFactory.createDaggerModule(resources, screen) };
    } else {
        // We need every screen to have a scope, so that anything it injects is scoped.  We need
        // this even if the screen doesn't declare a module, because Dagger allows injection of
        // objects that are annotated even if they don't appear in a module.
        childModule = new Object[0];
    }
    MortarScope childScope = parentScope.findChild(name);
    if (childScope == null) {
        childScope = parentScope.buildChild().withService(ObjectGraphService.SERVICE_NAME, ObjectGraphService.create(parentScope, childModule)).build(name);
    }
    return childScope;
}
Also used : MortarScope(mortar.MortarScope)

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