diff options
Diffstat (limited to 'tests')
7 files changed, 164 insertions, 87 deletions
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardThemeTests.java b/tests/src/com/android/inputmethod/keyboard/KeyboardThemeTests.java index 34cf4072f..d642a1073 100644 --- a/tests/src/com/android/inputmethod/keyboard/KeyboardThemeTests.java +++ b/tests/src/com/android/inputmethod/keyboard/KeyboardThemeTests.java @@ -348,6 +348,31 @@ public class KeyboardThemeTests extends AndroidTestCase { } /* + * Test that KeyboardTheme array should be sorted by descending order of + * {@link KeyboardTheme#mMinApiVersion}. + */ + private static void assertSortedKeyboardThemeArray(final KeyboardTheme[] array) { + assertNotNull(array); + final int length = array.length; + assertTrue("array length=" + length, length > 0); + for (int index = 0; index < length - 1; index++) { + final KeyboardTheme theme = array[index]; + final KeyboardTheme nextTheme = array[index + 1]; + assertTrue("sorted MinApiVersion: " + + theme.mThemeName + ": minApiVersion=" + theme.mMinApiVersion, + theme.mMinApiVersion >= nextTheme.mMinApiVersion); + } + } + + public void testSortedKeyboardTheme() { + assertSortedKeyboardThemeArray(KeyboardTheme.KEYBOARD_THEMES); + } + + public void testSortedAvailableKeyboardTheme() { + assertSortedKeyboardThemeArray(KeyboardTheme.getAvailableThemeArray(getContext())); + } + + /* * Test for missing selected theme. */ private static KeyboardTheme[] LIMITED_THEMES = { @@ -356,6 +381,7 @@ public class KeyboardThemeTests extends AndroidTestCase { }; static { Arrays.sort(LIMITED_THEMES); + assertSortedKeyboardThemeArray(LIMITED_THEMES); } public void testMissingSelectedThemeIcs() { diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index 6860bea45..ee7942450 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -39,6 +39,8 @@ import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; import com.android.inputmethod.event.Event; import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.Dictionary.PhonyDictionary; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.settings.DebugSettings; import com.android.inputmethod.latin.settings.Settings; @@ -61,6 +63,10 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> { protected static final int DELAY_TO_WAIT_FOR_PREDICTIONS = 200; private final int TIMEOUT_TO_WAIT_FOR_LOADING_MAIN_DICTIONARY_IN_SECONDS = 60; + // Type for a test phony dictionary + private static final String TYPE_TEST = "test"; + private static final PhonyDictionary DICTIONARY_TEST = new PhonyDictionary(TYPE_TEST); + protected LatinIME mLatinIME; protected Keyboard mKeyboard; protected MyEditText mEditText; @@ -353,7 +359,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> { protected void pickSuggestionManually(final String suggestion) { mLatinIME.pickSuggestionManually(new SuggestedWordInfo(suggestion, 1, - SuggestedWordInfo.KIND_CORRECTION, null /* sourceDict */, + SuggestedWordInfo.KIND_CORRECTION, DICTIONARY_TEST, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); } diff --git a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java index 563261f8f..221541e4a 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java +++ b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java @@ -59,40 +59,6 @@ public class SuggestedWordsTests extends AndroidTestCase { SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); } - public void testGetSuggestedWordsExcludingTypedWord() { - final String TYPED_WORD = "typed"; - final int NUMBER_OF_ADDED_SUGGESTIONS = 5; - final int KIND_OF_SECOND_CORRECTION = SuggestedWordInfo.KIND_CORRECTION; - final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); - list.add(createTypedWordInfo(TYPED_WORD)); - for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { - list.add(createCorrectionWordInfo(Integer.toString(i))); - } - - final SuggestedWords words = new SuggestedWords( - list, null /* rawSuggestions */, - false /* typedWordValid */, - false /* willAutoCorrect */, - false /* isObsoleteSuggestions */, - SuggestedWords.INPUT_STYLE_NONE); - assertEquals(NUMBER_OF_ADDED_SUGGESTIONS + 1, words.size()); - assertEquals("typed", words.getWord(0)); - assertTrue(words.getInfo(0).isKindOf(SuggestedWordInfo.KIND_TYPED)); - assertEquals("0", words.getWord(1)); - assertTrue(words.getInfo(1).isKindOf(KIND_OF_SECOND_CORRECTION)); - assertEquals("4", words.getWord(5)); - assertTrue(words.getInfo(5).isKindOf(KIND_OF_SECOND_CORRECTION)); - - final SuggestedWords wordsWithoutTyped = - words.getSuggestedWordsExcludingTypedWordForRecorrection(); - // Make sure that the typed word has indeed been excluded, by testing the size of the - // suggested words, the string and the kind of the top suggestion, which should match - // the string and kind of what we inserted after the typed word. - assertEquals(words.size() - 1, wordsWithoutTyped.size()); - assertEquals("0", wordsWithoutTyped.getWord(0)); - assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(KIND_OF_SECOND_CORRECTION)); - } - // Helper for testGetTransformedWordInfo private SuggestedWordInfo transformWordInfo(final String info, final int trailingSingleQuotesCount) { @@ -141,9 +107,14 @@ public class SuggestedWordsTests extends AndroidTestCase { assertNotNull(typedWord); assertEquals(TYPED_WORD, typedWord.mWord); - // Make sure getTypedWordInfoOrNull() returns null. - final SuggestedWords wordsWithoutTypedWord = - wordsWithTypedWord.getSuggestedWordsExcludingTypedWordForRecorrection(); + // Make sure getTypedWordInfoOrNull() returns null when no typed word. + list.remove(0); + final SuggestedWords wordsWithoutTypedWord = new SuggestedWords( + list, null /* rawSuggestions */, + false /* typedWordValid */, + false /* willAutoCorrect */, + false /* isObsoleteSuggestions */, + SuggestedWords.INPUT_STYLE_NONE); assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull()); // Make sure getTypedWordInfoOrNull() returns null. diff --git a/tests/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiverTests.java b/tests/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiverTests.java index 00857e54e..832817967 100644 --- a/tests/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiverTests.java +++ b/tests/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiverTests.java @@ -23,7 +23,7 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.test.AndroidTestCase; -import com.android.inputmethod.latin.settings.Settings; +import com.android.inputmethod.latin.settings.LocalSettingsConstants; /** * Tests for {@link AccountsChangedReceiver}. @@ -40,7 +40,7 @@ public class AccountsChangedReceiverTests extends AndroidTestCase { super.setUp(); mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); // Keep track of the current account so that we restore it when the test finishes. - mLastKnownAccount = mPrefs.getString(Settings.PREF_ACCOUNT_NAME, null); + mLastKnownAccount = mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null); } @Override @@ -99,13 +99,14 @@ public class AccountsChangedReceiverTests extends AndroidTestCase { private void updateAccountName(String accountName) { if (accountName == null) { - mPrefs.edit().remove(Settings.PREF_ACCOUNT_NAME).apply(); + mPrefs.edit().remove(LocalSettingsConstants.PREF_ACCOUNT_NAME).apply(); } else { - mPrefs.edit().putString(Settings.PREF_ACCOUNT_NAME, accountName).apply(); + mPrefs.edit().putString(LocalSettingsConstants.PREF_ACCOUNT_NAME, accountName).apply(); } } private void assertAccountName(String expectedAccountName) { - assertEquals(expectedAccountName, mPrefs.getString(Settings.PREF_ACCOUNT_NAME, null)); + assertEquals(expectedAccountName, + mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null)); } } diff --git a/tests/src/com/android/inputmethod/latin/network/BlockingHttpClientTests.java b/tests/src/com/android/inputmethod/latin/network/BlockingHttpClientTests.java index d151732aa..fed8be920 100644 --- a/tests/src/com/android/inputmethod/latin/network/BlockingHttpClientTests.java +++ b/tests/src/com/android/inputmethod/latin/network/BlockingHttpClientTests.java @@ -16,8 +16,8 @@ package com.android.inputmethod.latin.network; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.eq; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -53,41 +53,52 @@ public class BlockingHttpClientTests extends AndroidTestCase { MockitoAnnotations.initMocks(this); } - public void testError_badGateway() throws IOException { + public void testError_badGateway() throws IOException, AuthException { when(mMockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_GATEWAY); final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection); - final FakeErrorResponseProcessor processor = - new FakeErrorResponseProcessor(HttpURLConnection.HTTP_BAD_GATEWAY); - - client.execute(null /* empty request */, processor); - assertTrue("ResponseProcessor was not invoked", processor.mInvoked); + final FakeErrorResponseProcessor processor = new FakeErrorResponseProcessor(); + + try { + client.execute(null /* empty request */, processor); + fail("Expecting an HttpException"); + } catch (HttpException e) { + // expected HttpException + assertEquals(HttpURLConnection.HTTP_BAD_GATEWAY, e.getHttpStatusCode()); + } } - public void testError_clientTimeout() throws IOException { + public void testError_clientTimeout() throws Exception { when(mMockHttpConnection.getResponseCode()).thenReturn( HttpURLConnection.HTTP_CLIENT_TIMEOUT); final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection); - final FakeErrorResponseProcessor processor = - new FakeErrorResponseProcessor(HttpURLConnection.HTTP_CLIENT_TIMEOUT); - - client.execute(null /* empty request */, processor); - assertTrue("ResponseProcessor was not invoked", processor.mInvoked); + final FakeErrorResponseProcessor processor = new FakeErrorResponseProcessor(); + + try { + client.execute(null /* empty request */, processor); + fail("Expecting an HttpException"); + } catch (HttpException e) { + // expected HttpException + assertEquals(HttpURLConnection.HTTP_CLIENT_TIMEOUT, e.getHttpStatusCode()); + } } - public void testError_forbiddenWithRequest() throws IOException { + public void testError_forbiddenWithRequest() throws Exception { final OutputStream mockOutputStream = Mockito.mock(OutputStream.class); when(mMockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_FORBIDDEN); when(mMockHttpConnection.getOutputStream()).thenReturn(mockOutputStream); final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection); - final FakeErrorResponseProcessor processor = - new FakeErrorResponseProcessor(HttpURLConnection.HTTP_FORBIDDEN); + final FakeErrorResponseProcessor processor = new FakeErrorResponseProcessor(); - client.execute(new byte[100], processor); + try { + client.execute(new byte[100], processor); + fail("Expecting an HttpException"); + } catch (HttpException e) { + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, e.getHttpStatusCode()); + } verify(mockOutputStream).write(any(byte[].class), eq(0), eq(100)); - assertTrue("ResponseProcessor was not invoked", processor.mInvoked); } - public void testSuccess_emptyRequest() throws IOException { + public void testSuccess_emptyRequest() throws Exception { final Random rand = new Random(); byte[] response = new byte[100]; rand.nextBytes(response); @@ -101,7 +112,7 @@ public class BlockingHttpClientTests extends AndroidTestCase { assertTrue("ResponseProcessor was not invoked", processor.mInvoked); } - public void testSuccess() throws IOException { + public void testSuccess() throws Exception { final OutputStream mockOutputStream = Mockito.mock(OutputStream.class); final Random rand = new Random(); byte[] response = new byte[100]; @@ -117,28 +128,15 @@ public class BlockingHttpClientTests extends AndroidTestCase { assertTrue("ResponseProcessor was not invoked", processor.mInvoked); } - private static class FakeErrorResponseProcessor implements ResponseProcessor { - private final int mExpectedStatusCode; - - boolean mInvoked; - - FakeErrorResponseProcessor(int expectedStatusCode) { - mExpectedStatusCode = expectedStatusCode; - } - + private static class FakeErrorResponseProcessor implements ResponseProcessor<Void> { @Override - public void onError(int httpStatusCode, String message) { - mInvoked = true; - assertEquals("onError:", mExpectedStatusCode, httpStatusCode); - } - - @Override - public void onSuccess(InputStream response) { + public Void onSuccess(InputStream response) { fail("Expected an error but received success"); + return null; } } - private static class FakeSuccessResponseProcessor implements ResponseProcessor { + private static class FakeSuccessResponseProcessor implements ResponseProcessor<Void> { private final byte[] mExpectedResponse; boolean mInvoked; @@ -148,12 +146,7 @@ public class BlockingHttpClientTests extends AndroidTestCase { } @Override - public void onError(int httpStatusCode, String message) { - fail("Expected a response but received an error"); - } - - @Override - public void onSuccess(InputStream response) { + public Void onSuccess(InputStream response) { try { mInvoked = true; BufferedInputStream in = new BufferedInputStream(response); @@ -169,6 +162,7 @@ public class BlockingHttpClientTests extends AndroidTestCase { } catch (IOException ex) { fail("IOException in onSuccess"); } + return null; } } } diff --git a/tests/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilderTests.java b/tests/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilderTests.java index 2b43d5b14..5b3e78eaf 100644 --- a/tests/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilderTests.java +++ b/tests/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilderTests.java @@ -142,4 +142,13 @@ public class HttpUrlConnectionBuilderTests extends AndroidTestCase { assertTrue(connection.getDoInput()); assertTrue(connection.getDoOutput()); } + + public void testSetAuthToken() throws IOException { + HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder(); + builder.setUrl("https://www.example.com"); + builder.setAuthToken("some-random-auth-token"); + HttpURLConnection connection = builder.build(); + assertEquals("some-random-auth-token", + connection.getRequestProperty(HttpUrlConnectionBuilder.HTTP_HEADER_AUTHORIZATION)); + } } diff --git a/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java b/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java index 2ef8b548f..273d7faf7 100644 --- a/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java +++ b/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java @@ -16,14 +16,21 @@ package com.android.inputmethod.latin.settings; +import static com.android.inputmethod.latin.settings.AccountsSettingsFragment.AUTHORITY; + +import android.accounts.Account; import android.app.AlertDialog; import android.app.Dialog; +import android.content.ContentResolver; import android.content.Intent; import android.test.ActivityInstrumentationTestCase2; import android.test.suitebuilder.annotation.MediumTest; import android.view.View; import android.widget.ListView; +import com.android.inputmethod.latin.accounts.LoginAccountUtils; +import com.android.inputmethod.latin.define.ProductionFlags; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -32,6 +39,9 @@ public class AccountsSettingsFragmentTests extends ActivityInstrumentationTestCase2<TestFragmentActivity> { private static final String FRAG_NAME = AccountsSettingsFragment.class.getName(); private static final long TEST_TIMEOUT_MILLIS = 5000; + private static final String TEST_ACCOUNT_NAME = "AccountsSettingsFragmentTests"; + private static final Account TEST_ACCOUNT = + new Account(TEST_ACCOUNT_NAME, LoginAccountUtils.ACCOUNT_TYPE); private AlertDialog mDialog; @@ -47,6 +57,13 @@ public class AccountsSettingsFragmentTests setActivityIntent(intent); } + @Override + protected void tearDown() throws Exception { + super.tearDown(); + // reset the syncable state to unknown + ContentResolver.setIsSyncable(TEST_ACCOUNT, AUTHORITY, -1); + } + public void testEmptyAccounts() { final AccountsSettingsFragment fragment = (AccountsSettingsFragment) getActivity().mFragment; @@ -129,4 +146,57 @@ public class AccountsSettingsFragmentTests assertEquals(View.VISIBLE, mDialog.getButton(Dialog.BUTTON_NEGATIVE).getVisibility()); assertEquals(View.VISIBLE, mDialog.getButton(Dialog.BUTTON_POSITIVE).getVisibility()); } + + public void testUpdateSyncPolicy_enable() { + // This test is a no-op when ENABLE_PERSONAL_DICTIONARY_SYNC is not enabled + if (!ProductionFlags.ENABLE_PERSONAL_DICTIONARY_SYNC) { + return; + } + // Should be unknown by default. + assertTrue(ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY) < 0); + + final AccountsSettingsFragment fragment = + (AccountsSettingsFragment) getActivity().mFragment; + fragment.updateSyncPolicy(true, TEST_ACCOUNT_NAME); + + // Should be syncable now. + assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); + } + + public void testUpdateSyncPolicy_disable() { + // This test is a no-op when ENABLE_PERSONAL_DICTIONARY_SYNC is not enabled + if (!ProductionFlags.ENABLE_PERSONAL_DICTIONARY_SYNC) { + return; + } + // Should be unknown by default. + assertTrue(ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY) < 0); + + final AccountsSettingsFragment fragment = + (AccountsSettingsFragment) getActivity().mFragment; + fragment.updateSyncPolicy(false, TEST_ACCOUNT_NAME); + + // Should not be syncable now. + assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); + } + + public void testUpdateSyncPolicy_enableDisable() { + // This test is a no-op when ENABLE_PERSONAL_DICTIONARY_SYNC is not enabled + if (!ProductionFlags.ENABLE_PERSONAL_DICTIONARY_SYNC) { + return; + } + // Should be unknown by default. + assertTrue(ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY) < 0); + + final AccountsSettingsFragment fragment = + (AccountsSettingsFragment) getActivity().mFragment; + fragment.updateSyncPolicy(true, TEST_ACCOUNT_NAME); + + // Should be syncable now. + assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); + + fragment.updateSyncPolicy(false, TEST_ACCOUNT_NAME); + + // Should not be syncable now. + assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); + } } |