main

square/leakcanary

Last updated at: 29/12/2023 09:39

InstrumentationHeapDumpFileProvider.kt

TLDR

This file provides a class called InstrumentationHeapDumpFileProvider which is responsible for generating unique file names for each heap dump in instrumentation tests.

Classes

InstrumentationHeapDumpFileProvider

This class is responsible for providing unique file names for each heap dump in instrumentation tests. It has the following properties and methods:

  • heapDumpDirectory (property): Represents the directory where the heap dump files will be stored.
  • newHeapDumpFile() (method): Generates a new file name for the heap dump file based on the current date and time. The generated file name has the format "instrumentation_tests_yyyy-MM-dd_HH-mm-ss_SSS.hprof".
package leakcanary.internal

import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 * Provides unique file names for each heap dump in instrumentation tests.
 */
internal class InstrumentationHeapDumpFileProvider(
  private val heapDumpDirectory: File = getInstrumentation().targetContext.filesDir
) {

  /**
   * Returns a file for where the heap should be dumped.
   */
  fun newHeapDumpFile(): File {
    // File name is unique as analysis may run several times per test
    val fileName =
      SimpleDateFormat("'instrumentation_tests_'yyyy-MM-dd_HH-mm-ss_SSS'.hprof'", Locale.US)
        .format(Date())
    return File(heapDumpDirectory, fileName)
  }
}