Search in sources :

Example 1 with TestLatch

use of org.wikipedia.testlib.TestLatch in project apps-android-wikipedia by wikimedia.

the class SaneAsyncTaskTest method testAppropriateThreadException.

@Test
public void testAppropriateThreadException() {
    final TestLatch latch = new TestLatch();
    runOnMainSync(() -> new SaneAsyncTask<Void>() {

        @Override
        public void onBeforeExecute() {
            assertUiThread();
        }

        @Override
        public void onFinish(Void result) {
            fail("onFinish called even when there is an exception");
        }

        @Override
        public void onCatch(Throwable caught) {
            assertUiThread();
            latch.countDown();
        }

        @Override
        public Void performTask() throws Throwable {
            assertNotUiThread();
            throw new Exception();
        }
    }.execute());
    latch.await();
}
Also used : TestLatch(org.wikipedia.testlib.TestLatch) Test(org.junit.Test)

Example 2 with TestLatch

use of org.wikipedia.testlib.TestLatch in project apps-android-wikipedia by wikimedia.

the class SaneAsyncTaskTest method testFinishHandling.

@Test
public void testFinishHandling() {
    final TestLatch latch = new TestLatch();
    final Integer returned = 42;
    runOnMainSync(() -> new SaneAsyncTask<Integer>() {

        @Override
        public void onFinish(Integer result) {
            assertThat(returned, is(result));
            latch.countDown();
        }

        @Override
        public void onCatch(Throwable caught) {
            fail("Exception called despite success");
        }

        @Override
        public Integer performTask() throws Throwable {
            return returned;
        }
    }.execute());
    latch.await();
}
Also used : TestLatch(org.wikipedia.testlib.TestLatch) Test(org.junit.Test)

Example 3 with TestLatch

use of org.wikipedia.testlib.TestLatch in project apps-android-wikipedia by wikimedia.

the class MwMobileViewPageLeadTest method testThumbUrls.

@Test
@SuppressWarnings("checkstyle:magicnumber")
public void testThumbUrls() throws Throwable {
    enqueueFromFile("page_lead_mw.json");
    final TestLatch latch = new TestLatch();
    subject.lead(CacheControl.FORCE_NETWORK, null, "foo", 640).enqueue(new Callback<PageLead>() {

        @Override
        public void onResponse(@NonNull Call<PageLead> call, @NonNull Response<PageLead> response) {
            assertThat(response.body().getLeadImageUrl(640).contains("640px"), is(true));
            assertThat(response.body().getThumbUrl().contains(preferredThumbSizeString()), is(true));
            assertThat(response.body().getDescription(), is("Mexican boxer"));
            latch.countDown();
        }

        @Override
        public void onFailure(@NonNull Call<PageLead> call, @NonNull Throwable t) {
            fail();
            latch.countDown();
        }
    });
    latch.await();
}
Also used : TestLatch(org.wikipedia.testlib.TestLatch) PageLead(org.wikipedia.dataclient.page.PageLead) BasePageLeadTest(org.wikipedia.dataclient.page.BasePageLeadTest) Test(org.junit.Test)

Example 4 with TestLatch

use of org.wikipedia.testlib.TestLatch in project apps-android-wikipedia by wikimedia.

the class RbPageLeadTest method testThumbUrls.

@Test
@SuppressWarnings("checkstyle:magicnumber")
public void testThumbUrls() throws Throwable {
    enqueueFromFile("page_lead_rb.json");
    final TestLatch latch = new TestLatch();
    subject.lead(CacheControl.FORCE_NETWORK, null, "foo", 640).enqueue(new Callback<PageLead>() {

        @Override
        public void onResponse(@NonNull Call<PageLead> call, @NonNull Response<PageLead> response) {
            assertThat(response.body().getLeadImageUrl(640).contains("640px"), is(true));
            assertThat(response.body().getThumbUrl().contains(preferredThumbSizeString()), is(true));
            assertThat(response.body().getDescription(), is("Mexican boxer"));
            latch.countDown();
        }

        @Override
        public void onFailure(@NonNull Call<PageLead> call, @NonNull Throwable t) {
            fail();
            latch.countDown();
        }
    });
    latch.await();
}
Also used : TestLatch(org.wikipedia.testlib.TestLatch) MwMobileViewPageLead(org.wikipedia.dataclient.mwapi.page.MwMobileViewPageLead) PageLead(org.wikipedia.dataclient.page.PageLead) BasePageLeadTest(org.wikipedia.dataclient.page.BasePageLeadTest) Test(org.junit.Test)

Example 5 with TestLatch

use of org.wikipedia.testlib.TestLatch in project apps-android-wikipedia by wikimedia.

the class BatchUtilTest method testMwApiBatches.

@Test
@SuppressWarnings("checkstyle:magicnumber")
public void testMwApiBatches() throws Throwable {
    final TestLatch latch = new TestLatch();
    BatchUtil.makeBatches(titles, new BatchUtil.Handler<Integer>() {

        private List<Integer> sizes = new ArrayList<>();

        private int count;

        @Override
        public void handleBatch(@NonNull List<PageTitle> batchTitles, int total, BatchUtil.Callback<Integer> cb) {
            sizes.add(batchTitles.size());
            count += batchTitles.size();
            if (count == TOTAL) {
                assertThat(sizes.get(0), is(50));
                assertThat(sizes.get(1), is(50));
                assertThat(sizes.get(2), is(20));
                latch.countDown();
            }
        }
    }, null);
    latch.await();
}
Also used : TestLatch(org.wikipedia.testlib.TestLatch) ArrayList(java.util.ArrayList) PageTitle(org.wikipedia.page.PageTitle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 TestLatch (org.wikipedia.testlib.TestLatch)7 BasePageLeadTest (org.wikipedia.dataclient.page.BasePageLeadTest)2 PageLead (org.wikipedia.dataclient.page.PageLead)2 ArrayList (java.util.ArrayList)1 MwMobileViewPageLead (org.wikipedia.dataclient.mwapi.page.MwMobileViewPageLead)1 PageTitle (org.wikipedia.page.PageTitle)1