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

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

Powered by Google App Engine
This is Rietveld 408576698