package org.wikimedia.wikipedia.test.concurrency;

import android.content.Intent;
import android.test.ActivityUnitTestCase;
import org.wikimedia.wikipedia.concurrency.ExceptionHandlingAsyncTask;
import org.wikimedia.wikipedia.test.TestDummyActivity;

import java.util.concurrent.*;

public class AsyncTaskTest extends ActivityUnitTestCase<TestDummyActivity> {
    public static final int TASK_COMPLETION_TIMEOUT = 1000;
    private Executor executor;

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

    private Executor getDefaultExecutor() {
        if (executor == null) {
            executor = new ScheduledThreadPoolExecutor(1);
        }
        return executor;
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        startActivity(new Intent(), null, null);
    }

    public void testFinishHandling() throws Exception {
        final CountDownLatch onFinishLatch = new CountDownLatch(1);
        final Integer returned = 42;
        new ExceptionHandlingAsyncTask<Integer>(getDefaultExecutor()) {
            @Override
            public void onFinish(Integer result) {
                assertEquals(returned, result);
                onFinishLatch.countDown();
            }

            @Override
            public void onCatch(Throwable caught) {
                assertTrue("Exception called despite success", false);
            }

            @Override
            public Integer performTask() throws Throwable {
                return returned;
            }
        }.execute();
        assertTrue(onFinishLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }

    public void testExceptionHandling() throws Exception {
        final CountDownLatch exceptionLatch = new CountDownLatch(1);
        final Throwable thrown = new Exception();
        new ExceptionHandlingAsyncTask<Void>(getDefaultExecutor()) {
            @Override
            public void onFinish(Void result) {
                assertTrue("onFinish called despite exception", false);
            }

            @Override
            public void onCatch(Throwable caught) {
                assertSame(caught, thrown);
                exceptionLatch.countDown();
            }

            @Override
            public Void performTask() throws Throwable {
                throw thrown;
            }
        }.execute();
        assertTrue(exceptionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }

    public void testAppropriateThreadFinish() throws Throwable {
        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                final Thread callingThread = Thread.currentThread();
                new ExceptionHandlingAsyncTask<Thread>(getDefaultExecutor()) {
                    @Override
                    public void onBeforeExecute() {
                        assertSame(callingThread, Thread.currentThread());
                    }

                    @Override
                    public void onFinish(Thread result) {
                        assertNotSame(result, Thread.currentThread());
                        assertSame(Thread.currentThread(), callingThread);
                        completionLatch.countDown();
                    }

                    @Override
                    public Thread performTask() throws Throwable {
                        assertNotSame(callingThread, Thread.currentThread());
                        return Thread.currentThread();
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}

@@ -874,33 +874,33 @@
 ng() throws 
-Exception
+Throwable
  %7B%0A        f
@@ -995,16 +995,121 @@
 d = 42;%0A
+        runTestOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         
@@ -1176,32 +1176,40 @@
 ) %7B%0A            
+  
+      
 @Override%0A      
@@ -1194,32 +1194,40 @@
       @Override%0A
+        
             publ
@@ -1273,24 +1273,32 @@
             
+ 
+       
 assertEquals
@@ -1333,16 +1333,24 @@
         
+        
 onFinish
@@ -1372,35 +1372,51 @@
 ();%0A            
+ 
-%7D%0A%0A
+       %7D%0A%0A        
             @Ove
@@ -1425,32 +1425,40 @@
 ide%0A            
+        
 public void onCa
@@ -1473,32 +1473,40 @@
 wable caught) %7B%0A
+        
                 
@@ -1564,35 +1564,49 @@
 e);%0A            
-%7D%0A%0A
+        %7D%0A%0A      
             @Ove
@@ -1593,32 +1593,34 @@
                 
+  
 @Override%0A      
@@ -1605,32 +1605,40 @@
       @Override%0A
+        
             publ
@@ -1693,24 +1693,32 @@
             
+        
 return retur
@@ -1730,34 +1730,46 @@
             
-%7D%0A
+        %7D%0A    
         
 %7D.execute();
@@ -1748,32 +1748,36 @@
   %7D%0A            
+    
 %7D.execute();%0A   
@@ -1765,32 +1765,58 @@
    %7D.execute();%0A
+            %7D%0A        %7D);%0A
         assertTr
@@ -1938,33 +1938,33 @@
 ng() throws 
-Exception
+Throwable
  %7B%0A        f
@@ -2069,24 +2069,129 @@
 xception();%0A
+        runTestOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         new 
@@ -2251,32 +2251,40 @@
 ) %7B%0A            
+  
+      
 @Override%0A      
@@ -2269,32 +2269,40 @@
       @Override%0A
+        
             publ
@@ -2341,32 +2341,40 @@
                 
+ 
+       
 assertTrue(%22onFi
@@ -2417,35 +2417,47 @@
 e);%0A            
-%7D%0A%0A
+        %7D%0A%0A    
             @Ove
@@ -2444,32 +2444,36 @@
                 
+    
 @Override%0A      
@@ -2458,32 +2458,40 @@
       @Override%0A
+        
             publ
@@ -2534,32 +2534,40 @@
                 
+        
 assertSame(caugh
@@ -2594,16 +2594,24 @@
         
+       
+ 
 exceptio
@@ -2634,35 +2634,49 @@
 ();%0A            
-%7D%0A%0A
+        %7D%0A%0A      
             @Ove
@@ -2663,32 +2663,34 @@
                 
+  
 @Override%0A      
@@ -2675,32 +2675,40 @@
       @Override%0A
+        
             publ
@@ -2764,16 +2764,24 @@
         
+        
 throw th
@@ -2798,26 +2798,38 @@
         
-%7D%0A
+        %7D%0A    
         
 %7D.execut
@@ -2812,32 +2812,36 @@
   %7D%0A            
+    
 %7D.execute();%0A   
@@ -2829,32 +2829,58 @@
    %7D.execute();%0A
+            %7D%0A        %7D);%0A
         assertTr

Make all AsyncTask based tests run on the UI Thread