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

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

Issue 1133883002: [Cronet] Enable persistence mode for Sdch (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@quic_server_remove_loop
Patch Set: Created 5 years, 7 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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.test.suitebuilder.annotation.SmallTest; 7 import android.test.suitebuilder.annotation.SmallTest;
8 8
9 import org.chromium.base.Log;
9 import org.chromium.base.test.util.Feature; 10 import org.chromium.base.test.util.Feature;
10 11
12 import java.io.ByteArrayOutputStream;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
11 import java.util.Arrays; 18 import java.util.Arrays;
12 import java.util.HashMap; 19 import java.util.HashMap;
20 import java.util.List;
13 import java.util.Map; 21 import java.util.Map;
14 22
15 /** 23 /**
16 * Tests Sdch support. 24 * Tests Sdch support.
17 */ 25 */
18 public class SdchTest extends CronetTestBase { 26 public class SdchTest extends CronetTestBase {
27 private static final String TAG = Log.makeTag("SdchTest");
19 private CronetTestActivity mActivity; 28 private CronetTestActivity mActivity;
20 29
21 private void setUp(boolean enableSdch) { 30 private void setUp(boolean enableSdch) {
22 UrlRequestContextConfig config = new UrlRequestContextConfig(); 31 List<String> commandLineArgs = new ArrayList<String>();
23 config.enableSDCH(enableSdch); 32 commandLineArgs.add(CronetTestActivity.CACHE_KEY);
24 config.setLibraryName("cronet_tests"); 33 commandLineArgs.add(CronetTestActivity.CACHE_DISK);
25 config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY, 100 * 1024); 34 if (enableSdch) {
26 String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, config.toStri ng()}; 35 commandLineArgs.add(CronetTestActivity.SDCH_KEY);
27 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLi neArgs); 36 commandLineArgs.add("enable");
28 mActivity.startNetLog(); 37 }
38 String[] args = new String[commandLineArgs.size()];
39 mActivity =
40 launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineAr gs.toArray(args));
29 41
30 // Registers custom DNS mapping for legacy ChromiumUrlRequestFactory.
31 ChromiumUrlRequestFactory factory = (ChromiumUrlRequestFactory) mActivit y.mRequestFactory; 42 ChromiumUrlRequestFactory factory = (ChromiumUrlRequestFactory) mActivit y.mRequestFactory;
32 long legacyAdapter = factory.getRequestContext().getUrlRequestContextAda pterForTesting(); 43 registerDNSMapping(factory.getRequestContext().getUrlRequestContextAdapt er(), true);
33 assertTrue(legacyAdapter != 0);
34 NativeTestServer.registerHostResolverProc(legacyAdapter, true);
35 44
36 // Registers custom DNS mapping for CronetUrlRequestContext.
37 CronetUrlRequestContext requestContext = 45 CronetUrlRequestContext requestContext =
38 (CronetUrlRequestContext) mActivity.mUrlRequestContext; 46 (CronetUrlRequestContext) mActivity.mUrlRequestContext;
39 long adapter = requestContext.getUrlRequestContextAdapter(); 47 registerDNSMapping(requestContext.getUrlRequestContextAdapter(), false);
40 assertTrue(adapter != 0);
41 NativeTestServer.registerHostResolverProc(adapter, false);
42 48
43 // Starts NativeTestServer. 49 // Start NativeTestServer.
44 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g etTargetContext())); 50 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g etTargetContext()));
45 } 51 }
46 52
47 @Override 53 @Override
48 protected void tearDown() throws Exception { 54 protected void tearDown() throws Exception {
49 NativeTestServer.shutdownNativeTestServer(); 55 NativeTestServer.shutdownNativeTestServer();
50 mActivity.stopNetLog();
51 super.tearDown(); 56 super.tearDown();
52 } 57 }
53 58
54 @SmallTest 59 @SmallTest
55 @Feature({"Cronet"}) 60 @Feature({"Cronet"})
56 public void testSdchEnabled_LegacyAPI() throws Exception { 61 public void testSdchEnabled_LegacyAPI() throws Exception {
57 setUp(true); 62 setUp(true);
58 // Make a request to /sdch/index which advertises the dictionary. 63 // Make a request to /sdch/index which advertises the dictionary.
59 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI ( 64 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI (
60 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); 65 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test"); 110 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test");
106 assertEquals(200, listener2.mHttpStatusCode); 111 assertEquals(200, listener2.mHttpStatusCode);
107 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 112 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
108 } 113 }
109 114
110 @SmallTest 115 @SmallTest
111 @Feature({"Cronet"}) 116 @Feature({"Cronet"})
112 public void testSdchEnabled() throws Exception { 117 public void testSdchEnabled() throws Exception {
113 setUp(true); 118 setUp(true);
114 // Make a request to /sdch which advertises the dictionary. 119 // Make a request to /sdch which advertises the dictionary.
115 TestUrlRequestListener listener1 = 120 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr lRequestContext,
116 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=LeQxM80O"); 121 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
117 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 122 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
118 assertEquals("This is an index page.\n", listener1.mResponseAsString); 123 assertEquals("This is an index page.\n", listener1.mResponseAsString);
119 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 124 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
120 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 125 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
121 126
122 waitForDictionaryAdded("LeQxM80O", false); 127 waitForDictionaryAdded("LeQxM80O", false);
123 128
124 // Make a request to fetch encoded response at /sdch/test. 129 // Make a request to fetch encoded response at /sdch/test.
125 TestUrlRequestListener listener2 = 130 TestUrlRequestListener listener2 = startAndWaitForComplete(
126 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); 131 mActivity.mUrlRequestContext, NativeTestServer.getSdchURL() + "/ sdch/test");
127 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 132 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
128 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 133 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
134
135 // Wait for a bit until SimpleCache finished closing entries before
136 // calling shutdown on the UrlRequestContext.
137 // TODO(xunjieli): Remove once crbug.com/486120 is fixed.
138 Thread.sleep(5000);
139 mActivity.mUrlRequestContext.shutdown();
140
141 // Shutting down the context will make JsonPrefStore to flush pending
142 // writes to disk.
143 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O";
144 assertTrue(prefFileContains("local_prefs.json", dictUrl));
145
146 // Test persistence.
147 CronetUrlRequestContext newContext = new CronetUrlRequestContext(
148 getInstrumentation().getTargetContext(), mActivity.getContextCon fig());
149 registerDNSMapping(newContext.getUrlRequestContextAdapter(), false);
150
151 // Wait until dictionaries are reloaded.
152 // FIXME: confirm with Elly where there's a less flaky way.
xunjieli 2015/05/11 15:50:41 FYI. Elly said she might be able to put in a CL to
153 Thread.sleep(15000);
154
155 // Make a request to fetch encoded response at /sdch/test.
156 TestUrlRequestListener listener3 =
157 startAndWaitForComplete(newContext, NativeTestServer.getSdchURL( ) + "/sdch/test");
158 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode());
159 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3 .mResponseAsString);
129 } 160 }
130 161
131 @SmallTest 162 @SmallTest
132 @Feature({"Cronet"}) 163 @Feature({"Cronet"})
133 public void testSdchDisabled() throws Exception { 164 public void testSdchDisabled() throws Exception {
134 setUp(false); 165 setUp(false);
135 // Make a request to /sdch. 166 // Make a request to /sdch.
136 // Since Sdch is not enabled, no dictionary should be advertised. 167 // Since Sdch is not enabled, no dictionary should be advertised.
137 TestUrlRequestListener listener = 168 TestUrlRequestListener listener = startAndWaitForComplete(mActivity.mUrl RequestContext,
138 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=LeQxM80O"); 169 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
139 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 170 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
140 assertEquals("This is an index page.\n", listener.mResponseAsString); 171 assertEquals("This is an index page.\n", listener.mResponseAsString);
141 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary")); 172 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary"));
142 } 173 }
143 174
144 @SmallTest 175 @SmallTest
145 @Feature({"Cronet"}) 176 @Feature({"Cronet"})
146 public void testDictionaryNotFound() throws Exception { 177 public void testDictionaryNotFound() throws Exception {
147 setUp(true); 178 setUp(true);
148 // Make a request to /sdch/index which advertises a bad dictionary that 179 // Make a request to /sdch/index which advertises a bad dictionary that
149 // does not exist. 180 // does not exist.
150 TestUrlRequestListener listener1 = 181 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr lRequestContext,
151 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=NotFound"); 182 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound");
152 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 183 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
153 assertEquals("This is an index page.\n", listener1.mResponseAsString); 184 assertEquals("This is an index page.\n", listener1.mResponseAsString);
154 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 185 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
155 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 186 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
156 187
157 waitForDictionaryAdded("NotFound", false); 188 waitForDictionaryAdded("NotFound", false);
158 189
159 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used. 190 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used.
160 TestUrlRequestListener listener2 = 191 TestUrlRequestListener listener2 = startAndWaitForComplete(
161 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); 192 mActivity.mUrlRequestContext, NativeTestServer.getSdchURL() + "/ sdch/test");
162 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); 193 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
163 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 194 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
164 } 195 }
165 196
166 /** 197 /**
167 * Helper method to wait for dictionary to be fetched and added to the list of available 198 * Helper method to wait for dictionary to be fetched and added to the list of available
168 * dictionaries. 199 * dictionaries.
169 */ 200 */
170 private void waitForDictionaryAdded(String dict, boolean isLegacyAPI) throws Exception { 201 private void waitForDictionaryAdded(String dict, boolean isLegacyAPI) throws Exception {
171 // Since Http cache is enabled, making a request to the dictionary expli citly will 202 // Since Http cache is enabled, making a request to the dictionary expli citly will
172 // be served from the cache. 203 // be served from the cache.
173 String url = NativeTestServer.getSdchURL() + "/sdch/dict/" + dict; 204 String url = NativeTestServer.getSdchURL() + "/sdch/dict/" + dict;
174 if (isLegacyAPI) { 205 if (isLegacyAPI) {
175 TestHttpUrlRequestListener listener = startAndWaitForComplete_Legacy API(url); 206 TestHttpUrlRequestListener listener = startAndWaitForComplete_Legacy API(url);
176 if (dict.equals("NotFound")) { 207 if (dict.equals("NotFound")) {
177 assertEquals(404, listener.mHttpStatusCode); 208 assertEquals(404, listener.mHttpStatusCode);
178 } else { 209 } else {
179 assertEquals(200, listener.mHttpStatusCode); 210 assertEquals(200, listener.mHttpStatusCode);
180 assertTrue(listener.mWasCached); 211 assertTrue(listener.mWasCached);
181 } 212 }
182 } else { 213 } else {
183 TestUrlRequestListener listener = startAndWaitForComplete(url); 214 TestUrlRequestListener listener =
215 startAndWaitForComplete(mActivity.mUrlRequestContext, url);
184 if (dict.equals("NotFound")) { 216 if (dict.equals("NotFound")) {
185 assertEquals(404, listener.mResponseInfo.getHttpStatusCode()); 217 assertEquals(404, listener.mResponseInfo.getHttpStatusCode());
186 } else { 218 } else {
187 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 219 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
188 assertTrue(listener.mResponseInfo.wasCached()); 220 assertTrue(listener.mResponseInfo.wasCached());
189 } 221 }
190 } 222 }
191 // Now wait for dictionary to be added to the manager, which occurs 223 // Now wait for dictionary to be added to the manager, which occurs
192 // asynchronously. 224 // asynchronously.
193 // TODO(xunjieli): Rather than setting an arbitrary delay, consider 225 // TODO(xunjieli): Rather than setting an arbitrary delay, consider
194 // implementing a SdchObserver to watch for dictionary add events once 226 // implementing a SdchObserver to watch for dictionary add events once
195 // add event is implemented in SdchObserver. 227 // add event is implemented in SdchObserver.
196 Thread.sleep(1000); 228 Thread.sleep(1000);
197 } 229 }
198 230
199 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(String url) 231 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(String url)
200 throws Exception { 232 throws Exception {
201 Map<String, String> headers = new HashMap<String, String>(); 233 Map<String, String> headers = new HashMap<String, String>();
202 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); 234 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
203 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( 235 HttpUrlRequest request = mActivity.mRequestFactory.createRequest(
204 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); 236 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
205 request.start(); 237 request.start();
206 listener.blockForComplete(); 238 listener.blockForComplete();
207 return listener; 239 return listener;
208 } 240 }
209 241
210 private TestUrlRequestListener startAndWaitForComplete(String url) throws Ex ception { 242 private TestUrlRequestListener startAndWaitForComplete(
243 UrlRequestContext requestContext, String url) throws Exception {
211 TestUrlRequestListener listener = new TestUrlRequestListener(); 244 TestUrlRequestListener listener = new TestUrlRequestListener();
212 UrlRequest request = 245 UrlRequest request = requestContext.createRequest(url, listener, listene r.getExecutor());
213 mActivity.mUrlRequestContext.createRequest(url, listener, listen er.getExecutor());
214 request.start(); 246 request.start();
215 listener.blockForDone(); 247 listener.blockForDone();
216 return listener; 248 return listener;
217 } 249 }
250
251 // Returns whether a pref file contains a particular string.
252 private boolean prefFileContains(String filename, String content) throws IOE xception {
253 File prefFile = new File(mActivity.getTestStorage() + "/" + filename);
254 assertTrue(prefFile.exists());
255 boolean contains = false;
256 InputStream in = null;
257 ByteArrayOutputStream out = null;
258 try {
259 in = new FileInputStream(prefFile);
mef 2015/05/12 16:39:17 It seems that canonical way of reading file into s
xunjieli 2015/05/12 19:17:50 Done. That's much cleaner! thanks.
260 out = new ByteArrayOutputStream();
261 int b;
262 while ((b = in.read()) != -1) {
263 out.write(b);
264 }
265 String prefFileContent = out.toString();
266 contains = prefFileContent.contains(content);
267 if (!contains) {
268 Log.i(TAG, "Did not find \"%s\" in file which contains \"%s\"", content,
269 prefFileContent);
270 }
271 } finally {
272 if (in != null) in.close();
273 if (out != null) out.close();
274 }
275 return contains;
276 }
277
278 // Registers custom DNS mapping for a native UrlRequestContextAdapter object .
279 private void registerDNSMapping(long urlRequestContextAdapter, boolean isLeg acyAPI) {
280 assertTrue(urlRequestContextAdapter != 0);
281 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, isLe gacyAPI);
282 }
218 } 283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698