Chromium Code Reviews| 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.Environment; | 8 import android.os.Environment; |
| 9 | 9 |
| 10 import static junit.framework.Assert.assertEquals; | |
| 11 import static junit.framework.Assert.assertTrue; | 10 import static junit.framework.Assert.assertTrue; |
| 12 | 11 |
| 13 import org.chromium.base.Log; | |
| 14 import org.chromium.base.PathUtils; | 12 import org.chromium.base.PathUtils; |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | 13 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 import org.chromium.net.impl.CronetEngineBase; | 14 import org.chromium.net.impl.CronetEngineBase; |
| 17 | 15 |
| 18 import java.io.File; | 16 import java.io.File; |
| 19 import java.net.URLStreamHandlerFactory; | |
| 20 | 17 |
| 21 /** | 18 /** |
| 22 * Framework for testing Cronet. | 19 * Framework for testing Cronet. |
| 23 */ | 20 */ |
| 24 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") | 21 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 25 public class CronetTestFramework { | 22 public class CronetTestFramework { |
| 26 private static final String TAG = CronetTestFramework.class.getSimpleName(); | 23 private static final String TAG = CronetTestFramework.class.getSimpleName(); |
| 27 | 24 |
| 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; | 25 public CronetEngineBase mCronetEngine; |
| 63 | 26 |
| 64 private final String[] mCommandLine; | |
| 65 private final Context mContext; | 27 private final Context mContext; |
| 66 | 28 |
| 67 // CronetEngine.Builder used for this activity. | 29 public CronetTestFramework(Context context, ExperimentalCronetEngine.Builder builder) { |
| 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; | 30 mContext = context; |
| 76 | 31 if (builder == null) { |
| 77 // Print out extra arguments passed in starting this activity. | 32 builder = new ExperimentalCronetEngine.Builder(context); |
| 78 if (commandLine != null) { | 33 builder.enableHttp2(true).enableQuic(true); |
| 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 } | 34 } |
| 85 | 35 mCronetEngine = (CronetEngineBase) builder.build(); |
| 86 // Initializes CronetEngine.Builder from commandLine args. | 36 // Start collecting metrics. |
| 87 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild er(builder); | 37 mCronetEngine.getGlobalMetricsDeltas(); |
| 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 } | 38 } |
| 109 | 39 |
| 110 /** | 40 /** |
| 111 * Prepares the path for the test storage (http cache, QUIC server info). | 41 * Prepares the path for the test storage (http cache, QUIC server info). |
| 112 */ | 42 */ |
| 113 public static void prepareTestStorage(Context context) { | 43 public static void prepareTestStorage(Context context) { |
| 114 File storage = new File(getTestStorageDirectory()); | 44 File storage = new File(getTestStorageDirectory()); |
| 115 if (storage.exists()) { | 45 if (storage.exists()) { |
| 116 assertTrue(recursiveDelete(storage)); | 46 assertTrue(recursiveDelete(storage)); |
| 117 } | 47 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 if (path.isDirectory()) { | 80 if (path.isDirectory()) { |
| 151 for (File c : path.listFiles()) { | 81 for (File c : path.listFiles()) { |
| 152 if (!recursiveDelete(c)) { | 82 if (!recursiveDelete(c)) { |
| 153 return false; | 83 return false; |
| 154 } | 84 } |
| 155 } | 85 } |
| 156 } | 86 } |
| 157 return path.delete(); | 87 return path.delete(); |
| 158 } | 88 } |
| 159 | 89 |
| 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() { | 90 public void startNetLog() { |
|
mgersh
2017/05/23 18:12:13
This class is kind of a strange combination of thi
pauljensen
2017/05/25 15:15:15
Done, netlog stuff was actually dead, so I just mo
| |
| 218 if (mCronetEngine != null) { | 91 if (mCronetEngine != null) { |
| 219 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath() | 92 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath() |
| 220 + "/cronet_sample_netlog_new_api.json", | 93 + "/cronet_sample_netlog_new_api.json", |
| 221 false); | 94 false); |
| 222 } | 95 } |
| 223 } | 96 } |
| 224 | 97 |
| 225 public void stopNetLog() { | 98 public void stopNetLog() { |
| 226 if (mCronetEngine != null) { | 99 if (mCronetEngine != null) { |
| 227 mCronetEngine.stopNetLog(); | 100 mCronetEngine.stopNetLog(); |
| 228 } | 101 } |
| 229 } | 102 } |
| 230 } | 103 } |
| OLD | NEW |