Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.util.leak; |
| 18 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.anyInt; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.never; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.os.Looper; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper; |
| 29 | import android.testing.TestableLooper.RunWithLooper; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 30 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 31 | import androidx.test.filters.SmallTest; |
| 32 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 33 | import com.android.systemui.SysuiTestCase; |
| 34 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
| 39 | @SmallTest |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 40 | @RunWith(AndroidTestingRunner.class) |
| 41 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 42 | public class GarbageMonitorTest extends SysuiTestCase { |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 43 | |
| 44 | private LeakReporter mLeakReporter; |
| 45 | private TrackedGarbage mTrackedGarbage; |
| 46 | private TestableGarbageMonitor mGarbageMonitor; |
| 47 | |
| 48 | @Before |
| 49 | public void setup() { |
| 50 | mTrackedGarbage = mock(TrackedGarbage.class); |
| 51 | mLeakReporter = mock(LeakReporter.class); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 52 | mGarbageMonitor = |
| 53 | new TestableGarbageMonitor( |
| 54 | mContext, |
| 55 | TestableLooper.get(this).getLooper(), |
| 56 | new LeakDetector(null, mTrackedGarbage, null), |
| 57 | mLeakReporter); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void testCallbacks_getScheduled() { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 62 | mGarbageMonitor.startLeakMonitor(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 63 | mGarbageMonitor.runCallbacksOnce(); |
| 64 | mGarbageMonitor.runCallbacksOnce(); |
| 65 | mGarbageMonitor.runCallbacksOnce(); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testNoGarbage_doesntDump() { |
| 70 | when(mTrackedGarbage.countOldGarbage()).thenReturn(0); |
| 71 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 72 | mGarbageMonitor.startLeakMonitor(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 73 | mGarbageMonitor.runCallbacksOnce(); |
| 74 | mGarbageMonitor.runCallbacksOnce(); |
| 75 | mGarbageMonitor.runCallbacksOnce(); |
| 76 | |
| 77 | verify(mLeakReporter, never()).dumpLeak(anyInt()); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testALittleGarbage_doesntDump() { |
| 82 | when(mTrackedGarbage.countOldGarbage()).thenReturn(4); |
| 83 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 84 | mGarbageMonitor.startLeakMonitor(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 85 | mGarbageMonitor.runCallbacksOnce(); |
| 86 | mGarbageMonitor.runCallbacksOnce(); |
| 87 | mGarbageMonitor.runCallbacksOnce(); |
| 88 | |
| 89 | verify(mLeakReporter, never()).dumpLeak(anyInt()); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testTransientGarbage_doesntDump() { |
| 94 | when(mTrackedGarbage.countOldGarbage()).thenReturn(100); |
| 95 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 96 | mGarbageMonitor.startLeakMonitor(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 97 | mGarbageMonitor.runInspectCallback(); |
| 98 | |
| 99 | when(mTrackedGarbage.countOldGarbage()).thenReturn(0); |
| 100 | |
| 101 | mGarbageMonitor.runReinspectCallback(); |
| 102 | |
| 103 | verify(mLeakReporter, never()).dumpLeak(anyInt()); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testLotsOfPersistentGarbage_dumps() { |
| 108 | when(mTrackedGarbage.countOldGarbage()).thenReturn(100); |
| 109 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 110 | mGarbageMonitor.startLeakMonitor(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 111 | mGarbageMonitor.runCallbacksOnce(); |
| 112 | |
| 113 | verify(mLeakReporter).dumpLeak(anyInt()); |
| 114 | } |
| 115 | |
| 116 | private static class TestableGarbageMonitor extends GarbageMonitor { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 117 | public TestableGarbageMonitor( |
| 118 | Context context, |
| 119 | Looper looper, |
| 120 | LeakDetector leakDetector, |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 121 | LeakReporter leakReporter) { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 122 | super(context, looper, leakDetector, leakReporter); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void runInspectCallback() { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 126 | startLeakMonitor(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void runReinspectCallback() { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 130 | reinspectGarbageAfterGc(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void runCallbacksOnce() { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 134 | // Note that TestableLooper doesn't currently support delayed messages so we need to run |
| 135 | // callbacks explicitly. |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 136 | runInspectCallback(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 137 | runReinspectCallback(); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |