From c0aa76f897a5bdba65290e16b997bfa1c480039e Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 24 Jul 2018 11:29:20 -0700 Subject: [PATCH] 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 --- .../MoreKeySpecStringReferenceTests.java | 38 +++++++++++---- .../AccountsSettingsFragmentTests.java | 48 ++++++++++++------- 2 files changed, 62 insertions(+), 24 deletions(-) diff --git a/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecStringReferenceTests.java b/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecStringReferenceTests.java index e06ecaedf..0ffc96a34 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecStringReferenceTests.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/MoreKeySpecStringReferenceTests.java @@ -16,26 +16,33 @@ package com.android.inputmethod.keyboard.internal; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import android.app.Instrumentation; import android.content.Context; import android.content.res.Resources; -import android.test.InstrumentationTestCase; -import android.test.suitebuilder.annotation.SmallTest; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.inputmethod.latin.tests.R; import java.util.Locale; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + @SmallTest -public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { +@RunWith(AndroidJUnit4.class) +public class MoreKeySpecStringReferenceTests { private static final Locale TEST_LOCALE = Locale.ENGLISH; private final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); - @Override - protected void setUp() throws Exception { - super.setUp(); - - final Instrumentation instrumentation = getInstrumentation(); + @Before + public void setUp() throws Exception { + final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); final Context testContext = instrumentation.getContext(); final Resources testRes = testContext.getResources(); final String testPackageName = testRes.getResourcePackageName(R.string.empty_string); @@ -59,16 +66,19 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { } } + @Test public void testResolveNullText() { assertEquals("resolve null", mTextsSet.resolveTextReference(null), null); } + @Test public void testResolveEmptyText() { assertEquals("resolve empty text", mTextsSet.resolveTextReference("!string/empty_string"), null); } + @Test public void testSplitSingleEscaped() { assertTextArray("Escaped !string", "\\!string", "\\!string"); @@ -82,6 +92,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "\\!STRING/EMPTY_STRING"); } + @Test public void testSplitMultiEscaped() { assertTextArray("Multiple escaped !string", "\\!,\\!string/empty_string", "\\!", "\\!string/empty_string"); @@ -89,15 +100,18 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "\\!", "\\!STRING/EMPTY_STRING"); } + @Test public void testSplitStringReferenceError() { assertError("Incomplete resource name", "!string/", "!string/"); assertError("Non existing resource", "!string/non_existing"); } + @Test public void testSplitEmptyStringReference() { assertTextArray("Empty string", "!string/empty_string"); } + @Test public void testSplitResourceSingle() { assertTextArray("Single char", "!string/single_char", "a"); @@ -119,6 +133,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "\\\\a"); } + @Test public void testSplitResourceSingleEscaped() { assertTextArray("Escaped char", "!string/escaped_char", "\\a"); @@ -146,6 +161,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "!string/escaped_label_with_escape", "a\\\\c"); } + @Test public void testSplitResourceMulti() { assertTextArray("Multiple chars", "!string/multiple_chars", "a", "b", "c"); @@ -158,6 +174,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "!string/multiple_labels_surrounded_by_spaces", " abc ", " def ", " ghi "); } + @Test public void testSplitResourcetMultiEscaped() { assertTextArray("Multiple chars with comma", "!string/multiple_chars_with_comma", @@ -179,6 +196,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { " ab\\\\ ", " d\\\\\\, ", " g\\,i "); } + @Test public void testSplitMultipleResources() { assertTextArray("Literals and resources", "1,!string/multiple_chars,z", @@ -203,6 +221,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "abcabc", "def", "ghi"); } + @Test public void testSplitIndirectReference() { assertTextArray("Indirect", "!string/indirect_string", "a", "b", "c"); @@ -212,11 +231,13 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "!string/indirect2_string", "a", "b", "c"); } + @Test public void testSplitInfiniteIndirectReference() { assertError("Infinite indirection", "1,!string/infinite_indirection,2", "1", "infinite", "", "loop", "2"); } + @Test public void testLabelReferece() { assertTextArray("Indirect naviagte actions as more key", "!string/keyspec_indirect_navigate_actions", @@ -225,6 +246,7 @@ public class MoreKeySpecStringReferenceTests extends InstrumentationTestCase { "!hasLabels!", "ActionNext|!code/key_action_next"); } + @Test public void testUselessUpperCaseSpecifier() { assertTextArray("EMPTY STRING", "!STRING/EMPTY_STRING", "!STRING/EMPTY_STRING"); diff --git a/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java b/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java index f2d8973f7..49a9a25da 100644 --- a/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java +++ b/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java @@ -16,6 +16,9 @@ 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.Mockito.when; @@ -23,13 +26,18 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.test.ActivityInstrumentationTestCase2; -import android.test.suitebuilder.annotation.MediumTest; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.MediumTest; +import android.support.test.runner.AndroidJUnit4; import android.view.View; import android.widget.ListView; 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.MockitoAnnotations; @@ -37,36 +45,41 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @MediumTest -public class AccountsSettingsFragmentTests - extends ActivityInstrumentationTestCase2 { +@RunWith(AndroidJUnit4.class) +public class AccountsSettingsFragmentTests { private static final String FRAG_NAME = AccountsSettingsFragment.class.getName(); private static final long TEST_TIMEOUT_MILLIS = 5000; @Mock private ManagedProfileUtils mManagedProfileUtils; - public AccountsSettingsFragmentTests() { - super(TestFragmentActivity.class); + private TestFragmentActivity mActivity; + private TestFragmentActivity getActivity() { + return mActivity; } - @Override - protected void setUp() throws Exception { - super.setUp(); - + @Before + public void setUp() throws Exception { // Initialize the mocks. MockitoAnnotations.initMocks(this); ManagedProfileUtils.setTestInstance(mManagedProfileUtils); - Intent intent = new Intent(); - intent.putExtra(TestFragmentActivity.EXTRA_SHOW_FRAGMENT, FRAG_NAME); - setActivityIntent(intent); + final Intent intent = new Intent() + .setAction(Intent.ACTION_MAIN) + .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 { ManagedProfileUtils.setTestInstance(null); - super.tearDown(); + mActivity = null; } + @Test public void testEmptyAccounts() { final AccountsSettingsFragment fragment = (AccountsSettingsFragment) getActivity().mFragment; @@ -83,6 +96,7 @@ public class AccountsSettingsFragmentTests DialogHolder() {} } + @Test public void testMultipleAccounts_noSettingsForManagedProfile() { when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(true); @@ -95,6 +109,7 @@ public class AccountsSettingsFragmentTests assertNull(fragment.findPreference(AccountsSettingsFragment.PREF_ACCCOUNT_SWITCHER)); } + @Test public void testMultipleAccounts_noCurrentAccount() { when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(false); @@ -116,6 +131,7 @@ public class AccountsSettingsFragmentTests dialog.getButton(DialogInterface.BUTTON_POSITIVE).getVisibility()); } + @Test public void testMultipleAccounts_currentAccount() { when(mManagedProfileUtils.hasWorkProfile(any(Context.class))).thenReturn(false); @@ -164,7 +180,7 @@ public class AccountsSettingsFragmentTests } catch (InterruptedException ex) { fail(); } - getInstrumentation().waitForIdleSync(); + InstrumentationRegistry.getInstrumentation().waitForIdleSync(); return dialogHolder; } }