org.robolectric.android.controller.ServiceController Java Examples

The following examples show how to use org.robolectric.android.controller.ServiceController. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeaconServiceTest.java    From android-beacon-library with Apache License 2.0 6 votes vote down vote up
/**
     * 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
     */
    @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, 123456L);

        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);
    }
 
Example #2
Source File: CommCareTestApplication.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private static CommCareSessionService startRoboCommCareService() {
    Intent startIntent =
            new Intent(RuntimeEnvironment.application, CommCareSessionService.class);
    ServiceController<CommCareSessionService> serviceController =
            Robolectric.buildService(CommCareSessionService.class, startIntent);
    serviceController
            .create()
            .startCommand(0, 1);
    return serviceController.get();
}