Commit 61af1bec authored by Wei-Yin Chen (陳威尹)'s avatar Wei-Yin Chen (陳威尹) Committed by Commit Bot

Add Robolectric test for BitmapDynamicResource

Add unit tests before tinkering around.

Bug: 965580
Change-Id: Ia17826cd4368cdfa70695d331369318e0497372e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1625745Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662865}
parent d1b1ecc8
...@@ -3405,6 +3405,7 @@ if (is_android) { ...@@ -3405,6 +3405,7 @@ if (is_android) {
testonly = true testonly = true
java_files = [ java_files = [
"android/junit/src/org/chromium/base/metrics/test/ShadowRecordHistogram.java", "android/junit/src/org/chromium/base/metrics/test/ShadowRecordHistogram.java",
"android/junit/src/org/chromium/base/util/GarbageCollectionTestUtil.java",
"test/android/junit/src/org/chromium/base/task/test/BackgroundShadowAsyncTask.java", "test/android/junit/src/org/chromium/base/task/test/BackgroundShadowAsyncTask.java",
"test/android/junit/src/org/chromium/base/task/test/CustomShadowAsyncTask.java", "test/android/junit/src/org/chromium/base/task/test/CustomShadowAsyncTask.java",
"test/android/junit/src/org/chromium/base/test/BaseRobolectricTestRunner.java", "test/android/junit/src/org/chromium/base/test/BaseRobolectricTestRunner.java",
...@@ -3436,6 +3437,7 @@ if (is_android) { ...@@ -3436,6 +3437,7 @@ if (is_android) {
"android/junit/src/org/chromium/base/process_launcher/ChildConnectionAllocatorTest.java", "android/junit/src/org/chromium/base/process_launcher/ChildConnectionAllocatorTest.java",
"android/junit/src/org/chromium/base/process_launcher/ChildProcessConnectionTest.java", "android/junit/src/org/chromium/base/process_launcher/ChildProcessConnectionTest.java",
"android/junit/src/org/chromium/base/task/TaskTraitsTest.java", "android/junit/src/org/chromium/base/task/TaskTraitsTest.java",
"android/junit/src/org/chromium/base/util/GarbageCollectionTestUtilTest.java",
"test/android/junit/src/org/chromium/base/test/SetUpStatementTest.java", "test/android/junit/src/org/chromium/base/test/SetUpStatementTest.java",
"test/android/junit/src/org/chromium/base/test/TestListInstrumentationRunListenerTest.java", "test/android/junit/src/org/chromium/base/test/TestListInstrumentationRunListenerTest.java",
"test/android/junit/src/org/chromium/base/test/util/AnnotationProcessingUtilsTest.java", "test/android/junit/src/org/chromium/base/test/util/AnnotationProcessingUtilsTest.java",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.util;
import java.lang.ref.WeakReference;
/**
* Util for doing garbage collection tests.
*/
public class GarbageCollectionTestUtil {
/**
* Do garbage collection and see if an object is released.
* @param reference A {@link WeakReference} pointing to the object.
* @return Whether the object can be garbage-collected.
*/
public static boolean isGarbageCollected(WeakReference<?> reference) {
Runtime runtime = Runtime.getRuntime();
runtime.runFinalization();
runtime.gc();
return reference.get() == null;
}
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.util;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.chromium.base.util.GarbageCollectionTestUtil.isGarbageCollected;
import android.graphics.Bitmap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import java.lang.ref.WeakReference;
/**
* Tests for {@link GarbageCollectionTestUtil}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class GarbageCollectionTestUtilTest {
@Test
public void testIsGarbageCollected() {
Bitmap bitmap = Bitmap.createBitmap(1, 2, Bitmap.Config.ARGB_8888);
WeakReference<Bitmap> bitmapWeakReference = new WeakReference<>(bitmap);
assertNotNull(bitmapWeakReference.get());
assertFalse(isGarbageCollected(bitmapWeakReference));
bitmap = null;
assertTrue(isGarbageCollected(bitmapWeakReference));
}
}
...@@ -381,6 +381,7 @@ junit_binary("ui_junit_tests") { ...@@ -381,6 +381,7 @@ junit_binary("ui_junit_tests") {
"junit/src/org/chromium/ui/modelutil/PropertyListModelTest.java", "junit/src/org/chromium/ui/modelutil/PropertyListModelTest.java",
"junit/src/org/chromium/ui/modelutil/PropertyModelTest.java", "junit/src/org/chromium/ui/modelutil/PropertyModelTest.java",
"junit/src/org/chromium/ui/modelutil/SimpleListObservableTest.java", "junit/src/org/chromium/ui/modelutil/SimpleListObservableTest.java",
"junit/src/org/chromium/ui/resources/dynamics/BitmapDynamicResourceTest.java",
"junit/src/org/chromium/ui/resources/dynamics/ViewResourceAdapterTest.java", "junit/src/org/chromium/ui/resources/dynamics/ViewResourceAdapterTest.java",
"junit/src/org/chromium/ui/shadows/ShadowAsyncLayoutInflater.java", "junit/src/org/chromium/ui/shadows/ShadowAsyncLayoutInflater.java",
"junit/src/org/chromium/ui/shadows/ShadowAppCompatResources.java", "junit/src/org/chromium/ui/shadows/ShadowAppCompatResources.java",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.ui.resources.dynamics;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.chromium.base.util.GarbageCollectionTestUtil.isGarbageCollected;
import android.graphics.Bitmap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import java.lang.ref.WeakReference;
/**
* Tests for {@link BitmapDynamicResource}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class BitmapDynamicResourceTest {
private BitmapDynamicResource mResource;
@Before
public void setup() {
mResource = new BitmapDynamicResource(1);
}
@Test
public void testGetBitmap() {
Bitmap bitmap = Bitmap.createBitmap(1, 2, Bitmap.Config.ARGB_8888);
mResource.setBitmap(bitmap);
assertEquals(bitmap, mResource.getBitmap());
}
@Test
public void testSetBitmapGCed() {
Bitmap bitmap = Bitmap.createBitmap(1, 2, Bitmap.Config.ARGB_8888);
WeakReference<Bitmap> bitmapWeakReference = new WeakReference<>(bitmap);
mResource.setBitmap(bitmap);
bitmap = null;
assertFalse(isGarbageCollected(bitmapWeakReference));
Bitmap bitmap2 = Bitmap.createBitmap(3, 4, Bitmap.Config.ARGB_8888);
mResource.setBitmap(bitmap2);
assertTrue(isGarbageCollected(bitmapWeakReference));
}
}
...@@ -12,6 +12,8 @@ import static org.junit.Assert.assertTrue; ...@@ -12,6 +12,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.MockitoAnnotations.initMocks;
import static org.chromium.base.util.GarbageCollectionTestUtil.isGarbageCollected;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Rect; import android.graphics.Rect;
...@@ -213,25 +215,4 @@ public class ViewResourceAdapterTest { ...@@ -213,25 +215,4 @@ public class ViewResourceAdapterTest {
assertEquals(mViewWidth, rect.width()); assertEquals(mViewWidth, rect.width());
assertEquals(mViewHeight, rect.height()); assertEquals(mViewHeight, rect.height());
} }
/**
* Sanity test for {@link #isGarbageCollected(WeakReference)}.
*/
@Test
public void testIsGarbageCollected() {
Bitmap bitmap = Bitmap.createBitmap(1, 2, Bitmap.Config.ARGB_8888);
WeakReference<Bitmap> bitmapWeakReference = new WeakReference<>(bitmap);
assertNotNull(bitmapWeakReference.get());
assertFalse(isGarbageCollected(bitmapWeakReference));
bitmap = null;
assertTrue(isGarbageCollected(bitmapWeakReference));
}
private boolean isGarbageCollected(WeakReference<Bitmap> reference) {
Runtime runtime = Runtime.getRuntime();
runtime.runFinalization();
runtime.gc();
return reference.get() == null;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment