blob: bcc20c2d1b378dcdd653c2ba4b0da655e2a49739 [file] [log] [blame]
Adrian Roos91250682017-02-06 14:48:15 -08001/*
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
Adrian Roos91250682017-02-06 14:48:15 -080019import static org.mockito.ArgumentMatchers.anyInt;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.never;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
Dan Sandler4d90d1e2018-03-23 16:29:06 -040025import android.content.Context;
26import android.os.Looper;
Dan Sandler4d90d1e2018-03-23 16:29:06 -040027import android.testing.AndroidTestingRunner;
28import android.testing.TestableLooper;
29import android.testing.TestableLooper.RunWithLooper;
Adrian Roos91250682017-02-06 14:48:15 -080030
Brett Chabot84151d92019-02-27 15:37:59 -080031import androidx.test.filters.SmallTest;
32
Jason Monkfba8faf2017-05-23 10:42:59 -040033import com.android.systemui.SysuiTestCase;
34
Adrian Roos91250682017-02-06 14:48:15 -080035import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39@SmallTest
Dan Sandler4d90d1e2018-03-23 16:29:06 -040040@RunWith(AndroidTestingRunner.class)
41@RunWithLooper
Jason Monkfba8faf2017-05-23 10:42:59 -040042public class GarbageMonitorTest extends SysuiTestCase {
Adrian Roos91250682017-02-06 14:48:15 -080043
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 Sandler4d90d1e2018-03-23 16:29:06 -040052 mGarbageMonitor =
53 new TestableGarbageMonitor(
54 mContext,
55 TestableLooper.get(this).getLooper(),
56 new LeakDetector(null, mTrackedGarbage, null),
57 mLeakReporter);
Adrian Roos91250682017-02-06 14:48:15 -080058 }
59
60 @Test
61 public void testCallbacks_getScheduled() {
Dan Sandler4d90d1e2018-03-23 16:29:06 -040062 mGarbageMonitor.startLeakMonitor();
Adrian Roos91250682017-02-06 14:48:15 -080063 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 Sandler4d90d1e2018-03-23 16:29:06 -040072 mGarbageMonitor.startLeakMonitor();
Adrian Roos91250682017-02-06 14:48:15 -080073 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 Sandler4d90d1e2018-03-23 16:29:06 -040084 mGarbageMonitor.startLeakMonitor();
Adrian Roos91250682017-02-06 14:48:15 -080085 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 Sandler4d90d1e2018-03-23 16:29:06 -040096 mGarbageMonitor.startLeakMonitor();
Adrian Roos91250682017-02-06 14:48:15 -080097 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 Sandler4d90d1e2018-03-23 16:29:06 -0400110 mGarbageMonitor.startLeakMonitor();
Adrian Roos91250682017-02-06 14:48:15 -0800111 mGarbageMonitor.runCallbacksOnce();
112
113 verify(mLeakReporter).dumpLeak(anyInt());
114 }
115
116 private static class TestableGarbageMonitor extends GarbageMonitor {
Dan Sandler4d90d1e2018-03-23 16:29:06 -0400117 public TestableGarbageMonitor(
118 Context context,
119 Looper looper,
120 LeakDetector leakDetector,
Adrian Roos91250682017-02-06 14:48:15 -0800121 LeakReporter leakReporter) {
Dan Sandler4d90d1e2018-03-23 16:29:06 -0400122 super(context, looper, leakDetector, leakReporter);
Adrian Roos91250682017-02-06 14:48:15 -0800123 }
124
125 void runInspectCallback() {
Dan Sandler4d90d1e2018-03-23 16:29:06 -0400126 startLeakMonitor();
Adrian Roos91250682017-02-06 14:48:15 -0800127 }
128
129 void runReinspectCallback() {
Dan Sandler4d90d1e2018-03-23 16:29:06 -0400130 reinspectGarbageAfterGc();
Adrian Roos91250682017-02-06 14:48:15 -0800131 }
132
133 void runCallbacksOnce() {
Dan Sandler4d90d1e2018-03-23 16:29:06 -0400134 // Note that TestableLooper doesn't currently support delayed messages so we need to run
135 // callbacks explicitly.
Adrian Roos91250682017-02-06 14:48:15 -0800136 runInspectCallback();
Dan Sandler4d90d1e2018-03-23 16:29:06 -0400137 runReinspectCallback();
Adrian Roos91250682017-02-06 14:48:15 -0800138 }
139 }
140}