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

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: Remove pref file support from legacy API 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.SdchObserverCallback;
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(CronetTestActivity.SDCH_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 ? getContextAdapter((ChromiumUrlRequestFactory) mActivity.mReque stFactory)
54 : getContextAdapter((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 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test";
71 long contextAdapter =
72 getContextAdapter((ChromiumUrlRequestFactory) mActivity.mRequest Factory);
73 Callback callback = new Callback();
mef 2015/05/21 17:29:44 Optional: I think 'Callback' is too generic, so it
xunjieli 2015/05/21 17:45:26 Done.
74 assertTrue(SdchTestUtil.registerSdchObserverCallback(
75 targetUrl, contextAdapter, callback, true /** Legacy API */));
76
58 // Make a request to /sdch/index which advertises the dictionary. 77 // Make a request to /sdch/index which advertises the dictionary.
59 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI ( 78 TestHttpUrlRequestListener listener1 =
60 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); 79 startAndWaitForComplete_LegacyAPI(mActivity.mRequestFactory,
80 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O" );
61 assertEquals(200, listener1.mHttpStatusCode); 81 assertEquals(200, listener1.mHttpStatusCode);
62 assertEquals("This is an index page.\n", listener1.mResponseAsString); 82 assertEquals("This is an index page.\n", listener1.mResponseAsString);
63 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 83 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
64 listener1.mResponseHeaders.get("Get-Dictionary")); 84 listener1.mResponseHeaders.get("Get-Dictionary"));
65 85
66 waitForDictionaryAdded("LeQxM80O", true); 86 callback.waitForDictionaryAdded("LeQxM80O");
67 87
68 // Make a request to fetch encoded response at /sdch/test. 88 // Make a request to fetch encoded response at /sdch/test.
69 TestHttpUrlRequestListener listener2 = 89 TestHttpUrlRequestListener listener2 =
70 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test"); 90 startAndWaitForComplete_LegacyAPI(mActivity.mRequestFactory, tar getUrl);
71 assertEquals(200, listener2.mHttpStatusCode); 91 assertEquals(200, listener2.mHttpStatusCode);
72 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 92 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
73 } 93 }
74 94
75 @SmallTest 95 @SmallTest
76 @Feature({"Cronet"}) 96 @Feature({"Cronet"})
77 public void testSdchDisabled_LegacyAPI() throws Exception { 97 public void testSdchDisabled_LegacyAPI() throws Exception {
78 setUp(false); 98 setUp(SDCH.DISABLED, API.LEGACY);
79 // Make a request to /sdch/index. 99 // Make a request to /sdch/index.
80 // Since Sdch is not enabled, no dictionary should be advertised. 100 // Since Sdch is not enabled, no dictionary should be advertised.
81 TestHttpUrlRequestListener listener = startAndWaitForComplete_LegacyAPI( 101 TestHttpUrlRequestListener listener =
82 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); 102 startAndWaitForComplete_LegacyAPI(mActivity.mRequestFactory,
103 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O" );
83 assertEquals(200, listener.mHttpStatusCode); 104 assertEquals(200, listener.mHttpStatusCode);
84 assertEquals("This is an index page.\n", listener.mResponseAsString); 105 assertEquals("This is an index page.\n", listener.mResponseAsString);
85 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary")); 106 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary"));
86 } 107 }
87 108
88 @SmallTest 109 @SmallTest
89 @Feature({"Cronet"}) 110 @Feature({"Cronet"})
90 public void testDictionaryNotFound_LegacyAPI() throws Exception { 111 public void testDictionaryNotFound_LegacyAPI() throws Exception {
91 setUp(true); 112 setUp(SDCH.ENABLED, API.LEGACY);
92 // Make a request to /sdch/index which advertises a bad dictionary that 113 // Make a request to /sdch/index which advertises a bad dictionary that
93 // does not exist. 114 // does not exist.
94 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI ( 115 TestHttpUrlRequestListener listener1 =
95 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound"); 116 startAndWaitForComplete_LegacyAPI(mActivity.mRequestFactory,
117 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound" );
96 assertEquals(200, listener1.mHttpStatusCode); 118 assertEquals(200, listener1.mHttpStatusCode);
97 assertEquals("This is an index page.\n", listener1.mResponseAsString); 119 assertEquals("This is an index page.\n", listener1.mResponseAsString);
98 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 120 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
99 listener1.mResponseHeaders.get("Get-Dictionary")); 121 listener1.mResponseHeaders.get("Get-Dictionary"));
100 122
101 waitForDictionaryAdded("NotFound", true); 123 // Make a request to fetch /sdch/test, and make sure request succeeds.
102 124 TestHttpUrlRequestListener listener2 = startAndWaitForComplete_LegacyAPI (
103 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used. 125 mActivity.mRequestFactory, NativeTestServer.getSdchURL() + "/sdc h/test");
104 TestHttpUrlRequestListener listener2 =
105 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test");
106 assertEquals(200, listener2.mHttpStatusCode); 126 assertEquals(200, listener2.mHttpStatusCode);
107 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 127 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
108 } 128 }
109 129
110 @SmallTest 130 @SmallTest
111 @Feature({"Cronet"}) 131 @Feature({"Cronet"})
112 public void testSdchEnabled() throws Exception { 132 public void testSdchEnabled() throws Exception {
113 setUp(true); 133 setUp(SDCH.ENABLED, API.ASYNC);
134 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test";
135 long contextAdapter =
136 getContextAdapter((CronetUrlRequestContext) mActivity.mUrlReques tContext);
137 Callback callback = new Callback();
138 assertTrue(SdchTestUtil.registerSdchObserverCallback(
139 targetUrl, contextAdapter, callback, false /** Legacy API */));
140
114 // Make a request to /sdch which advertises the dictionary. 141 // Make a request to /sdch which advertises the dictionary.
115 TestUrlRequestListener listener1 = 142 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr lRequestContext,
116 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=LeQxM80O"); 143 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
117 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 144 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
118 assertEquals("This is an index page.\n", listener1.mResponseAsString); 145 assertEquals("This is an index page.\n", listener1.mResponseAsString);
119 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 146 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
120 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 147 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
121 148
122 waitForDictionaryAdded("LeQxM80O", false); 149 callback.waitForDictionaryAdded("LeQxM80O");
123 150
124 // Make a request to fetch encoded response at /sdch/test. 151 // Make a request to fetch encoded response at /sdch/test.
125 TestUrlRequestListener listener2 = 152 TestUrlRequestListener listener2 =
126 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); 153 startAndWaitForComplete(mActivity.mUrlRequestContext, targetUrl) ;
127 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 154 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
128 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 155 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
156
157 // Wait for a bit until SimpleCache finished closing entries before
158 // calling shutdown on the UrlRequestContext.
159 // TODO(xunjieli): Remove once crbug.com/486120 is fixed.
160 Thread.sleep(5000);
161 mActivity.mUrlRequestContext.shutdown();
162
163 // Shutting down the context will make JsonPrefStore to flush pending
164 // writes to disk.
165 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O";
166 assertTrue(fileContainsString("local_prefs.json", dictUrl));
167
168 // Test persistence.
169 CronetUrlRequestContext newContext = new CronetUrlRequestContext(
170 getInstrumentation().getTargetContext(), mActivity.getContextCon fig());
171
172 long newContextAdapter = getContextAdapter(newContext);
173 NativeTestServer.registerHostResolverProc(newContextAdapter, false);
174 Callback newCallback = new Callback();
175 // Only wait for dictionaries when they are yet to be loaded.
176 if (SdchTestUtil.registerSdchObserverCallback(
177 targetUrl, newContextAdapter, newCallback, false /** Legacy API */)) {
178 newCallback.waitForDictionaryAdded("LeQxM80O");
179 }
180
181 // Make a request to fetch encoded response at /sdch/test.
182 TestUrlRequestListener listener3 = startAndWaitForComplete(newContext, t argetUrl);
183 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode());
184 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3 .mResponseAsString);
129 } 185 }
130 186
131 @SmallTest 187 @SmallTest
132 @Feature({"Cronet"}) 188 @Feature({"Cronet"})
133 public void testSdchDisabled() throws Exception { 189 public void testSdchDisabled() throws Exception {
134 setUp(false); 190 setUp(SDCH.DISABLED, API.ASYNC);
135 // Make a request to /sdch. 191 // Make a request to /sdch.
136 // Since Sdch is not enabled, no dictionary should be advertised. 192 // Since Sdch is not enabled, no dictionary should be advertised.
137 TestUrlRequestListener listener = 193 TestUrlRequestListener listener = startAndWaitForComplete(mActivity.mUrl RequestContext,
138 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=LeQxM80O"); 194 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
139 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 195 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
140 assertEquals("This is an index page.\n", listener.mResponseAsString); 196 assertEquals("This is an index page.\n", listener.mResponseAsString);
141 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary")); 197 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary"));
142 } 198 }
143 199
144 @SmallTest 200 @SmallTest
145 @Feature({"Cronet"}) 201 @Feature({"Cronet"})
146 public void testDictionaryNotFound() throws Exception { 202 public void testDictionaryNotFound() throws Exception {
147 setUp(true); 203 setUp(SDCH.ENABLED, API.ASYNC);
148 // Make a request to /sdch/index which advertises a bad dictionary that 204 // Make a request to /sdch/index which advertises a bad dictionary that
149 // does not exist. 205 // does not exist.
150 TestUrlRequestListener listener1 = 206 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr lRequestContext,
151 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i ndex?q=NotFound"); 207 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound");
152 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 208 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
153 assertEquals("This is an index page.\n", listener1.mResponseAsString); 209 assertEquals("This is an index page.\n", listener1.mResponseAsString);
154 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 210 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
155 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 211 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
156 212
157 waitForDictionaryAdded("NotFound", false);
158
159 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used. 213 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used.
160 TestUrlRequestListener listener2 = 214 TestUrlRequestListener listener2 = startAndWaitForComplete(
161 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); 215 mActivity.mUrlRequestContext, NativeTestServer.getSdchURL() + "/ sdch/test");
162 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); 216 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
163 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 217 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
164 } 218 }
165 219
166 /** 220 private static class Callback extends SdchObserverCallback {
167 * Helper method to wait for dictionary to be fetched and added to the list of available 221 ConditionVariable mBlock = new ConditionVariable();
168 * dictionaries. 222 private String mLastAddedDict;
169 */ 223
170 private void waitForDictionaryAdded(String dict, boolean isLegacyAPI) throws Exception { 224 @Override
171 // Since Http cache is enabled, making a request to the dictionary expli citly will 225 public void onDictionaryAdded(String url) {
172 // be served from the cache. 226 Log.i(TAG, "Dictionary %s is added ", url);
173 String url = NativeTestServer.getSdchURL() + "/sdch/dict/" + dict; 227 mLastAddedDict = url;
174 if (isLegacyAPI) { 228 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 } 229 }
191 // Now wait for dictionary to be added to the manager, which occurs 230
192 // asynchronously. 231 public void waitForDictionaryAdded(String dictName) {
193 // TODO(xunjieli): Rather than setting an arbitrary delay, consider 232 mBlock.block();
194 // implementing a SdchObserver to watch for dictionary add events once 233 mBlock.close();
195 // add event is implemented in SdchObserver. 234 assertTrue(mLastAddedDict.contains(dictName));
196 Thread.sleep(1000); 235 }
197 } 236 }
198 237
199 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(String url) 238 private long getContextAdapter(ChromiumUrlRequestFactory factory) {
200 throws Exception { 239 return factory.getRequestContext().getUrlRequestContextAdapter();
240 }
241
242 private long getContextAdapter(CronetUrlRequestContext requestContext) {
243 return requestContext.getUrlRequestContextAdapter();
244 }
245
246 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(
247 HttpUrlRequestFactory factory, String url) throws Exception {
201 Map<String, String> headers = new HashMap<String, String>(); 248 Map<String, String> headers = new HashMap<String, String>();
202 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); 249 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
203 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( 250 HttpUrlRequest request = factory.createRequest(
204 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); 251 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
205 request.start(); 252 request.start();
206 listener.blockForComplete(); 253 listener.blockForComplete();
207 return listener; 254 return listener;
208 } 255 }
209 256
210 private TestUrlRequestListener startAndWaitForComplete(String url) throws Ex ception { 257 private TestUrlRequestListener startAndWaitForComplete(
258 UrlRequestContext requestContext, String url) throws Exception {
211 TestUrlRequestListener listener = new TestUrlRequestListener(); 259 TestUrlRequestListener listener = new TestUrlRequestListener();
212 UrlRequest request = 260 UrlRequest request = requestContext.createRequest(url, listener, listene r.getExecutor());
213 mActivity.mUrlRequestContext.createRequest(url, listener, listen er.getExecutor());
214 request.start(); 261 request.start();
215 listener.blockForDone(); 262 listener.blockForDone();
216 return listener; 263 return listener;
217 } 264 }
265
266 // Returns whether a file contains a particular string.
267 private boolean fileContainsString(String filename, String content) throws I OException {
268 BufferedReader reader =
269 new BufferedReader(new FileReader(mActivity.getTestStorage() + " /" + filename));
270 String line;
271 while ((line = reader.readLine()) != null) {
272 if (line.contains(content)) {
273 reader.close();
274 return true;
275 }
276 }
277 reader.close();
278 return false;
279 }
218 } 280 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698