
package org.wikipedia.test;

import org.wikipedia.page.PageTitle;
import org.wikipedia.Site;
import org.wikipedia.editing.EditPreviewTask;
import android.test.ActivityUnitTestCase;

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

public class PreviewTaskTests extends ActivityUnitTestCase<TestDummyActivity> {
    private static final int TASK_COMPLETION_TIMEOUT = 20000;

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

    public void testPreview() throws Throwable {
        final PageTitle title = new PageTitle(null, "Test_page_for_app_testing/Section1", new Site("test.wikipedia.org"));
        long randomTime = System.currentTimeMillis();
        final String wikitext = "== Section 2 ==\n\nEditing section INSERT RANDOM & HERE test at " + randomTime;
        final String expected = "<div></div><h2><span class=\"mw-headline\" id=\"Section_2\">Section 2</span><span><a href=\"#/editor/1\" title=\"Edit section: Section 2\" data-section=\"1\" class=\"mw-ui-icon mw-ui-icon-element mw-ui-icon-edit-enabled edit-page icon-32px\">Edit</a></span></h2><div>\n<p>Editing section INSERT RANDOM &amp; HERE test at " + randomTime + "</p>\n\n\n\n\n\n</div>";
        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                new EditPreviewTask(getInstrumentation().getTargetContext(), wikitext, title) {
                    @Override
                    public void onFinish(String result) {
                        assertNotNull(result);
                        assertEquals(result, expected);
                        completionLatch.countDown();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}

@@ -1,9 +1,8 @@
-%0A
 package 
@@ -33,36 +33,149 @@
 ort 
-org.wikipedia.page.PageTitle
+android.support.annotation.NonNull;%0Aimport android.support.test.runner.AndroidJUnit4;%0A%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith
 ;%0Aim
@@ -256,280 +256,291 @@
 ort 
-android.test.ActivityUnitTestCas
+org.wikipedia.page.PageTitl
 e;%0A
-%0A
 import 
-java.util.concurrent.CountDown
+org.wikipedia.testlib.Test
 Latch;%0A
+%0A
 import 
-java.util.concurrent.TimeUnit;%0A%0Apublic class PreviewTaskTests extends ActivityUnitTestCase%3CTestDummyActivity%3E %7B%0A    private static final int TASK_COMPLETION_TIMEOUT = 20000;%0A%0A    public
+static android.support.test.InstrumentationRegistry.getTargetContext;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.core.Is.is;%0A%0A@RunWith(AndroidJUnit4.class)%0Apublic class
  Pre
@@ -556,59 +556,20 @@
 ests
-()
  %7B%0A    
-    super(TestDummyActivity.class);%0A    %7D%0A
+@Test
 %0A   
@@ -816,17 +816,17 @@
 ing wiki
-t
+T
 ext = %22=
@@ -1301,24 +1301,25 @@
 /div%3E%22;%0A
+%0A
         
 final Co
@@ -1314,134 +1314,298 @@
     
-final CountDownLatch completionLatch = new CountDownLatch(1);%0A        runTestOnUiThread(new Runnable() %7B%0A            @Override
+String result = Subject.execute(wikiText, title);%0A        assertThat(result, is(expected));%0A    %7D%0A%0A    private static class Subject extends EditPreviewTask %7B%0A        public static String execute(String wikiText, PageTitle title) %7B%0A            Subject subject = new Subject(wikiText, title);
 %0A   
@@ -1617,27 +1617,62 @@
     
-public void run() %7B
+subject.execute();%0A            return subject.await();
 %0A   
@@ -1676,24 +1676,27 @@
         
+%7D%0A%0A
         
 new Edit
@@ -1691,49 +1691,168 @@
     
-new EditPreviewTask(getInstrumentation().
+@NonNull private final TestLatch latch = new TestLatch();%0A        private String result;%0A%0A        Subject(String wikiText, PageTitle title) %7B%0A            super(
 getT
@@ -1871,17 +1871,17 @@
 (), wiki
-t
+T
 ext, tit
@@ -1883,29 +1883,26 @@
 , title)
- %7B%0A  
+;%0A
         
         
@@ -1885,34 +1885,35 @@
 title);%0A        
-  
+%7D%0A%0A
         @Overrid
@@ -1914,28 +1914,16 @@
 verride%0A
-            
         
@@ -1976,33 +1976,22 @@
     
-            assertNotNull
+super.onFinish
 (res
@@ -2012,87 +2012,43 @@
     
-            assertEquals(result, expected);%0A                        completionL
+this.result = result;%0A            l
 atch
@@ -2065,32 +2065,35 @@
 ();%0A        
+%7D%0A%0A
         
     %7D%0A      
@@ -2084,164 +2084,100 @@
     
-    %7D%0A                %7D.execute();%0A            %7D%0A        %7D);%0A        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
+public String await() %7B%0A            latch.await();%0A            return result;%0A        %7D
 %0A    %7D%0A%7D
 %0A
@@ -2175,9 +2175,8 @@
 %0A    %7D%0A%7D
-%0A

refactor PreviewTaskTests