Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android :java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

Tags:

android

junit

Hi there I am new to Android Junit testing:

I have written some test code in MainActivityFunctionalTest.java file

MainActivityFunctionalTest.java:

package com.example.myfirstapp2.test;  public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<Login>{  private static final String TAG = "MainActivityFunctionalTest"; private Login activity;    public MainActivityFunctionalTest() {     super(Login.class);   }     @Override   protected void setUp() throws Exception {      Log.d(TAG,"Set-Up");      super.setUp();     setActivityInitialTouchMode(false);     activity = getActivity();   }    public void testStartSecondActivity() throws Exception {       // add monitor to check for the second activity         ActivityMonitor monitor =             getInstrumentation().               addMonitor(DisplayMessageActivity.class.getName(), null, false);         //addMonitor(MainActivity.class.getName(), null, false);      // find button and click it         Button view = (Button) activity.findViewById(R.id.btnLogin);          // TouchUtils handles the sync with the main thread internally         TouchUtils.clickView(this, view);          // to click on a click, e.g., in a listview         // listView.getChildAt(0);          // wait 2 seconds for the start of the activity         DisplayMessageActivity startedActivity = (DisplayMessageActivity)        monitor             .waitForActivityWithTimeout(5000);         assertNotNull(startedActivity);          // search for the textView         TextView textView = (TextView) startedActivity.findViewById(R.id.Email);          // check that the TextView is on the screen         ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(),             textView);         // validate the text on the TextView         assertEquals("Text incorrect", "1http://www.vogella.com",            textView.getText().toString());          // press back and click again         this.sendKeys(KeyEvent.KEYCODE_BACK);          TouchUtils.clickView(this, view);    }       } 

However,I get an error: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

at com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)

 TouchUtils.clickView(this, view); 

Please help

like image 603
user3238961 Avatar asked Mar 04 '14 05:03

user3238961


2 Answers

I had the same problem, and my code was something like this (for a normal login activity):

    onView(withId(R.id.username))             .perform(new TypeTextAction("test_user"));     onView(withId(R.id.password))             .perform(new TypeTextAction("test123"));     onView(withId(R.id.login)).perform(click()); 

The last line was crashing with SecurityException. Turned out after the last text typing, the keyboard was left open, hence the next click was considered on a different application.

To fix this, I simply had to close the keyboard after typing. I also had to add some sleep to make sure the keyboard is closed, otherwise the test would break every now and then. So the final code looked like this:

    onView(withId(R.id.username))             .perform(new TypeTextAction("test_user"));     onView(withId(R.id.password))             .perform(new TypeTextAction("test123")).perform(closeSoftKeyboard());     Thread.sleep(250);     onView(withId(R.id.login)).perform(click()); 

This worked just fine.

like image 135
m.hashemian Avatar answered Sep 20 '22 17:09

m.hashemian


I had the same issue and adding the closeSoftKeyboard() method resolved it for me.

onView(withId(R.id.view)).perform(typeText(text_to_be_typed), closeSoftKeyboard()); 
like image 44
moyheen Avatar answered Sep 20 '22 17:09

moyheen