Migrate to Android Testing Support Lib (part 7/N)

InstrumentationTestCase and ActivityInstrumentationTestCase2 are
deprecated.  This CL rewrites tests that rely on those deprecated
classes by using Android Testing Support Library.

Bug: 110805255
Test: verified as follows. No new test failures.
    tapas adb LatinIME LatinIMETests arm64 userdebug && \
    DISABLE_PROGUARD=true make -j LatinIME && \
    adb install -r $OUT/system/app/LatinIME/LatinIME.apk && \
    atest LatinIMETests:com.android.inputmethod.keyboard.internal.MoreKeySpecStringReferenceTests LatinIMETests:com.android.inputmethod.latin.settings.AccountsSettingsFragmentTests
Change-Id: I4fefccaa0c480cfba7142ed36883da2f19b6a5f9
main
Yohei Yukawa 2018-07-24 11:29:20 -07:00
parent 560a5bc596
commit c0aa76f897
2 changed files with 62 additions and 24 deletions

View File

@ -16,26 +16,33 @@
package com.android.inputmethod.keyboard.internal; package com.android.inputmethod.keyboard.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.test.InstrumentationTestCase; import android.support.test.InstrumentationRegistry;
import android.test.suitebuilder.annotation.SmallTest; import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import com.android.inputmethod.latin.tests.R; import com.android.inputmethod.latin.tests.R;
import java.util.Locale; import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@SmallTest @SmallTest
public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { @RunWith(AndroidJUnit4.class)
public class MoreKeySpecStringReferenceTests {
private static final Locale TEST_LOCALE = Locale.ENGLISH; private static final Locale TEST_LOCALE = Locale.ENGLISH;
private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet();
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
final Instrumentation instrumentation = getInstrumentation();
final Context testContext = instrumentation.getContext(); final Context testContext = instrumentation.getContext();
final Resources testRes = testContext.getResources(); final Resources testRes = testContext.getResources();
final String testPackageName = testRes.getResourcePackageName(R.string.empty_string); final String testPackageName = testRes.getResourcePackageName(R.string.empty_string);
@ -59,16 +66,19 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
} }
} }
@Test
public void testResolveNullText() { public void testResolveNullText() {
assertEquals("resolve null", assertEquals("resolve null",
mTextsSet.resolveTextReference(null), null); mTextsSet.resolveTextReference(null), null);
} }
@Test
public void testResolveEmptyText() { public void testResolveEmptyText() {
assertEquals("resolve empty text", assertEquals("resolve empty text",
mTextsSet.resolveTextReference("!string/empty_string"), null); mTextsSet.resolveTextReference("!string/empty_string"), null);
} }
@Test
public void testSplitSingleEscaped() { public void testSplitSingleEscaped() {
assertTextArray("Escaped !string", "\\!string", assertTextArray("Escaped !string", "\\!string",
"\\!string"); "\\!string");
@ -82,6 +92,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"\\!STRING/EMPTY_STRING"); "\\!STRING/EMPTY_STRING");
} }
@Test
public void testSplitMultiEscaped() { public void testSplitMultiEscaped() {
assertTextArray("Multiple escaped !string", "\\!,\\!string/empty_string", assertTextArray("Multiple escaped !string", "\\!,\\!string/empty_string",
"\\!", "\\!string/empty_string"); "\\!", "\\!string/empty_string");
@ -89,15 +100,18 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"\\!", "\\!STRING/EMPTY_STRING"); "\\!", "\\!STRING/EMPTY_STRING");
} }
@Test
public void testSplitStringReferenceError() { public void testSplitStringReferenceError() {
assertError("Incomplete resource name", "!string/", "!string/"); assertError("Incomplete resource name", "!string/", "!string/");
assertError("Non existing resource", "!string/non_existing"); assertError("Non existing resource", "!string/non_existing");
} }
@Test
public void testSplitEmptyStringReference() { public void testSplitEmptyStringReference() {
assertTextArray("Empty string", "!string/empty_string"); assertTextArray("Empty string", "!string/empty_string");
} }
@Test
public void testSplitResourceSingle() { public void testSplitResourceSingle() {
assertTextArray("Single char", "!string/single_char", assertTextArray("Single char", "!string/single_char",
"a"); "a");
@ -119,6 +133,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"\\\\a"); "\\\\a");
} }
@Test
public void testSplitResourceSingleEscaped() { public void testSplitResourceSingleEscaped() {
assertTextArray("Escaped char", assertTextArray("Escaped char",
"!string/escaped_char", "\\a"); "!string/escaped_char", "\\a");
@ -146,6 +161,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"!string/escaped_label_with_escape", "a\\\\c"); "!string/escaped_label_with_escape", "a\\\\c");
} }
@Test
public void testSplitResourceMulti() { public void testSplitResourceMulti() {
assertTextArray("Multiple chars", assertTextArray("Multiple chars",
"!string/multiple_chars", "a", "b", "c"); "!string/multiple_chars", "a", "b", "c");
@ -158,6 +174,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"!string/multiple_labels_surrounded_by_spaces", " abc ", " def ", " ghi "); "!string/multiple_labels_surrounded_by_spaces", " abc ", " def ", " ghi ");
} }
@Test
public void testSplitResourcetMultiEscaped() { public void testSplitResourcetMultiEscaped() {
assertTextArray("Multiple chars with comma", assertTextArray("Multiple chars with comma",
"!string/multiple_chars_with_comma", "!string/multiple_chars_with_comma",
@ -179,6 +196,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
" ab\\\\ ", " d\\\\\\, ", " g\\,i "); " ab\\\\ ", " d\\\\\\, ", " g\\,i ");
} }
@Test
public void testSplitMultipleResources() { public void testSplitMultipleResources() {
assertTextArray("Literals and resources", assertTextArray("Literals and resources",
"1,!string/multiple_chars,z", "1,!string/multiple_chars,z",
@ -203,6 +221,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"abcabc", "def", "ghi"); "abcabc", "def", "ghi");
} }
@Test
public void testSplitIndirectReference() { public void testSplitIndirectReference() {
assertTextArray("Indirect", assertTextArray("Indirect",
"!string/indirect_string", "a", "b", "c"); "!string/indirect_string", "a", "b", "c");
@ -212,11 +231,13 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"!string/indirect2_string", "a", "b", "c"); "!string/indirect2_string", "a", "b", "c");
} }
@Test
public void testSplitInfiniteIndirectReference() { public void testSplitInfiniteIndirectReference() {
assertError("Infinite indirection", assertError("Infinite indirection",
"1,!string/infinite_indirection,2", "1", "infinite", "<infinite>", "loop", "2"); "1,!string/infinite_indirection,2", "1", "infinite", "<infinite>", "loop", "2");
} }
@Test
public void testLabelReferece() { public void testLabelReferece() {
assertTextArray("Indirect naviagte actions as more key", assertTextArray("Indirect naviagte actions as more key",
"!string/keyspec_indirect_navigate_actions", "!string/keyspec_indirect_navigate_actions",
@ -225,6 +246,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase {
"!hasLabels!", "ActionNext|!code/key_action_next"); "!hasLabels!", "ActionNext|!code/key_action_next");
} }
@Test
public void testUselessUpperCaseSpecifier() { public void testUselessUpperCaseSpecifier() {
assertTextArray("EMPTY STRING", assertTextArray("EMPTY STRING",
"!STRING/EMPTY_STRING", "!STRING/EMPTY_STRING"); "!STRING/EMPTY_STRING", "!STRING/EMPTY_STRING");

View File

@ -16,6 +16,9 @@
package com.android.inputmethod.latin.settings; package com.android.inputmethod.latin.settings;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -23,13 +26,18 @@ import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2; import android.support.test.InstrumentationRegistry;
import android.test.suitebuilder.annotation.MediumTest; import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;
import android.view.View; import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import com.android.inputmethod.latin.utils.ManagedProfileUtils; import com.android.inputmethod.latin.utils.ManagedProfileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -37,36 +45,41 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@MediumTest @MediumTest
public class AccountsSettingsFragmentTests @RunWith(AndroidJUnit4.class)
extends ActivityInstrumentationTestCase2<TestFragmentActivity> { public class AccountsSettingsFragmentTests {
private static final String FRAG_NAME = AccountsSettingsFragment.class.getName(); private static final String FRAG_NAME = AccountsSettingsFragment.class.getName();
private static final long TEST_TIMEOUT_MILLIS = 5000; private static final long TEST_TIMEOUT_MILLIS = 5000;
@Mock private ManagedProfileUtils mManagedProfileUtils; @Mock private ManagedProfileUtils mManagedProfileUtils;
public AccountsSettingsFragmentTests() { private TestFragmentActivity mActivity;
super(TestFragmentActivity.class); private TestFragmentActivity getActivity() {
return mActivity;
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
// Initialize the mocks. // Initialize the mocks.
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
ManagedProfileUtils.setTestInstance(mManagedProfileUtils); ManagedProfileUtils.setTestInstance(mManagedProfileUtils);
Intent intent = new Intent(); final Intent intent = new Intent()
intent.putExtra(TestFragmentActivity.EXTRA_SHOW_FRAGMENT, FRAG_NAME); .setAction(Intent.ACTION_MAIN)
setActivityIntent(intent); .setClass(InstrumentationRegistry.getTargetContext(), TestFragmentActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
.putExtra(TestFragmentActivity.EXTRA_SHOW_FRAGMENT, FRAG_NAME);
mActivity = (TestFragmentActivity) InstrumentationRegistry.getInstrumentation()
.startActivitySync(intent);
} }
@Override @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
ManagedProfileUtils.setTestInstance(null); ManagedProfileUtils.setTestInstance(null);
super.tearDown(); mActivity = null;
} }
@Test
public void testEmptyAccounts() { public void testEmptyAccounts() {
final AccountsSettingsFragment fragment = final AccountsSettingsFragment fragment =
(AccountsSettingsFragment) getActivity().mFragment; (AccountsSettingsFragment) getActivity().mFragment;
@ -83,6 +96,7 @@ public class AccountsSettingsFragmentTests
DialogHolder() {} DialogHolder() {}
} }
@Test
public void testMultipleAccounts_noSettingsForManagedProfile() { public void testMultipleAccounts_noSettingsForManagedProfile() {
when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(true); when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(true);
@ -95,6 +109,7 @@ public class AccountsSettingsFragmentTests
assertNull(fragment.findPreference(AccountsSettingsFragment.PREF_ACCCOUNT_SWITCHER)); assertNull(fragment.findPreference(AccountsSettingsFragment.PREF_ACCCOUNT_SWITCHER));
} }
@Test
public void testMultipleAccounts_noCurrentAccount() { public void testMultipleAccounts_noCurrentAccount() {
when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(false); when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(false);
@ -116,6 +131,7 @@ public class AccountsSettingsFragmentTests
dialog.getButton(DialogInterface.BUTTON_POSITIVE).getVisibility()); dialog.getButton(DialogInterface.BUTTON_POSITIVE).getVisibility());
} }
@Test
public void testMultipleAccounts_currentAccount() { public void testMultipleAccounts_currentAccount() {
when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(false); when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(false);
@ -164,7 +180,7 @@ public class AccountsSettingsFragmentTests
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
fail(); fail();
} }
getInstrumentation().waitForIdleSync(); InstrumentationRegistry.getInstrumentation().waitForIdleSync();
return dialogHolder; return dialogHolder;
} }
} }