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 | |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | import static org.mockito.Mockito.doAnswer; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | |
| 26 | import android.app.NotificationManager; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 27 | |
| 28 | import androidx.test.filters.MediumTest; |
| 29 | import androidx.test.runner.AndroidJUnit4; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 30 | |
| 31 | import com.android.systemui.SysuiTestCase; |
| 32 | |
| 33 | import org.junit.After; |
| 34 | import org.junit.Before; |
Adrian Roos | 09ed8f6 | 2017-02-16 00:55:42 +0100 | [diff] [blame] | 35 | import org.junit.Ignore; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.io.PrintWriter; |
| 41 | |
| 42 | @RunWith(AndroidJUnit4.class) |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 43 | @MediumTest |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 44 | public 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 Roos | 09ed8f6 | 2017-02-16 00:55:42 +0100 | [diff] [blame] | 78 | @Ignore("slow") |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 79 | @Test |
| 80 | public void testDump_postsNotification() { |
| 81 | mLeakReporter.dumpLeak(5); |
| 82 | verify(mNotificationManager).notify(any(), anyInt(), any()); |
| 83 | } |
| 84 | |
Adrian Roos | 09ed8f6 | 2017-02-16 00:55:42 +0100 | [diff] [blame] | 85 | @Ignore("slow") |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 86 | @Test |
| 87 | public void testDump_Repeated() { |
| 88 | mLeakReporter.dumpLeak(1); |
| 89 | mLeakReporter.dumpLeak(2); |
| 90 | } |
| 91 | |
Adrian Roos | 09ed8f6 | 2017-02-16 00:55:42 +0100 | [diff] [blame] | 92 | @Ignore("slow") |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 93 | @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 | } |