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