package org.wikipedia.test;

import android.content.Intent;
import android.test.ActivityUnitTestCase;
import org.wikipedia.PageTitle;
import org.wikipedia.Site;
import org.wikipedia.WikipediaApp;
import org.wikipedia.editing.AbuseFilterEditResult;
import org.wikipedia.editing.DoEditTask;
import org.wikipedia.editing.EditingResult;

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

public class TriggerAbuseFilterTest extends ActivityUnitTestCase<TestDummyActivity> {
    // TODO: Document the tests in this class.
    // TODO: Add links to filters that are triggered so you can check for changes if the test fails.
    private static final int TASK_COMPLETION_TIMEOUT = 20000;

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

    public void testAbuseFilterTriggerWarn() throws Throwable {
        startActivity(new Intent(), null, null);
        final PageTitle title = new PageTitle(null, "User:Yuvipandaaaaaaaa", new Site("test.wikipedia.org"));
        final String wikitext = "Testing Abusefilter by simply editing this page. Triggering rule 94 at " + System.currentTimeMillis();
        final WikipediaApp app = (WikipediaApp) getInstrumentation().getTargetContext().getApplicationContext();
        app.getEditTokenStorage().clearAllTokens();
        app.getCookieManager().clearAllCookies();
        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                new DoEditTask(getInstrumentation().getTargetContext(), title, wikitext, 0, "+\\", "") {
                    @Override
                    public void onFinish(EditingResult result) {
                        assertNotNull(result);
                        assertTrue(result instanceof AbuseFilterEditResult);
                        assertEquals(((AbuseFilterEditResult) result).getType(), AbuseFilterEditResult.TYPE_WARNING);
                        completionLatch.countDown();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }

    public void testAbuseFilterTriggerStop() throws Throwable {
        startActivity(new Intent(), null, null);
        final PageTitle title = new PageTitle(null, "Test_page_for_app_testing/AbuseFilter", new Site("test.wikipedia.org"));
        final String wikitext = "== Section 2 ==\n\nTriggering AbuseFilter number 2 by saying poop many times at " + System.currentTimeMillis();
        final WikipediaApp app = (WikipediaApp) getInstrumentation().getTargetContext().getApplicationContext();
        app.getEditTokenStorage().clearAllTokens();
        app.getCookieManager().clearAllCookies();
        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                new DoEditTask(getInstrumentation().getTargetContext(), title, wikitext, 0, "+\\", "") {
                    @Override
                    public void onFinish(EditingResult result) {
                        assertNotNull(result);
                        assertTrue(result instanceof AbuseFilterEditResult);
                        assertEquals(((AbuseFilterEditResult) result).getType(), AbuseFilterEditResult.TYPE_ERROR);
                        completionLatch.countDown();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }

    /**
     * Test this filter: https://test.wikipedia.org/wiki/Special:AbuseFilter/152
     * TODO: Improve this documentation so that it doesn't just make sense to the method's author.
     * @throws Throwable
     */
    public void testAbuseFilterTriggerStopOnArbitraryErrorCode() throws Throwable {
        startActivity(new Intent(), null, null);
        final PageTitle title = new PageTitle(null, "Test_page_for_app_testing/AbuseFilter", new Site("test.wikipedia.org"));
        final String wikitext = "== Section 2 ==\n\nTriggering AbuseFilter number 152 by saying appcrashtest many times at " + System.currentTimeMillis();
        final WikipediaApp app = (WikipediaApp) getInstrumentation().getTargetContext().getApplicationContext();
        app.getEditTokenStorage().clearAllTokens();
        app.getCookieManager().clearAllCookies();
        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                new DoEditTask(getInstrumentation().getTargetContext(), title, wikitext, 0, "+\\", "") {
                    @Override
                    public void onFinish(EditingResult result) {
                        assertNotNull(result);
                        assertTrue(result instanceof AbuseFilterEditResult);
                        assertEquals(((AbuseFilterEditResult) result).getType(), AbuseFilterEditResult.TYPE_ERROR);
                        completionLatch.countDown();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}
@@ -504,299 +504,456 @@
     
-// TODO: Document the tests in this class.%0A    // TODO: Add links to filters that are triggered so you can check for changes if the test fails.%0A    private static final int TASK_COMPLETION_TIMEOUT = 20000;%0A%0A    public TriggerAbuseFilterTest() %7B%0A        super(TestDummyActivity.class);%0A    %7D%0A
+private static final int TASK_COMPLETION_TIMEOUT = 20000;%0A%0A    public TriggerAbuseFilterTest() %7B%0A        super(TestDummyActivity.class);%0A    %7D%0A%0A    /**%0A     * Test handling of abuse filter warnings which warn users about making edits of a certain kind.%0A     *%0A     * Type:   warn%0A     * Action: editing any userspace page while logged out%0A     * Filter: https://test.wikipedia.org/wiki/Special:AbuseFilter/94%0A     *%0A     * @throws Throwable%0A     */
 %0A   
@@ -2356,24 +2356,316 @@
 S));%0A    %7D%0A%0A
+    /**%0A     * Test handling of abuse filter errors which completely disallow edits of a certain kind.%0A     *%0A     * Type:   disallow%0A     * Action: adding string %22poop%22 to page text%0A     * Filter: https://test.wikipedia.org/wiki/Special:AbuseFilter/2%0A     *%0A     * @throws Throwable%0A     */%0A
     public v
@@ -4112,173 +4112,238 @@
 t th
-is filter: https://test.wikipedia.org/wiki/Special:AbuseFilter/152%0A     * TODO: Improve this documentation so that it doesn't just make sense to the method's author.
+e app's handling of the abuse filter emitting arbitrary error codes.%0A     *%0A     * Type:   warn%0A     * Action: adding string %22appcrashtest%22 to page text%0A     * Filter: https://test.wikipedia.org/wiki/Special:AbuseFilter/152%0A     *
 %0A   
@@ -5484,32 +5484,131 @@
 terEditResult);%0A
+                        // For now we handle arbitrary error codes as TYPE_ERROR. This may change.%0A
                 

Add documentation to TriggerAbuseFilterTest class.