This source file includes following definitions.
- run
- setUp
- collectGarbage
- Feature
- testCreateSingle
- Feature
- testCreateMany
package org.chromium.content.common;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import java.util.concurrent.atomic.AtomicInteger;
public class CleanupReferenceTest extends InstrumentationTestCase {
private static AtomicInteger sObjectCount = new AtomicInteger();
private static class ReferredObject {
private CleanupReference mRef;
private static class DestroyRunnable implements Runnable {
@Override
public void run() {
sObjectCount.decrementAndGet();
}
};
public ReferredObject() {
sObjectCount.incrementAndGet();
mRef = new CleanupReference(this, new DestroyRunnable());
}
}
@Override
public void setUp() throws Exception {
super.setUp();
sObjectCount.set(0);
}
private void collectGarbage() {
System.gc();
}
@SmallTest
@Feature({"AndroidWebView"})
public void testCreateSingle() throws Throwable {
assertEquals(0, sObjectCount.get());
ReferredObject instance = new ReferredObject();
assertEquals(1, sObjectCount.get());
instance = null;
assertTrue(instance == null);
collectGarbage();
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return sObjectCount.get() == 0;
}
}));
}
@SmallTest
@Feature({"AndroidWebView"})
public void testCreateMany() throws Throwable {
assertEquals(0, sObjectCount.get());
final int INSTANCE_COUNT = 20;
ReferredObject[] instances = new ReferredObject[INSTANCE_COUNT];
for (int i = 0; i < INSTANCE_COUNT; ++i) {
instances[i] = new ReferredObject();
assertEquals(i + 1, sObjectCount.get());
}
instances = null;
assertTrue(instances == null);
collectGarbage();
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return sObjectCount.get() == 0;
}
}));
}
}