package org.wikipedia.test;

import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import android.test.UiThreadTest;

import org.wikipedia.WikipediaApp;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.language.AppLanguageLookUpTable;

import java.util.Locale;

@SmallTest
public class LanguageVariantTests extends InstrumentationTestCase {
    private Locale defaultLocale;
    private String appLanguage;

    /** Ensure that the more specific dialect is first in the list. */
    @UiThreadTest
    public void testDefaultLocaleAndAcceptLanguageAgree() throws Throwable {
        preserveAppState();

        testDefaultLocaleAndAcceptLanguageAgree("zh,zh-hant;q=0.8", "zh",
                Locale.TRADITIONAL_CHINESE);
        testDefaultLocaleAndAcceptLanguageAgree("zh,zh-hans;q=0.8", "zh",
                Locale.SIMPLIFIED_CHINESE);
        testDefaultLocaleAndAcceptLanguageAgree("zh,en;q=0.8", "zh", Locale.US);
        testDefaultLocaleAndAcceptLanguageAgree("zh,en;q=0.8", "zh", Locale.ENGLISH);
        testDefaultLocaleAndAcceptLanguageAgree("en,zh-hans;q=0.8", "en",
                Locale.SIMPLIFIED_CHINESE);
        testDefaultLocaleAndAcceptLanguageAgree("test,zh-hans;q=0.8", "test",
                Locale.SIMPLIFIED_CHINESE);
        testDefaultLocaleAndAcceptLanguageAgree("es,zh-hans;q=0.9,zh-hant;q=0.8",
                AppLanguageLookUpTable.SIMPLIFIED_CHINESE_LANGUAGE_CODE,
                Locale.TRADITIONAL_CHINESE, WikiSite.forLanguageCode("es"));
        testDefaultLocaleAndAcceptLanguageAgree("zh-hant",
                AppLanguageLookUpTable.TRADITIONAL_CHINESE_LANGUAGE_CODE,
                Locale.TRADITIONAL_CHINESE);

        restoreAppState();
    }

    private void testDefaultLocaleAndAcceptLanguageAgree(String expected, String appLanguage,
            Locale systemLocale) {
        testDefaultLocaleAndAcceptLanguageAgree(expected, appLanguage, systemLocale, null);
    }

    private void testDefaultLocaleAndAcceptLanguageAgree(String expected,
             String appLanguage, Locale systemLocale, WikiSite wiki) {
        WikipediaApp.getInstance().setAppLanguageCode(appLanguage);
        Locale.setDefault(systemLocale);
        assertEquals(expected, WikipediaApp.getInstance().getAcceptLanguage(wiki));
    }

    private void preserveAppState() {
        preserveDefaultLocale();
        preserveAppLanguage();
    }

    private void restoreAppState() {
        restoreAppLanguage();
        restoreDefaultLocale();
    }

    private void preserveAppLanguage() {
        appLanguage = WikipediaApp.getInstance().getAppLanguageCode();
    }

    private void restoreAppLanguage() {
        WikipediaApp.getInstance().setAppLanguageCode(appLanguage);
    }

    private void preserveDefaultLocale() {
        defaultLocale = Locale.getDefault();
    }

    private void restoreDefaultLocale() {
        Locale.setDefault(defaultLocale);
    }
}

@@ -73,88 +73,32 @@
 st;%0A
+%0A
 import 
-android.test.InstrumentationTestCase;%0Aimport android.test.UiThread
+org.junit.
 Test;%0A
-%0A
 impo
@@ -251,16 +251,110 @@
 ocale;%0A%0A
+import static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0A%0A
 @SmallTe
@@ -394,40 +394,8 @@
 sts 
-extends InstrumentationTestCase 
 %7B%0A  
@@ -539,24 +539,12 @@
    @
-UiThreadTest%0A   
+Test
  pub
@@ -597,25 +597,8 @@
 ee()
- throws Throwable
  %7B%0A 
@@ -2214,14 +2214,12 @@
 sert
-Equals
+That
 (exp
@@ -2225,16 +2225,19 @@
 pected, 
+is(
 Wikipedi
@@ -2279,16 +2279,17 @@
 e(wiki))
+)
 ;%0A    %7D%0A

convert LanguageVariantTests to JUnit 4