1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package com.android.systemui.util.leak; 18 19 20import android.support.test.filters.SmallTest; 21import android.support.test.runner.AndroidJUnit4; 22 23import com.android.systemui.SysuiTestCase; 24import com.android.systemui.util.leak.ReferenceTestUtils.CollectionWaiter; 25 26import org.junit.Before; 27import org.junit.Test; 28import org.junit.runner.RunWith; 29 30import java.io.FileDescriptor; 31import java.io.FileNotFoundException; 32import java.io.FileOutputStream; 33import java.io.PrintWriter; 34import java.util.ArrayList; 35import java.util.Collection; 36 37@SmallTest 38@RunWith(AndroidJUnit4.class) 39public class LeakDetectorTest extends SysuiTestCase { 40 41 private LeakDetector mLeakDetector; 42 43 @Before 44 public void setup() { 45 mLeakDetector = LeakDetector.create(); 46 47 // Note: Do not try to factor out object / collection waiter creation. The optimizer will 48 // try and cache accesses to fields and thus create a GC root for the duration of the test 49 // method, thus breaking the test. 50 } 51 52 @Test 53 public void trackInstance_doesNotLeakTrackedObject() { 54 Object object = new Object(); 55 CollectionWaiter collectionWaiter = ReferenceTestUtils.createCollectionWaiter(object); 56 57 mLeakDetector.trackInstance(object); 58 object = null; 59 collectionWaiter.waitForCollection(); 60 } 61 62 @Test 63 public void trackCollection_doesNotLeakTrackedObject() { 64 Collection<?> object = new ArrayList<>(); 65 CollectionWaiter collectionWaiter = ReferenceTestUtils.createCollectionWaiter(object); 66 67 mLeakDetector.trackCollection(object, "tag"); 68 object = null; 69 collectionWaiter.waitForCollection(); 70 } 71 72 @Test 73 public void trackGarbage_doesNotLeakTrackedObject() { 74 Object object = new Object(); 75 CollectionWaiter collectionWaiter = ReferenceTestUtils.createCollectionWaiter(object); 76 77 mLeakDetector.trackGarbage(object); 78 object = null; 79 collectionWaiter.waitForCollection(); 80 } 81 82 @Test 83 public void testDump() throws Exception { 84 Object o1 = new Object(); 85 Object o2 = new Object(); 86 Collection<Object> col1 = new ArrayList<>(); 87 88 mLeakDetector.trackInstance(o1); 89 mLeakDetector.trackCollection(col1, "tag"); 90 mLeakDetector.trackGarbage(o2); 91 92 FileOutputStream fos = new FileOutputStream("/dev/null"); 93 mLeakDetector.dump(fos.getFD(), new PrintWriter(fos), new String[0]); 94 } 95 96 @Test 97 public void testDisabled() throws Exception { 98 mLeakDetector = new LeakDetector(null, null, null); 99 100 Object o1 = new Object(); 101 Object o2 = new Object(); 102 Collection<Object> col1 = new ArrayList<>(); 103 104 mLeakDetector.trackInstance(o1); 105 mLeakDetector.trackCollection(col1, "tag"); 106 mLeakDetector.trackGarbage(o2); 107 108 FileOutputStream fos = new FileOutputStream("/dev/null"); 109 mLeakDetector.dump(fos.getFD(), new PrintWriter(fos), new String[0]); 110 } 111}