/** * This test verifies that processing a beacon in a scan (which starts its own thread) does not * affect the size of the available threads in the main Android AsyncTask.THREAD_POOL_EXECUTOR * @throws Exception */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void beaconScanCallbackTest() throws Exception { final ServiceController<BeaconService> beaconServiceServiceController = Robolectric.buildService(BeaconService.class); beaconServiceServiceController.attach(); BeaconService beaconService = beaconServiceServiceController.get(); beaconService.onCreate(); CycledLeScanCallback callback = beaconService.getCycledLeScanCallback(); ThreadPoolExecutor executor = (ThreadPoolExecutor) AsyncTask.THREAD_POOL_EXECUTOR; int activeThreadCountBeforeScan = executor.getActiveCount(); byte[] scanRecord = new byte[1]; callback.onLeScan(null, -59, scanRecord); int activeThreadCountAfterScan = executor.getActiveCount(); assertEquals("The size of the Android thread pool should be unchanged by beacon scanning", activeThreadCountBeforeScan, activeThreadCountAfterScan); // Need to sleep here until the thread in the above method completes, otherwise an exception // is thrown. Maybe we don't care about this exception, so we could remove this. Thread.sleep(100); }
@Before public void setUp() { serviceIntent = new Intent(RuntimeEnvironment.application, RegistrationIntentService.class); ((TestNetComponent)((TestElloApp) RuntimeEnvironment.application).getNetComponent()).inject(this); ShadowApplication.getInstance().startService(serviceIntent); ServiceController<RegistrationIntentService> serviceController = Robolectric.buildService(RegistrationIntentService.class); serviceController.attach() .create() .startCommand(0, 1); service = serviceController.get(); }
private static CommCareSessionService startRoboCommCareService() { Intent startIntent = new Intent(RuntimeEnvironment.application, CommCareSessionService.class); ServiceController<CommCareSessionService> serviceController = Robolectric.buildService(CommCareSessionService.class, startIntent); serviceController.attach() .create() .startCommand(0, 1); return serviceController.get(); }
public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass) { return new ServiceController<T>(serviceClass); }
public static <T extends Service> T setupService(Class<T> serviceClass) { return new ServiceController<T>(serviceClass).attach().create().get(); }