| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 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; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.os.Environment; | |
| 9 | |
| 10 import static junit.framework.Assert.assertEquals; | |
| 11 import static junit.framework.Assert.assertTrue; | |
| 12 | |
| 13 import org.chromium.base.Log; | |
| 14 import org.chromium.base.PathUtils; | |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | |
| 16 import org.chromium.net.impl.CronetEngineBase; | |
| 17 | |
| 18 import java.io.File; | |
| 19 import java.net.URLStreamHandlerFactory; | |
| 20 | |
| 21 /** | |
| 22 * Framework for testing Cronet. | |
| 23 */ | |
| 24 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") | |
| 25 public class CronetTestFramework { | |
| 26 private static final String TAG = CronetTestFramework.class.getSimpleName(); | |
| 27 | |
| 28 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; | |
| 29 public static final String POST_DATA_KEY = "postData"; | |
| 30 public static final String CACHE_KEY = "cache"; | |
| 31 public static final String SDCH_KEY = "sdch"; | |
| 32 public static final String LIBRARY_INIT_KEY = "libraryInit"; | |
| 33 | |
| 34 // Uses disk cache. | |
| 35 public static final String CACHE_DISK = "disk"; | |
| 36 | |
| 37 // Uses disk cache but does not store http data. | |
| 38 public static final String CACHE_DISK_NO_HTTP = "diskNoHttp"; | |
| 39 | |
| 40 // Uses in-memory cache. | |
| 41 public static final String CACHE_IN_MEMORY = "memory"; | |
| 42 | |
| 43 // Enables Sdch. | |
| 44 public static final String SDCH_ENABLE = "enable"; | |
| 45 | |
| 46 /** | |
| 47 * Library init type strings to use along with {@link #LIBRARY_INIT_KEY}. | |
| 48 * If unspecified, {@link LibraryInitType#CRONET} will be used. | |
| 49 */ | |
| 50 public static final class LibraryInitType { | |
| 51 // Initializes Cronet Async API. | |
| 52 public static final String CRONET = "cronet"; | |
| 53 // Initializes Cronet HttpURLConnection API. | |
| 54 public static final String HTTP_URL_CONNECTION = "http_url_connection"; | |
| 55 // Do not initialize. | |
| 56 public static final String NONE = "none"; | |
| 57 | |
| 58 private LibraryInitType() {} | |
| 59 } | |
| 60 | |
| 61 public URLStreamHandlerFactory mStreamHandlerFactory; | |
| 62 public CronetEngineBase mCronetEngine; | |
| 63 | |
| 64 private final String[] mCommandLine; | |
| 65 private final Context mContext; | |
| 66 | |
| 67 // CronetEngine.Builder used for this activity. | |
| 68 private ExperimentalCronetEngine.Builder mCronetEngineBuilder; | |
| 69 | |
| 70 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio
n. | |
| 71 @SuppressFBWarnings("EI_EXPOSE_REP2") | |
| 72 public CronetTestFramework(String appUrl, String[] commandLine, Context cont
ext, | |
| 73 ExperimentalCronetEngine.Builder builder) { | |
| 74 mCommandLine = commandLine; | |
| 75 mContext = context; | |
| 76 | |
| 77 // Print out extra arguments passed in starting this activity. | |
| 78 if (commandLine != null) { | |
| 79 assertEquals(0, commandLine.length % 2); | |
| 80 for (int i = 0; i < commandLine.length / 2; i++) { | |
| 81 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], | |
| 82 commandLine[i * 2 + 1]); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 // Initializes CronetEngine.Builder from commandLine args. | |
| 87 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild
er(builder); | |
| 88 | |
| 89 String initString = getCommandLineArg(LIBRARY_INIT_KEY); | |
| 90 | |
| 91 if (initString == null) { | |
| 92 initString = LibraryInitType.CRONET; | |
| 93 } | |
| 94 | |
| 95 switch (initString) { | |
| 96 case LibraryInitType.NONE: | |
| 97 break; | |
| 98 case LibraryInitType.HTTP_URL_CONNECTION: | |
| 99 mCronetEngine = initCronetEngine(); | |
| 100 mStreamHandlerFactory = mCronetEngine.createURLStreamHandlerFact
ory(); | |
| 101 break; | |
| 102 default: | |
| 103 mCronetEngine = initCronetEngine(); | |
| 104 // Start collecting metrics. | |
| 105 mCronetEngine.getGlobalMetricsDeltas(); | |
| 106 break; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Prepares the path for the test storage (http cache, QUIC server info). | |
| 112 */ | |
| 113 public static void prepareTestStorage(Context context) { | |
| 114 File storage = new File(getTestStorageDirectory()); | |
| 115 if (storage.exists()) { | |
| 116 assertTrue(recursiveDelete(storage)); | |
| 117 } | |
| 118 ensureTestStorageExists(); | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * Returns the path for the test storage (http cache, QUIC server info). | |
| 123 * NOTE: Does not ensure it exists; tests should use {@link #getTestStorage}
. | |
| 124 */ | |
| 125 private static String getTestStorageDirectory() { | |
| 126 return PathUtils.getDataDirectory() + "/test_storage"; | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * Ensures test storage directory exists, i.e. creates one if it does not ex
ist. | |
| 131 */ | |
| 132 private static void ensureTestStorageExists() { | |
| 133 File storage = new File(getTestStorageDirectory()); | |
| 134 if (!storage.exists()) { | |
| 135 assertTrue(storage.mkdir()); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 /** | |
| 140 * Returns the path for the test storage (http cache, QUIC server info). | |
| 141 * Also ensures it exists. | |
| 142 */ | |
| 143 static String getTestStorage(Context context) { | |
| 144 ensureTestStorageExists(); | |
| 145 return getTestStorageDirectory(); | |
| 146 } | |
| 147 | |
| 148 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") | |
| 149 private static boolean recursiveDelete(File path) { | |
| 150 if (path.isDirectory()) { | |
| 151 for (File c : path.listFiles()) { | |
| 152 if (!recursiveDelete(c)) { | |
| 153 return false; | |
| 154 } | |
| 155 } | |
| 156 } | |
| 157 return path.delete(); | |
| 158 } | |
| 159 | |
| 160 ExperimentalCronetEngine.Builder getCronetEngineBuilder() { | |
| 161 return mCronetEngineBuilder; | |
| 162 } | |
| 163 | |
| 164 private ExperimentalCronetEngine.Builder initializeCronetEngineBuilderWithPr
esuppliedBuilder( | |
| 165 ExperimentalCronetEngine.Builder builder) { | |
| 166 return createCronetEngineBuilderWithPresuppliedBuilder(mContext, builder
); | |
| 167 } | |
| 168 | |
| 169 ExperimentalCronetEngine.Builder createCronetEngineBuilder(Context context)
{ | |
| 170 return createCronetEngineBuilderWithPresuppliedBuilder(context, null); | |
| 171 } | |
| 172 | |
| 173 private ExperimentalCronetEngine.Builder createCronetEngineBuilderWithPresup
pliedBuilder( | |
| 174 Context context, ExperimentalCronetEngine.Builder cronetEngineBuilde
r) { | |
| 175 if (cronetEngineBuilder == null) { | |
| 176 cronetEngineBuilder = new ExperimentalCronetEngine.Builder(context); | |
| 177 cronetEngineBuilder.enableHttp2(true).enableQuic(true); | |
| 178 } | |
| 179 | |
| 180 String cacheString = getCommandLineArg(CACHE_KEY); | |
| 181 if (CACHE_DISK.equals(cacheString)) { | |
| 182 cronetEngineBuilder.setStoragePath(getTestStorage(context)); | |
| 183 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); | |
| 184 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | |
| 185 cronetEngineBuilder.setStoragePath(getTestStorage(context)); | |
| 186 cronetEngineBuilder.enableHttpCache( | |
| 187 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); | |
| 188 } else if (CACHE_IN_MEMORY.equals(cacheString)) { | |
| 189 cronetEngineBuilder.enableHttpCache( | |
| 190 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); | |
| 191 } | |
| 192 | |
| 193 String sdchString = getCommandLineArg(SDCH_KEY); | |
| 194 if (SDCH_ENABLE.equals(sdchString)) { | |
| 195 cronetEngineBuilder.enableSdch(true); | |
| 196 } | |
| 197 | |
| 198 return cronetEngineBuilder; | |
| 199 } | |
| 200 | |
| 201 // Helper function to initialize Cronet engine. Also used in testing. | |
| 202 public CronetEngineBase initCronetEngine() { | |
| 203 return (CronetEngineBase) mCronetEngineBuilder.build(); | |
| 204 } | |
| 205 | |
| 206 private String getCommandLineArg(String key) { | |
| 207 if (mCommandLine != null) { | |
| 208 for (int i = 0; i < mCommandLine.length; ++i) { | |
| 209 if (mCommandLine[i].equals(key)) { | |
| 210 return mCommandLine[++i]; | |
| 211 } | |
| 212 } | |
| 213 } | |
| 214 return null; | |
| 215 } | |
| 216 | |
| 217 public void startNetLog() { | |
| 218 if (mCronetEngine != null) { | |
| 219 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect
ory().getPath() | |
| 220 + "/cronet_sample_netlog_new_api.json", | |
| 221 false); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 public void stopNetLog() { | |
| 226 if (mCronetEngine != null) { | |
| 227 mCronetEngine.stopNetLog(); | |
| 228 } | |
| 229 } | |
| 230 } | |
| OLD | NEW |