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
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;
27import android.support.test.filters.MediumTest;
28import android.support.test.runner.AndroidJUnit4;
29
30import com.android.systemui.SysuiTestCase;
31
32import org.junit.After;
33import org.junit.Before;
34import org.junit.Ignore;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38import java.io.File;
39import java.io.PrintWriter;
40
41@RunWith(AndroidJUnit4.class)
42@MediumTest
43public class LeakReporterTest extends SysuiTestCase {
44
45    private LeakDetector mLeakDetector;
46    private LeakReporter mLeakReporter;
47    private File mLeakDir;
48    private File mLeakDump;
49    private File mLeakHprof;
50    private NotificationManager mNotificationManager;
51
52    @Before
53    public void setup() {
54        mLeakDir = new File(mContext.getCacheDir(), LeakReporter.LEAK_DIR);
55        mLeakDump = new File(mLeakDir, LeakReporter.LEAK_DUMP);
56        mLeakHprof = new File(mLeakDir, LeakReporter.LEAK_HPROF);
57
58        mNotificationManager = mock(NotificationManager.class);
59        mContext.addMockSystemService(NotificationManager.class, mNotificationManager);
60
61        mLeakDetector = mock(LeakDetector.class);
62        doAnswer(invocation -> {
63            invocation.<PrintWriter>getArgument(1).println("test");
64            return null;
65        }).when(mLeakDetector).dump(any(), any(), any());
66
67        mLeakReporter = new LeakReporter(mContext, mLeakDetector, "test@example.com");
68    }
69
70    @After
71    public void teardown() {
72        mLeakDump.delete();
73        mLeakHprof.delete();
74        mLeakDir.delete();
75    }
76
77    @Ignore("slow")
78    @Test
79    public void testDump_postsNotification() {
80        mLeakReporter.dumpLeak(5);
81        verify(mNotificationManager).notify(any(), anyInt(), any());
82    }
83
84    @Ignore("slow")
85    @Test
86    public void testDump_Repeated() {
87        mLeakReporter.dumpLeak(1);
88        mLeakReporter.dumpLeak(2);
89    }
90
91    @Ignore("slow")
92    @Test
93    public void testDump_ProducesNonZeroFiles() {
94        mLeakReporter.dumpLeak(5);
95
96        assertTrue(mLeakDump.exists());
97        assertTrue(mLeakDump.length() > 0);
98
99        assertTrue(mLeakHprof.exists());
100        assertTrue(mLeakHprof.length() > 0);
101    }
102}