@Override public Context getTargetContext() { return base.getTargetContext(); }
/** * Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test. * * @param turnedOn true if mobile data is to be turned on and false if not */ public void setMobileData(Boolean turnedOn){ ConnectivityManager dataManager=(ConnectivityManager)instrumentation.getTargetContext().getSystemService(Context.CONNECTIVITY_SERVICE); Method dataClass = null; try { dataClass = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); dataClass.setAccessible(true); dataClass.invoke(dataManager, turnedOn); } catch (Exception e) { e.printStackTrace(); } }
/** * Sets if wifi data should be turned on or off. Requires android.permission.CHANGE_WIFI_STATE in the AndroidManifest.xml of the application under test. * * * @param turnedOn true if mobile wifi is to be turned on and false if not */ public void setWiFiData(Boolean turnedOn){ WifiManager wifiManager = (WifiManager)instrumentation.getTargetContext().getSystemService(Context.WIFI_SERVICE); try{ wifiManager.setWifiEnabled(turnedOn); }catch(Exception e){ e.printStackTrace(); } } }
/** * Returns a localized string * * @param id the resource ID for the string * @return the localized string */ public String getString(String id) { Context targetContext = instrumentation.getTargetContext(); String packageName = targetContext.getPackageName(); int viewId = targetContext.getResources().getIdentifier(id, "string", packageName); if(viewId == 0){ viewId = targetContext.getResources().getIdentifier(id, "string", "android"); } return getString(viewId); }
/** * Returns the height of the scroll or list view parent * @param view the view who's parents height should be returned * @return the height of the scroll or list view parent */ @SuppressWarnings("deprecation") public float getScrollListWindowHeight(View view) { final int[] xyParent = new int[2]; View parent = getScrollOrListParent(view); final float windowHeight; if(parent == null){ WindowManager windowManager = (WindowManager) instrumentation.getTargetContext().getSystemService(Context.WINDOW_SERVICE); windowHeight = windowManager.getDefaultDisplay().getHeight(); } else{ parent.getLocationOnScreen(xyParent); windowHeight = xyParent[1] + parent.getHeight(); } parent = null; return windowHeight; }
/** * Scrolls horizontally. * * @param side the side to which to scroll; {@link Side#RIGHT} or {@link Side#LEFT} * @param scrollPosition the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.55. * @param stepCount how many move steps to include in the scroll. Less steps results in a faster scroll */ @SuppressWarnings("deprecation") public void scrollToSide(Side side, float scrollPosition, int stepCount) { WindowManager windowManager = (WindowManager) inst.getTargetContext().getSystemService(Context.WINDOW_SERVICE); int screenHeight = windowManager.getDefaultDisplay() .getHeight(); int screenWidth = windowManager.getDefaultDisplay() .getWidth(); float x = screenWidth * scrollPosition; float y = screenHeight / 2.0f; if (side == Side.LEFT) drag(70, x, y, y, stepCount); else if (side == Side.RIGHT) drag(x, 0, y, y, stepCount); }
@Override public void requestPermissions() { checkNotNull(permissions); Application application = (Application) InstrumentationRegistry.getInstrumentation().getTargetContext(); ShadowApplication shadowApplication = Shadow.extract(application); shadowApplication.grantPermissions(permissions); } }
/** * Returns a {@code View} with a given id. * * @param id the id of the {@link View} to return * @param index the index of the {@link View}. {@code 0} if only one is available * @return a {@code View} with a given id */ public View getView(String id, int index){ View viewToReturn = null; Context targetContext = instrumentation.getTargetContext(); String packageName = targetContext.getPackageName(); int viewId = targetContext.getResources().getIdentifier(id, "id", packageName); if(viewId != 0){ viewToReturn = getView(viewId, index, TIMEOUT); } if(viewToReturn == null){ int androidViewId = targetContext.getResources().getIdentifier(id, "id", "android"); if(androidViewId != 0){ viewToReturn = getView(androidViewId, index, TIMEOUT); } } if(viewToReturn != null){ return viewToReturn; } return getView(viewId, index); }
public Context getContext(){ return getInstrumentation().getTargetContext(); }
private MainApplication getApplication() { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); MainApplication app = (MainApplication) instrumentation.getTargetContext().getApplicationContext(); return app; }
@Override public void setUp() throws Exception { super.setUp(); // Work around dexmaker issue when running tests on Android 4.3 System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath()); }
protected Context getTargetContext() { return InstrumentationRegistry.getInstrumentation().getTargetContext(); }
private Intent createTargetIntent() { Intent intentLaunchActivity = UserDetailsActivity.getCallingIntent(getInstrumentation().getTargetContext(), FAKE_USER_ID); return intentLaunchActivity; } }
private Intent createTargetIntent() { Intent intentLaunchActivity = UserListActivity.getCallingIntent(getInstrumentation().getTargetContext()); return intentLaunchActivity; } }
private StoreRetrieveData getDataStorage() { Context context = getInstrumentation().getTargetContext(); return new StoreRetrieveData(context, MainActivity.FILENAME); } }
public @NonNull InstrumentationLeakResults detectLeaks() { Instrumentation instrumentation = getInstrumentation(); Context context = instrumentation.getTargetContext(); RefWatcher refWatcher = LeakCanary.installedRefWatcher(); Set<String> retainedKeys = refWatcher.getRetainedKeys();
protected Collection getCol() { return CollectionHelper.getInstance().getCol(InstrumentationRegistry.getInstrumentation().getTargetContext()); }
@Before public void initActivity() { instrumentation = InstrumentationRegistry.getInstrumentation(); Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(TestActivity.class.getName(), null, false); instrumentation.addMonitor(monitor); Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); instrumentation.startActivitySync(intent); activity = instrumentation.waitForMonitor(monitor); assertNotNull(activity); }
protected void setUp() throws Exception { super.setUp(); // Make sure the logging is turned on. FacebookSdk.setIsDebugEnabled(true); // Make sure we have read application ID and secret. readApplicationIdAndSecret(); FacebookSdk.sdkInitialize(getInstrumentation().getTargetContext()); FacebookSdk.setApplicationId(applicationId); FacebookSdk.setClientToken(clientToken); // These are useful for debugging unit test failures. FacebookSdk.addLoggingBehavior(LoggingBehavior.REQUESTS); FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); // We want the UI thread to be in StrictMode to catch any violations. // TODO: reenable this // turnOnStrictModeForUiThread(); // Needed to bypass a dexmaker bug for mockito System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath()); }
@Before public void setUp() { MainApplication application = getApplication(); List<Object> childTestModules = getTestModules(); Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); List<Object> testModules = new LinkedList<>(childTestModules); testModules.add(new BaseTestModule(context)); ObjectGraph objectGraph = application.plusGraph(testModules); application.replaceGraph(objectGraph); objectGraph.inject(this); }