package org.wikipedia.test;

import android.support.annotation.NonNull;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.wikipedia.Site;
import org.wikipedia.editing.EditPreviewTask;
import org.wikipedia.page.PageTitle;
import org.wikipedia.testlib.TestLatch;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

@RunWith(AndroidJUnit4.class)
public class PreviewTaskTests {
    @Test
    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>";

        String result = Subject.execute(wikiText, title);
        assertThat(result, is(expected));
    }

    private static class Subject extends EditPreviewTask {
        public static String execute(String wikiText, PageTitle title) {
            Subject subject = new Subject(wikiText, title);
            subject.execute();
            return subject.await();
        }

        @NonNull private final TestLatch latch = new TestLatch();
        private String result;

        Subject(String wikiText, PageTitle title) {
            super(getTargetContext(), wikiText, title);
        }

        @Override
        public void onFinish(String result) {
            super.onFinish(result);
            this.result = result;
            latch.countDown();
        }

        public String await() {
            latch.await();
            return result;
        }
    }
}
@@ -484,18 +484,31 @@
 est.
-core.Is.is
+Matchers.containsString
 ;%0A%0A@
@@ -920,405 +920,8 @@
 ime;
-%0A        final String expected = %22%3Cdiv%3E%3C/div%3E%3Ch2%3E%3Cspan class=%5C%22mw-headline%5C%22 id=%5C%22Section_2%5C%22%3ESection 2%3C/span%3E%3Cspan%3E%3Ca href=%5C%22#/editor/1%5C%22 title=%5C%22Edit section: Section 2%5C%22 data-section=%5C%221%5C%22 class=%5C%22mw-ui-icon mw-ui-icon-element mw-ui-icon-edit-enabled edit-page icon-32px%5C%22%3EEdit%3C/a%3E%3C/span%3E%3C/h2%3E%3Cdiv%3E%5Cn%3Cp%3EEditing section INSERT RANDOM &amp; HERE test at %22 + randomTime + %22%3C/p%3E%5Cn%5Cn%5Cn%5Cn%5Cn%5Cn%3C/div%3E%22;
 %0A%0A  
@@ -1007,19 +1007,49 @@
 lt, 
-is(expected
+containsString(String.valueOf(randomTime)
 ));%0A

Fix PreviewTaskTests