From 119342af838d03a32c07ab8eec20b71dcf48bf4c Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Tue, 12 Oct 2021 17:17:09 -0700 Subject: [PATCH] Fix flaky test, BatteryStatsHistoryIteratorTest Bug: 202448492 Test: atest --rerun-until-failure 20 FrameworksCoreTests:BatteryStatsHistoryIteratorTest Change-Id: If8c1c4db31fbec4859af7da2962110500f187372 --- .../os/BatteryStatsHistoryIteratorTest.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsHistoryIteratorTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsHistoryIteratorTest.java index 9c641e6437e22..2262c057d8421 100644 --- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsHistoryIteratorTest.java +++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsHistoryIteratorTest.java @@ -18,15 +18,15 @@ package com.android.internal.os; import static com.google.common.truth.Truth.assertThat; -import android.content.Context; import android.os.BatteryManager; import android.os.BatteryStats; import android.os.Process; -import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; +import libcore.testing.io.TestIoUtils; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,16 +43,8 @@ public class BatteryStatsHistoryIteratorTest { @Before public void setup() { - Context context = InstrumentationRegistry.getContext(); - - File historyDir = new File(context.getDataDir(), BatteryStatsHistory.HISTORY_DIR); - String[] files = historyDir.list(); - if (files != null) { - for (int i = 0; i < files.length; i++) { - new File(historyDir, files[i]).delete(); - } - } - historyDir.delete(); + final File historyDir = + TestIoUtils.createTemporaryDirectory(getClass().getSimpleName()); mBatteryStats = new MockBatteryStatsImpl(mMockClock, historyDir); }