From 819045fc60000b2f918ee57be402146e9421b62d Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Mon, 7 Jun 2021 13:10:49 +0900 Subject: [PATCH] Partial cherry-pick to remove IoUtils#deleteContents usage in FrameworksNetTests It's a partial cherry-pick of https://r.android.com/1731232 Bug: 154796679 eest: atest ConnectivityCoverageTests Merged-In: I5366e1cc9cd6ddb41f24718246381cc2b1cc1383 Change-Id: I23565c4144e665130585a86ce66cee6c2da07b60 --- tests/unit/Android.bp | 1 + .../android/server/net/NetworkStatsFactoryTest.java | 10 ++-------- .../android/server/net/NetworkStatsServiceTest.java | 9 ++------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp index beae0cf7a3..909a192971 100644 --- a/tests/unit/Android.bp +++ b/tests/unit/Android.bp @@ -65,6 +65,7 @@ android_test { static_libs: [ "androidx.test.rules", "bouncycastle-repackaged-unbundled", + "core-tests-support", "FrameworksNetCommonTests", "frameworks-base-testutils", "frameworks-net-integration-testutils", diff --git a/tests/unit/java/com/android/server/net/NetworkStatsFactoryTest.java b/tests/unit/java/com/android/server/net/NetworkStatsFactoryTest.java index f3ae9b051e..40d44468a6 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsFactoryTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsFactoryTest.java @@ -46,6 +46,7 @@ import com.android.frameworks.tests.net.R; import libcore.io.IoUtils; import libcore.io.Streams; +import libcore.testing.io.TestIoUtils; import org.junit.After; import org.junit.Before; @@ -69,10 +70,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @Before public void setUp() throws Exception { - mTestProc = new File(InstrumentationRegistry.getContext().getFilesDir(), "proc"); - if (mTestProc.exists()) { - IoUtils.deleteContents(mTestProc); - } + mTestProc = TestIoUtils.createTemporaryDirectory("proc"); // The libandroid_servers which have the native method is not available to // applications. So in order to have a test support native library, the native code @@ -85,10 +83,6 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest { @After public void tearDown() throws Exception { mFactory = null; - - if (mTestProc.exists()) { - IoUtils.deleteContents(mTestProc); - } } @Test diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java index c32c1d2124..cebdfe1b73 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java @@ -117,7 +117,7 @@ import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config; import com.android.testutils.HandlerUtils; import com.android.testutils.TestableNetworkStatsProviderBinder; -import libcore.io.IoUtils; +import libcore.testing.io.TestIoUtils; import org.junit.After; import org.junit.Before; @@ -213,10 +213,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { MockitoAnnotations.initMocks(this); final Context context = InstrumentationRegistry.getContext(); mServiceContext = new MockContext(context); - mStatsDir = context.getFilesDir(); - if (mStatsDir.exists()) { - IoUtils.deleteContents(mStatsDir); - } + mStatsDir = TestIoUtils.createTemporaryDirectory(getClass().getSimpleName()); PowerManager powerManager = (PowerManager) mServiceContext.getSystemService( Context.POWER_SERVICE); @@ -285,8 +282,6 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { @After public void tearDown() throws Exception { - IoUtils.deleteContents(mStatsDir); - mServiceContext = null; mStatsDir = null;