Java 类org.robolectric.util.ServiceController 实例源码

项目:android-beacon-library    文件:BeaconServiceTest.java   
/**
 * 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);
}
项目:ello-android    文件:RegistrationIntentServiceTest.java   
@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();
}
项目:commcare-android    文件:CommCareTestApplication.java   
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();
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass) {
  return new ServiceController<T>(serviceClass);
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
public static <T extends Service> T setupService(Class<T> serviceClass) {
  return new ServiceController<T>(serviceClass).attach().create().get();
}