, когда я тестирую свое приложение с помощью ActivityInstrumentationcase2 на моем эмуляторе, оно работает нормально, но когда я запускаю его на устройстве, оно показывает
java.lang.SecurityException: для внедрения в другое приложение требуетсяРазрешение INJECT_EVENTS
, но я дал это разрешение в файле манифеста.
Мой код:
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.getTargetContext(),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);
}
}