/** * Returns a Fragment with a given tag or id. * * @param tag the tag of the Fragment or null if no tag * @param id the id of the Fragment * @return a SupportFragment with a given tag or id */ private android.app.Fragment getFragment(String tag, int id) { try { if (tag == null) return activityUtils.getCurrentActivity().getFragmentManager().findFragmentById(id); else return activityUtils.getCurrentActivity().getFragmentManager().findFragmentByTag(tag); } catch (Throwable ignored) { } return null; }
/** * Waits for the given {@link Activity}. * * @param activityClass the class of the {@code Activity} to wait for * @param timeout the amount of time in milliseconds to wait * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it * does not */ public boolean waitForActivity(Class<? extends Activity> activityClass, int timeout) { Activity currentActivity = activityUtils.getCurrentActivity(false, false); final long endTime = SystemClock.uptimeMillis() + timeout; while (SystemClock.uptimeMillis() < endTime) { if (currentActivity != null && currentActivity.getClass().equals(activityClass)) { return true; } sleeper.sleep(MINISLEEP); currentActivity = activityUtils.getCurrentActivity(false, false); } return false; }
/** * Takes a screenshot and saves it in the {@link Config} objects save path. Requires write * permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the * application under test. * * @param view the view to take screenshot of * @param name the name to give the screenshot image * @param quality the compression rate. From 0 (compress for lowest size) to 100 (compress for * maximum quality). */ public void takeScreenshot(final String name, final int quality) { View decorView = getScreenshotView(); if (decorView == null) return; initScreenShotSaver(); ScreenshotRunnable runnable = new ScreenshotRunnable(decorView, name, quality); activityUtils.getCurrentActivity(false).runOnUiThread(runnable); }
/** * Waits for the given {@link Activity}. * * @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"} * @param timeout the amount of time in milliseconds to wait * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it * does not */ public boolean waitForActivity(String name, int timeout) { Activity currentActivity = activityUtils.getCurrentActivity(false, false); final long endTime = SystemClock.uptimeMillis() + timeout; while (SystemClock.uptimeMillis() < endTime) { if (currentActivity != null && currentActivity.getClass().getSimpleName().equals(name)) { return true; } if (activityUtils.getCurrentActivityName().contains(name)) { return true; } sleeper.sleep(MINISLEEP); currentActivity = activityUtils.getCurrentActivity(false, false); } return false; }
/** * Asserts that an expected {@link Activity} is currently active one, with the possibility to * verify that the expected {@code Activity} is a new instance of the {@code Activity}. * * @param message the message that should be displayed if the assert fails * @param expectedClass the {@code Class} object that is expected to be active e.g. {@code * MyActivity.class} * @param isNewInstance {@code true} if the expected {@code Activity} is a new instance of the * {@code Activity} */ public void assertCurrentActivity( String message, Class<? extends Activity> expectedClass, boolean isNewInstance) { boolean found = false; assertCurrentActivity(message, expectedClass); Activity activity = activityUtils.getCurrentActivity(); for (int i = 0; i < activityUtils.getAllOpenedActivities().size() - 1; i++) { String instanceString = activityUtils.getAllOpenedActivities().get(i).toString(); if (instanceString.equals(activity.toString())) found = true; } Assert.assertNotSame(message + ", isNewInstance: actual and ", isNewInstance, found); }
/** * Returns a {@code View} with a given id. * * @param id the R.id of the {@code View} to be returned * @param index the index of the {@link View}. {@code 0} if only one is available * @param timeout the timeout in milliseconds * @return a {@code View} with a given id */ public View getView(int id, int index, int timeout) { final Activity activity = activityUtils.getCurrentActivity(false); View viewToReturn = null; if (index < 1) { index = 0; viewToReturn = activity.findViewById(id); } if (viewToReturn != null) { return viewToReturn; } return waiter.waitForView(id, index, timeout); }
/** * Returns a SupportFragment with a given tag or id. * * @param tag the tag of the SupportFragment or null if no tag * @param id the id of the SupportFragment * @return a SupportFragment with a given tag or id */ private Fragment getSupportFragment(String tag, int id) { FragmentActivity fragmentActivity = null; try { fragmentActivity = (FragmentActivity) activityUtils.getCurrentActivity(false); } catch (ClassCastException ignored) { } if (fragmentActivity != null) { try { if (tag == null) return fragmentActivity.getSupportFragmentManager().findFragmentById(id); else return fragmentActivity.getSupportFragmentManager().findFragmentByTag(tag); } catch (NoSuchMethodError ignored) { } } return null; }
/** * Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/". Requires write permission * (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under * test. * * @param view the view to take screenshot of * @param name the name to give the screenshot image * @param quality the compression rate. From 0 (compress for lowest size) to 100 (compress for * maximum quality). */ public void takeScreenshot(final View view, final String name, final int quality) { activityUtils .getCurrentActivity(false) .runOnUiThread( new Runnable() { Bitmap b; public void run() { if (view != null) { if (view instanceof WebView) { b = getBitmapOfWebView((WebView) view); } else { b = getBitmapOfView(view); } saveFile(name, b, quality); view.destroyDrawingCache(); } } }); }
/** * Returns a localized string * * @param id the resource ID for the string * @return the localized string */ public String getString(int id) { Activity activity = activityUtils.getCurrentActivity(false); return activity.getString(id); }
/** Asserts that the available memory in the system is not low. */ public void assertMemoryNotLow() { ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); ((ActivityManager) activityUtils.getCurrentActivity().getSystemService("activity")) .getMemoryInfo(mi); Assert.assertFalse("Low memory available: " + mi.availMem + " bytes", mi.lowMemory); }
/** * Asserts that an expected {@link Activity} is currently active one, with the possibility to * verify that the expected {@code Activity} is a new instance of the {@code Activity}. * * @param message the message that should be displayed if the assert fails * @param name the name of the {@code Activity} that is expected to be active e.g. {@code * "MyActivity"} * @param isNewInstance {@code true} if the expected {@code Activity} is a new instance of the * {@code Activity} */ public void assertCurrentActivity(String message, String name, boolean isNewInstance) { assertCurrentActivity(message, name); assertCurrentActivity(message, activityUtils.getCurrentActivity().getClass(), isNewInstance); }
/** * Asserts that an expected {@link Activity} is currently active one. * * @param message the message that should be displayed if the assert fails * @param expectedClass the {@code Class} object that is expected to be active e.g. {@code * MyActivity.class} */ public void assertCurrentActivity(String message, Class<? extends Activity> expectedClass) { sleeper.sleep(); Assert.assertEquals( message, expectedClass.getName(), activityUtils.getCurrentActivity().getClass().getName()); }
/** * Asserts that an expected {@link Activity} is currently active one. * * @param message the message that should be displayed if the assert fails * @param name the name of the {@code Activity} that is expected to be active e.g. {@code * "MyActivity"} */ public void assertCurrentActivity(String message, String name) { sleeper.sleep(); Assert.assertEquals( message, name, activityUtils.getCurrentActivity().getClass().getSimpleName()); }