Skip to content

Commit

Permalink
Fixing unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
menny committed Oct 21, 2017
1 parent 78a7d6e commit 9269dd6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> 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);
}
}
}
}

0 comments on commit 9269dd6

Please sign in to comment.