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);
}
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();
}
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();
}
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");
}
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();
}
Aggregations