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

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: Missing include 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.os.ConditionVariable;
7 import android.test.suitebuilder.annotation.SmallTest; 8 import android.test.suitebuilder.annotation.SmallTest;
8 9
10 import org.chromium.base.Log;
9 import org.chromium.base.test.util.Feature; 11 import org.chromium.base.test.util.Feature;
12 import org.chromium.net.SdchTestUtil.SdchObserver;
10 13
14 import java.io.BufferedReader;
15 import java.io.FileReader;
16 import java.io.IOException;
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 enum SDCH {
22 UrlRequestContextConfig config = new UrlRequestContextConfig(); 31 ENABLED,
23 config.enableSDCH(enableSdch); 32 DISABLED,
24 config.setLibraryName("cronet_tests"); 33 }
25 config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY, 100 * 1024);
26 String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, config.toStri ng()};
27 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLi neArgs);
28 mActivity.startNetLog();
29 34
30 // Registers custom DNS mapping for legacy ChromiumUrlRequestFactory. 35 private enum API {
31 ChromiumUrlRequestFactory factory = (ChromiumUrlRequestFactory) mActivit y.mRequestFactory; 36 LEGACY,
32 long legacyAdapter = factory.getRequestContext().getUrlRequestContextAda pterForTesting(); 37 ASYNC,
33 assertTrue(legacyAdapter != 0); 38 }
34 NativeTestServer.registerHostResolverProc(legacyAdapter, true);
35 39
36 // Registers custom DNS mapping for CronetUrlRequestContext. 40 private void setUp(SDCH setting, API api) {
37 CronetUrlRequestContext requestContext = 41 List<String> commandLineArgs = new ArrayList<String>();
38 (CronetUrlRequestContext) mActivity.mUrlRequestContext; 42 commandLineArgs.add(CronetTestActivity.CACHE_KEY);
39 long adapter = requestContext.getUrlRequestContextAdapter(); 43 commandLineArgs.add(CronetTestActivity.CACHE_DISK);
40 assertTrue(adapter != 0); 44 if (setting == SDCH.ENABLED) {
41 NativeTestServer.registerHostResolverProc(adapter, false); 45 commandLineArgs.add(CronetTestActivity.SDCH_KEY);
46 commandLineArgs.add("enable");
47 }
42 48
43 // Starts NativeTestServer. 49 String[] args = new String[commandLineArgs.size()];
50 mActivity =
51 launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineAr gs.toArray(args));
52 long urlRequestContextAdapter = (api == API.LEGACY)
53 ? getRequestAdapter((ChromiumUrlRequestFactory) mActivity.mReque stFactory)
54 : getRequestAdapter((CronetUrlRequestContext) mActivity.mUrlRequ estContext);
55 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, api == API.LEGACY);
56 // Start NativeTestServer.
44 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g etTargetContext())); 57 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g etTargetContext()));
45 } 58 }
46 59
47 @Override 60 @Override
48 protected void tearDown() throws Exception { 61 protected void tearDown() throws Exception {
49 NativeTestServer.shutdownNativeTestServer(); 62 NativeTestServer.shutdownNativeTestServer();
50 mActivity.stopNetLog();
51 super.tearDown(); 63 super.tearDown();
52 } 64 }
53 65
54 @SmallTest 66 @SmallTest
55 @Feature({"Cronet"}) 67 @Feature({"Cronet"})
56 public void testSdchEnabled_LegacyAPI() throws Exception { 68 public void testSdchEnabled_LegacyAPI() throws Exception {
57 setUp(true); 69 setUp(SDCH.ENABLED, API.LEGACY);
70 long requestAdapter =
71 getRequestAdapter((ChromiumUrlRequestFactory) mActivity.mRequest Factory);
72 Observer observer = addObserver(requestAdapter, API.LEGACY);
58 // Make a request to /sdch/index which advertises the dictionary. 73 // Make a request to /sdch/index which advertises the dictionary.
59 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI ( 74 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI (
60 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); 75 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
61 assertEquals(200, listener1.mHttpStatusCode); 76 assertEquals(200, listener1.mHttpStatusCode);
62 assertEquals("This is an index page.\n", listener1.mResponseAsString); 77 assertEquals("This is an index page.\n", listener1.mResponseAsString);
63 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 78 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
64 listener1.mResponseHeaders.get("Get-Dictionary")); 79 listener1.mResponseHeaders.get("Get-Dictionary"));
65 80
66 waitForDictionaryAdded("LeQxM80O", true); 81 observer.waitForDictionaryAdded("LeQxM80O");
67 82
68 // Make a request to fetch encoded response at /sdch/test. 83 // Make a request to fetch encoded response at /sdch/test.
69 TestHttpUrlRequestListener listener2 = 84 TestHttpUrlRequestListener listener2 =
70 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test"); 85 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test");
71 assertEquals(200, listener2.mHttpStatusCode); 86 assertEquals(200, listener2.mHttpStatusCode);
72 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 87 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
88 removeObserver(requestAdapter, observer, API.LEGACY);
73 } 89 }
74 90
75 @SmallTest 91 @SmallTest
76 @Feature({"Cronet"}) 92 @Feature({"Cronet"})
77 public void testSdchDisabled_LegacyAPI() throws Exception { 93 public void testSdchDisabled_LegacyAPI() throws Exception {
78 setUp(false); 94 setUp(SDCH.DISABLED, API.LEGACY);
79 // Make a request to /sdch/index. 95 // Make a request to /sdch/index.
80 // Since Sdch is not enabled, no dictionary should be advertised. 96 // Since Sdch is not enabled, no dictionary should be advertised.
81 TestHttpUrlRequestListener listener = startAndWaitForComplete_LegacyAPI( 97 TestHttpUrlRequestListener listener = startAndWaitForComplete_LegacyAPI(
82 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); 98 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
83 assertEquals(200, listener.mHttpStatusCode); 99 assertEquals(200, listener.mHttpStatusCode);
84 assertEquals("This is an index page.\n", listener.mResponseAsString); 100 assertEquals("This is an index page.\n", listener.mResponseAsString);
85 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary")); 101 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary"));
86 } 102 }
87 103
88 @SmallTest 104 @SmallTest
89 @Feature({"Cronet"}) 105 @Feature({"Cronet"})
90 public void testDictionaryNotFound_LegacyAPI() throws Exception { 106 public void testDictionaryNotFound_LegacyAPI() throws Exception {
91 setUp(true); 107 setUp(SDCH.ENABLED, API.LEGACY);
92 // Make a request to /sdch/index which advertises a bad dictionary that 108 // Make a request to /sdch/index which advertises a bad dictionary that
93 // does not exist. 109 // does not exist.
94 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI ( 110 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI (
95 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound"); 111 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound");
96 assertEquals(200, listener1.mHttpStatusCode); 112 assertEquals(200, listener1.mHttpStatusCode);
97 assertEquals("This is an index page.\n", listener1.mResponseAsString); 113 assertEquals("This is an index page.\n", listener1.mResponseAsString);
98 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 114 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
99 listener1.mResponseHeaders.get("Get-Dictionary")); 115 listener1.mResponseHeaders.get("Get-Dictionary"));
100 116
101 waitForDictionaryAdded("NotFound", true); 117 // Make a request to fetch /sdch/test, and make sure request succeeds.
102
103 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used.
104 TestHttpUrlRequestListener listener2 = 118 TestHttpUrlRequestListener listener2 =
105 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test"); 119 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test");
106 assertEquals(200, listener2.mHttpStatusCode); 120 assertEquals(200, listener2.mHttpStatusCode);
107 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 121 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
108 } 122 }
109 123
110 @SmallTest 124 @SmallTest
111 @Feature({"Cronet"}) 125 @Feature({"Cronet"})
112 public void testSdchEnabled() throws Exception { 126 public void testSdchEnabled() throws Exception {
113 setUp(true); 127 setUp(SDCH.ENABLED, API.ASYNC);
128 long requestAdapter =
129 getRequestAdapter((CronetUrlRequestContext) mActivity.mUrlReques tContext);
130 Observer observer = addObserver(requestAdapter, API.ASYNC);
114 // Make a request to /sdch which advertises the dictionary. 131 // Make a request to /sdch which advertises the dictionary.
115 TestUrlRequestListener listener1 = 132 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr lRequestContext,
116 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=LeQxM80O"); 133 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
117 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 134 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
118 assertEquals("This is an index page.\n", listener1.mResponseAsString); 135 assertEquals("This is an index page.\n", listener1.mResponseAsString);
119 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 136 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
120 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 137 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
121 138
122 waitForDictionaryAdded("LeQxM80O", false); 139 observer.waitForDictionaryAdded("LeQxM80O");
123 140
124 // Make a request to fetch encoded response at /sdch/test. 141 // Make a request to fetch encoded response at /sdch/test.
125 TestUrlRequestListener listener2 = 142 TestUrlRequestListener listener2 = startAndWaitForComplete(
126 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); 143 mActivity.mUrlRequestContext, NativeTestServer.getSdchURL() + "/ sdch/test");
127 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 144 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
128 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 145 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
146 removeObserver(requestAdapter, observer, API.ASYNC);
147
148 // Wait for a bit until SimpleCache finished closing entries before
149 // calling shutdown on the UrlRequestContext.
150 // TODO(xunjieli): Remove once crbug.com/486120 is fixed.
151 Thread.sleep(5000);
152 mActivity.mUrlRequestContext.shutdown();
153
154 // Shutting down the context will make JsonPrefStore to flush pending
155 // writes to disk.
156 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O";
157 assertTrue(prefFileContains("local_prefs.json", dictUrl));
158
159 // Test persistence.
160 CronetUrlRequestContext newContext = new CronetUrlRequestContext(
161 getInstrumentation().getTargetContext(), mActivity.getContextCon fig());
162
163 long newRequestAdapter = getRequestAdapter(newContext);
164 NativeTestServer.registerHostResolverProc(newRequestAdapter, false);
165 Observer newObserver = addObserver(newRequestAdapter, API.ASYNC);
166
167 newObserver.waitForDictionaryAdded("LeQxM80O");
168
169 // Make a request to fetch encoded response at /sdch/test.
170 TestUrlRequestListener listener3 =
171 startAndWaitForComplete(newContext, NativeTestServer.getSdchURL( ) + "/sdch/test");
172 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode());
173 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3 .mResponseAsString);
174 removeObserver(newRequestAdapter, newObserver, API.ASYNC);
129 } 175 }
130 176
131 @SmallTest 177 @SmallTest
132 @Feature({"Cronet"}) 178 @Feature({"Cronet"})
133 public void testSdchDisabled() throws Exception { 179 public void testSdchDisabled() throws Exception {
134 setUp(false); 180 setUp(SDCH.DISABLED, API.ASYNC);
135 // Make a request to /sdch. 181 // Make a request to /sdch.
136 // Since Sdch is not enabled, no dictionary should be advertised. 182 // Since Sdch is not enabled, no dictionary should be advertised.
137 TestUrlRequestListener listener = 183 TestUrlRequestListener listener = startAndWaitForComplete(mActivity.mUrl RequestContext,
138 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=LeQxM80O"); 184 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
139 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 185 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
140 assertEquals("This is an index page.\n", listener.mResponseAsString); 186 assertEquals("This is an index page.\n", listener.mResponseAsString);
141 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary")); 187 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary"));
142 } 188 }
143 189
144 @SmallTest 190 @SmallTest
145 @Feature({"Cronet"}) 191 @Feature({"Cronet"})
146 public void testDictionaryNotFound() throws Exception { 192 public void testDictionaryNotFound() throws Exception {
147 setUp(true); 193 setUp(SDCH.ENABLED, API.ASYNC);
148 // Make a request to /sdch/index which advertises a bad dictionary that 194 // Make a request to /sdch/index which advertises a bad dictionary that
149 // does not exist. 195 // does not exist.
150 TestUrlRequestListener listener1 = 196 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr lRequestContext,
151 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=NotFound"); 197 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound");
152 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 198 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
153 assertEquals("This is an index page.\n", listener1.mResponseAsString); 199 assertEquals("This is an index page.\n", listener1.mResponseAsString);
154 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 200 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
155 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 201 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
156 202
157 waitForDictionaryAdded("NotFound", false);
158
159 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used. 203 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used.
160 TestUrlRequestListener listener2 = 204 TestUrlRequestListener listener2 = startAndWaitForComplete(
161 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); 205 mActivity.mUrlRequestContext, NativeTestServer.getSdchURL() + "/ sdch/test");
162 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); 206 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
163 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 207 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
164 } 208 }
165 209
166 /** 210 private static class Observer implements SdchObserver {
167 * Helper method to wait for dictionary to be fetched and added to the list of available 211 ConditionVariable mBlock = new ConditionVariable();
168 * dictionaries. 212 private String mLastAddedDict;
169 */ 213
170 private void waitForDictionaryAdded(String dict, boolean isLegacyAPI) throws Exception { 214 @Override
171 // Since Http cache is enabled, making a request to the dictionary expli citly will 215 public void onDictionaryAdded(String url) {
172 // be served from the cache. 216 Log.i(TAG, "Dictionary %s is added ", url);
173 String url = NativeTestServer.getSdchURL() + "/sdch/dict/" + dict; 217 mLastAddedDict = url;
174 if (isLegacyAPI) { 218 mBlock.open();
175 TestHttpUrlRequestListener listener = startAndWaitForComplete_Legacy API(url);
176 if (dict.equals("NotFound")) {
177 assertEquals(404, listener.mHttpStatusCode);
178 } else {
179 assertEquals(200, listener.mHttpStatusCode);
180 assertTrue(listener.mWasCached);
181 }
182 } else {
183 TestUrlRequestListener listener = startAndWaitForComplete(url);
184 if (dict.equals("NotFound")) {
185 assertEquals(404, listener.mResponseInfo.getHttpStatusCode());
186 } else {
187 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
188 assertTrue(listener.mResponseInfo.wasCached());
189 }
190 } 219 }
191 // Now wait for dictionary to be added to the manager, which occurs 220
192 // asynchronously. 221 public void waitForDictionaryAdded(String dictName) {
193 // TODO(xunjieli): Rather than setting an arbitrary delay, consider 222 mBlock.block();
194 // implementing a SdchObserver to watch for dictionary add events once 223 mBlock.close();
195 // add event is implemented in SdchObserver. 224 assertTrue(mLastAddedDict.contains(dictName));
196 Thread.sleep(1000); 225 }
226 }
227
228 // Registers Observer.
229 private Observer addObserver(long contextAdapter, API api) {
230 Observer observer = new Observer();
231 SdchTestUtil.addSdchObserver(contextAdapter, observer, api == API.LEGACY );
232 return observer;
233 }
234
235 // Unregisters Observer.
236 private void removeObserver(long contextAdapter, Observer observer, API api) {
237 SdchTestUtil.removeSdchObserver(contextAdapter, observer, api == API.LEG ACY);
238 }
239
240 private long getRequestAdapter(ChromiumUrlRequestFactory factory) {
241 return factory.getRequestContext().getUrlRequestContextAdapter();
242 }
243
244 private long getRequestAdapter(CronetUrlRequestContext requestContext) {
245 return requestContext.getUrlRequestContextAdapter();
197 } 246 }
198 247
199 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(String url) 248 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(String url)
200 throws Exception { 249 throws Exception {
201 Map<String, String> headers = new HashMap<String, String>(); 250 Map<String, String> headers = new HashMap<String, String>();
202 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); 251 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
203 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( 252 HttpUrlRequest request = mActivity.mRequestFactory.createRequest(
204 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); 253 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
205 request.start(); 254 request.start();
206 listener.blockForComplete(); 255 listener.blockForComplete();
207 return listener; 256 return listener;
208 } 257 }
209 258
210 private TestUrlRequestListener startAndWaitForComplete(String url) throws Ex ception { 259 private TestUrlRequestListener startAndWaitForComplete(
260 UrlRequestContext requestContext, String url) throws Exception {
211 TestUrlRequestListener listener = new TestUrlRequestListener(); 261 TestUrlRequestListener listener = new TestUrlRequestListener();
212 UrlRequest request = 262 UrlRequest request = requestContext.createRequest(url, listener, listene r.getExecutor());
213 mActivity.mUrlRequestContext.createRequest(url, listener, listen er.getExecutor());
214 request.start(); 263 request.start();
215 listener.blockForDone(); 264 listener.blockForDone();
216 return listener; 265 return listener;
217 } 266 }
267
268 // Returns whether a pref file contains a particular string.
269 private boolean prefFileContains(String filename, String content) throws IOE xception {
270 BufferedReader reader =
271 new BufferedReader(new FileReader(mActivity.getTestStorage() + " /" + filename));
272 StringBuilder builder = new StringBuilder();
273 String line;
274 while ((line = reader.readLine()) != null) {
275 builder.append(line);
276 }
277 reader.close();
278 String fileContent = builder.toString();
279 final boolean contains = fileContent.contains(content);
280 if (!contains) {
281 Log.i(TAG, "Did not find \"%s\" in file which contains \"%s\"", cont ent, fileContent);
282 }
283 return contains;
284 }
218 } 285 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698