when I am testing my application using ActivityInstrumentationcase2 on my emulator it is working fine, but when i run it on the device it is showing
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
but I have given that permission in the manifest file.
My code is:
package cybercom.datamatics.baba.test;
import static android.test.ViewAsserts.assertOnScreen;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.test.InstrumentationTestCase;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.View;
import cybercom.datamatics.baba.CDIS_SMSActivity;
import cybercom.datamatics.baba.Compose;
public class CDIS_SMSTests extends InstrumentationTestCase
{
@MediumTest
public void testActivity()
{
Instrumentation instrumentation = getInstrumentation();
Instrumentation.ActivityMonitor monitor =instrumentation.addMonitor(CDIS_SMSActivity.class.getName(),null,false);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.ge开发者_开发问答tTargetContext(),CDIS_SMSActivity.class.getName());
instrumentation.startActivitySync(intent);
Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor,5);
assertNotNull(currentActivity);
View view = currentActivity.findViewById(cybercom.datamatics.baba.R.id.Composemessage);
assertNotNull(view);
View origin = currentActivity.getWindow().getDecorView();
assertOnScreen(origin, view);
instrumentation.removeMonitor(monitor);
monitor = instrumentation.addMonitor(Compose.class.getName(),null,false);
Intent ointent = new Intent();
ointent.setClassName(instrumentation.getTargetContext(),Compose.class.getName());
ointent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instrumentation.startActivitySync(ointent);
Activity nextActivity = getInstrumentation().waitForMonitorWithTimeout(monitor,5);
assertNotNull(nextActivity);
View numberview = nextActivity.findViewById(cybercom.datamatics.baba.R.id.number);
assertNotNull(numberview);
View nextview = nextActivity.getWindow().getDecorView();
assertOnScreen(nextview, numberview);
TouchUtils.clickView(this,numberview);
instrumentation.sendStringSync("9964973058");
View messageview = nextActivity.findViewById(cybercom.datamatics.baba.R.id.message);
assertNotNull(messageview);
TouchUtils.clickView(this,messageview);
assertOnScreen(nextview, messageview);
instrumentation.sendStringSync("hi");
View sendview = nextActivity.findViewById(cybercom.datamatics.baba.R.id.send);
assertNotNull(sendview);
assertOnScreen(nextview, sendview);
TouchUtils.clickView(this,sendview);
}
}
Check that you are clicking where you think you are - in my case this was using the Robotium method Solo.clickOnView
.
When I debugged into the code, I found the View.getLocationOnScreen
method for the view I was trying to click was populating the values as 0,0, which is clearly outside the bounds of my app (and yours). I'm still not sure why the view was reporting these values, but it was a child of a GridView
inside a DialogFragment
, and thankfully I could work out the correct offsets using the GridView
, the code to get the correct screen location from such a view is as below:
private int[] getLocationOnScreen(View view) {
Rect r = new Rect();
view.getGlobalVisibleRect(r);
int[] xy = new int[2];
View viewParent = (View) view.getParent();
viewParent.getLocationOnScreen(xy);
xy[0] += r.left;
xy[1] += r.top;
viewParent.getGlobalVisibleRect(r);
xy[0] -= r.left;
xy[1] -= r.top;
return xy;
}
This problem can also happens with emulator.
you have to check that your screen is not locked. When screen is locked, the app you are testing doesn't have the focus, so UI commands are sent to the lockscreen, not to your application. This raises the security exception.
Adding the permission would not fix the problem, the only solution is to unlock the screen manually or programmaticaly.
精彩评论