package org.wikipedia.editing;

import android.support.test.filters.SmallTest;
import android.test.ActivityUnitTestCase;

import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.page.PageTitle;
import org.wikipedia.test.TestDummyActivity;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@SmallTest
public class FetchSectionWikitextTaskTest extends ActivityUnitTestCase<TestDummyActivity> {
    private static final int TASK_COMPLETION_TIMEOUT = 20_000;

    public FetchSectionWikitextTaskTest() {
        super(TestDummyActivity.class);
    }

    public void testPageFetch() throws Throwable {
        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                PageTitle title = new PageTitle(null, "Test_page_for_app_testing/Section1", WikiSite.forLanguageCode("test"));
                new FetchSectionWikitextTask(getInstrumentation().getTargetContext(), title, 2) {
                    @Override
                    public void onFinish(String result) {
                        assertNotNull(result);
                        assertEquals(result, "=== Section1.2 ===\nThis is a subsection");
                        completionLatch.countDown();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}


@@ -76,50 +76,31 @@
 st;%0A
+%0A
 import 
-android.test.ActivityU
+org.ju
 nit
+.
 Test
-Case;%0A
+;
 %0Aimp
@@ -204,289 +204,327 @@
 test
+lib
 .Test
-DummyActivity;%0A%0Aimport java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.TimeUnit;%0A%0A@SmallTest%0Apublic class FetchSectionWikitextTaskTest extends ActivityUnitTestCase%3CTestDummyActivity%3E %7B%0A    priva
+Latch;%0A%0Aimport static android.support.test.InstrumentationRegistry.getInstrumentation;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.notNullValue;%0Aimport static org.wikipedia.
 te
- 
 st
-atic final int TASK_COMPLETION_TIMEOUT = 20_000;%0A%0A    public
+.TestUtil.runOnMainSync;%0A%0A@SmallTest%0Apublic class
  Fet
@@ -552,151 +552,76 @@
 Test
-()
  %7B%0A    
-    super(TestDummyActivity.class);%0A    %7D%0A%0A    public void testPageFetch() throws Throwable %7B%0A        final CountDownLatch completionL
+@Test public void testPageFetch() %7B%0A        final TestLatch l
 atch
@@ -631,24 +631,18 @@
 new 
-CountDown
+Test
 Latch(
-1
 );%0A 
@@ -655,22 +655,18 @@
  run
-TestOnUiThread
+OnMainSync
 (new
@@ -1080,22 +1080,35 @@
 sert
-NotNull(result
+That(result, notNullValue()
 );%0A 
@@ -1140,14 +1140,12 @@
 sert
-Equals
+That
 (res
@@ -1149,16 +1149,19 @@
 result, 
+is(
 %22=== Sec
@@ -1195,16 +1195,17 @@
 ection%22)
+)
 ;%0A      
@@ -1222,27 +1222,17 @@
         
-completionL
+l
 atch.cou
@@ -1330,88 +1330,20 @@
     
-assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS)
+latch.await(
 );%0A 
@@ -1348,10 +1348,8 @@
 %0A    %7D%0A%7D
-%0A%0A

update FetchSectionWikitextTaskTest