Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Functional tests for key events
Browse files Browse the repository at this point in the history
  • Loading branch information
simopete committed Sep 7, 2015
1 parent 640b2a4 commit 22e1d4e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions AndroidAnnotations/functional-test-1-5/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<activity android:name=".ActivityWithGenerics_" />
<activity android:name=".WakeLockActivity_" />
<activity android:name=".FragmentStartedActivity_" />
<activity android:name="org.androidannotations.test15.keyevents.KeyEventsActivity_" />

<receiver android:name="org.androidannotations.test15.ereceiver.MyReceiver_" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.test15.keyevents;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.KeyDown;
import org.androidannotations.annotations.KeyLongPress;
import org.androidannotations.annotations.KeyMultiple;
import org.androidannotations.annotations.KeyUp;

import android.app.Activity;
import android.view.KeyEvent;

@EActivity
public class KeyEventsActivity extends Activity {

boolean isEnterDown = false;
boolean isUKeyUp = false;
boolean isWMultiple = false;
boolean isNineMultiple = false;
int nineMultipleCount;
boolean isELongPressed = false;
boolean isELongPressedInAnnotatedClass = false;

@KeyDown
void enterPressed() {
isEnterDown = true;
}

@KeyUp
void u() {
isUKeyUp = true;
}

@KeyMultiple({ KeyEvent.KEYCODE_9, KeyEvent.KEYCODE_W })
void multiple(int count, KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_W:
isWMultiple = true;
break;
case KeyEvent.KEYCODE_9:
isNineMultiple = true;
nineMultipleCount = count;
}
}

@KeyLongPress(KeyEvent.KEYCODE_E)
boolean eLongPress() {
isELongPressed = true;
return false;
}

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_E) {
isELongPressedInAnnotatedClass = true;
}
return super.onKeyLongPress(keyCode, event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.test15.keyevents;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import android.view.KeyEvent;

@RunWith(RobolectricTestRunner.class)
public class KeyEventsActivityTest {

private KeyEventsActivity_ activity;

@Before
public void setUp() {
activity = Robolectric.buildActivity(KeyEventsActivity_.class).create().get();
}

@Test
public void subclassTakesPrecedenceInKeyEventHandling() {
KeyEvent keyEvent = mock(KeyEvent.class);
int keyCode = KeyEvent.KEYCODE_E;

activity.onKeyLongPress(keyCode, keyEvent);

assertThat(activity.isELongPressed).isTrue();
assertThat(activity.isELongPressedInAnnotatedClass).isFalse();
}

@Test
public void keyDownKeyCodeNameFromMethod() {
KeyEvent keyEvent = mock(KeyEvent.class);
int keyCode = KeyEvent.KEYCODE_ENTER;

activity.onKeyDown(keyCode, keyEvent);

assertThat(activity.isEnterDown).isTrue();
}

@Test
public void keyUpKeyCodeNameFromMethod() {
KeyEvent keyEvent = mock(KeyEvent.class);
int keyCode = KeyEvent.KEYCODE_U;

activity.onKeyUp(keyCode, keyEvent);

assertThat(activity.isUKeyUp).isTrue();
}

@Test
public void multipleArguments() {
KeyEvent keyEvent = mock(KeyEvent.class);
int keyCode = KeyEvent.KEYCODE_W;
int count = 1;

when(keyEvent.getKeyCode()).thenReturn(keyCode);

activity.onKeyMultiple(keyCode, count, keyEvent);

assertThat(activity.isWMultiple).isTrue();
assertThat(activity.isNineMultiple).isFalse();
}

@Test
public void keyMultipleWithCount() {
KeyEvent keyEvent = mock(KeyEvent.class);
int keyCode = KeyEvent.KEYCODE_9;
int count = 9;

when(keyEvent.getKeyCode()).thenReturn(keyCode);

activity.onKeyMultiple(keyCode, count, keyEvent);

assertThat(activity.isNineMultiple).isTrue();
assertThat(activity.nineMultipleCount).isEqualTo(count);
}

@Test
public void goodMethodReturnIfKeyLongPress() {
KeyEvent keyEvent = mock(KeyEvent.class);
int keyCode = KeyEvent.KEYCODE_E;

boolean eKeyLongPressReturn = activity.onKeyLongPress(keyCode, keyEvent);

assertThat(activity.isELongPressed).isTrue();
assertThat(eKeyLongPressReturn).isFalse();
}
}

0 comments on commit 22e1d4e

Please sign in to comment.