OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 package org.chromium.chrome.browser.omnibox; |
| 5 |
| 6 import static org.junit.Assert.assertEquals; |
| 7 import static org.junit.Assert.assertFalse; |
| 8 import static org.junit.Assert.assertNotNull; |
| 9 import static org.junit.Assert.assertTrue; |
| 10 import static org.mockito.Mockito.inOrder; |
| 11 import static org.mockito.Mockito.spy; |
| 12 |
| 13 import android.content.Context; |
| 14 import android.text.TextUtils; |
| 15 import android.util.AttributeSet; |
| 16 import android.view.inputmethod.BaseInputConnection; |
| 17 import android.view.inputmethod.InputConnection; |
| 18 |
| 19 import org.junit.Before; |
| 20 import org.junit.Test; |
| 21 import org.junit.runner.RunWith; |
| 22 import org.mockito.InOrder; |
| 23 import org.mockito.MockitoAnnotations; |
| 24 import org.robolectric.RuntimeEnvironment; |
| 25 import org.robolectric.annotation.Config; |
| 26 import org.robolectric.shadows.ShadowLog; |
| 27 |
| 28 import org.chromium.base.Log; |
| 29 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 30 |
| 31 /** |
| 32 * A robolectric test for {@link AutocompleteEditText} class. |
| 33 */ |
| 34 @RunWith(LocalRobolectricTestRunner.class) |
| 35 @Config(manifest = Config.NONE) |
| 36 public class AutocompleteEditTextTest { |
| 37 private static final String TAG = "cr_AutocompleteEdit"; |
| 38 private static final boolean DEBUG = false; |
| 39 |
| 40 private InOrder mInOrder; |
| 41 private AutocompleteEditText mAutocomplete; |
| 42 private Context mContext; |
| 43 private InputConnection mInputConnection; |
| 44 private BaseInputConnection mDummyTargetInputConnection; |
| 45 private AutocompleteEmbedder mEmbedder; |
| 46 |
| 47 // This is needed to limit the target of inOrder#verify. |
| 48 private static class AutocompleteEmbedder { |
| 49 public void onAutocompleteTextStateChanged(boolean textDeleted, boolean
updateDisplay) { |
| 50 if (DEBUG) Log.i(TAG, "onAutocompleteTextStateChanged"); |
| 51 } |
| 52 } |
| 53 |
| 54 private static class TestAutocompleteEditText extends AutocompleteEditText { |
| 55 private AutocompleteEmbedder mEmbedder; |
| 56 |
| 57 public TestAutocompleteEditText( |
| 58 AutocompleteEmbedder embedder, Context context, AttributeSet att
rs) { |
| 59 super(context, attrs); |
| 60 mEmbedder = embedder; |
| 61 } |
| 62 |
| 63 @Override |
| 64 public void onAutocompleteTextStateChanged(boolean textDeleted, boolean
updateDisplay) { |
| 65 // This function is called in super(), so mEmbedder may be null. |
| 66 if (mEmbedder != null) { |
| 67 mEmbedder.onAutocompleteTextStateChanged(textDeleted, updateDisp
lay); |
| 68 } |
| 69 } |
| 70 |
| 71 @Override |
| 72 public boolean hasFocus() { |
| 73 return true; |
| 74 } |
| 75 } |
| 76 |
| 77 @Before |
| 78 public void setUp() throws Exception { |
| 79 ShadowLog.stream = System.out; |
| 80 if (DEBUG) Log.i(TAG, "setUp started."); |
| 81 MockitoAnnotations.initMocks(this); |
| 82 mContext = RuntimeEnvironment.application; |
| 83 mEmbedder = spy(new AutocompleteEmbedder()); |
| 84 mAutocomplete = spy(new TestAutocompleteEditText(mEmbedder, mContext, nu
ll)); |
| 85 assertNotNull(mAutocomplete); |
| 86 // Note: this cannot catch the first {@link |
| 87 // AutocompleteEditText#onAutocompleteTextStateChanged(boolean, boolean)
}, which is caused |
| 88 // by View constructor's call to setText(). |
| 89 mInOrder = inOrder(mEmbedder); |
| 90 mDummyTargetInputConnection = new BaseInputConnection(mAutocomplete, tru
e); |
| 91 mInputConnection = mAutocomplete.createInputConnection(mDummyTargetInput
Connection); |
| 92 assertNotNull(mInputConnection); |
| 93 mInOrder.verifyNoMoreInteractions(); |
| 94 if (DEBUG) Log.i(TAG, "setUp finished."); |
| 95 } |
| 96 |
| 97 private void assertTexts(String userText, String autocompleteText) { |
| 98 assertEquals(userText, mAutocomplete.getTextWithoutAutocomplete()); |
| 99 assertEquals(userText + autocompleteText, mAutocomplete.getTextWithAutoc
omplete()); |
| 100 assertEquals(autocompleteText.length(), mAutocomplete.getAutocompleteLen
gth()); |
| 101 assertEquals(!TextUtils.isEmpty(autocompleteText), mAutocomplete.hasAuto
complete()); |
| 102 } |
| 103 |
| 104 @Test |
| 105 public void testAppend_CommitText() { |
| 106 // Feeder should call this at the beginning. |
| 107 mAutocomplete.setIgnoreTextChangesForAutocomplete(false); |
| 108 // User types "h". |
| 109 assertTrue(mInputConnection.commitText("h", 1)); |
| 110 // User types "hello". |
| 111 assertTrue(mInputConnection.commitText("ello", 1)); |
| 112 mInOrder.verifyNoMoreInteractions(); |
| 113 // The controller kicks in. |
| 114 mAutocomplete.setAutocompleteText("hello", " world"); |
| 115 assertTexts("hello", " world"); |
| 116 assertFalse(mInputConnection.beginBatchEdit()); |
| 117 assertTrue(mInputConnection.commitText(" ", 1)); |
| 118 mInOrder.verify(mEmbedder).onAutocompleteTextStateChanged(false, false); |
| 119 assertTexts("hello ", "world"); |
| 120 assertFalse(mInputConnection.endBatchEdit()); |
| 121 mAutocomplete.setAutocompleteText("hello ", "world"); |
| 122 assertTexts("hello ", "world"); |
| 123 mInOrder.verifyNoMoreInteractions(); |
| 124 } |
| 125 } |
OLD | NEW |