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; | 8 import android.os.ConditionVariable; |
9 import android.os.Environment; | 9 import android.os.Environment; |
10 | 10 |
11 import static junit.framework.Assert.assertEquals; | 11 import static junit.framework.Assert.assertEquals; |
12 import static junit.framework.Assert.assertTrue; | 12 import static junit.framework.Assert.assertTrue; |
13 import static junit.framework.Assert.fail; | |
14 | 13 |
15 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
16 import org.chromium.base.PathUtils; | 15 import org.chromium.base.PathUtils; |
17 import org.chromium.base.annotations.SuppressFBWarnings; | 16 import org.chromium.base.annotations.SuppressFBWarnings; |
18 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 17 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; |
19 | 18 |
20 import java.io.ByteArrayInputStream; | 19 import java.io.ByteArrayInputStream; |
21 import java.io.File; | 20 import java.io.File; |
22 import java.io.InputStream; | 21 import java.io.InputStream; |
23 | 22 |
24 import java.nio.channels.Channels; | 23 import java.nio.channels.Channels; |
25 import java.nio.channels.ReadableByteChannel; | 24 import java.nio.channels.ReadableByteChannel; |
26 import java.util.HashMap; | 25 import java.util.HashMap; |
27 | 26 |
28 /** | 27 /** |
29 * Framework for testing Cronet. | 28 * Framework for testing Cronet. |
30 */ | 29 */ |
31 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") | 30 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
32 public class CronetTestFramework { | 31 public class CronetTestFramework { |
33 private static final String TAG = "CronetTestFramework"; | 32 private static final String TAG = "CronetTestFramework"; |
34 | 33 |
35 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; | 34 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; |
36 public static final String POST_DATA_KEY = "postData"; | 35 public static final String POST_DATA_KEY = "postData"; |
37 public static final String CONFIG_KEY = "config"; | |
38 public static final String CACHE_KEY = "cache"; | 36 public static final String CACHE_KEY = "cache"; |
39 public static final String SDCH_KEY = "sdch"; | 37 public static final String SDCH_KEY = "sdch"; |
40 | 38 |
41 public static final String LIBRARY_INIT_KEY = "libraryInit"; | 39 public static final String LIBRARY_INIT_KEY = "libraryInit"; |
42 /** | 40 /** |
43 * Skips library initialization. | 41 * Skips library initialization. |
44 */ | 42 */ |
45 public static final String LIBRARY_INIT_SKIP = "skip"; | 43 public static final String LIBRARY_INIT_SKIP = "skip"; |
46 | 44 |
47 // Uses disk cache. | 45 // Uses disk cache. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 mComplete.open(); | 95 mComplete.open(); |
98 } | 96 } |
99 | 97 |
100 public void blockForComplete() { | 98 public void blockForComplete() { |
101 mComplete.block(); | 99 mComplete.block(); |
102 } | 100 } |
103 } | 101 } |
104 | 102 |
105 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio
n. | 103 // TODO(crbug.com/547160): Fix this findbugs error and remove the suppressio
n. |
106 @SuppressFBWarnings("EI_EXPOSE_REP2") | 104 @SuppressFBWarnings("EI_EXPOSE_REP2") |
107 public CronetTestFramework(String appUrl, String[] commandLine, Context cont
ext) { | 105 public CronetTestFramework( |
| 106 String appUrl, String[] commandLine, Context context, CronetEngine.B
uilder builder) { |
108 mCommandLine = commandLine; | 107 mCommandLine = commandLine; |
109 mContext = context; | 108 mContext = context; |
110 prepareTestStorage(); | 109 prepareTestStorage(); |
111 | 110 |
112 // Print out extra arguments passed in starting this activity. | 111 // Print out extra arguments passed in starting this activity. |
113 if (commandLine != null) { | 112 if (commandLine != null) { |
114 assertEquals(0, commandLine.length % 2); | 113 assertEquals(0, commandLine.length % 2); |
115 for (int i = 0; i < commandLine.length / 2; i++) { | 114 for (int i = 0; i < commandLine.length / 2; i++) { |
116 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], | 115 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], |
117 commandLine[i * 2 + 1]); | 116 commandLine[i * 2 + 1]); |
118 } | 117 } |
119 } | 118 } |
120 | 119 |
121 // Initializes CronetEngine.Builder from commandLine args. | 120 // Initializes CronetEngine.Builder from commandLine args. |
122 mCronetEngineBuilder = initializeCronetEngineBuilder(); | 121 mCronetEngineBuilder = initializeCronetEngineBuilderWithPresuppliedBuild
er(builder); |
123 Log.i(TAG, "Using Config: " + mCronetEngineBuilder.toString()); | |
124 | 122 |
125 String initString = getCommandLineArg(LIBRARY_INIT_KEY); | 123 String initString = getCommandLineArg(LIBRARY_INIT_KEY); |
126 if (LIBRARY_INIT_SKIP.equals(initString)) { | 124 if (LIBRARY_INIT_SKIP.equals(initString)) { |
127 return; | 125 return; |
128 } | 126 } |
129 | 127 |
130 mCronetEngine = initCronetEngine(); | 128 mCronetEngine = initCronetEngine(); |
131 | 129 |
132 if (LIBRARY_INIT_WRAPPER.equals(initString)) { | 130 if (LIBRARY_INIT_WRAPPER.equals(initString)) { |
133 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(mCronetEng
ine); | 131 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(mCronetEng
ine); |
134 } | 132 } |
135 | 133 |
136 mHistogramManager = HistogramManager.createHistogramManager(); | 134 mHistogramManager = HistogramManager.createHistogramManager(); |
137 | 135 |
138 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { | 136 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { |
139 return; | 137 return; |
140 } | 138 } |
141 | 139 |
142 mRequestFactory = initRequestFactory(); | 140 mRequestFactory = initRequestFactory(); |
143 if (appUrl != null) { | 141 if (appUrl != null) { |
144 startWithURL(appUrl); | 142 startWithURL(appUrl); |
145 } | 143 } |
146 } | 144 } |
147 | 145 |
148 /** | 146 /** |
149 * Prepares the path for the test storage (http cache, QUIC server info). | 147 * Prepares the path for the test storage (http cache, QUIC server info). |
150 */ | 148 */ |
151 private void prepareTestStorage() { | 149 private void prepareTestStorage() { |
152 File storage = new File(getTestStorage()); | 150 File storage = new File(getTestStorage(mContext)); |
153 if (storage.exists()) { | 151 if (storage.exists()) { |
154 assertTrue(recursiveDelete(storage)); | 152 assertTrue(recursiveDelete(storage)); |
155 } | 153 } |
156 assertTrue(storage.mkdir()); | 154 assertTrue(storage.mkdir()); |
157 } | 155 } |
158 | 156 |
159 String getTestStorage() { | 157 static String getTestStorage(Context context) { |
160 return PathUtils.getDataDirectory(mContext) + "/test_storage"; | 158 return PathUtils.getDataDirectory(context) + "/test_storage"; |
161 } | 159 } |
162 | 160 |
163 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") | 161 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") |
164 private boolean recursiveDelete(File path) { | 162 private boolean recursiveDelete(File path) { |
165 if (path.isDirectory()) { | 163 if (path.isDirectory()) { |
166 for (File c : path.listFiles()) { | 164 for (File c : path.listFiles()) { |
167 if (!recursiveDelete(c)) { | 165 if (!recursiveDelete(c)) { |
168 return false; | 166 return false; |
169 } | 167 } |
170 } | 168 } |
171 } | 169 } |
172 return path.delete(); | 170 return path.delete(); |
173 } | 171 } |
174 | 172 |
175 CronetEngine.Builder getCronetEngineBuilder() { | 173 CronetEngine.Builder getCronetEngineBuilder() { |
176 return mCronetEngineBuilder; | 174 return mCronetEngineBuilder; |
177 } | 175 } |
178 | 176 |
179 private CronetEngine.Builder initializeCronetEngineBuilder() { | 177 private CronetEngine.Builder initializeCronetEngineBuilderWithPresuppliedBui
lder( |
180 return createCronetEngineBuilder(mContext); | 178 CronetEngine.Builder builder) { |
| 179 return createCronetEngineBuilderWithPresuppliedBuilder(mContext, builder
); |
181 } | 180 } |
182 | 181 |
183 CronetEngine.Builder createCronetEngineBuilder(Context context) { | 182 CronetEngine.Builder createCronetEngineBuilder(Context context) { |
184 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(cont
ext); | 183 return createCronetEngineBuilderWithPresuppliedBuilder(context, null); |
185 cronetEngineBuilder.enableHTTP2(true).enableQUIC(true); | 184 } |
186 | 185 |
187 // Override config if it is passed from the launcher. | 186 private CronetEngine.Builder createCronetEngineBuilderWithPresuppliedBuilder
( |
188 String configString = getCommandLineArg(CONFIG_KEY); | 187 Context context, CronetEngine.Builder cronetEngineBuilder) { |
189 if (configString != null) { | 188 if (cronetEngineBuilder == null) { |
190 try { | 189 cronetEngineBuilder = new CronetEngine.Builder(context); |
191 cronetEngineBuilder = new CronetEngine.Builder(mContext, configS
tring); | 190 cronetEngineBuilder.enableHTTP2(true).enableQUIC(true); |
192 } catch (org.json.JSONException e) { | |
193 fail("Invalid Config." + e); | |
194 return null; | |
195 } | |
196 } | 191 } |
197 | 192 |
198 String cacheString = getCommandLineArg(CACHE_KEY); | 193 String cacheString = getCommandLineArg(CACHE_KEY); |
199 if (CACHE_DISK.equals(cacheString)) { | 194 if (CACHE_DISK.equals(cacheString)) { |
200 cronetEngineBuilder.setStoragePath(getTestStorage()); | 195 cronetEngineBuilder.setStoragePath(getTestStorage(context)); |
201 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); | 196 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); |
202 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | 197 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { |
203 cronetEngineBuilder.setStoragePath(getTestStorage()); | 198 cronetEngineBuilder.setStoragePath(getTestStorage(context)); |
204 cronetEngineBuilder.enableHttpCache( | 199 cronetEngineBuilder.enableHttpCache( |
205 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); | 200 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); |
206 } else if (CACHE_IN_MEMORY.equals(cacheString)) { | 201 } else if (CACHE_IN_MEMORY.equals(cacheString)) { |
207 cronetEngineBuilder.enableHttpCache( | 202 cronetEngineBuilder.enableHttpCache( |
208 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); | 203 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); |
209 } | 204 } |
210 | 205 |
211 String sdchString = getCommandLineArg(SDCH_KEY); | 206 String sdchString = getCommandLineArg(SDCH_KEY); |
212 if (SDCH_ENABLE.equals(sdchString)) { | 207 if (SDCH_ENABLE.equals(sdchString)) { |
213 cronetEngineBuilder.enableSDCH(true); | 208 cronetEngineBuilder.enableSDCH(true); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 285 |
291 public void stopNetLog() { | 286 public void stopNetLog() { |
292 if (mRequestFactory != null) { | 287 if (mRequestFactory != null) { |
293 mRequestFactory.stopNetLog(); | 288 mRequestFactory.stopNetLog(); |
294 } | 289 } |
295 if (mCronetEngine != null) { | 290 if (mCronetEngine != null) { |
296 mCronetEngine.stopNetLog(); | 291 mCronetEngine.stopNetLog(); |
297 } | 292 } |
298 } | 293 } |
299 } | 294 } |
OLD | NEW |