|
| 1 | +package com.google.firebase.perf.metrics; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import android.util.SparseIntArray; |
| 6 | + |
| 7 | +import androidx.core.app.FrameMetricsAggregator; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.robolectric.RobolectricTestRunner; |
| 12 | + |
| 13 | +/** Unit tests for {@link FrameMetricsCalculator}. */ |
| 14 | +@RunWith(RobolectricTestRunner.class) |
| 15 | +public class FrameMetricsCalculatorTest { |
| 16 | + @Test |
| 17 | + public void calculateFrameMetrics_inputIsNull_returnsFrameMetricsWithAllZeros() { |
| 18 | + SparseIntArray[] arr = new SparseIntArray[1]; |
| 19 | + arr[FrameMetricsAggregator.TOTAL_INDEX] = null; |
| 20 | + FrameMetricsCalculator.FrameMetrics metrics = FrameMetricsCalculator.calculateFrameMetrics(arr); |
| 21 | + assertThat(metrics.getTotalFrames()).isEqualTo(0); |
| 22 | + assertThat(metrics.getSlowFrames()).isEqualTo(0); |
| 23 | + assertThat(metrics.getFrozenFrames()).isEqualTo(0); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void calculateFrameMetrics_validSparseIntArray_returnsCorrectFrameMetrics() { |
| 28 | + // For a given non-null SparseIntArray, the results stored are the number of samples at each millisecond value (rounded). |
| 29 | + // Slow frames are duration greater than 16ms and frozen frames are duration greater than 700ms. |
| 30 | + SparseIntArray sparseIntArray = new SparseIntArray(); |
| 31 | + sparseIntArray.append(5, 3); |
| 32 | + sparseIntArray.append(20, 2); |
| 33 | + sparseIntArray.append(800, 5); |
| 34 | + SparseIntArray[] arr = new SparseIntArray[1]; |
| 35 | + arr[FrameMetricsAggregator.TOTAL_INDEX] = sparseIntArray; |
| 36 | + |
| 37 | + FrameMetricsCalculator.FrameMetrics metrics = FrameMetricsCalculator.calculateFrameMetrics(arr); |
| 38 | + assertThat(metrics.getTotalFrames()).isEqualTo(10); |
| 39 | + assertThat(metrics.getSlowFrames()).isEqualTo(7); |
| 40 | + assertThat(metrics.getFrozenFrames()).isEqualTo(5); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void calculateFrameMetrics_validSparseIntArrayWithoutFrozenFrames_returnsCorrectFrameMetrics() { |
| 45 | + // For a given non-null SparseIntArray, the results stored are the number of samples at each millisecond value (rounded). |
| 46 | + // Slow frames are duration greater than 16ms and frozen frames are duration greater than 700ms. |
| 47 | + SparseIntArray sparseIntArray = new SparseIntArray(); |
| 48 | + sparseIntArray.append(5, 3); |
| 49 | + sparseIntArray.append(20, 2); |
| 50 | + SparseIntArray[] arr = new SparseIntArray[1]; |
| 51 | + arr[FrameMetricsAggregator.TOTAL_INDEX] = sparseIntArray; |
| 52 | + |
| 53 | + FrameMetricsCalculator.FrameMetrics metrics = FrameMetricsCalculator.calculateFrameMetrics(arr); |
| 54 | + assertThat(metrics.getTotalFrames()).isEqualTo(5); |
| 55 | + assertThat(metrics.getSlowFrames()).isEqualTo(2); |
| 56 | + assertThat(metrics.getFrozenFrames()).isEqualTo(0); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void calculateFrameMetrics_validSparseIntArrayWithoutSlowFrames_returnsCorrectFrameMetrics() { |
| 61 | + // For a given non-null SparseIntArray, the results stored are the number of samples at each millisecond value (rounded). |
| 62 | + // Slow frames are duration greater than 16ms and frozen frames are duration greater than 700ms. |
| 63 | + SparseIntArray sparseIntArray = new SparseIntArray(); |
| 64 | + sparseIntArray.append(5, 3); |
| 65 | + sparseIntArray.append(701, 2); |
| 66 | + SparseIntArray[] arr = new SparseIntArray[1]; |
| 67 | + arr[FrameMetricsAggregator.TOTAL_INDEX] = sparseIntArray; |
| 68 | + |
| 69 | + FrameMetricsCalculator.FrameMetrics metrics = FrameMetricsCalculator.calculateFrameMetrics(arr); |
| 70 | + assertThat(metrics.getTotalFrames()).isEqualTo(5); |
| 71 | + assertThat(metrics.getSlowFrames()).isEqualTo(2); |
| 72 | + assertThat(metrics.getFrozenFrames()).isEqualTo(2); |
| 73 | + } |
| 74 | +} |
0 commit comments