Skip to content

Commit

Permalink
[third_party] Avoid saving Robolectric shadow instances; instead, sav…
Browse files Browse the repository at this point in the history
…e the real framework object, and get its shadow when needed using Shadows.shadowOf(). Call Android framework methods directly on the framework object instead of its shadow whenever possible.

LSC: []
Additional details: []

Cleanup change automatically generated by javacflume/refactory
Refactoring: //third_party/java_src/robolectric/errorprone:ShadowUsageCheck

Tested:
    TAP --sample for global presubmit queue
    []

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=204784445
  • Loading branch information
xian authored and sjudd committed Jul 30, 2018
1 parent c4b08d4 commit 229cb11
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.mockito.Mockito.when;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.util.Pools;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Key;
Expand Down Expand Up @@ -229,14 +230,14 @@ public void testDoesNotNotifyCancelledIfReceivedException() {

@Test
public void testReleasesResourceIfCancelledOnReady() {
ShadowLooper shadowLooper = Shadows.shadowOf(harness.mainHandler.getLooper());
shadowLooper.pause();
Looper looper = harness.mainHandler.getLooper();
Shadows.shadowOf(looper).pause();

EngineJob<Object> job = harness.getJob();
job.start(harness.decodeJob);
job.onResourceReady(harness.resource, harness.dataSource);
job.cancel();
shadowLooper.runOneTask();
Shadows.shadowOf(looper).runOneTask();

verify(harness.resource).recycle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowBitmap;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 18)
Expand All @@ -29,52 +28,52 @@ public void testIGetNullIfNoMatchingBitmapExists() {

@Test
public void testICanAddAndGetABitmapOfTheSameSizeAndDimensions() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
strategy.put(bitmap);
assertEquals(bitmap,
strategy.get(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888));
}

@Test
public void testICantGetABitmapOfTheSameDimensionsButDifferentConfigs() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
strategy.put(bitmap);
assertNull(strategy.get(100, 100, Bitmap.Config.RGB_565));
}

@Test
public void testICantGetABitmapOfTheSameDimensionsAndSizeButDifferentConfigs() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444);
strategy.put(bitmap);
assertNull(strategy.get(100, 100, Bitmap.Config.RGB_565));
}

@Test
public void testICantGetABitmapOfDifferentWidths() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
strategy.put(bitmap);
assertNull(strategy.get(99, 100, Bitmap.Config.ARGB_8888));
}

@Test
public void testICantGetABitmapOfDifferentHeights() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
strategy.put(bitmap);
assertNull(strategy.get(100, 99, Bitmap.Config.ARGB_8888));
}

@Test
public void testICantGetABitmapOfDifferentDimensionsButTheSameSize() {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
strategy.put(bitmap);
assertNull(strategy.get(50, 200, Bitmap.Config.ARGB_8888));
}

@Test
public void testMultipleBitmapsOfDifferentAttributesCanBeAddedAtOnce() {
Bitmap first = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
Bitmap second = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap third = ShadowBitmap.createBitmap(120, 120, Bitmap.Config.RGB_565);
Bitmap first = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
Bitmap second = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap third = Bitmap.createBitmap(120, 120, Bitmap.Config.RGB_565);

strategy.put(first);
strategy.put(second);
Expand All @@ -87,9 +86,9 @@ public void testMultipleBitmapsOfDifferentAttributesCanBeAddedAtOnce() {

@Test
public void testLeastRecentlyUsedAttributeSetIsRemovedFirst() {
final Bitmap leastRecentlyUsed = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
final Bitmap other = ShadowBitmap.createBitmap(1000, 1000, Bitmap.Config.RGB_565);
final Bitmap mostRecentlyUsed = ShadowBitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
final Bitmap leastRecentlyUsed = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
final Bitmap other = Bitmap.createBitmap(1000, 1000, Bitmap.Config.RGB_565);
final Bitmap mostRecentlyUsed = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

strategy.get(100, 100, Bitmap.Config.ALPHA_8);
strategy.get(1000, 1000, Bitmap.Config.RGB_565);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowBitmap;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 18)
Expand Down Expand Up @@ -239,7 +238,7 @@ private Bitmap createMutableBitmap() {
}

private Bitmap createMutableBitmap(Bitmap.Config config) {
Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, config);
Bitmap bitmap = Bitmap.createBitmap(100, 100, config);
Shadows.shadowOf(bitmap).setMutable(true);
return bitmap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.ViewGroup;
Expand All @@ -44,7 +45,6 @@
import org.robolectric.Shadows;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowDisplay;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 19)
Expand Down Expand Up @@ -456,14 +456,13 @@ public void getSize_withWidthAndHeightEqualToPadding_doesNotCallSizeReady() {
private void setDisplayDimens(Integer width, Integer height) {
WindowManager windowManager =
(WindowManager) RuntimeEnvironment.application.getSystemService(Context.WINDOW_SERVICE);
ShadowDisplay shadowDisplay =
Shadows.shadowOf(Preconditions.checkNotNull(windowManager).getDefaultDisplay());
Display display = Preconditions.checkNotNull(windowManager).getDefaultDisplay();
if (width != null) {
shadowDisplay.setWidth(width);
Shadows.shadowOf(display).setWidth(width);
}

if (height != null) {
shadowDisplay.setHeight(height);
Shadows.shadowOf(display).setHeight(height);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.ViewTreeObserver;
Expand Down Expand Up @@ -46,7 +47,6 @@
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowDisplay;
import org.robolectric.shadows.ShadowView;

@RunWith(RobolectricTestRunner.class)
Expand Down Expand Up @@ -470,14 +470,13 @@ public void getSize_withWidthAndHeightEqualToPadding_doesNotCallSizeReady() {
private void setDisplayDimens(Integer width, Integer height) {
WindowManager windowManager =
(WindowManager) RuntimeEnvironment.application.getSystemService(Context.WINDOW_SERVICE);
ShadowDisplay shadowDisplay =
Shadows.shadowOf(Preconditions.checkNotNull(windowManager).getDefaultDisplay());
Display display = Preconditions.checkNotNull(windowManager).getDefaultDisplay();
if (width != null) {
shadowDisplay.setWidth(width);
Shadows.shadowOf(display).setWidth(width);
}

if (height != null) {
shadowDisplay.setHeight(height);
Shadows.shadowOf(display).setHeight(height);
}
}

Expand Down

0 comments on commit 229cb11

Please sign in to comment.