blob: abec80eef3f9acda68a4da31728ade139fb1fe34 [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
19import static org.junit.Assert.assertTrue;
20import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.ArgumentMatchers.anyInt;
22import static org.mockito.Mockito.doAnswer;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.verify;
25
26import android.app.NotificationManager;
Brett Chabot84151d92019-02-27 15:37:59 -080027
28import androidx.test.filters.MediumTest;
29import androidx.test.runner.AndroidJUnit4;
Adrian Roos91250682017-02-06 14:48:15 -080030
31import com.android.systemui.SysuiTestCase;
32
33import org.junit.After;
34import org.junit.Before;
Adrian Roos09ed8f62017-02-16 00:55:42 +010035import org.junit.Ignore;
Adrian Roos91250682017-02-06 14:48:15 -080036import org.junit.Test;
37import org.junit.runner.RunWith;
38
39import java.io.File;
40import java.io.PrintWriter;
41
42@RunWith(AndroidJUnit4.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040043@MediumTest
Adrian Roos91250682017-02-06 14:48:15 -080044public class LeakReporterTest extends SysuiTestCase {
45
46 private LeakDetector mLeakDetector;
47 private LeakReporter mLeakReporter;
48 private File mLeakDir;
49 private File mLeakDump;
50 private File mLeakHprof;
51 private NotificationManager mNotificationManager;
52
53 @Before
54 public void setup() {
55 mLeakDir = new File(mContext.getCacheDir(), LeakReporter.LEAK_DIR);
56 mLeakDump = new File(mLeakDir, LeakReporter.LEAK_DUMP);
57 mLeakHprof = new File(mLeakDir, LeakReporter.LEAK_HPROF);
58
59 mNotificationManager = mock(NotificationManager.class);
60 mContext.addMockSystemService(NotificationManager.class, mNotificationManager);
61
62 mLeakDetector = mock(LeakDetector.class);
63 doAnswer(invocation -> {
64 invocation.<PrintWriter>getArgument(1).println("test");
65 return null;
66 }).when(mLeakDetector).dump(any(), any(), any());
67
68 mLeakReporter = new LeakReporter(mContext, mLeakDetector, "test@example.com");
69 }
70
71 @After
72 public void teardown() {
73 mLeakDump.delete();
74 mLeakHprof.delete();
75 mLeakDir.delete();
76 }
77
Adrian Roos09ed8f62017-02-16 00:55:42 +010078 @Ignore("slow")
Adrian Roos91250682017-02-06 14:48:15 -080079 @Test
80 public void testDump_postsNotification() {
81 mLeakReporter.dumpLeak(5);
82 verify(mNotificationManager).notify(any(), anyInt(), any());
83 }
84
Adrian Roos09ed8f62017-02-16 00:55:42 +010085 @Ignore("slow")
Adrian Roos91250682017-02-06 14:48:15 -080086 @Test
87 public void testDump_Repeated() {
88 mLeakReporter.dumpLeak(1);
89 mLeakReporter.dumpLeak(2);
90 }
91
Adrian Roos09ed8f62017-02-16 00:55:42 +010092 @Ignore("slow")
Adrian Roos91250682017-02-06 14:48:15 -080093 @Test
94 public void testDump_ProducesNonZeroFiles() {
95 mLeakReporter.dumpLeak(5);
96
97 assertTrue(mLeakDump.exists());
98 assertTrue(mLeakDump.length() > 0);
99
100 assertTrue(mLeakHprof.exists());
101 assertTrue(mLeakHprof.length() > 0);
102 }
103}