1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.net.smoke; 6 7 import android.content.Context; 8 9 import androidx.test.core.app.ApplicationProvider; 10 11 import org.junit.Assert; 12 import org.junit.rules.TestRule; 13 import org.junit.runner.Description; 14 import org.junit.runners.model.Statement; 15 16 import org.chromium.net.CronetEngine; 17 import org.chromium.net.ExperimentalCronetEngine; 18 import org.chromium.net.UrlResponseInfo; 19 20 /** 21 * Base test class. This class should not import any classes from the org.chromium.base package. 22 */ 23 public class CronetSmokeTestRule implements TestRule { 24 /** 25 * The key in the string resource file that specifies {@link TestSupport} that should 26 * be instantiated. 27 */ 28 private static final String SUPPORT_IMPL_RES_KEY = "TestSupportImplClass"; 29 30 public ExperimentalCronetEngine.Builder mCronetEngineBuilder; 31 public CronetEngine mCronetEngine; 32 public TestSupport mTestSupport; 33 34 @Override apply(final Statement base, Description desc)35 public Statement apply(final Statement base, Description desc) { 36 return new Statement() { 37 @Override 38 public void evaluate() throws Throwable { 39 ruleSetUp(); 40 base.evaluate(); 41 ruleTearDown(); 42 } 43 }; 44 } 45 46 public TestSupport getTestSupport() { 47 return mTestSupport; 48 } 49 50 public CronetEngine getCronetEngine() { 51 return mCronetEngine; 52 } 53 54 public ExperimentalCronetEngine.Builder getCronetEngineBuilder() { 55 return mCronetEngineBuilder; 56 } 57 58 private void ruleSetUp() throws Exception { 59 mCronetEngineBuilder = 60 new ExperimentalCronetEngine.Builder(ApplicationProvider.getApplicationContext()); 61 initTestSupport(); 62 } 63 64 private void ruleTearDown() throws Exception { 65 if (mCronetEngine != null) { 66 mCronetEngine.shutdown(); 67 } 68 } 69 70 public void initCronetEngine() { 71 mCronetEngine = mCronetEngineBuilder.build(); 72 } 73 74 static void assertSuccessfulNonEmptyResponse(SmokeTestRequestCallback callback, String url) { 75 // Check the request state 76 if (callback.getFinalState() == SmokeTestRequestCallback.State.Failed) { 77 throw new RuntimeException( 78 "The request failed with an error", callback.getFailureError()); 79 } 80 Assert.assertEquals(SmokeTestRequestCallback.State.Succeeded, callback.getFinalState()); 81 82 // Check the response info 83 UrlResponseInfo responseInfo = callback.getResponseInfo(); 84 Assert.assertNotNull(responseInfo); 85 Assert.assertFalse(responseInfo.wasCached()); 86 Assert.assertEquals(url, responseInfo.getUrl()); 87 Assert.assertEquals( 88 url, responseInfo.getUrlChain().get(responseInfo.getUrlChain().size() - 1)); 89 Assert.assertEquals(200, responseInfo.getHttpStatusCode()); 90 Assert.assertTrue(responseInfo.toString().length() > 0); 91 } 92 93 static void assertJavaEngine(CronetEngine engine) { 94 Assert.assertNotNull(engine); 95 Assert.assertEquals("org.chromium.net.impl.JavaCronetEngine", engine.getClass().getName()); 96 } 97 98 static void assertNativeEngine(CronetEngine engine) { 99 Assert.assertNotNull(engine); 100 Assert.assertEquals( 101 "org.chromium.net.impl.CronetUrlRequestContext", engine.getClass().getName()); 102 } 103 104 /** 105 * Instantiates a concrete implementation of {@link TestSupport} interface. 106 * The name of the implementation class is determined dynamically by reading 107 * the value of |TestSupportImplClass| from the Android string resource file. 108 * 109 * @throws Exception if the class cannot be instantiated. 110 */ 111 @SuppressWarnings("DiscouragedApi") 112 private void initTestSupport() throws Exception { 113 Context ctx = ApplicationProvider.getApplicationContext(); 114 String packageName = ctx.getPackageName(); 115 int resId = ctx.getResources().getIdentifier(SUPPORT_IMPL_RES_KEY, "string", packageName); 116 String className = ctx.getResources().getString(resId); 117 Class<? extends TestSupport> cl = Class.forName(className).asSubclass(TestSupport.class); 118 mTestSupport = cl.newInstance(); 119 } 120 } 121