@Before public void setUp() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { getInstrumentation().getUiAutomation().executeShellCommand( "pm grant " + mActivityRule.getActivity().getPackageName() + " android.permission.READ_CONTACTS"); } mActivityRule.getActivity(); // Initialize to default preferences KissApplication.getApplication(mActivityRule.getActivity()).getDataHandler().clearHistory(); PreferenceManager.getDefaultSharedPreferences(mActivityRule.getActivity()).edit().clear().apply(); PreferenceManager.setDefaultValues(mActivityRule.getActivity(), R.xml.preferences, true); // Remove lock screen Runnable wakeUpDevice = new Runnable() { public void run() { mActivityRule.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }; mActivityRule.getActivity().runOnUiThread(wakeUpDevice); } }
public boolean injectInputEvent(int action, float x, float y, int metaState) { MotionEvent e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), action, x, y, metaState); e.setSource(InputDevice.SOURCE_TOUCHSCREEN); boolean b = uiAutomation.injectInputEvent(e, true); e.recycle(); return b; }
@Override protected void failed(Throwable e, Description description) { Log.i(LOG_TAG, "taking screenshot of failed test " + description.getMethodName()); if (uiAutomation == null) { Log.i(LOG_TAG, "api level doesn't support screenshots."); return; } Bitmap screenshot = uiAutomation.takeScreenshot(); if (screenshot == null) { Log.e(LOG_TAG, "failed to take screenshot"); return; } File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/screenshots/" + InstrumentationRegistry.getTargetContext().getPackageName()); if (!path.exists() && !path.mkdirs()) { Log.e(LOG_TAG, "failed to create screenshot path"); } String filename = description.getClassName() + "-" + description.getMethodName() + ".png"; saveScreenshot(screenshot, new File(path, filename)); }
@Override public void run() { while (!stopLooping) { AccessibilityEvent accessibilityEvent = null; //return true if the AccessibilityEvent type is NOTIFICATION type UiAutomation.AccessibilityEventFilter eventFilter = new UiAutomation.AccessibilityEventFilter() { @Override public boolean accept(AccessibilityEvent event) { return event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; } }; Runnable runnable = new Runnable() { @Override public void run() { // Not performing any event. } }; try { //wait for AccessibilityEvent filter accessibilityEvent = Device.getInstance().getUiAutomation() .executeAndWaitForEvent(runnable /*executable event*/, eventFilter /* event to filter*/, 500 /*time out in ms*/); } catch (Exception ignore) {} if (accessibilityEvent != null) { toastMessages.addAll(accessibilityEvent.getText()); } } } }
public static boolean record(String msg) { List<AccessibilityWindowInfo> list = InstrumentationRegistry.getInstrumentation().getUiAutomation().getWindows(); for (AccessibilityWindowInfo win : list) { Log.i(TAG_MAIN, win.getClass().getName());
private static void grantReal(UiAutomation automation, String permission) { try { String targetPackageName = InstrumentationRegistry.getTargetContext().getPackageName(); ParcelFileDescriptor pfn = automation.executeShellCommand("pm grant " + targetPackageName + " " + permission); pfn.close(); } catch (IOException e) { Log.w(TAG, e); } } }
private void grantPermission(Context context, String permission) { if (Build.VERSION.SDK_INT < 23) { return; } UiAutomation automation = Registry.getRegistry().instrumentation.getUiAutomation(); String command = String.format(Locale.ENGLISH, "pm grant %s %s", context.getPackageName(), permission); ParcelFileDescriptor pfd = automation.executeShellCommand(command); InputStream stream = new FileInputStream(pfd.getFileDescriptor()); try { byte[] buffer = new byte[1024]; while (stream.read(buffer) != -1) { // Consume stdout to ensure the command completes } } catch (IOException ignored) { } finally { try { stream.close(); } catch (IOException ignored) { } } }
/** * Safe way to remove granted permission to you app without app restart. */ public static void resetAllPermission() { // permissions handling only available since android marshmallow if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return; } InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("pm reset-permissions"); EspWait.forDelay(DELAY_FOR_COMMAND_EXECUTION); }
private static void grantWritePermissionsForScreenshots() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { try { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); String appPackage = getTargetContext().getPackageName(); instrumentation.getUiAutomation().executeShellCommand("pm grant " + appPackage + " android.permission.READ_EXTERNAL_STORAGE"); instrumentation.getUiAutomation().executeShellCommand("pm grant " + appPackage + " android.permission.WRITE_EXTERNAL_STORAGE"); } catch (Exception e) { throw new RuntimeException( "Exception while granting external storage access to application apk", e); } } }