Revert Tests & Utils to `upstream-import`

* Reverted all of the test utils + test files back to their original
form as found in aosp/upstream-import branch at version 121.0.6103.2

* Added @SkipPresubmit tags back again on some tests, this is the only divergence
left. It will be removed once aosp/2910846 gets merged then we can change
`exclude-annotation` to `exclude-filter` in TEST_MAPPING.

Bug: 304217500
Test: atest NetHttpTests
Change-Id: Idf8b76078aaf99613fc23ad65f9d0c6410f0d4a1
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/BrotliTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/BrotliTest.java
index 81b88c0..d172a43 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/BrotliTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/BrotliTest.java
@@ -15,7 +15,6 @@
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -51,15 +50,13 @@
                                         builder, QuicTestServer.createMockCertVerifier());
                             });
         }
-        assertThat(NativeTestServer.startNativeTestServer(mTestRule.getTestFramework().getContext())).isTrue();
+        assertThat(Http2TestServer.startHttp2TestServer(mTestRule.getTestFramework().getContext()))
+                .isTrue();
     }
 
     @After
     public void tearDown() throws Exception {
-        NativeTestServer.shutdownNativeTestServer();
-        if (mCronetEngine != null) {
-            mCronetEngine.shutdown();
-        }
+        assertThat(Http2TestServer.shutdownHttp2TestServer()).isTrue();
     }
 
     @Test
@@ -73,17 +70,17 @@
                         });
 
         mCronetEngine = mTestRule.getTestFramework().startEngine();
-        String url = NativeTestServer.getEchoAllHeadersURL();
+        String url = Http2TestServer.getEchoAllHeadersUrl();
         TestUrlRequestCallback callback = startAndWaitForComplete(url);
         assertThat(callback.getResponseInfoWithChecks()).hasHttpStatusCodeThat().isEqualTo(200);
-        assertThat(callback.mResponseAsString).contains("Accept-Encoding: gzip, deflate, br");
+        assertThat(callback.mResponseAsString).contains("accept-encoding: gzip, deflate, br");
     }
 
     @Test
     @SmallTest
     public void testBrotliNotAdvertised() throws Exception {
         mCronetEngine = mTestRule.getTestFramework().startEngine();
-        String url = NativeTestServer.getEchoAllHeadersURL();
+        String url = Http2TestServer.getEchoAllHeadersUrl();
         TestUrlRequestCallback callback = startAndWaitForComplete(url);
         assertThat(callback.getResponseInfoWithChecks()).hasHttpStatusCodeThat().isEqualTo(200);
         assertThat(callback.mResponseAsString).doesNotContain("br");
@@ -91,7 +88,6 @@
 
     @Test
     @SmallTest
-    @Ignore // TODO(danstahr): Add test server support for setting the Brotli header
     public void testBrotliDecoded() throws Exception {
         mTestRule
                 .getTestFramework()
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/CronetStressTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/CronetStressTest.java
index a5b3968..a237050 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/CronetStressTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/CronetStressTest.java
@@ -11,8 +11,6 @@
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.filters.LargeTest;
 
-import org.chromium.base.test.util.Batch;
-import org.chromium.net.apihelpers.UploadDataProviders;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/CronetTestRuleTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/CronetTestRuleTest.java
index 8190ab7..3fa22d1 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/CronetTestRuleTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/CronetTestRuleTest.java
@@ -26,6 +26,7 @@
 import org.chromium.net.CronetTestRule.RequiresMinAndroidApi;
 import org.chromium.net.CronetTestRule.RequiresMinApi;
 import org.chromium.net.impl.CronetUrlRequestContext;
+import org.chromium.net.impl.JavaCronetEngine;
 
 /** Tests features of CronetTestRule. */
 @RunWith(AndroidJUnit4.class)
@@ -84,6 +85,34 @@
      */
     @Test
     @SmallTest
+    public void testAllImplsMustRun() {
+        assertThat(mTestWasRun).isFalse();
+        mTestWasRun = true;
+        mNumberOfReruns++;
+        assertThat(mNumberOfReruns).isLessThan(4);
+        switch (mTestRule.implementationUnderTest()) {
+            case STATICALLY_LINKED:
+                assertThat(mNativeImplWasRun).isFalse();
+                mNativeImplWasRun = true;
+                break;
+            case FALLBACK:
+                assertThat(mFallbackImplWasRun).isFalse();
+                mFallbackImplWasRun = true;
+                break;
+            case AOSP_PLATFORM:
+                assertThat(mPlatformImplWasRun).isFalse();
+                mPlatformImplWasRun = true;
+                break;
+        }
+        if (mNumberOfReruns == 3) {
+            assertThat(mFallbackImplWasRun).isTrue();
+            assertThat(mPlatformImplWasRun).isTrue();
+            assertThat(mNativeImplWasRun).isTrue();
+        }
+    }
+
+    @Test
+    @SmallTest
     @IgnoreFor(
             implementations = {CronetImplementation.FALLBACK, CronetImplementation.AOSP_PLATFORM},
             reason = "Testing the rule")
@@ -96,4 +125,41 @@
         assertThat(mTestRule.getTestFramework().getEngine())
                 .isInstanceOf(CronetUrlRequestContext.class);
     }
+
+    @Test
+    @SmallTest
+    @IgnoreFor(
+            implementations = {
+                CronetImplementation.STATICALLY_LINKED,
+                CronetImplementation.AOSP_PLATFORM
+            },
+            reason = "Testing the rule")
+    public void testRunOnlyJavaMustRun() {
+        assertThat(mTestRule.testingJavaImpl()).isTrue();
+        assertThat(mTestRule.implementationUnderTest()).isEqualTo(CronetImplementation.FALLBACK);
+        assertThat(mTestWasRun).isFalse();
+        mTestWasRun = true;
+        assertThat(mTestRule.getTestFramework().getEngine()).isInstanceOf(JavaCronetEngine.class);
+    }
+
+    @Test
+    @SmallTest
+    @IgnoreFor(
+            implementations = {
+                CronetImplementation.STATICALLY_LINKED,
+                CronetImplementation.FALLBACK
+            },
+            reason = "Testing the rule")
+    @RequiresMinAndroidApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
+    public void testRunOnlyAospPlatformMustRun() {
+        assertThat(mTestRule.testingJavaImpl()).isFalse();
+        assertThat(mTestRule.implementationUnderTest())
+                .isEqualTo(CronetImplementation.AOSP_PLATFORM);
+        assertThat(mTestWasRun).isFalse();
+        mTestWasRun = true;
+        assertThat(mTestRule.getTestFramework().getEngine())
+                .isNotInstanceOf(JavaCronetEngine.class);
+        assertThat(mTestRule.getTestFramework().getEngine())
+                .isNotInstanceOf(CronetUrlRequestContext.class);
+    }
 }
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java
index 5ed4cde..94e7dd0 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java
@@ -5,11 +5,13 @@
 package org.chromium.net;
 
 import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assume.assumeTrue;
+
 import static org.chromium.net.CronetEngine.Builder.HTTP_CACHE_IN_MEMORY;
 import static org.chromium.net.CronetTestRule.getTestStorage;
 import static org.chromium.net.truth.UrlResponseInfoSubject.assertThat;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assume.assumeTrue;
 
 import android.net.Network;
 import android.os.Build;
@@ -18,9 +20,11 @@
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Process;
+
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.filters.SmallTest;
 import com.android.testutils.SkipPresubmit;
+
 import org.jni_zero.JNINamespace;
 import org.jni_zero.NativeMethods;
 import org.json.JSONObject;
@@ -31,7 +35,6 @@
 import org.junit.runner.RunWith;
 
 import org.chromium.base.Log;
-import org.chromium.base.FileUtils;
 import org.chromium.base.PathUtils;
 import org.chromium.base.test.util.DoNotBatch;
 import org.chromium.net.CronetTestRule.CronetImplementation;
@@ -78,21 +81,23 @@
     private static final String MOCK_CRONET_TEST_SUCCESS_URL = "http://mock.http/success.txt";
     private static final int MAX_FILE_SIZE = 1000000000;
 
+    private EmbeddedTestServer mTestServer;
     private String mUrl;
     private String mUrl404;
     private String mUrl500;
 
-  @Before
-  public void setUp() throws Exception {
-    assertThat(NativeTestServer.startNativeTestServer(mTestRule.getTestFramework().getContext())).isTrue();
-    mUrl = NativeTestServer.getSuccessURL();
-    mUrl404 = NativeTestServer.getNotFoundURL();
-        mUrl500 = NativeTestServer.getServerErrorURL();
+    @Before
+    public void setUp() throws Exception {
+        mTestServer =
+                EmbeddedTestServer.createAndStartServer(mTestRule.getTestFramework().getContext());
+        mUrl = mTestServer.getURL("/echo?status=200");
+        mUrl404 = mTestServer.getURL("/echo?status=404");
+        mUrl500 = mTestServer.getURL("/echo?status=500");
     }
 
     @After
     public void tearDown() throws Exception {
-        NativeTestServer.shutdownNativeTestServer();
+        mTestServer.stopAndDestroyServer();
     }
 
     class RequestThread extends Thread {
@@ -784,7 +789,7 @@
         assertThat(logFile.exists()).isTrue();
         assertThat(logFile.length()).isNotEqualTo(0);
         assertThat(hasBytesInNetLog(logFile)).isFalse();
-        FileUtils.recursivelyDeleteFile(netLogDir, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir);
         assertThat(netLogDir.exists()).isFalse();
     }
 
@@ -845,12 +850,13 @@
         assertThat(logFile.exists()).isTrue();
         assertThat(logFile.length()).isNotEqualTo(0);
 
-        FileUtils.recursivelyDeleteFile(netLogDir, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir);
         assertThat(netLogDir.exists()).isFalse();
     }
 
     @Test
     @SmallTest
+    @SkipPresubmit(reason = "b/293141085 flaky test")
     @IgnoreFor(
             implementations = {CronetImplementation.AOSP_PLATFORM},
             reason = "ActiveRequestCount is not available in AOSP")
@@ -1030,6 +1036,9 @@
     @Test
     @SmallTest
     @SkipPresubmit(reason = "b/293141085 flaky test")
+    @IgnoreFor(
+        implementations = {CronetImplementation.AOSP_PLATFORM},
+        reason = "ActiveRequestCount is not available in AOSP")
     public void testGetActiveRequestCountWithError() throws Exception {
         final String badUrl = "www.unreachable-url.com";
         ExperimentalCronetEngine cronetEngine = mTestRule.getTestFramework().startEngine();
@@ -1267,9 +1276,9 @@
         assertThat(containsStringInNetLog(logFile2, mUrl404)).isTrue();
         assertThat(containsStringInNetLog(logFile2, mUrl500)).isTrue();
 
-        FileUtils.recursivelyDeleteFile(netLogDir1, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir1);
         assertThat(netLogDir1.exists()).isFalse();
-        FileUtils.recursivelyDeleteFile(netLogDir2, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir2);
         assertThat(netLogDir2.exists()).isFalse();
     }
 
@@ -1292,7 +1301,10 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @SkipPresubmit(reason = "b/293141085 flaky test")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "Fallback implementation does not have a network thread.")
     // Tests that if CronetEngine is shut down on the network thread, an appropriate exception
     // is thrown.
     public void testShutDownEngineOnNetworkThread() throws Exception {
@@ -1350,7 +1362,10 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @SkipPresubmit(reason = "b/293141085 flaky test")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "Fallback implementation has no support for caches")
     // Tests that if CronetEngine is shut down when reading from disk cache,
     // there isn't a crash. See crbug.com/486120.
     public void testShutDownEngineWhenReadingFromDiskCache() throws Exception {
@@ -1449,7 +1464,7 @@
                                         netLogDir.getPath(), false, MAX_FILE_SIZE));
         assertThat(e).hasMessageThat().isEqualTo("Engine is shut down.");
         assertThat(logFile.exists()).isFalse();
-        FileUtils.recursivelyDeleteFile(netLogDir, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir);
         assertThat(netLogDir.exists()).isFalse();
     }
 
@@ -1511,7 +1526,7 @@
         assertThat(logFile.exists()).isTrue();
         assertThat(logFile.length()).isNotEqualTo(0);
         assertThat(hasBytesInNetLog(logFile)).isFalse();
-        FileUtils.recursivelyDeleteFile(netLogDir, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir);
         assertThat(netLogDir.exists()).isFalse();
     }
 
@@ -1575,7 +1590,7 @@
         assertThat(logFile.exists()).isTrue();
         assertThat(logFile.length()).isNotEqualTo(0);
         assertThat(hasBytesInNetLog(logFile)).isFalse();
-        FileUtils.recursivelyDeleteFile(netLogDir, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir);
         assertThat(netLogDir.exists()).isFalse();
     }
 
@@ -1631,7 +1646,7 @@
         assertThat(logFile.exists()).isTrue();
         assertThat(logFile.length()).isNotEqualTo(0);
         assertThat(hasBytesInNetLog(logFile)).isTrue();
-        FileUtils.recursivelyDeleteFile(netLogDir, FileUtils.DELETE_ALL);
+        FileUtils.recursivelyDeleteFile(netLogDir);
         assertThat(netLogDir.exists()).isFalse();
     }
 
@@ -1721,7 +1736,9 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "No caches support for fallback implementation")
     public void testEnableHttpCacheDisk() throws Exception {
         CronetEngine cronetEngine =
                 createCronetEngineWithCache(CronetEngine.Builder.HTTP_CACHE_DISK);
@@ -1735,7 +1752,10 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @SkipPresubmit(reason = "b/293141085 flaky test")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "No caches support for fallback implementation")
     public void testNoConcurrentDiskUsage() throws Exception {
         CronetEngine cronetEngine =
                 createCronetEngineWithCache(CronetEngine.Builder.HTTP_CACHE_DISK);
@@ -1756,7 +1776,10 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @SkipPresubmit(reason = "b/293141085 flaky test")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "No caches support for fallback implementation")
     public void testEnableHttpCacheDiskNoHttp() throws Exception {
         CronetEngine cronetEngine =
                 createCronetEngineWithCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP);
@@ -1777,7 +1800,9 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "No caches support for fallback implementation")
     public void testDisableCache() throws Exception {
         CronetEngine cronetEngine =
                 createCronetEngineWithCache(CronetEngine.Builder.HTTP_CACHE_DISK);
@@ -1816,7 +1841,9 @@
 
     @Test
     @SmallTest
-    @SkipPresubmit(reason = "b/293141085 Tests that enable disk cache are flaky")
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK},
+            reason = "No caches support for fallback implementation")
     public void testEnableHttpCacheDiskNewEngine() throws Exception {
         CronetEngine cronetEngine =
                 createCronetEngineWithCache(CronetEngine.Builder.HTTP_CACHE_DISK);
@@ -2038,6 +2065,9 @@
 
     @Test
     @SmallTest
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK, CronetImplementation.AOSP_PLATFORM},
+            reason = "LibraryLoader is supported only by the native implementation")
     public void testSetLibraryLoaderIsEnforcedByDefaultEmbeddedProvider() throws Exception {
         CronetEngine.Builder builder =
                 new CronetEngine.Builder(mTestRule.getTestFramework().getContext());
@@ -2058,6 +2088,9 @@
 
     @Test
     @SmallTest
+    @IgnoreFor(
+            implementations = {CronetImplementation.FALLBACK, CronetImplementation.AOSP_PLATFORM},
+            reason = "LibraryLoader is supported only by the native implementation")
     public void testSetLibraryLoaderIsIgnoredInNativeCronetEngineBuilderImpl() throws Exception {
         CronetEngine.Builder builder =
                 new CronetEngine.Builder(
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestTest.java
index 876921d..1bcb2fc 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestTest.java
@@ -25,7 +25,6 @@
 import org.jni_zero.NativeMethods;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -43,7 +42,9 @@
 import org.chromium.net.impl.CronetUrlRequest;
 import org.chromium.net.impl.NetworkExceptionImpl;
 import org.chromium.net.impl.UrlResponseInfoImpl;
+import org.chromium.net.test.EmbeddedTestServer;
 import org.chromium.net.test.FailurePhase;
+import org.chromium.net.test.ServerCertificate;
 
 import java.io.IOException;
 import java.net.ConnectException;
@@ -1022,19 +1023,21 @@
      */
     @Test
     @SmallTest
-    @Ignore ("b/267353182 "
-            + "Figure out why EmbeddedTestServer was used instead of MockUrlForSSLCertificateError"
-            + "since this is similar test to one above")
     @IgnoreFor(
             implementations = {CronetImplementation.FALLBACK},
             reason = "crbug.com/1495320: Refactor error checking")
     public void testSSLCertificateError() throws Exception {
-        // EmbeddedTestServer sslServer = EmbeddedTestServer.createAndStartHTTPSServer(
-             //   mTestRule.getTestFramework().getContext(), ServerCertificate.CERT_EXPIRED);
+        EmbeddedTestServer sslServer =
+                EmbeddedTestServer.createAndStartHTTPSServer(
+                        mTestRule.getTestFramework().getContext(), ServerCertificate.CERT_EXPIRED);
 
         TestUrlRequestCallback callback = new TestUrlRequestCallback();
-        UrlRequest.Builder builder = mTestRule.getTestFramework().getEngine().newUrlRequestBuilder(
-                /* sslServer.getURL("/") */ null, callback, callback.getExecutor());
+        UrlRequest.Builder builder =
+                mTestRule
+                        .getTestFramework()
+                        .getEngine()
+                        .newUrlRequestBuilder(
+                                sslServer.getURL("/"), callback, callback.getExecutor());
 
         TestUploadDataProvider dataProvider =
                 new TestUploadDataProvider(
@@ -1054,7 +1057,7 @@
         mTestRule.assertCronetInternalErrorCode((NetworkException) callback.mError, -201);
         assertThat(callback.mResponseStep).isEqualTo(ResponseStep.ON_FAILED);
 
-        // sslServer.stopAndDestroyServer();
+        sslServer.stopAndDestroyServer();
     }
 
     /** Checks that the buffer is updated correctly, when starting at an offset. */
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/DiskStorageTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/DiskStorageTest.java
index d971d4f..98dde3a 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/DiskStorageTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/DiskStorageTest.java
@@ -13,7 +13,6 @@
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.filters.SmallTest;
-
 import com.android.testutils.SkipPresubmit;
 
 import org.junit.After;
@@ -22,7 +21,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import org.chromium.base.FileUtils;
 import org.chromium.base.PathUtils;
 import org.chromium.base.test.util.DoNotBatch;
 import org.chromium.net.CronetTestRule.CronetImplementation;
@@ -57,7 +55,7 @@
     @After
     public void tearDown() throws Exception {
         if (mReadOnlyStoragePath != null) {
-            FileUtils.recursivelyDeleteFile(new File(mReadOnlyStoragePath), FileUtils.DELETE_ALL);
+            FileUtils.recursivelyDeleteFile(new File(mReadOnlyStoragePath));
         }
         NativeTestServer.shutdownNativeTestServer();
     }
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java
index 01a63a5..6e0ae49 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java
@@ -21,13 +21,11 @@
 
 import com.android.testutils.SkipPresubmit;
 
-import org.json.JSONException;
 import org.jni_zero.JNINamespace;
 import org.jni_zero.NativeMethods;
 import org.json.JSONObject;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -41,6 +39,7 @@
 import org.chromium.net.CronetTestRule.DisableAutomaticNetLog;
 import org.chromium.net.CronetTestRule.IgnoreFor;
 import org.chromium.net.impl.CronetUrlRequestContext;
+import org.chromium.net.test.EmbeddedTestServer;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -96,7 +95,6 @@
 
     @Test
     @MediumTest
-    @Ignore("b/275345637 Needs HTTP2 Server")
     @DisableAutomaticNetLog(reason = "Test is targeting NetLog")
     // Tests that NetLog writes effective experimental options to NetLog.
     public void testNetLog() throws Exception {
@@ -158,7 +156,6 @@
 
     @Test
     @MediumTest
-    @Ignore("b/275345637 Needs HTTP2 Server")
     public void testSetSSLKeyLogFile() throws Exception {
         String url = Http2TestServer.getEchoMethodUrl();
         File dir = new File(PathUtils.getDataDirectory());
@@ -234,9 +231,10 @@
     // Tests that basic Cronet functionality works when host cache persistence is enabled, and that
     // persistence works.
     public void testHostCachePersistence() throws Exception {
-        assertThat(NativeTestServer.startNativeTestServer(mTestRule.getTestFramework().getContext())).isTrue();
+        EmbeddedTestServer testServer =
+                EmbeddedTestServer.createAndStartServer(mTestRule.getTestFramework().getContext());
 
-        String realUrl = NativeTestServer.getSuccessURL();
+        String realUrl = testServer.getURL("/echo?status=200");
         URL javaUrl = new URL(realUrl);
         String realHost = javaUrl.getHost();
         int realPort = javaUrl.getPort();
@@ -288,7 +286,6 @@
         callback.blockForDone();
         assertThat(callback.getResponseInfoWithChecks()).hasHttpStatusCodeThat().isEqualTo(200);
         context.shutdown();
-        NativeTestServer.shutdownNativeTestServer();
     }
 
     @Test
@@ -316,7 +313,6 @@
 
     @Test
     @MediumTest
-    @Ignore("b/275345637 Needs HTTP2 Server")
     public void testDetectBrokenConnection() throws Exception {
         String url = Http2TestServer.getEchoMethodUrl();
         mTestRule
@@ -348,7 +344,6 @@
     @DisabledTest(message = "crbug.com/1320725")
     @Test
     @LargeTest
-    @Ignore("b/275345637 Needs HTTP2 Server")
     public void testDetectBrokenConnectionOnNetworkFailure() throws Exception {
         // HangingRequestUrl stops the server from replying until mHangingUrlLatch is opened,
         // simulating a network failure between client and server.
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/Http2TestServer.java b/components/cronet/android/test/javatests/src/org/chromium/net/Http2TestServer.java
deleted file mode 100644
index 55c3601..0000000
--- a/components/cronet/android/test/javatests/src/org/chromium/net/Http2TestServer.java
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Copyright (C) 2023 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.chromium.net;
-
-import android.content.Context;
-
-import java.io.File;
-import java.util.concurrent.CountDownLatch;
-
-
-/**
- * This is a STUB AOSP-only version of org.chromium.net.test.Http2TestServer. This will allow us
- * to not have comment out the tests but rather simply @Ignore them till we figure out what the
- * plan for a H2 server is.
- */
-public final class Http2TestServer {
-
-    public static boolean shutdownHttp2TestServer() throws Exception {
-        return true;
-    }
-
-    public static String getServerHost() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static int getServerPort() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static String getServerUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static ReportingCollector getReportingCollector() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static String getEchoAllHeadersUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static String getEchoHeaderUrl(String headerName) {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static String getEchoMethodUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * When using this you must provide a CountDownLatch in the call to startHttp2TestServer.
-     * The request handler will continue to hang until the provided CountDownLatch reaches 0.
-     *
-     * @return url of the server resource which will hang indefinitely.
-     */
-    public static String getHangingRequestUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * @return url of the server resource which will echo every received stream data frame.
-     */
-    public static String getEchoStreamUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * @return url of the server resource which will echo request headers as response trailers.
-     */
-    public static String getEchoTrailersUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * @return url of a brotli-encoded server resource.
-     */
-    public static String getServeSimpleBrotliResponse() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * @return url of the reporting collector
-     */
-    public static String getReportingCollectorUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * @return url of a resource that includes Reporting and NEL policy headers in its response
-     */
-    public static String getSuccessWithNELHeadersUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    /**
-     * @return url of a resource that sends response headers with the same key
-     */
-    public static String getCombinedHeadersUrl() {
-        throw new UnsupportedOperationException("This is a stub class");
-    }
-
-    public static boolean startHttp2TestServer(Context context) throws Exception {
-        return true;
-    }
-
-    public static boolean startHttp2TestServer(Context context, CountDownLatch hangingUrlLatch)
-            throws Exception {
-        return true;
-    }
-
-    private Http2TestServer() {
-    }
-
-    private static class ReportingCollector {
-    }
-}
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/NQETest.java b/components/cronet/android/test/javatests/src/org/chromium/net/NQETest.java
index 1e490a7..7710eca 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/NQETest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/NQETest.java
@@ -18,7 +18,6 @@
 import org.json.JSONObject;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -30,6 +29,7 @@
 import org.chromium.net.CronetTestRule.CronetImplementation;
 import org.chromium.net.CronetTestRule.IgnoreFor;
 import org.chromium.net.MetricsTestUtil.TestExecutor;
+import org.chromium.net.test.EmbeddedTestServer;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -50,6 +50,7 @@
 
     @Rule public final CronetTestRule mTestRule = CronetTestRule.withManualEngineStartup();
 
+    private EmbeddedTestServer mTestServer;
     private String mUrl;
 
     // Thread on which network quality listeners should be notified.
@@ -57,13 +58,14 @@
 
     @Before
     public void setUp() throws Exception {
-        assertThat(NativeTestServer.startNativeTestServer(mTestRule.getTestFramework().getContext())).isTrue();
-        mUrl = NativeTestServer.getSuccessURL();
+        mTestServer =
+                EmbeddedTestServer.createAndStartServer(mTestRule.getTestFramework().getContext());
+        mUrl = mTestServer.getURL("/echo?status=200");
     }
 
     @After
     public void tearDown() throws Exception {
-        NativeTestServer.shutdownNativeTestServer();
+        mTestServer.stopAndDestroyServer();
     }
 
     private class ExecutorThreadFactory implements ThreadFactory {
@@ -277,7 +279,6 @@
 
     @Test
     @SmallTest
-    @Ignore("b/267353182 Permission denied error")
     public void testPrefsWriteRead() throws Exception {
         // When the loop is run for the first time, network quality is written to the disk. The
         // test verifies that in the next loop, the network quality is read back.
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java
index b8d7442..2f2959e 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java
@@ -13,7 +13,6 @@
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.filters.LargeTest;
 import androidx.test.filters.SmallTest;
-
 import com.android.testutils.SkipPresubmit;
 
 import org.json.JSONObject;
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/RequestFinishedInfoTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/RequestFinishedInfoTest.java
index 1b0b0fd..d55e8af 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/RequestFinishedInfoTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/RequestFinishedInfoTest.java
@@ -26,6 +26,7 @@
 import org.chromium.net.CronetTestRule.RequiresMinApi;
 import org.chromium.net.MetricsTestUtil.TestExecutor;
 import org.chromium.net.impl.CronetMetrics;
+import org.chromium.net.test.EmbeddedTestServer;
 
 import java.util.ArrayList;
 import java.util.Date;
@@ -43,6 +44,7 @@
 public class RequestFinishedInfoTest {
     @Rule public final CronetTestRule mTestRule = CronetTestRule.withAutomaticEngineStartup();
 
+    private EmbeddedTestServer mTestServer;
     private String mUrl;
 
     // A subclass of TestRequestFinishedListener to additionally assert that UrlRequest.Callback's
@@ -68,13 +70,14 @@
 
     @Before
     public void setUp() throws Exception {
-        assertThat(NativeTestServer.startNativeTestServer(mTestRule.getTestFramework().getContext())).isTrue();
-        mUrl = NativeTestServer.getSuccessURL();
+        mTestServer =
+                EmbeddedTestServer.createAndStartServer(mTestRule.getTestFramework().getContext());
+        mUrl = mTestServer.getURL("/echo?status=200");
     }
 
     @After
     public void tearDown() throws Exception {
-        NativeTestServer.shutdownNativeTestServer();
+        mTestServer.stopAndDestroyServer();
     }
 
     static class DirectExecutor implements Executor {
diff --git a/components/cronet/android/test/mock_cert_verifier.cc b/components/cronet/android/test/mock_cert_verifier.cc
index 3fac267..21558fb 100644
--- a/components/cronet/android/test/mock_cert_verifier.cc
+++ b/components/cronet/android/test/mock_cert_verifier.cc
@@ -62,7 +62,7 @@
   for (const auto& cert : certs) {
     net::CertVerifyResult verify_result;
     verify_result.verified_cert =
-        net::ImportCertFromFile(test_data_dir, cert);
+        net::ImportCertFromFile(net::GetTestCertsDirectory(), cert);
 
     // By default, HPKP verification is enabled for known trust roots only.
     verify_result.is_issued_by_known_root = jknown_root;
diff --git a/components/cronet/android/test/proguard.cfg b/components/cronet/android/test/proguard.cfg
index 5df6efe..797504a 100644
--- a/components/cronet/android/test/proguard.cfg
+++ b/components/cronet/android/test/proguard.cfg
@@ -11,6 +11,10 @@
 # https://android.googlesource.com/platform/sdk/+/marshmallow-mr1-release/files/proguard-android.txt#54
 -dontwarn android.support.**
 
+# Do not obfuscate this class for testing since some of the tests check the class
+# name in order to check that an instantiated engine is the Java one.
+-keepnames class org.chromium.net.impl.JavaCronetEngine
+
 # These classes should be explicitly kept to avoid failure if
 # class/merging/horizontal proguard optimization is enabled.
 # NOTE: make sure that only test classes are added to this list.
diff --git a/components/cronet/android/test/quic_test_server.cc b/components/cronet/android/test/quic_test_server.cc
index d0c65cb..6e6a0da 100644
--- a/components/cronet/android/test/quic_test_server.cc
+++ b/components/cronet/android/test/quic_test_server.cc
@@ -71,12 +71,12 @@
   quic::QuicConfig config;
 
   // Set up server certs.
+  base::FilePath directory = test_data_dir.Append("net/data/ssl/certificates");
   std::unique_ptr<net::ProofSourceChromium> proof_source(
       new net::ProofSourceChromium());
-  CHECK(proof_source->Initialize(
-        test_data_dir.Append("quic-chain.pem"),
-        test_data_dir.Append("quic-leaf-cert.key"),
-        base::FilePath()));
+  CHECK(proof_source->Initialize(directory.Append("quic-chain.pem"),
+                                 directory.Append("quic-leaf-cert.key"),
+                                 base::FilePath()));
   g_quic_server = std::make_unique<net::QuicSimpleServer>(
       std::move(proof_source), config,
       quic::QuicCryptoServerConfig::ConfigOptions(),
diff --git a/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/MissingNativeLibraryTest.java b/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/MissingNativeLibraryTest.java
index 9701ac5..fcebecd 100644
--- a/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/MissingNativeLibraryTest.java
+++ b/components/cronet/android/test/smoketests/src/org/chromium/net/smoke/MissingNativeLibraryTest.java
@@ -19,6 +19,7 @@
 import org.junit.runner.RunWith;
 
 import org.chromium.net.CronetEngine;
+import org.chromium.net.CronetProvider;
 import org.chromium.net.ExperimentalCronetEngine;
 
 import java.util.List;
diff --git a/components/cronet/android/test/src/org/chromium/net/CronetTestUtil.java b/components/cronet/android/test/src/org/chromium/net/CronetTestUtil.java
index 20225d8..c85e3a3 100644
--- a/components/cronet/android/test/src/org/chromium/net/CronetTestUtil.java
+++ b/components/cronet/android/test/src/org/chromium/net/CronetTestUtil.java
@@ -5,8 +5,6 @@
 package org.chromium.net;
 
 import android.net.Network;
-import org.chromium.net.CronetEngine;
-import org.chromium.net.UrlRequest;
 
 import org.jni_zero.JNINamespace;
 import org.jni_zero.NativeMethods;
@@ -92,11 +90,11 @@
     }
 
     public static void setMockCertVerifierForTesting(
-             ExperimentalCronetEngine.Builder builder, long mockCertVerifier) {
+            ExperimentalCronetEngine.Builder builder, long mockCertVerifier) {
         getCronetEngineBuilderImpl(builder).setMockCertVerifierForTesting(mockCertVerifier);
     }
 
-    public static CronetEngineBuilderImpl getCronetEngineBuilderImpl(
+    static CronetEngineBuilderImpl getCronetEngineBuilderImpl(
             ExperimentalCronetEngine.Builder builder) {
         return (CronetEngineBuilderImpl) ((ExperimentalOptionsTranslatingCronetEngineBuilder)
                                                   builder.getBuilderDelegate())
diff --git a/components/cronet/android/test/src/org/chromium/net/MockCertVerifier.java b/components/cronet/android/test/src/org/chromium/net/MockCertVerifier.java
index 80ae37f..307ec81 100644
--- a/components/cronet/android/test/src/org/chromium/net/MockCertVerifier.java
+++ b/components/cronet/android/test/src/org/chromium/net/MockCertVerifier.java
@@ -7,7 +7,6 @@
 import org.jni_zero.JNINamespace;
 import org.jni_zero.NativeMethods;
 
-import org.chromium.base.ContextUtils;
 import org.chromium.base.test.util.UrlUtils;
 
 /**
@@ -26,8 +25,8 @@
      * @return a pointer to the newly created net::MockCertVerifier.
      */
     public static long createMockCertVerifier(String[] certs, boolean knownRoot) {
-        return MockCertVerifierJni.get().createMockCertVerifier(certs, knownRoot,
-                TestFilesInstaller.getInstalledPath(ContextUtils.getApplicationContext()));
+        return MockCertVerifierJni.get().createMockCertVerifier(
+                certs, knownRoot, UrlUtils.getIsolatedTestRoot());
     }
 
     /**
@@ -40,7 +39,7 @@
     }
 
     @NativeMethods("cronet_tests")
-    public interface Natives {
+    interface Natives {
         long createMockCertVerifier(String[] certs, boolean knownRoot, String testDataDir);
         long createFreeForAllMockCertVerifier();
     }
diff --git a/components/cronet/android/test/src/org/chromium/net/MockUrlRequestJobFactory.java b/components/cronet/android/test/src/org/chromium/net/MockUrlRequestJobFactory.java
index 00d3feb..935ece4 100644
--- a/components/cronet/android/test/src/org/chromium/net/MockUrlRequestJobFactory.java
+++ b/components/cronet/android/test/src/org/chromium/net/MockUrlRequestJobFactory.java
@@ -93,7 +93,7 @@
     }
 
     @NativeMethods("cronet_tests")
-    public interface Natives {
+    interface Natives {
         long addUrlInterceptors(long requestContextAdapter);
         void removeUrlInterceptorJobFactory(long interceptorHandle);
         String getMockUrlWithFailure(int phase, int netError);
diff --git a/components/cronet/android/test/src/org/chromium/net/QuicTestServer.java b/components/cronet/android/test/src/org/chromium/net/QuicTestServer.java
index 3e60d8a..fd6e6fe 100644
--- a/components/cronet/android/test/src/org/chromium/net/QuicTestServer.java
+++ b/components/cronet/android/test/src/org/chromium/net/QuicTestServer.java
@@ -33,10 +33,8 @@
             throw new IllegalStateException("Quic server is already running");
         }
         TestFilesInstaller.installIfNeeded(context);
-        String test_dir = TestFilesInstaller.getInstalledPath(context);
-        // TestFilesInstaller implementation changed such that the test files and test data dir
-        // are now the same. See aosp/2475670
-        QuicTestServerJni.get().startQuicTestServer(test_dir, test_dir);
+        QuicTestServerJni.get().startQuicTestServer(
+                TestFilesInstaller.getInstalledPath(context), UrlUtils.getIsolatedTestRoot());
         sServerRunning = true;
     }
 
diff --git a/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java b/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java
index 1e65dd5..4227082 100644
--- a/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java
+++ b/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java
@@ -5,71 +5,29 @@
 package org.chromium.net;
 
 import android.content.Context;
-import android.content.res.AssetManager;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Files;
-import java.nio.file.StandardCopyOption;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Queue;
+
+import org.chromium.base.test.util.UrlUtils;
 
 /**
- * Helper class to install test files. This moves the files to an accessible storage directory.
+ * Helper class to install test files.
  */
 public final class TestFilesInstaller {
-    private static final String INSTALLED_PATH_SUFFIX = "cronet_test_data";
+    // Name of the asset directory in which test files are stored.
+    private static final String TEST_FILE_ASSET_PATH = "components/cronet/testing/test_server/data";
 
     /**
      * Installs test files if files have not been installed.
      */
     public static void installIfNeeded(Context context) {
-        File testDataDir = new File(context.getCacheDir(), INSTALLED_PATH_SUFFIX);
-
-        if (testDataDir.exists()) {
-            return;
-        }
-
-        AssetManager assets = context.getAssets();
-        try {
-            for (String assetPath : listAllAssets(assets)) {
-                File copiedAssetFile = new File(testDataDir, assetPath);
-                copiedAssetFile.getParentFile().mkdirs();
-                try (InputStream inputStream = assets.open(assetPath)) {
-                    Files.copy(inputStream, copiedAssetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
-                }
-            }
-        } catch (IOException e) {
-            throw new AssertionError("Failed to copy test files", e);
-        }
+        // Do nothing.
+        // NOTE(pauljensen): This hook is used (overridden) when tests are run in other
+        // configurations, so it should not be removed.
     }
 
     /**
      * Returns the installed path of the test files.
      */
     public static String getInstalledPath(Context context) {
-        return new File(context.getCacheDir(), INSTALLED_PATH_SUFFIX).getAbsolutePath();
-    }
-
-    private static List<String> listAllAssets(AssetManager assets) throws IOException {
-        Queue<String> toProcess = new ArrayDeque<>();
-        toProcess.add("");
-        List<String> result = new ArrayList<>();
-        while (!toProcess.isEmpty()) {
-            String parent = toProcess.remove();
-            String[] children = assets.list(parent);
-            if (children.length > 0) {
-                // It's a folder
-                for (String child : children) {
-                    toProcess.add(new File(parent, child).toString());
-                }
-            } else if (!parent.isEmpty()) {
-                // It's a file
-                result.add(parent);
-            } // Else it's the empty root folder, in which case do nothing
-        }
-        return result;
+        return UrlUtils.getIsolatedTestRoot() + "/" + TEST_FILE_ASSET_PATH;
     }
 }
diff --git a/components/cronet/android/test/src/org/chromium/net/TestUploadDataStreamHandler.java b/components/cronet/android/test/src/org/chromium/net/TestUploadDataStreamHandler.java
index 2c166f5..5010d4c 100644
--- a/components/cronet/android/test/src/org/chromium/net/TestUploadDataStreamHandler.java
+++ b/components/cronet/android/test/src/org/chromium/net/TestUploadDataStreamHandler.java
@@ -7,7 +7,6 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import android.content.Context;
-import org.chromium.net.CronetEngine;
 import android.os.ConditionVariable;
 
 import org.jni_zero.CalledByNative;