Java Code Examples for org.robolectric.Robolectric#buildService()

The following examples show how to use org.robolectric.Robolectric#buildService() . 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: ItemSyncAdapterTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    reset(TestRestServiceFactory.hnRestService);
    reset(readabilityClient);
    serviceController = Robolectric.buildService(ItemSyncService.class);
    service = serviceController.create().get();
    setNetworkType(ConnectivityManager.TYPE_WIFI);
    PreferenceManager.getDefaultSharedPreferences(service)
            .edit()
            .putBoolean(service.getString(R.string.pref_saved_item_sync), true)
            .putBoolean(service.getString(R.string.pref_offline_comments), true)
            .apply();
    adapter = new TestItemSyncAdapter(service, new TestRestServiceFactory(), readabilityClient);
    syncPreferences = service.getSharedPreferences(
            service.getPackageName() +
                    SyncDelegate.SYNC_PREFERENCES_FILE, Context.MODE_PRIVATE);
    syncScheduler = new SyncScheduler();
}
 
Example 2
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 3
Source File: RegistrationIntentServiceTest.java    From ello-android with MIT License 5 votes vote down vote up
@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();
}
 
Example 4
Source File: UIControllerTest.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    controller = Robolectric.buildService(TestUIHostService.class);
    hostService = controller.create().get();
    uiController = hostService.controller;
    context = ApplicationProvider.getApplicationContext();
}
 
Example 5
Source File: WebCacheServiceTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testWebCache() {
    controller = Robolectric.buildService(WebCacheService.class,
            new Intent()
                    .putExtra(WebCacheService.EXTRA_URL, "http://example.com"));
    ShadowWebView.lastGlobalLoadedUrl = null;
    controller.startCommand(0, 0);
    service = controller.create().get();
    assertThat(ShadowWebView.getLastGlobalLoadedUrl()).contains("http://example.com");

}
 
Example 6
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();
}
 
Example 7
Source File: MyServiceTest.java    From Awesome-WanAndroid with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    ShadowLog.stream = System.out;
    mServiceController = Robolectric.buildService(MyService.class);
    mMyService = mServiceController.get();
}
 
Example 8
Source File: UIHostServiceTest.java    From Taskbar with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    controller = Robolectric.buildService(TestUIHostService.class);
    hostService = controller.create().get();
    uiController = hostService.controller;
}
 
Example 9
Source File: AuthenticatorServiceTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    controller = Robolectric.buildService(AuthenticatorService.class);
    service = controller.create().get();
}
 
Example 10
Source File: WebCacheServiceTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    controller = Robolectric.buildService(WebCacheService.class);
    service = controller.create().get();
}
 
Example 11
Source File: ItemSyncJobServiceTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    controller = Robolectric.buildService(TestItemSyncJobService.class);
    service = controller.create().get();
}