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

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

Powered by Google App Engine
This is Rietveld 408576698