Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.content.common; | |
| 6 | |
| 7 import android.test.InstrumentationTestCase; | |
| 8 import android.test.suitebuilder.annotation.SmallTest; | |
| 9 | |
| 10 import org.chromium.base.test.util.Feature; | |
| 11 import org.chromium.content.browser.test.util.Criteria; | |
| 12 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 13 | |
| 14 import java.util.concurrent.atomic.AtomicInteger; | |
| 15 | |
| 16 public class CleanupReferenceTest extends InstrumentationTestCase { | |
| 17 | |
| 18 private static AtomicInteger sObjectCount = new AtomicInteger(); | |
| 19 | |
| 20 private static class ReferredObject { | |
| 21 | |
| 22 private CleanupReference mRef; | |
| 23 | |
| 24 // Remember: this MUST be a static class, to avoid an implicit ref back to the | |
| 25 // owning ReferredObject instance which would defeat GC of that object. | |
| 26 private static class DestroyRunnable implements Runnable { | |
| 27 @Override | |
| 28 public void run() { | |
| 29 sObjectCount.decrementAndGet(); | |
| 30 } | |
| 31 }; | |
| 32 | |
| 33 public ReferredObject() { | |
| 34 sObjectCount.incrementAndGet(); | |
| 35 mRef = new CleanupReference(this, new DestroyRunnable()); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 @Override | |
| 40 public void setUp() throws Exception { | |
| 41 super.setUp(); | |
| 42 sObjectCount.set(0); | |
| 43 } | |
| 44 | |
| 45 @SmallTest | |
| 46 @Feature({"AndroidWebView"}) | |
| 47 public void testCreateSingle() throws Throwable { | |
| 48 assertEquals(0, sObjectCount.get()); | |
| 49 | |
| 50 ReferredObject instance = new ReferredObject(); | |
| 51 assertEquals(1, sObjectCount.get()); | |
| 52 | |
| 53 instance = null; | |
| 54 System.gc(); | |
|
Kristian Monsen
2013/03/20 03:59:02
Is this sure to work in the future? I think System
joth
2013/03/20 21:00:39
Broadly, if it stops working in future this is pro
| |
| 55 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
| 56 @Override | |
| 57 public boolean isSatisfied() { | |
| 58 return sObjectCount.get() == 0; | |
| 59 } | |
| 60 })); | |
| 61 } | |
| 62 | |
| 63 @SmallTest | |
| 64 @Feature({"AndroidWebView"}) | |
| 65 public void testCreateMany() throws Throwable { | |
| 66 assertEquals(0, sObjectCount.get()); | |
| 67 | |
| 68 final int INSTANCE_COUNT = 20; | |
| 69 ReferredObject[] instances = new ReferredObject[INSTANCE_COUNT]; | |
| 70 | |
| 71 for (int i = 0; i < INSTANCE_COUNT; ++i) { | |
| 72 instances[i] = new ReferredObject(); | |
| 73 assertEquals(i + 1, sObjectCount.get()); | |
| 74 } | |
| 75 | |
| 76 instances = null; | |
| 77 System.gc(); | |
| 78 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
| 79 @Override | |
| 80 public boolean isSatisfied() { | |
| 81 return sObjectCount.get() == 0; | |
| 82 } | |
| 83 })); | |
| 84 } | |
| 85 | |
| 86 } | |
| OLD | NEW |