views:

21

answers:

0

Hi,

My aim is to take a photo using the native android camera by using the Key or motion events. But it is not possible. I tried with junit testing.

Code for invoking camera

**package com.two.camera;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  public class CameraTwo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent i = new Intent();
    i.setClassName("com.android.camera","com.android.camera.Camera");
    startActivity(i); 
}
}**

I tried to send key events by writing a Android unit testing project with code.

package com.two.camera.test;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.view.KeyEvent;

import com.two.camera.CameraTwo;


public class TestOne extends ActivityInstrumentationTestCase2<CameraTwo> {
private static final String CODE_1 = "KEYCODE_DPAD_CENTER";
private static final String CODE_2 = "KEYCODE_HOME";

public TestOne() {
   super ("com.two.camera", CameraTwo.class);
}

@Override
protected void setUp() throws Exception {
   super.setUp();
   CameraTwo mainActivity = getActivity();
}
public void takephoto() {

   sendKeys(CODE_1);
   sleep(1000);
   sleep(1000);
   sendKeys(CODE_2);
   // now on Add button
   sendKeys("ENTER");

   //SECOND METHOD
   Instrumentation i = new Instrumentation();
   i.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);

   //THIRD METHOD
   KeyEvent eventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
   KeyEvent eventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
   dispatchKeyEvent(eventDown);
   dispatchKeyEvent(eventUp);

   Object j = null;
// JUST FOR TESTING THAT CODE IS MOVED UPTO HERE OR NOT
   assertTrue("Add result should be 98", j.equals(1));
}

  private void dispatchKeyEvent(KeyEvent eventDown) {
  // TODO Auto-generated method stub
  return;
}

private void sleep(int i) {
// TODO Auto-generated method stub
return;
}

}

I tried by executing the above test using Right click -> run as android junit test and from command line using

$ adb shell am instrument com.two.camera.test/android.test.InstrumentationTestRunner

Please guide me in this regard Thanks in advance