Hello,
I am trying to integrate Junit to my application for unit testing. I
know I am missing something. Below is the code, please help me
identify the root cause.
I am able to send keys to the default focused edittext, but am unable
to press a button on the same screen. I have printed the id of the
button and it corresponds correctly to the hex id on R on my project.
And am unable to send a button click to that button. I know I am
missing something real obvious to some. I guess I should pick up the
reference of the button from the activity itself, but for that i need
to make the button public. That beats the purpose of using junit
right, I mean I need to have debug code in my production ready
application :).
Also if I replace the runOnUiThread (runnable code) with a
@UiThreadTest, I get a Runtime exception (the android doc seems to
suggest that I should have one of them to avoid having this exception,
but the doc seems outdated and wrong).
Please help. ;-) this time I will wait for responses and wont fix it
myself.
package com.my.android.test;
import com.my.android.R;
import com.my.android.MyActivity;
import android.app.Activity;
import android.app.Instrumentation;
import android.os.Looper;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
public class TestCartView extends
android.test.ActivityInstrumentationTestCase2<com.My.android.MyActivity>
{
Activity MyActivity;
View rootView, firstscreen;
Looper MyLooper ;
Instrumentation instrumentation ;
public TestCartView() {
super("com.My.android.MyActivity", MyActivity.class);
}
public TestCartView(String pkg, Class<MyActivity> activityClass) {
super(pkg, activityClass);
}
protected void setUp() throws Exception {
super.setUp();
instrumentation = getInstrumentation();
MyActivity = super.getActivity();
MyLooper = MyActivity.getMainLooper() ;
rootView = MyActivity.getLayoutInflater().inflate(
com.My.android.R.layout.homescreen, null);
firstscreen = MyActivity.getLayoutInflater().inflate(
com.My.android.R.layout.firstscreen, null);
}
public void testSomethingSilly() throws Throwable {
assertNotNull(myActivity);
assertNotNull(rootView) ;
assertNotNull(firstscreen) ;
-->Working sendKeys("9 0 2 1 0");
clickButtonOnUiThread(com.my.android.R.id.btn_clickme,
firstscreen);
// clickButtonOnUiThread(com.my.android.R.id.row_deals, rootView);
}
private void clickButtonOnUiThread(int buttonId, View viewHolder)
throws Throwable {
-->id of button final View button = viewHolder.findViewById(buttonId);
--> breakpoint here Log.v("Junit","buttonname "+button.getId());
myActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
}
public void testSomethingSillier() {
assertNotNull(myActivity);
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
Regards
Conny
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en