package org.wikipedia.test;

import android.content.Intent;
import android.test.ActivityUnitTestCase;
import org.wikipedia.PageTitle;
import org.wikipedia.Site;
import org.wikipedia.page.Section;
import org.wikipedia.page.SectionsFetchTask;

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

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

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

    public void testPageFetch() throws Throwable {
        final CountDownLatch completionLatch = new CountDownLatch(1);
        startActivity(new Intent(), null, null);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                new SectionsFetchTask(getInstrumentation().getTargetContext(), new PageTitle(null, "Test_page_for_app_testing/Section1", new Site("test.wikipedia.org")), "all") {
                    @Override
                    public void onFinish(List<Section> result) {
                        assertNotNull(result);
                        assertEquals(4, result.size());
                        completionLatch.countDown();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}

@@ -1438,10 +1438,955 @@
 ;%0A    %7D%0A
+%0A%0A    /** Inspired by https://bugzilla.wikimedia.org/show_bug.cgi?id=66152 */%0A    public void testPageFetchWithAmpersand() throws Throwable %7B%0A        final CountDownLatch completionLatch = new CountDownLatch(1);%0A        startActivity(new Intent(), null, null);%0A        runTestOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                new SectionsFetchTask(getInstrumentation().getTargetContext(), new PageTitle(null, %22Ampersand & title%22, new Site(%22test.wikipedia.org%22)), %22all%22) %7B%0A                    @Override%0A                    public void onFinish(List%3CSection%3E result) %7B%0A                        assertNotNull(result);%0A                        assertEquals(1, result.size());%0A                        completionLatch.countDown();%0A                    %7D%0A                %7D.execute();%0A            %7D%0A        %7D);%0A        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));%0A    %7D%0A
 %7D%0A

Add test for "Open page with ampersand in title caused network issues"