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

The following examples show how to use org.robolectric.Robolectric#application() . 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: AcDisplayRobolectricTest.java    From HeadsUp with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFileUtils() {
    Context context = Robolectric.application;
    File file = new File(context.getFilesDir(), "test_file_utils.txt");

    Random random = new Random();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 5000; i++) {
        sb.append(random.nextInt(10));
    }
    String text = sb.toString();

    // Test writing to file.
    assertTrue(FileUtils.writeToFile(file, text));

    // Test reading from file.
    assertTrue(text.equals(FileUtils.readTextFile(file)));

    // Test removing file.
    assertTrue(FileUtils.deleteRecursive(file));
    assertTrue(!file.exists());
}
 
Example 2
Source File: ClickGuardTest.java    From clickguard with Apache License 2.0 6 votes vote down vote up
@Test
public void guardMultipleViews() {
    CountClickListener listener = new CountClickListener() {
        @Override
        public void onClick(View v) {
            super.onClick(v);
            assertEquals(1, getClickedCount());
        }
    };
    View view1 = new View(Robolectric.application);
    view1.setOnClickListener(listener);
    View view2 = new View(Robolectric.application);
    view2.setOnClickListener(listener);
    View view3 = new View(Robolectric.application);
    view3.setOnClickListener(listener);

    ClickGuard.guard(view1, view2, view3);

    clickViews(view1, view2, view3);
}
 
Example 3
Source File: DrawPathTaskTest.java    From open with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
    application = (TestMapzenApplication) Robolectric.application;
    application.inject(this);
    TestHelper.initBaseActivity();
    TestMap testMap = (TestMap) mapController.getMap();
    viewController = Mockito.mock(ViewController.class);
    testMap.setViewport(viewController);
    task = new DrawPathTask(application);
    box = Mockito.mock(BoundingBox.class);
    stub(viewController.getBBox()).toReturn(box);
    outsideBefore1 = new Location("f");
    outsideBefore1.setLatitude(1);
    outsideBefore2 = new Location("f");
    outsideBefore2.setLatitude(2);
    inside1 = new Location("f");
    inside1.setLatitude(3);
    inside2 = new Location("f");
    inside2.setLatitude(4);
    outSideAfter1 = new Location("f");
    outSideAfter1.setLatitude(5);
    outSideAfter2 = new Location("f");
    outSideAfter2.setLatitude(6);
}
 
Example 4
Source File: ClickGuardTest.java    From clickguard with Apache License 2.0 5 votes vote down vote up
@Test
public void guardedListenerPreventsMultipleClicks() {
    View view = new View(Robolectric.application);
    CountClickGuardedOnClickListener listener = new CountClickGuardedOnClickListener();
    view.setOnClickListener(listener);

    clickView(view, 5);
    assertEquals(1, listener.clickedCount);

    Robolectric.runUiThreadTasksIncludingDelayedTasks();

    clickView(view, 5);
    assertEquals(2, listener.clickedCount);
}
 
Example 5
Source File: SectionCursorAdapterTest.java    From SectionCursorAdapter with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);

    when(cursor.getPosition()).thenReturn(0);
    when(cursor.getCount()).thenReturn(10);

    context = Robolectric.application;
    adapter = new TestAdapter(context, cursor, 0);
    spyAdapter = spy(adapter);
}
 
Example 6
Source File: DLNAServiceTest.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
private DLNAService makeServiceWithControlURL(String base, String controlURL) {
    List<Service> services = new ArrayList<Service>();
    Service service = new Service();
    service.baseURL = base;
    service.controlURL = controlURL;
    service.serviceType = DLNAService.AV_TRANSPORT;
    services.add(service);

    ServiceDescription description = Mockito.mock(ServiceDescription.class);
    Mockito.when(description.getServiceList()).thenReturn(services);
    return new DLNAService(description, Mockito.mock(ServiceConfig.class), Robolectric.application, null);
}
 
Example 7
Source File: IterableCursorWrapperTest.java    From cursor-utils with MIT License 5 votes vote down vote up
@Test
public void isEmpty() {
    TestDb db0 = new TestDb(Robolectric.application);
    TestDb db1 = new TestDb(Robolectric.application);

    db0.insertRow(0, 0l, 0f, 0d, (short) 0, true, new byte[]{0, 0}, "0");

    IterableCursorWrapper<Pojo> cursor0 = new PojoCursor(db0.query());
    IterableCursorWrapper<Pojo> cursor1 = new PojoCursor(db1.query());

    assertFalse(cursor0.isEmpty());
    assertTrue(cursor1.isEmpty());
}
 
Example 8
Source File: DataUploadServiceTest.java    From open with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    service = new DataUploadService();
    service.onCreate();
    app = (MapzenApplication) Robolectric.application;
    app.inject(this);
}
 
Example 9
Source File: InjectedTestRunner.java    From Inside_Android_Testing with Apache License 2.0 5 votes vote down vote up
@Override
public void prepareTest(Object test) {
    SampleRoboApplication application = (SampleRoboApplication) Robolectric.application;

    RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
            RoboGuice.newDefaultRoboModule(application), new RobolectricSampleTestModule());

    RoboGuice.getInjector(application).injectMembers(test);
}
 
Example 10
Source File: SSDPDiscoveryProviderTest.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    byte[] data = new byte[1];
    when(ssdpClient.responseReceive()).thenReturn(new DatagramPacket(data, 1));
    when(ssdpClient.multicastReceive()).thenReturn(new DatagramPacket(data, 1));
    dp = new StubSSDPDiscoveryProvider(Robolectric.application);
    assertNotNull(dp);
}
 
Example 11
Source File: MatomoTest.java    From matomo-sdk-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNewTracker() {
    MatomoTestApplication app = (MatomoTestApplication) Robolectric.application;
    Tracker tracker = app.onCreateTrackerConfig().build(Matomo.getInstance(Robolectric.application));
    assertNotNull(tracker);
    assertEquals(app.onCreateTrackerConfig().getApiUrl(), tracker.getAPIUrl());
    assertEquals(app.onCreateTrackerConfig().getSiteId(), tracker.getSiteId());
}
 
Example 12
Source File: ZeroConfDiscoveryPrividerTest.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    dp = new StubZeroConfDiscoveryProvider(Robolectric.application);
    mDNS = mock(StubJmDNS.class);
    eventMock = mock(StubServiceEvent.class);

    dp.jmdns = mDNS;
}
 
Example 13
Source File: ClickGuardTest.java    From clickguard with Apache License 2.0 5 votes vote down vote up
@Test
public void wrappedListenerPreventsMultipleClicks() {
    View view = new View(Robolectric.application);
    CountClickListener listener = new CountClickListener();
    view.setOnClickListener(ClickGuard.wrap(listener));

    clickView(view, 5);
    assertEquals(1, listener.getClickedCount());

    Robolectric.runUiThreadTasksIncludingDelayedTasks();

    clickView(view, 5);
    assertEquals(2, listener.getClickedCount());
}
 
Example 14
Source File: IterableCursorWrapperTest.java    From cursor-utils with MIT License 4 votes vote down vote up
@Test
public void retrievalHelpers() {
    TestDb db = new TestDb(Robolectric.application);

    db.insertRow(1, 1l, 1.1f, 1.2d, (short) 1, true, new byte[]{1, 2, 3}, "a");
    IterableCursorWrapper<?> cursor = new IterableCursorWrapper<Object>(db.query()) {
        @Override
        public Object peek() {
            return null;
        }
    };

    String strFound = cursor.getString("some_str", "not_found");
    assertEquals("a", strFound);
    String strNotFound = cursor.getString("other", "not_found");
    assertEquals("not_found", strNotFound);

    int intFound = cursor.getInteger("some_int", -1);
    assertEquals(1, intFound);
    int intNotFound = cursor.getInteger("other", -1);
    assertEquals(-1, intNotFound);

    long longFound = cursor.getLong("some_long", -1l);
    assertEquals(1l, longFound);
    long longNotFound = cursor.getLong("other", -1l);
    assertEquals(-1l, longNotFound);

    boolean booleanFound = cursor.getBoolean("some_boolean", false);
    assertEquals(true, booleanFound);
    boolean booleanNotFound = cursor.getBoolean("other", false);
    assertEquals(false, booleanNotFound);

    float floatFound = cursor.getFloat("some_float", 2.0f);
    assertEquals(1.1f, floatFound, DELTA);
    float floatNotFound = cursor.getFloat("other", 2.0f);
    assertEquals(2.0f, floatNotFound, DELTA);

    double doubleFound = cursor.getDouble("some_double", 3.0d);
    assertEquals(1.2d, doubleFound, DELTA);
    double doubleNotFound = cursor.getDouble("other", 3.0d);
    assertEquals(3.0d, doubleNotFound, DELTA);

    short shortFound = cursor.getShort("some_short", (short) 4);
    assertEquals((short) 1, shortFound);
    short shortNotFound = cursor.getShort("other", (short) 4);
    assertEquals((short) 4, shortNotFound);

    byte[] blobFound = cursor.getBlob("some_byte_array", new byte[]{4, 5, 6});
    assertTrue(Arrays.equals(new byte[]{1, 2, 3}, blobFound));
    byte[] blobNotFound = cursor.getBlob("other", new byte[]{4, 5, 6});
    assertTrue(Arrays.equals(new byte[]{4, 5, 6}, blobNotFound));
}
 
Example 15
Source File: IterableCursorAdapterTest.java    From cursor-utils with MIT License 4 votes vote down vote up
private TestAdapter(IterableCursor<Object> c) {
    super(Robolectric.application, c, false /* autoRequery */);
}
 
Example 16
Source File: MapControllerTest.java    From open with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void restoreFromSavedLocation_shouldActivateMapLocationUpdates() {
    controller.restoreFromSavedLocation();
    MapzenApplication app = ((MapzenApplication) Robolectric.application);
    assertThat(app.shouldMoveMapToLocation()).isTrue();
}
 
Example 17
Source File: DLNAServiceTest.java    From Connect-SDK-Android-Core with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    dlnaServer = Mockito.mock(DLNAHttpServer.class);
    service = new DLNAService(Mockito.mock(ServiceDescription.class),
            Mockito.mock(ServiceConfig.class), Robolectric.application, dlnaServer);
}
 
Example 18
Source File: DefaultTestCase.java    From matomo-sdk-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Tracker createTracker() {
    MatomoTestApplication app = (MatomoTestApplication) Robolectric.application;
    final Tracker tracker = app.onCreateTrackerConfig().build(Matomo.getInstance(Robolectric.application));
    tracker.getPreferences().edit().clear().apply();
    return tracker;
}
 
Example 19
Source File: IabHelperTest.java    From pay-me with Apache License 2.0 4 votes vote down vote up
private Context registerServiceWithPackageManager() {
    Context context = Robolectric.application;
    RobolectricPackageManager pm = (RobolectricPackageManager) context.getPackageManager();
    pm.addResolveInfoForIntent(IabHelper.BIND_BILLING_SERVICE, new ResolveInfo());
    return context;
}
 
Example 20
Source File: MatomoApplicationTest.java    From matomo-sdk-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testApplication() {
    MatomoApplication matomoApplication = (MatomoApplication) Robolectric.application;
    Assert.assertEquals(matomoApplication.getMatomo(), Matomo.getInstance(matomoApplication));
}