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

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

Powered by Google App Engine
This is Rietveld 408576698