@SuppressWarnings("unused") @Implementation public static long elapsedRealtime() { // The default is to return something using the main looper, which doesn't exist on // Volley's threads. return System.currentTimeMillis(); }
@Implementation @SuppressWarnings("unused") public Bitmap getFrameAtTime() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Shadows.shadowOf(bitmap).appendDescription(" from MediaMetadataRetriever"); return bitmap; }
@Implementation public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int duration) { Snackbar snackbar = null; try { Constructor<Snackbar> constructor = Snackbar.class.getDeclaredConstructor(ViewGroup.class); //just in case, maybe they'll change the method signature in the future if (null == constructor) throw new IllegalArgumentException("Seems like the constructor was not found!"); if (Modifier.isPrivate(constructor.getModifiers())) { constructor.setAccessible(true); } snackbar = constructor.newInstance(findSuitableParent(view)); snackbar.setText(text); snackbar.setDuration(duration); } catch (Exception e) { e.printStackTrace(); } shadowOf(snackbar).text = text.toString(); shadowSnackbars.add(shadowOf(snackbar)); return snackbar; }
@Implementation public InputStream openInputStream(Uri uri) { if (!URI_TO_INPUT_STREAMS.containsKey(uri)) { throw new IllegalArgumentException( "You must first register an InputStream for uri: " + uri); } return URI_TO_INPUT_STREAMS.get(uri); }
@Implementation public AssetFileDescriptor openAssetFileDescriptor(Uri uri, String type) { if (!URI_TO_FILE_DESCRIPTOR.containsKey(uri)) { throw new IllegalArgumentException( "You must first register an AssetFileDescriptor for " + "uri: " + uri); } return URI_TO_FILE_DESCRIPTOR.get(uri); }
@Override @Implementation public boolean equals(Object object) { if (object == null) return false; if (this == object) return true; if (object.getClass() != PointF.class) return false; PointF that = (PointF) object; if (this.realPointF.x == that.x && this.realPointF.y == that.y) return true; return false; }
@Implementation public static Bitmap createBitmap(int width, int height, Bitmap.Config config) { // Robolectric doesn't match the framework behavior with null configs, so we have to do so // here. Preconditions.checkNotNull("Config must not be null"); return ShadowBitmap.createBitmap(width, height, config); }
@Implementation public static Bitmap createBitmap(int colors[], int width, int height, Bitmap.Config config) { Bitmap bitmap = Shadow.newInstanceOf(Bitmap.class); MyShadowBitmap shadowBitmap = (MyShadowBitmap) ShadowExtractor.extract(bitmap); shadowBitmap.width = width; shadowBitmap.height = height; shadowBitmap.mPixels = new int[width * height]; for (int i = 0; i < colors.length; i++) { shadowBitmap.mPixels[i] = colors[i]; } return bitmap; }
@Implementation(minSdk = LOLLIPOP) @RequiresApi(LOLLIPOP) public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) { networkCallbacks.add(networkCallback); // simulate available connection networkCallback.onAvailable(getActiveNetwork()); }
@Implementation @SuppressWarnings("unused") public static String get(String key) { if ("ro.product.cpu.abilist".equals(key)) { return "armeabi"; } return ShadowSystemProperties.get(key); }
@Override @Implementation public Object getSystemService(String name) { if ("appops".equals(name)) { return null; } return super.getSystemService(name); }
@Implementation public static synchronized boolean isLoggable(String tag, int level) { if ((TextUtils.equals(tag, "CursorWindowStats") && level <= Log.INFO) || (TextUtils.equals(tag, "SQLiteCursor") && level <= Log.DEBUG)) { return false; } return org.robolectric.shadows.ShadowLog.isLoggable(tag, level); }
@Implementation public AsyncTask<Void, Integer, Intent> executeOnExecutor(Executor executor, Void... params) { return super.execute(params); }
@Override @Implementation public String toString() { return "Point(" + realPointF.x + ", " + realPointF.y + ")"; }
@Implementation public static void endMethod(int trackingId) { mockTracker.endMethod(trackingId); }
@Implementation public void setLinkUpstreamBandwidthKbps(int upKbps) { mLinkUpBandwidthKbps = upKbps; }
@SuppressWarnings("unused") @Implementation public void addOnPreDrawListener(OnPreDrawListener listener) { checkIsAlive(); preDrawListeners.add(listener); }
@SuppressWarnings("unused") @Implementation public void removeOnPreDrawListener(OnPreDrawListener listener) { checkIsAlive(); preDrawListeners.remove(listener); }
@Implementation public boolean isAlive() { return isAlive; }
@Implementation public void requestLayout() { isLayoutRequested = true; }
@Implementation public int getHeight() { return height; }
@Implementation public boolean isLaidOut() { return isLaidOut; }
@Implementation public void connect() throws IOException { if (!connectionSucceed) { throw new IOException("Connection failed"); } }
@Implementation public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { realObject.onBindViewHolder(holder, position); holders.put(position, holder); }
@Implementation public static void endCustom(int trackingId) { mockTracker.endCustom(trackingId); }
@Implementation public void updateAppWidget(ComponentName componentName, RemoteViews views) { updatedComponentName = componentName; updatedRemoteViews = views; }
@Implementation public void notify(String notificationTag, int notificationId, Notification notification) { updatedNotificationTag = notificationTag; updatedNotificationId = notificationId; updatedNotification = notification; }
@Implementation public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { drawnBitmaps.add(bitmap); }
@Implementation @Override public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { // Do nothing. }
@Implementation public int getAllocationByteCount() { return getWidth() * getHeight() * (getConfig() == Bitmap.Config.ARGB_8888 ? 4 : 2); }
@Implementation public boolean getBackgroundDataSetting() { return backgroundDataSetting; }
@Implementation public static int startCustom(String measurementId) { return mockTracker.startCustom(measurementId); }
@Implementation public boolean isLowRamDevice() { return isLowRam; }
@Implementation public int getLinkDownstreamBandwidthKbps() { return mLinkDownBandwidthKbps; }
@Implementation public static MessageQueue myQueue() { return mock(MessageQueue.class); }
@Implementation public InputStream openInputStream(Uri uri) { return inputStreamMap.get(uri); }
@Implementation public static int startUrl(URL url, String verb) { return mockTracker.startUrl(url, verb); }
@Implementation public void setLinkDownstreamBandwidthKbps(int downKbps) { mLinkDownBandwidthKbps = downKbps; }
@Implementation(minSdk = M) public Network getActiveNetwork() { return netIdToNetwork.get(getActiveNetworkInfo().getType()); }
@Implementation public static int startMethod(Object object, String method) { return mockTracker.startMethod(object, method); }