| 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.annotations.SuppressFBWarnings; | |
| 11 import org.chromium.base.test.util.Feature; | |
| 12 import org.chromium.content.browser.test.util.Criteria; | |
| 13 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 14 | |
| 15 import java.util.concurrent.Callable; | |
| 16 import java.util.concurrent.atomic.AtomicInteger; | |
| 17 | |
| 18 /** Test suite for {@link CleanupReference}. */ | |
| 19 public class CleanupReferenceTest extends InstrumentationTestCase { | |
| 20 | |
| 21 private static AtomicInteger sObjectCount = new AtomicInteger(); | |
| 22 | |
| 23 private static class ReferredObject { | |
| 24 | |
| 25 private CleanupReference mRef; | |
| 26 | |
| 27 // Remember: this MUST be a static class, to avoid an implicit ref back
to the | |
| 28 // owning ReferredObject instance which would defeat GC of that object. | |
| 29 private static class DestroyRunnable implements Runnable { | |
| 30 @Override | |
| 31 public void run() { | |
| 32 sObjectCount.decrementAndGet(); | |
| 33 } | |
| 34 }; | |
| 35 | |
| 36 @SuppressFBWarnings("URF_UNREAD_FIELD") | |
| 37 public ReferredObject() { | |
| 38 sObjectCount.incrementAndGet(); | |
| 39 mRef = new CleanupReference(this, new DestroyRunnable()); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 @Override | |
| 44 public void setUp() throws Exception { | |
| 45 super.setUp(); | |
| 46 sObjectCount.set(0); | |
| 47 } | |
| 48 | |
| 49 @SuppressFBWarnings("DM_GC") | |
| 50 private void collectGarbage() { | |
| 51 // While this is only a 'hint' to the VM, it's generally effective and s
ufficient on | |
| 52 // dalvik. If this changes in future, maybe try allocating a few gargant
uan objects | |
| 53 // too, to force the GC to work. | |
| 54 Runtime.getRuntime().gc(); | |
| 55 } | |
| 56 | |
| 57 @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") | |
| 58 @SmallTest | |
| 59 @Feature({"AndroidWebView"}) | |
| 60 public void testCreateSingle() throws Throwable { | |
| 61 assertEquals(0, sObjectCount.get()); | |
| 62 | |
| 63 ReferredObject instance = new ReferredObject(); | |
| 64 assertEquals(1, sObjectCount.get()); | |
| 65 | |
| 66 instance = null; | |
| 67 // Ensure compiler / instrumentation does not strip out the assignment. | |
| 68 assertTrue(instance == null); | |
| 69 collectGarbage(); | |
| 70 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | |
| 71 @Override | |
| 72 public Integer call() { | |
| 73 return sObjectCount.get(); | |
| 74 } | |
| 75 })); | |
| 76 } | |
| 77 | |
| 78 @SuppressFBWarnings("UC_USELESS_OBJECT") | |
| 79 @SmallTest | |
| 80 @Feature({"AndroidWebView"}) | |
| 81 public void testCreateMany() throws Throwable { | |
| 82 assertEquals(0, sObjectCount.get()); | |
| 83 | |
| 84 final int instanceCount = 20; | |
| 85 ReferredObject[] instances = new ReferredObject[instanceCount]; | |
| 86 | |
| 87 for (int i = 0; i < instanceCount; ++i) { | |
| 88 instances[i] = new ReferredObject(); | |
| 89 assertEquals(i + 1, sObjectCount.get()); | |
| 90 } | |
| 91 | |
| 92 instances = null; | |
| 93 // Ensure compiler / instrumentation does not strip out the assignment. | |
| 94 assertTrue(instances == null); | |
| 95 // Calling sObjectCount.get() before collectGarbage() seems to be requir
ed for the objects | |
| 96 // to be GC'ed only when building using GN. | |
| 97 assertTrue(sObjectCount.get() != -1); | |
| 98 collectGarbage(); | |
| 99 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable
<Integer>() { | |
| 100 @Override | |
| 101 public Integer call() { | |
| 102 return sObjectCount.get(); | |
| 103 } | |
| 104 })); | |
| 105 } | |
| 106 | |
| 107 } | |
| OLD | NEW |