package org.wikipedia.test;

import android.test.ActivityUnitTestCase;

import org.wikipedia.Site;
import org.wikipedia.WikipediaApp;
import org.wikipedia.editing.EditTokenStorage;
import org.wikipedia.login.LoginResult;
import org.wikipedia.login.LoginTask;

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

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

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

    public void testLogin() throws Throwable {
        final Site testWiki = new Site("test.wikipedia.org");
        final String username = getInstrumentation().getContext().getString(R.string.test_username);
        final String password = getInstrumentation().getContext().getString(R.string.test_password);
        final WikipediaApp app = (WikipediaApp)getInstrumentation().getTargetContext().getApplicationContext();

        final CountDownLatch completionLatch = new CountDownLatch(1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                new LoginTask(getInstrumentation().getTargetContext(), testWiki, username, password) {
                    @Override
                    public void onFinish(LoginResult result) {
                        super.onFinish(result);
                        assertNotNull(result);
                        assertEquals(result.getCode(), "Success");
                        app.getEditTokenStorage().get(testWiki, new EditTokenStorage.TokenRetrievedCallback() {
                            @Override
                            public void onTokenRetrieved(String token) {
                                assertNotNull(token);
                                assertFalse(token.equals("+\\"));
                                completionLatch.countDown();
                            }

                            @Override
                            public void onTokenFailed(Throwable caught) {
                                fail("onTokenFailed called: " + caught);
                            }
                        });
                    }
                }.execute();
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}
@@ -41,35 +41,144 @@
 oid.
-test.ActivityU
+support.annotation.StringRes;%0Aimport android.support.test.runner.AndroidJUnit4;%0A%0Aimport org.ju
 nit
+.
 Test
-Case;%0A
+;%0Aimport org.junit.runner.RunWith;
 %0Aimp
@@ -365,584 +365,587 @@
 sk;%0A
-%0A
 import 
-java.util.concurren
+org.wikipedia.testlib.TestLatch;%0A%0Aimport static org.hamcres
 t.Co
-untDownLatch;%0Aimport java.util.concurrent.TimeUnit;%0A%0Apublic class LoginTaskTest extends ActivityUnitTestCase%3CTestDummyActivity%3E %7B%0A    private static final int TASK_COMPLETION_TIMEOUT = 20000;%0A%0A    public LoginTaskTest() %7B%0A        super(TestDummyActivity
+reMatchers.equalTo;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static android.support.test.InstrumentationRegistry.getInstrumentation;%0A%0A@RunWith(AndroidJUnit4
 .class)
-;%0A    %7D%0A%0A    public void testLogin() throws Throwable %7B%0A        final Site testWiki = new Site(%22test.wikipedia.org%22);%0A        final String username = getInstrumentation().getContext().getString(R.string.test_username);%0A        final String password = getInstrumentation().getContext().
+%0Apublic class LoginTaskTest %7B%0A    private static final Site TEST_WIKI_SITE = new Site(%22test.wikipedia.org%22);%0A    private static final String SUCCESS = %22Success%22;%0A    private static final String USERNAME = getString(R.string.test_username);%0A    private static final String PASSWORD = 
 getS
@@ -975,23 +975,28 @@
 sword);%0A
+%0A
     
-   
+private
  final W
@@ -1013,17 +1013,16 @@
 p app = 
-(
 Wikipedi
@@ -1029,98 +1029,46 @@
 aApp
-)
+.
 getInst
-rumentation().getTargetContext().getApplicationContext();%0A%0A        final CountDown
+ance();%0A    private final Test
 Latc
@@ -1095,52 +1095,100 @@
 new 
-CountDown
+Test
 Latch(
-1
 );%0A
+%0A
     
-    runTestOnUiThread
+@Test%0A    public void testLogin() throws Throwable %7B%0A        runOnMainSync
 (new
@@ -1275,109 +1275,202 @@
     
-new LoginTask(getInstrumentation().getTargetContext(), testWiki, username, password) %7B%0A      
+loginTestTask.execute();%0A            %7D%0A        %7D);%0A        completionLatch.await();%0A    %7D%0A%0A    private LoginTask loginTestTask = new LoginTask(app, TEST_WIKI_SITE, USERNAME, PASSWORD) %7B%0A
         
     
@@ -1457,38 +1457,32 @@
 WORD) %7B%0A        
-      
 @Override%0A      
@@ -1479,36 +1479,24 @@
 ide%0A        
-            
 public void 
@@ -1522,36 +1522,24 @@
 t result) %7B%0A
-            
             
@@ -1578,107 +1578,122 @@
     
-            
 assert
-NotNull(result);%0A                        assertEquals(result.getCode(), %22Success%22
+That(result.getCode(), equalTo(SUCCESS));%0A            app.getEditTokenStorage().get(TEST_WIKI_SITE, callback
 );%0A 
@@ -1703,63 +1703,80 @@
     
+%7D%0A
     
-            app.getEditTokenStorage().get(testWiki,
+%7D;%0A%0A    private EditTokenStorage.TokenRetrievedCallback callback =
  new
@@ -1832,58 +1832,18 @@
     
-                    @Override%0A                    
+@Override%0A
     
@@ -1907,616 +1907,459 @@
     
-                    assertNotNull(token);%0A                                assertFalse(token.equals(%22+%5C%5C%22));%0A                                completionLatch.countDown();%0A                            %7D%0A%0A                            @Override%0A                            public void onTokenFailed(Throwable caught) %7B%0A                                fail(%22onTokenFailed called: %22 + caught);%0A                            %7D%0A                        %7D);%0A                    %7D%0A                %7D.execute();%0A            %7D%0A        %7D);%0A        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS)
+assertThat(token.equals(%22+%5C%5C%22), is(false));%0A            completionLatch.countDown();%0A        %7D%0A%0A        @Override%0A        public void onTokenFailed(Throwable caught) %7B%0A            throw new RuntimeException(caught);%0A        %7D%0A    %7D;%0A%0A    private void runOnMainSync(Runnable r) %7B%0A        getInstrumentation().runOnMainSync(r);%0A    %7D%0A%0A    private static String getString(@StringRes int id) %7B%0A        return getInstrumentation().getContext().getString(id
 );%0A 

Refactor LoginTaskTest