| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.ConditionVariable; | |
| 9 import android.os.Environment; | 8 import android.os.Environment; |
| 10 | 9 |
| 11 import static junit.framework.Assert.assertEquals; | 10 import static junit.framework.Assert.assertEquals; |
| 12 import static junit.framework.Assert.assertTrue; | 11 import static junit.framework.Assert.assertTrue; |
| 13 | 12 |
| 14 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
| 15 import org.chromium.base.PathUtils; | 14 import org.chromium.base.PathUtils; |
| 16 import org.chromium.base.annotations.SuppressFBWarnings; | 15 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 import org.chromium.net.impl.CronetEngineBase; |
| 17 | 17 |
| 18 import java.io.ByteArrayInputStream; | |
| 19 import java.io.File; | 18 import java.io.File; |
| 20 import java.io.InputStream; | |
| 21 import java.net.URLStreamHandlerFactory; | 19 import java.net.URLStreamHandlerFactory; |
| 22 import java.nio.channels.Channels; | |
| 23 import java.nio.channels.ReadableByteChannel; | |
| 24 import java.util.HashMap; | |
| 25 | 20 |
| 26 /** | 21 /** |
| 27 * Framework for testing Cronet. | 22 * Framework for testing Cronet. |
| 28 */ | 23 */ |
| 29 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") | 24 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 30 public class CronetTestFramework { | 25 public class CronetTestFramework { |
| 31 private static final String TAG = "CronetTestFramework"; | 26 private static final String TAG = "CronetTestFramework"; |
| 32 | 27 |
| 33 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; | 28 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; |
| 34 public static final String POST_DATA_KEY = "postData"; | 29 public static final String POST_DATA_KEY = "postData"; |
| 35 public static final String CACHE_KEY = "cache"; | 30 public static final String CACHE_KEY = "cache"; |
| 36 public static final String SDCH_KEY = "sdch"; | 31 public static final String SDCH_KEY = "sdch"; |
| 37 public static final String LIBRARY_INIT_KEY = "libraryInit"; | 32 public static final String LIBRARY_INIT_KEY = "libraryInit"; |
| 38 | 33 |
| 39 // Uses disk cache. | 34 // Uses disk cache. |
| 40 public static final String CACHE_DISK = "disk"; | 35 public static final String CACHE_DISK = "disk"; |
| 41 | 36 |
| 42 // Uses disk cache but does not store http data. | 37 // Uses disk cache but does not store http data. |
| 43 public static final String CACHE_DISK_NO_HTTP = "diskNoHttp"; | 38 public static final String CACHE_DISK_NO_HTTP = "diskNoHttp"; |
| 44 | 39 |
| 45 // Uses in-memory cache. | 40 // Uses in-memory cache. |
| 46 public static final String CACHE_IN_MEMORY = "memory"; | 41 public static final String CACHE_IN_MEMORY = "memory"; |
| 47 | 42 |
| 48 // Enables Sdch. | 43 // Enables Sdch. |
| 49 public static final String SDCH_ENABLE = "enable"; | 44 public static final String SDCH_ENABLE = "enable"; |
| 50 | 45 |
| 51 /** | 46 /** |
| 52 * Library init type strings to use along with {@link LIBRARY_INIT_KEY}. | 47 * Library init type strings to use along with {@link #LIBRARY_INIT_KEY}. |
| 53 * If unspecified, {@link LibraryInitType.CRONET} will be used. | 48 * If unspecified, {@link LibraryInitType#CRONET} will be used. |
| 54 */ | 49 */ |
| 55 public static final class LibraryInitType { | 50 public static final class LibraryInitType { |
| 56 // Initializes Cronet Async API. | 51 // Initializes Cronet Async API. |
| 57 public static final String CRONET = "cronet"; | 52 public static final String CRONET = "cronet"; |
| 58 // Initializes Cronet legacy API. | 53 // Initializes Cronet legacy API. |
| 59 public static final String LEGACY = "legacy"; | 54 public static final String LEGACY = "legacy"; |
| 60 // Initializes Cronet HttpURLConnection API. | 55 // Initializes Cronet HttpURLConnection API. |
| 61 public static final String HTTP_URL_CONNECTION = "http_url_connection"; | 56 public static final String HTTP_URL_CONNECTION = "http_url_connection"; |
| 62 // Do not initialize. | 57 // Do not initialize. |
| 63 public static final String NONE = "none"; | 58 public static final String NONE = "none"; |
| 64 | 59 |
| 65 private LibraryInitType() {} | 60 private LibraryInitType() {} |
| 66 } | 61 } |
| 67 | 62 |
| 68 public URLStreamHandlerFactory mStreamHandlerFactory; | 63 public URLStreamHandlerFactory mStreamHandlerFactory; |
| 69 public CronetEngine mCronetEngine; | 64 public CronetEngineBase mCronetEngine; |
| 70 @SuppressWarnings("deprecation") | |
| 71 HttpUrlRequestFactory mRequestFactory; | |
| 72 | 65 |
| 73 private final String[] mCommandLine; | 66 private final String[] mCommandLine; |
| 74 private final Context mContext; | 67 private final Context mContext; |
| 75 | 68 |
| 76 private String mUrl; | |
| 77 private int mHttpStatusCode = 0; | 69 private int mHttpStatusCode = 0; |
| 78 | 70 |
| 79 // CronetEngine.Builder used for this activity. | 71 // CronetEngine.Builder used for this activity. |
| 80 private CronetEngine.Builder mCronetEngineBuilder; | 72 private ExperimentalCronetEngine.Builder mCronetEngineBuilder; |
| 81 | |
| 82 @SuppressWarnings("deprecation") | |
| 83 private class TestHttpUrlRequestListener implements HttpUrlRequestListener { | |
| 84 private final ConditionVariable mComplete = new ConditionVariable(); | |
| 85 | |
| 86 public TestHttpUrlRequestListener() {} | |
| 87 | |
| 88 @Override | |
| 89 public void onResponseStarted(HttpUrlRequest request) { | |
| 90 mHttpStatusCode = request.getHttpStatusCode(); | |
| 91 } | |
| 92 | |
| 93 @Override | |
| 94 public void onRequestComplete(HttpUrlRequest request) { | |
| 95 mComplete.open(); | |
| 96 } | |
| 97 | |
| 98 public void blockForComplete() { | |
| 99 mComplete.block(); | |
| 100 } | |
| 101 } | |
| 102 | 73 |
| 103 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio
n. | 74 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio
n. |
| 104 @SuppressFBWarnings("EI_EXPOSE_REP2") | 75 @SuppressFBWarnings("EI_EXPOSE_REP2") |
| 105 public CronetTestFramework( | 76 public CronetTestFramework(String appUrl, String[] commandLine, Context cont
ext, |
| 106 String appUrl, String[] commandLine, Context context, CronetEngine.B
uilder builder) { | 77 ExperimentalCronetEngine.Builder builder) { |
| 107 mCommandLine = commandLine; | 78 mCommandLine = commandLine; |
| 108 mContext = context; | 79 mContext = context; |
| 109 | 80 |
| 110 // Print out extra arguments passed in starting this activity. | 81 // Print out extra arguments passed in starting this activity. |
| 111 if (commandLine != null) { | 82 if (commandLine != null) { |
| 112 assertEquals(0, commandLine.length % 2); | 83 assertEquals(0, commandLine.length % 2); |
| 113 for (int i = 0; i < commandLine.length / 2; i++) { | 84 for (int i = 0; i < commandLine.length / 2; i++) { |
| 114 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], | 85 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], |
| 115 commandLine[i * 2 + 1]); | 86 commandLine[i * 2 + 1]); |
| 116 } | 87 } |
| 117 } | 88 } |
| 118 | 89 |
| 119 // Initializes CronetEngine.Builder from commandLine args. | 90 // Initializes CronetEngine.Builder from commandLine args. |
| 120 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild
er(builder); | 91 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild
er(builder); |
| 121 | 92 |
| 122 String initString = getCommandLineArg(LIBRARY_INIT_KEY); | 93 String initString = getCommandLineArg(LIBRARY_INIT_KEY); |
| 123 | 94 |
| 124 if (initString == null) { | 95 if (initString == null) { |
| 125 initString = LibraryInitType.CRONET; | 96 initString = LibraryInitType.CRONET; |
| 126 } | 97 } |
| 127 | 98 |
| 128 switch (initString) { | 99 switch (initString) { |
| 129 case LibraryInitType.NONE: | 100 case LibraryInitType.NONE: |
| 130 break; | 101 break; |
| 131 case LibraryInitType.LEGACY: | |
| 132 mRequestFactory = initRequestFactory(); | |
| 133 if (appUrl != null) { | |
| 134 startWithURL(appUrl); | |
| 135 } | |
| 136 break; | |
| 137 case LibraryInitType.HTTP_URL_CONNECTION: | 102 case LibraryInitType.HTTP_URL_CONNECTION: |
| 138 mCronetEngine = initCronetEngine(); | 103 mCronetEngine = initCronetEngine(); |
| 139 mStreamHandlerFactory = mCronetEngine.createURLStreamHandlerFact
ory(); | 104 mStreamHandlerFactory = mCronetEngine.createURLStreamHandlerFact
ory(); |
| 140 break; | 105 break; |
| 141 default: | 106 default: |
| 142 mCronetEngine = initCronetEngine(); | 107 mCronetEngine = initCronetEngine(); |
| 143 // Start collecting metrics. | 108 // Start collecting metrics. |
| 144 mCronetEngine.getGlobalMetricsDeltas(); | 109 mCronetEngine.getGlobalMetricsDeltas(); |
| 145 break; | 110 break; |
| 146 } | 111 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 if (path.isDirectory()) { | 154 if (path.isDirectory()) { |
| 190 for (File c : path.listFiles()) { | 155 for (File c : path.listFiles()) { |
| 191 if (!recursiveDelete(c)) { | 156 if (!recursiveDelete(c)) { |
| 192 return false; | 157 return false; |
| 193 } | 158 } |
| 194 } | 159 } |
| 195 } | 160 } |
| 196 return path.delete(); | 161 return path.delete(); |
| 197 } | 162 } |
| 198 | 163 |
| 199 CronetEngine.Builder getCronetEngineBuilder() { | 164 ExperimentalCronetEngine.Builder getCronetEngineBuilder() { |
| 200 return mCronetEngineBuilder; | 165 return mCronetEngineBuilder; |
| 201 } | 166 } |
| 202 | 167 |
| 203 private CronetEngine.Builder initializeCronetEngineBuilderWithPresuppliedBui
lder( | 168 private ExperimentalCronetEngine.Builder initializeCronetEngineBuilderWithPr
esuppliedBuilder( |
| 204 CronetEngine.Builder builder) { | 169 ExperimentalCronetEngine.Builder builder) { |
| 205 return createCronetEngineBuilderWithPresuppliedBuilder(mContext, builder
); | 170 return createCronetEngineBuilderWithPresuppliedBuilder(mContext, builder
); |
| 206 } | 171 } |
| 207 | 172 |
| 208 CronetEngine.Builder createCronetEngineBuilder(Context context) { | 173 ExperimentalCronetEngine.Builder createCronetEngineBuilder(Context context)
{ |
| 209 return createCronetEngineBuilderWithPresuppliedBuilder(context, null); | 174 return createCronetEngineBuilderWithPresuppliedBuilder(context, null); |
| 210 } | 175 } |
| 211 | 176 |
| 212 private CronetEngine.Builder createCronetEngineBuilderWithPresuppliedBuilder
( | 177 private ExperimentalCronetEngine.Builder createCronetEngineBuilderWithPresup
pliedBuilder( |
| 213 Context context, CronetEngine.Builder cronetEngineBuilder) { | 178 Context context, ExperimentalCronetEngine.Builder cronetEngineBuilde
r) { |
| 214 if (cronetEngineBuilder == null) { | 179 if (cronetEngineBuilder == null) { |
| 215 cronetEngineBuilder = new CronetEngine.Builder(context); | 180 cronetEngineBuilder = new ExperimentalCronetEngine.Builder(context); |
| 216 cronetEngineBuilder.enableHttp2(true).enableQuic(true); | 181 cronetEngineBuilder.enableHttp2(true).enableQuic(true); |
| 217 } | 182 } |
| 218 | 183 |
| 219 String cacheString = getCommandLineArg(CACHE_KEY); | 184 String cacheString = getCommandLineArg(CACHE_KEY); |
| 220 if (CACHE_DISK.equals(cacheString)) { | 185 if (CACHE_DISK.equals(cacheString)) { |
| 221 cronetEngineBuilder.setStoragePath(getTestStorage(context)); | 186 cronetEngineBuilder.setStoragePath(getTestStorage(context)); |
| 222 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); | 187 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); |
| 223 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | 188 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { |
| 224 cronetEngineBuilder.setStoragePath(getTestStorage(context)); | 189 cronetEngineBuilder.setStoragePath(getTestStorage(context)); |
| 225 cronetEngineBuilder.enableHttpCache( | 190 cronetEngineBuilder.enableHttpCache( |
| 226 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); | 191 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); |
| 227 } else if (CACHE_IN_MEMORY.equals(cacheString)) { | 192 } else if (CACHE_IN_MEMORY.equals(cacheString)) { |
| 228 cronetEngineBuilder.enableHttpCache( | 193 cronetEngineBuilder.enableHttpCache( |
| 229 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); | 194 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); |
| 230 } | 195 } |
| 231 | 196 |
| 232 String sdchString = getCommandLineArg(SDCH_KEY); | 197 String sdchString = getCommandLineArg(SDCH_KEY); |
| 233 if (SDCH_ENABLE.equals(sdchString)) { | 198 if (SDCH_ENABLE.equals(sdchString)) { |
| 234 cronetEngineBuilder.enableSdch(true); | 199 cronetEngineBuilder.enableSdch(true); |
| 235 } | 200 } |
| 236 | 201 |
| 237 // Setting this here so it isn't overridden on the command line | 202 // Setting this here so it isn't overridden on the command line |
| 238 cronetEngineBuilder.setLibraryName("cronet_tests"); | 203 CronetTestUtil.setLibraryName(cronetEngineBuilder, "cronet_tests"); |
| 239 return cronetEngineBuilder; | 204 return cronetEngineBuilder; |
| 240 } | 205 } |
| 241 | 206 |
| 242 // Helper function to initialize Cronet engine. Also used in testing. | 207 // Helper function to initialize Cronet engine. Also used in testing. |
| 243 public CronetEngine initCronetEngine() { | 208 public CronetEngineBase initCronetEngine() { |
| 244 return mCronetEngineBuilder.build(); | 209 return (CronetEngineBase) mCronetEngineBuilder.build(); |
| 245 } | |
| 246 | |
| 247 // Helper function to initialize request factory. Also used in testing. | |
| 248 @SuppressWarnings("deprecation") | |
| 249 public HttpUrlRequestFactory initRequestFactory() { | |
| 250 return HttpUrlRequestFactory.createFactory(mContext, mCronetEngineBuilde
r); | |
| 251 } | 210 } |
| 252 | 211 |
| 253 private String getCommandLineArg(String key) { | 212 private String getCommandLineArg(String key) { |
| 254 if (mCommandLine != null) { | 213 if (mCommandLine != null) { |
| 255 for (int i = 0; i < mCommandLine.length; ++i) { | 214 for (int i = 0; i < mCommandLine.length; ++i) { |
| 256 if (mCommandLine[i].equals(key)) { | 215 if (mCommandLine[i].equals(key)) { |
| 257 return mCommandLine[++i]; | 216 return mCommandLine[++i]; |
| 258 } | 217 } |
| 259 } | 218 } |
| 260 } | 219 } |
| 261 return null; | 220 return null; |
| 262 } | 221 } |
| 263 | 222 |
| 264 @SuppressWarnings("deprecation") | |
| 265 private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) { | |
| 266 String postData = getCommandLineArg(POST_DATA_KEY); | |
| 267 if (postData != null) { | |
| 268 InputStream dataStream = new ByteArrayInputStream(postData.getBytes(
)); | |
| 269 ReadableByteChannel dataChannel = Channels.newChannel(dataStream); | |
| 270 request.setUploadChannel("text/plain", dataChannel, postData.length(
)); | |
| 271 request.setHttpMethod("POST"); | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 @SuppressWarnings("deprecation") | |
| 276 public void startWithURL(String url) { | |
| 277 Log.i(TAG, "Cronet started: %s", url); | |
| 278 mUrl = url; | |
| 279 | |
| 280 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 281 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
| 282 HttpUrlRequest request = mRequestFactory.createRequest( | |
| 283 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | |
| 284 applyCommandLineToHttpUrlRequest(request); | |
| 285 request.start(); | |
| 286 listener.blockForComplete(); | |
| 287 } | |
| 288 | |
| 289 public String getUrl() { | |
| 290 return mUrl; | |
| 291 } | |
| 292 | |
| 293 public int getHttpStatusCode() { | 223 public int getHttpStatusCode() { |
| 294 return mHttpStatusCode; | 224 return mHttpStatusCode; |
| 295 } | 225 } |
| 296 | 226 |
| 297 public void startNetLog() { | 227 public void startNetLog() { |
| 298 if (mRequestFactory != null) { | |
| 299 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire
ctory().getPath() | |
| 300 + "/cronet_sample_netlog_old_api.json", | |
| 301 false); | |
| 302 } | |
| 303 if (mCronetEngine != null) { | 228 if (mCronetEngine != null) { |
| 304 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect
ory().getPath() | 229 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect
ory().getPath() |
| 305 + "/cronet_sample_netlog_new_api.json", | 230 + "/cronet_sample_netlog_new_api.json", |
| 306 false); | 231 false); |
| 307 } | 232 } |
| 308 } | 233 } |
| 309 | 234 |
| 310 public void stopNetLog() { | 235 public void stopNetLog() { |
| 311 if (mRequestFactory != null) { | |
| 312 mRequestFactory.stopNetLog(); | |
| 313 } | |
| 314 if (mCronetEngine != null) { | 236 if (mCronetEngine != null) { |
| 315 mCronetEngine.stopNetLog(); | 237 mCronetEngine.stopNetLog(); |
| 316 } | 238 } |
| 317 } | 239 } |
| 318 } | 240 } |
| OLD | NEW |