Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: components/cronet/android/test/src/org/chromium/net/CronetTestFramework.java

Issue 2361413002: Obliterate legacy cronet API. All of its users have been migrated. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
17 16
18 import java.io.ByteArrayInputStream;
19 import java.io.File; 17 import java.io.File;
20 import java.io.InputStream;
21 import java.net.URLStreamHandlerFactory; 18 import java.net.URLStreamHandlerFactory;
22 import java.nio.channels.Channels;
23 import java.nio.channels.ReadableByteChannel;
24 import java.util.HashMap;
25 19
26 /** 20 /**
27 * Framework for testing Cronet. 21 * Framework for testing Cronet.
28 */ 22 */
29 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 23 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
30 public class CronetTestFramework { 24 public class CronetTestFramework {
31 private static final String TAG = "CronetTestFramework"; 25 private static final String TAG = "CronetTestFramework";
32 26
33 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; 27 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
34 public static final String POST_DATA_KEY = "postData"; 28 public static final String POST_DATA_KEY = "postData";
(...skipping 13 matching lines...) Expand all
48 // Enables Sdch. 42 // Enables Sdch.
49 public static final String SDCH_ENABLE = "enable"; 43 public static final String SDCH_ENABLE = "enable";
50 44
51 /** 45 /**
52 * Library init type strings to use along with {@link LIBRARY_INIT_KEY}. 46 * Library init type strings to use along with {@link LIBRARY_INIT_KEY}.
53 * If unspecified, {@link LibraryInitType.CRONET} will be used. 47 * If unspecified, {@link LibraryInitType.CRONET} will be used.
54 */ 48 */
55 public static final class LibraryInitType { 49 public static final class LibraryInitType {
56 // Initializes Cronet Async API. 50 // Initializes Cronet Async API.
57 public static final String CRONET = "cronet"; 51 public static final String CRONET = "cronet";
58 // Initializes Cronet legacy API.
59 public static final String LEGACY = "legacy";
60 // Initializes Cronet HttpURLConnection API. 52 // Initializes Cronet HttpURLConnection API.
61 public static final String HTTP_URL_CONNECTION = "http_url_connection"; 53 public static final String HTTP_URL_CONNECTION = "http_url_connection";
62 // Do not initialize. 54 // Do not initialize.
63 public static final String NONE = "none"; 55 public static final String NONE = "none";
64 56
65 private LibraryInitType() {} 57 private LibraryInitType() {}
66 } 58 }
67 59
68 public URLStreamHandlerFactory mStreamHandlerFactory; 60 public URLStreamHandlerFactory mStreamHandlerFactory;
69 public CronetEngine mCronetEngine; 61 public CronetEngine mCronetEngine;
70 @SuppressWarnings("deprecation")
71 HttpUrlRequestFactory mRequestFactory;
72 62
73 private final String[] mCommandLine; 63 private final String[] mCommandLine;
74 private final Context mContext; 64 private final Context mContext;
75 65
76 private String mUrl;
77 private int mHttpStatusCode = 0;
78
79 // CronetEngine.Builder used for this activity. 66 // CronetEngine.Builder used for this activity.
80 private CronetEngine.Builder mCronetEngineBuilder; 67 private CronetEngine.Builder mCronetEngineBuilder;
81 68
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
103 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio n. 69 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio n.
104 @SuppressFBWarnings("EI_EXPOSE_REP2") 70 @SuppressFBWarnings("EI_EXPOSE_REP2")
105 public CronetTestFramework( 71 public CronetTestFramework(
106 String appUrl, String[] commandLine, Context context, CronetEngine.B uilder builder) { 72 String appUrl, String[] commandLine, Context context, CronetEngine.B uilder builder) {
107 mCommandLine = commandLine; 73 mCommandLine = commandLine;
108 mContext = context; 74 mContext = context;
109 75
110 // Print out extra arguments passed in starting this activity. 76 // Print out extra arguments passed in starting this activity.
111 if (commandLine != null) { 77 if (commandLine != null) {
112 assertEquals(0, commandLine.length % 2); 78 assertEquals(0, commandLine.length % 2);
113 for (int i = 0; i < commandLine.length / 2; i++) { 79 for (int i = 0; i < commandLine.length / 2; i++) {
114 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], 80 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2],
115 commandLine[i * 2 + 1]); 81 commandLine[i * 2 + 1]);
116 } 82 }
117 } 83 }
118 84
119 // Initializes CronetEngine.Builder from commandLine args. 85 // Initializes CronetEngine.Builder from commandLine args.
120 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild er(builder); 86 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild er(builder);
121 87
122 String initString = getCommandLineArg(LIBRARY_INIT_KEY); 88 String initString = getCommandLineArg(LIBRARY_INIT_KEY);
123 89
124 if (initString == null) { 90 if (initString == null) {
125 initString = LibraryInitType.CRONET; 91 initString = LibraryInitType.CRONET;
126 } 92 }
127 93
128 switch (initString) { 94 switch (initString) {
129 case LibraryInitType.NONE: 95 case LibraryInitType.NONE:
130 break; 96 break;
131 case LibraryInitType.LEGACY:
132 mRequestFactory = initRequestFactory();
133 if (appUrl != null) {
134 startWithURL(appUrl);
135 }
136 break;
137 case LibraryInitType.HTTP_URL_CONNECTION: 97 case LibraryInitType.HTTP_URL_CONNECTION:
138 mCronetEngine = initCronetEngine(); 98 mCronetEngine = initCronetEngine();
139 mStreamHandlerFactory = mCronetEngine.createURLStreamHandlerFact ory(); 99 mStreamHandlerFactory = mCronetEngine.createURLStreamHandlerFact ory();
140 break; 100 break;
141 default: 101 default:
142 mCronetEngine = initCronetEngine(); 102 mCronetEngine = initCronetEngine();
143 // Start collecting metrics. 103 // Start collecting metrics.
144 mCronetEngine.getGlobalMetricsDeltas(); 104 mCronetEngine.getGlobalMetricsDeltas();
145 break; 105 break;
146 } 106 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 // Setting this here so it isn't overridden on the command line 197 // Setting this here so it isn't overridden on the command line
238 cronetEngineBuilder.setLibraryName("cronet_tests"); 198 cronetEngineBuilder.setLibraryName("cronet_tests");
239 return cronetEngineBuilder; 199 return cronetEngineBuilder;
240 } 200 }
241 201
242 // Helper function to initialize Cronet engine. Also used in testing. 202 // Helper function to initialize Cronet engine. Also used in testing.
243 public CronetEngine initCronetEngine() { 203 public CronetEngine initCronetEngine() {
244 return mCronetEngineBuilder.build(); 204 return mCronetEngineBuilder.build();
245 } 205 }
246 206
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 }
252
253 private String getCommandLineArg(String key) { 207 private String getCommandLineArg(String key) {
254 if (mCommandLine != null) { 208 if (mCommandLine != null) {
255 for (int i = 0; i < mCommandLine.length; ++i) { 209 for (int i = 0; i < mCommandLine.length; ++i) {
256 if (mCommandLine[i].equals(key)) { 210 if (mCommandLine[i].equals(key)) {
257 return mCommandLine[++i]; 211 return mCommandLine[++i];
258 } 212 }
259 } 213 }
260 } 214 }
261 return null; 215 return null;
262 } 216 }
263 217
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() {
294 return mHttpStatusCode;
295 }
296
297 public void startNetLog() { 218 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) { 219 if (mCronetEngine != null) {
304 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath() 220 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath()
305 + "/cronet_sample_netlog_new_api.json", 221 + "/cronet_sample_netlog_new_api.json",
306 false); 222 false);
307 } 223 }
308 } 224 }
309 225
310 public void stopNetLog() { 226 public void stopNetLog() {
311 if (mRequestFactory != null) {
312 mRequestFactory.stopNetLog();
313 }
314 if (mCronetEngine != null) { 227 if (mCronetEngine != null) {
315 mCronetEngine.stopNetLog(); 228 mCronetEngine.stopNetLog();
316 } 229 }
317 } 230 }
318 } 231 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698