1 package leakcanary.internal
2 
3 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
4 import java.io.File
5 import java.text.SimpleDateFormat
6 import java.util.Date
7 import java.util.Locale
8 
9 /**
10  * Provides unique file names for each heap dump in instrumentation tests.
11  */
12 internal class InstrumentationHeapDumpFileProvider(
13   private val heapDumpDirectory: File = getInstrumentation().targetContext.filesDir
14 ) {
15 
16   /**
17    * Returns a file for where the heap should be dumped.
18    */
newHeapDumpFilenull19   fun newHeapDumpFile(): File {
20     // File name is unique as analysis may run several times per test
21     val fileName =
22       SimpleDateFormat("'instrumentation_tests_'yyyy-MM-dd_HH-mm-ss_SSS'.hprof'", Locale.US)
23         .format(Date())
24     return File(heapDumpDirectory, fileName)
25   }
26 }
27