From 9269dd6c693494516445f4cb76488f50fddf6387 Mon Sep 17 00:00:00 2001 From: Menny Even Danan Date: Fri, 20 Oct 2017 21:39:34 -0400 Subject: [PATCH] Fixing unit-test --- .../views/AnyKeyboardViewBaseTest.java | 20 ----------- .../keyboards/views/AnyKeyboardViewTest.java | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewBaseTest.java b/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewBaseTest.java index e2b90524..5279b2db 100644 --- a/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewBaseTest.java +++ b/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewBaseTest.java @@ -10,7 +10,6 @@ import com.anysoftkeyboard.AnySoftKeyboardTestRunner; import com.anysoftkeyboard.ViewTestUtils; -import com.anysoftkeyboard.api.KeyCodes; import com.anysoftkeyboard.keyboards.AnyKeyboard; import com.anysoftkeyboard.keyboards.Keyboard; import com.menny.android.anysoftkeyboard.AnyApplication; @@ -153,25 +152,6 @@ public void testWithLongPressOutputRegularPressKeyPressState() { Assert.assertArrayEquals(provider.KEY_STATE_NORMAL, key.getCurrentDrawableState(provider)); } - @Test - public void testWithLongPressDeleteKeyOutput(){ - final AnyKeyboard.AnyKey key = findKey(KeyCodes.DELETE); - key.longPressCode = -7; - - KeyDrawableStateProvider provider = new KeyDrawableStateProvider(R.attr.key_type_function, R.attr.key_type_action, R.attr.action_done, R.attr.action_search, R.attr.action_go); - Assert.assertArrayEquals(provider.KEY_STATE_NORMAL, key.getCurrentDrawableState(provider)); - - Point keyPoint = ViewTestUtils.getKeyCenterPoint(key); - - ViewTestUtils.navigateFromTo(mUnderTest,keyPoint,keyPoint,60,true,true); - - Assert.assertArrayEquals(provider.KEY_STATE_PRESSED, key.getCurrentDrawableState(provider)); - - mUnderTest.onTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, keyPoint.x, keyPoint.y, 0)); - - Assert.assertArrayEquals(provider.KEY_STATE_NORMAL, key.getCurrentDrawableState(provider)); - } - @Nullable protected AnyKeyboard.AnyKey findKey(int codeToFind) { final int index = findKeyIndex(codeToFind); diff --git a/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewTest.java b/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewTest.java index e71baa77..363b8b12 100644 --- a/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewTest.java +++ b/app/src/test/java/com/anysoftkeyboard/keyboards/views/AnyKeyboardViewTest.java @@ -18,6 +18,7 @@ import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mockito; import org.robolectric.Robolectric; @@ -263,4 +264,38 @@ public void testPopTextOutOfKeyWhenNoRTLSupport() { mViewUnderTest.popTextOutOfKey("TEST"); Assert.assertFalse(Robolectric.getForegroundThreadScheduler().areAnyRunnable()); } + + @Test + public void testWithLongPressDeleteKeyOutput() { + final AnyKeyboard.AnyKey key = findKey(KeyCodes.DELETE); + key.longPressCode = KeyCodes.DELETE_WORD; + + ViewTestUtils.navigateFromTo(mViewUnderTest, key, key, 10, true, true); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Integer.class); + Mockito.verify(mMockKeyboardListener) + .onKey(captor.capture(), Mockito.same(key), Mockito.anyInt(), Mockito.any(int[].class), Mockito.anyBoolean()); + + Assert.assertEquals(KeyCodes.DELETE, captor.getValue().intValue()); + + Mockito.reset(mMockKeyboardListener); + + ViewTestUtils.navigateFromTo(mViewUnderTest, key, key, 1000, true, true); + + captor = ArgumentCaptor.forClass(Integer.class); + Mockito.verify(mMockKeyboardListener, Mockito.times(16)) + .onKey(captor.capture(), Mockito.same(key), Mockito.anyInt(), Mockito.any(int[].class), Mockito.anyBoolean()); + + for (int valueIndex = 0; valueIndex < captor.getAllValues().size(); valueIndex++) { + final int keyCode = captor.getAllValues().get(valueIndex); + //the first onKey will be the regular keycode + //then, the long-press timer will kick off and will + //repeat the long-press keycode. + if (valueIndex == 0) { + Assert.assertEquals(KeyCodes.DELETE, keyCode); + } else { + Assert.assertEquals(KeyCodes.DELETE_WORD, keyCode); + } + } + } } \ No newline at end of file