[android-developers] Re: Emulate long pression of keys

2012-06-28 Thread Sal Dekku
Hi Lew. I already use Robotium for touch interactions. In this case I
need to emulate the pression of a hardware key, such as the VOLUME
keys, the SEARCH key, the KEYPAD and so on.

For the normal down and up key stroke, I'm using Robotium own
pressKey() which is just a short hand for an Instrumentation
method called sendKeyDownUpSync()

Now i need a longPressKey function that emulates the down,
wait and then up key stroke, the one you use on the POWER
key to turn the device on and off.

The result should be that the both the onKeyDown and 
onKeyLongPress() methods in my testbed application should
be executed. (This is what actually happens running the application
on a device and performing a long pression of a key.)

What I get instead when I run the Test Case is that the
onKeyDown() function is executed repeatedly, once for each
repetition of the pressed key.

Thanks for your time,

DeK

On Friday, June 29, 2012 2:52:05 AM UTC+2, Lew wrote:

 Sal Dekku wrote:

 I've wrote some code to help me solve the problem, a little application to
 use as a testbed. Also, here's one of many test units I wrote to emulate
 the long press.


 I would just use Robotium, and its 'Solo#clickLongOn...()' methods. 

 -- 
 Lew



-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: Emulate long pression of keys

2012-06-21 Thread Sal Dekku
Hello!

I've wrote some code to help me solve the problem, a little application to
use as a testbed. Also, here's one of many test units I wrote to emulate
the long press.

package com.nofatclips.keyevent.test;

 import com.nofatclips.keyevent.KeyEventsDumpActivity;

 import android.os.SystemClock;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
 import android.view.KeyEvent;

 public class KeyEventTest extends 
 ActivityInstrumentationTestCase2KeyEventsDumpActivity {
 private KeyEventsDumpActivity mActivity;  // the activity under test

 public KeyEventTest() {
   super(com.nofatclips.keyevent, KeyEventsDumpActivity.class);
 }

 @Override
 protected void setUp() throws Exception {
 super.setUp();
 mActivity = this.getActivity();
 }

 public void testLongPress() {
 sendKeyDownUpLong(KeyEvent.KEYCODE_MENU);
 }

 public void sendKeyDownUpLong(final int key) {
 final KeyEvent downEvent = new KeyEvent (KeyEvent.ACTION_DOWN, 
 key);
 sendKeyDownUp(downEvent);
 sleep(500);

 for (int repetition = 0; repetition50; repetition++) {
 KeyEvent newEvent = KeyEvent.changeTimeRepeat(downEvent, 
 SystemClock.uptimeMillis(), repetition, downEvent.getFlags() | 
 KeyEvent.FLAG_LONG_PRESS);
 sendKeyDownUp(newEvent);
 sleep(5);
 }

 final KeyEvent upEvent = new KeyEvent (KeyEvent.ACTION_UP, key);
 sendKeyDownUp (upEvent);
 }

 public void sendKeyDownUp(KeyEvent key) {
 getInstrumentation().sendKeySync(key);
 getInstrumentation().waitForIdleSync();
 }

 public void sleep (int msec) {
 try {
 Thread.sleep(msec);
 } catch (InterruptedException e) {
 Log.e(nofatclips, Could not sleep. Mosquito alert!, e);
 return;
 }

 }

 }


And here's the testbed. The XML layout:


?xml version=1.0 encoding=utf-8?
 LinearLayout xmlns:android=http://schemas.android.com/apk/res/android;
 android:layout_width=match_parent
 android:layout_height=match_parent
 android:orientation=vertical 

 TextView
 android:id=@+id/textView1
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:text=last Key
 android:textAppearance=?android:attr/textAppearanceLarge /


 TextView
 android:id=@+id/lastKeyPressed
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:text=- /

 TextView
 android:id=@+id/textView2
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:text=Last Long Key
 android:textAppearance=?android:attr/textAppearanceLarge /

 TextView
 android:id=@+id/longKeyPressed
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:text=- /

 TextView
 android:id=@+id/textView4
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:text=Counter
 android:textAppearance=?android:attr/textAppearanceLarge /

 TextView
 android:id=@+id/counter
 android:layout_width=wrap_content
 android:layout_height=wrap_content
 android:text=0 /

 /LinearLayout

 
and the code for the activity:


package com.nofatclips.keyevent;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.KeyEvent;
 import android.widget.*;

 public class KeyEventsDumpActivity extends Activity {

 int counter = 0;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.key_event);
 }

 public boolean onKeyDown(int keyCode, KeyEvent event)  {
 counter++;
 ((TextView)findViewById(R.id.counter)).setText(+counter);
 ((TextView)findViewById(R.id.lastKeyPressed)).setText(+keyCode);
 event.startTracking();
 return true; //super.onKeyDown (keyCode, event);
 }

 public boolean onKeyLongPress(int keyCode, KeyEvent event)  {
 counter--;
 ((TextView)findViewById(R.id.counter)).setText(+counter);
 ((TextView)findViewById(R.id.longKeyPressed)).setText(+keyCode);
 return super.onKeyLongPress (keyCode, event);
 }

 }


-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Emulate long pression of keys

2012-06-14 Thread Sal Dekku
Hello!

I was wondering if it is possible to emulate the long pression
of a device button via the Instrumentation during the execution
of an Android Junit test (i.e. long pressing HOME to open the
Recent Tasks or long pressing VOL DOWN in eBook
readers to go down a page)

I tried to play with the source code of sendKeyDownUpSync()
from android.app.Instrumentation, writing my own version to add
a pause between the down event and the up event and/or a
FLAG_LONG_PRESS to the up KeyEvent, to no avail.

Thanks in advance

DeK

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en