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

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

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebase & Conflict Resolution Created 4 years, 1 month 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.Environment; 8 import android.os.Environment;
9 9
10 import static junit.framework.Assert.assertEquals; 10 import static junit.framework.Assert.assertEquals;
11 import static junit.framework.Assert.assertTrue; 11 import static junit.framework.Assert.assertTrue;
12 12
13 import org.chromium.base.Log; 13 import org.chromium.base.Log;
14 import org.chromium.base.PathUtils; 14 import org.chromium.base.PathUtils;
15 import org.chromium.base.annotations.SuppressFBWarnings; 15 import org.chromium.base.annotations.SuppressFBWarnings;
16 import org.chromium.net.impl.CronetEngineBase;
16 17
17 import java.io.File; 18 import java.io.File;
18 import java.net.URLStreamHandlerFactory; 19 import java.net.URLStreamHandlerFactory;
19 20
20 /** 21 /**
21 * Framework for testing Cronet. 22 * Framework for testing Cronet.
22 */ 23 */
23 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 24 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
24 public class CronetTestFramework { 25 public class CronetTestFramework {
25 private static final String TAG = "CronetTestFramework"; 26 private static final String TAG = "CronetTestFramework";
(...skipping 10 matching lines...) Expand all
36 // Uses disk cache but does not store http data. 37 // Uses disk cache but does not store http data.
37 public static final String CACHE_DISK_NO_HTTP = "diskNoHttp"; 38 public static final String CACHE_DISK_NO_HTTP = "diskNoHttp";
38 39
39 // Uses in-memory cache. 40 // Uses in-memory cache.
40 public static final String CACHE_IN_MEMORY = "memory"; 41 public static final String CACHE_IN_MEMORY = "memory";
41 42
42 // Enables Sdch. 43 // Enables Sdch.
43 public static final String SDCH_ENABLE = "enable"; 44 public static final String SDCH_ENABLE = "enable";
44 45
45 /** 46 /**
46 * 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}.
47 * If unspecified, {@link LibraryInitType.CRONET} will be used. 48 * If unspecified, {@link LibraryInitType#CRONET} will be used.
48 */ 49 */
49 public static final class LibraryInitType { 50 public static final class LibraryInitType {
50 // Initializes Cronet Async API. 51 // Initializes Cronet Async API.
51 public static final String CRONET = "cronet"; 52 public static final String CRONET = "cronet";
52 // Initializes Cronet HttpURLConnection API. 53 // Initializes Cronet HttpURLConnection API.
53 public static final String HTTP_URL_CONNECTION = "http_url_connection"; 54 public static final String HTTP_URL_CONNECTION = "http_url_connection";
54 // Do not initialize. 55 // Do not initialize.
55 public static final String NONE = "none"; 56 public static final String NONE = "none";
56 57
57 private LibraryInitType() {} 58 private LibraryInitType() {}
58 } 59 }
59 60
60 public URLStreamHandlerFactory mStreamHandlerFactory; 61 public URLStreamHandlerFactory mStreamHandlerFactory;
61 public CronetEngine mCronetEngine; 62 public CronetEngineBase mCronetEngine;
62 63
63 private final String[] mCommandLine; 64 private final String[] mCommandLine;
64 private final Context mContext; 65 private final Context mContext;
65 66
66 // CronetEngine.Builder used for this activity. 67 // CronetEngine.Builder used for this activity.
67 private CronetEngine.Builder mCronetEngineBuilder; 68 private ExperimentalCronetEngine.Builder mCronetEngineBuilder;
68 69
69 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio n. 70 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio n.
70 @SuppressFBWarnings("EI_EXPOSE_REP2") 71 @SuppressFBWarnings("EI_EXPOSE_REP2")
71 public CronetTestFramework( 72 public CronetTestFramework(String appUrl, String[] commandLine, Context cont ext,
72 String appUrl, String[] commandLine, Context context, CronetEngine.B uilder builder) { 73 ExperimentalCronetEngine.Builder builder) {
73 mCommandLine = commandLine; 74 mCommandLine = commandLine;
74 mContext = context; 75 mContext = context;
75 76
76 // Print out extra arguments passed in starting this activity. 77 // Print out extra arguments passed in starting this activity.
77 if (commandLine != null) { 78 if (commandLine != null) {
78 assertEquals(0, commandLine.length % 2); 79 assertEquals(0, commandLine.length % 2);
79 for (int i = 0; i < commandLine.length / 2; i++) { 80 for (int i = 0; i < commandLine.length / 2; i++) {
80 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], 81 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2],
81 commandLine[i * 2 + 1]); 82 commandLine[i * 2 + 1]);
82 } 83 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 if (path.isDirectory()) { 150 if (path.isDirectory()) {
150 for (File c : path.listFiles()) { 151 for (File c : path.listFiles()) {
151 if (!recursiveDelete(c)) { 152 if (!recursiveDelete(c)) {
152 return false; 153 return false;
153 } 154 }
154 } 155 }
155 } 156 }
156 return path.delete(); 157 return path.delete();
157 } 158 }
158 159
159 CronetEngine.Builder getCronetEngineBuilder() { 160 ExperimentalCronetEngine.Builder getCronetEngineBuilder() {
160 return mCronetEngineBuilder; 161 return mCronetEngineBuilder;
161 } 162 }
162 163
163 private CronetEngine.Builder initializeCronetEngineBuilderWithPresuppliedBui lder( 164 private ExperimentalCronetEngine.Builder initializeCronetEngineBuilderWithPr esuppliedBuilder(
164 CronetEngine.Builder builder) { 165 ExperimentalCronetEngine.Builder builder) {
165 return createCronetEngineBuilderWithPresuppliedBuilder(mContext, builder ); 166 return createCronetEngineBuilderWithPresuppliedBuilder(mContext, builder );
166 } 167 }
167 168
168 CronetEngine.Builder createCronetEngineBuilder(Context context) { 169 ExperimentalCronetEngine.Builder createCronetEngineBuilder(Context context) {
169 return createCronetEngineBuilderWithPresuppliedBuilder(context, null); 170 return createCronetEngineBuilderWithPresuppliedBuilder(context, null);
170 } 171 }
171 172
172 private CronetEngine.Builder createCronetEngineBuilderWithPresuppliedBuilder ( 173 private ExperimentalCronetEngine.Builder createCronetEngineBuilderWithPresup pliedBuilder(
173 Context context, CronetEngine.Builder cronetEngineBuilder) { 174 Context context, ExperimentalCronetEngine.Builder cronetEngineBuilde r) {
174 if (cronetEngineBuilder == null) { 175 if (cronetEngineBuilder == null) {
175 cronetEngineBuilder = new CronetEngine.Builder(context); 176 cronetEngineBuilder = new ExperimentalCronetEngine.Builder(context);
176 cronetEngineBuilder.enableHttp2(true).enableQuic(true); 177 cronetEngineBuilder.enableHttp2(true).enableQuic(true);
177 } 178 }
178 179
179 String cacheString = getCommandLineArg(CACHE_KEY); 180 String cacheString = getCommandLineArg(CACHE_KEY);
180 if (CACHE_DISK.equals(cacheString)) { 181 if (CACHE_DISK.equals(cacheString)) {
181 cronetEngineBuilder.setStoragePath(getTestStorage(context)); 182 cronetEngineBuilder.setStoragePath(getTestStorage(context));
182 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_ DISK, 1000 * 1024); 183 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_ DISK, 1000 * 1024);
183 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { 184 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) {
184 cronetEngineBuilder.setStoragePath(getTestStorage(context)); 185 cronetEngineBuilder.setStoragePath(getTestStorage(context));
185 cronetEngineBuilder.enableHttpCache( 186 cronetEngineBuilder.enableHttpCache(
186 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); 187 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024);
187 } else if (CACHE_IN_MEMORY.equals(cacheString)) { 188 } else if (CACHE_IN_MEMORY.equals(cacheString)) {
188 cronetEngineBuilder.enableHttpCache( 189 cronetEngineBuilder.enableHttpCache(
189 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); 190 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024);
190 } 191 }
191 192
192 String sdchString = getCommandLineArg(SDCH_KEY); 193 String sdchString = getCommandLineArg(SDCH_KEY);
193 if (SDCH_ENABLE.equals(sdchString)) { 194 if (SDCH_ENABLE.equals(sdchString)) {
194 cronetEngineBuilder.enableSdch(true); 195 cronetEngineBuilder.enableSdch(true);
195 } 196 }
196 197
197 // Setting this here so it isn't overridden on the command line 198 // Setting this here so it isn't overridden on the command line
198 cronetEngineBuilder.setLibraryName("cronet_tests"); 199 CronetTestUtil.setLibraryName(cronetEngineBuilder, "cronet_tests");
199 return cronetEngineBuilder; 200 return cronetEngineBuilder;
200 } 201 }
201 202
202 // Helper function to initialize Cronet engine. Also used in testing. 203 // Helper function to initialize Cronet engine. Also used in testing.
203 public CronetEngine initCronetEngine() { 204 public CronetEngineBase initCronetEngine() {
204 return mCronetEngineBuilder.build(); 205 return (CronetEngineBase) mCronetEngineBuilder.build();
205 } 206 }
206 207
207 private String getCommandLineArg(String key) { 208 private String getCommandLineArg(String key) {
208 if (mCommandLine != null) { 209 if (mCommandLine != null) {
209 for (int i = 0; i < mCommandLine.length; ++i) { 210 for (int i = 0; i < mCommandLine.length; ++i) {
210 if (mCommandLine[i].equals(key)) { 211 if (mCommandLine[i].equals(key)) {
211 return mCommandLine[++i]; 212 return mCommandLine[++i];
212 } 213 }
213 } 214 }
214 } 215 }
215 return null; 216 return null;
216 } 217 }
217 218
218 public void startNetLog() { 219 public void startNetLog() {
219 if (mCronetEngine != null) { 220 if (mCronetEngine != null) {
220 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath() 221 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath()
221 + "/cronet_sample_netlog_new_api.json", 222 + "/cronet_sample_netlog_new_api.json",
222 false); 223 false);
223 } 224 }
224 } 225 }
225 226
226 public void stopNetLog() { 227 public void stopNetLog() {
227 if (mCronetEngine != null) { 228 if (mCronetEngine != null) {
228 mCronetEngine.stopNetLog(); 229 mCronetEngine.stopNetLog();
229 } 230 }
230 } 231 }
231 } 232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698