OLD | NEW |
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.os.ConditionVariable; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
11 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; | |
12 import org.chromium.net.impl.ChromiumUrlRequestFactory; | |
13 import org.chromium.net.impl.CronetUrlRequestContext; | 11 import org.chromium.net.impl.CronetUrlRequestContext; |
14 import org.json.JSONException; | 12 import org.json.JSONException; |
15 import org.json.JSONObject; | 13 import org.json.JSONObject; |
16 | 14 |
17 import java.io.BufferedReader; | 15 import java.io.BufferedReader; |
18 import java.io.FileReader; | 16 import java.io.FileReader; |
19 import java.io.IOException; | 17 import java.io.IOException; |
20 import java.util.ArrayList; | 18 import java.util.ArrayList; |
21 import java.util.Arrays; | 19 import java.util.Arrays; |
22 import java.util.HashMap; | |
23 import java.util.List; | 20 import java.util.List; |
24 import java.util.Map; | |
25 | 21 |
26 /** | 22 /** |
27 * Tests Sdch support. | 23 * Tests Sdch support. |
28 */ | 24 */ |
29 public class SdchTest extends CronetTestBase { | 25 public class SdchTest extends CronetTestBase { |
30 private CronetTestFramework mTestFramework; | 26 private CronetTestFramework mTestFramework; |
31 | 27 |
32 private enum Sdch { | 28 private enum Sdch { |
33 ENABLED, | 29 ENABLED, |
34 DISABLED, | 30 DISABLED, |
(...skipping 16 matching lines...) Expand all Loading... |
51 | 47 |
52 if (api == Api.LEGACY) { | 48 if (api == Api.LEGACY) { |
53 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); | 49 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); |
54 commandLineArgs.add(CronetTestFramework.LibraryInitType.LEGACY); | 50 commandLineArgs.add(CronetTestFramework.LibraryInitType.LEGACY); |
55 } else { | 51 } else { |
56 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); | 52 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); |
57 commandLineArgs.add(CronetTestFramework.LibraryInitType.CRONET); | 53 commandLineArgs.add(CronetTestFramework.LibraryInitType.CRONET); |
58 } | 54 } |
59 | 55 |
60 String[] args = new String[commandLineArgs.size()]; | 56 String[] args = new String[commandLineArgs.size()]; |
61 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 57 ExperimentalCronetEngine.Builder builder = |
| 58 new ExperimentalCronetEngine.Builder(getContext()); |
62 JSONObject hostResolverParams = CronetTestUtil.generateHostResolverRules
(); | 59 JSONObject hostResolverParams = CronetTestUtil.generateHostResolverRules
(); |
63 JSONObject experimentalOptions = | 60 JSONObject experimentalOptions = |
64 new JSONObject().put("HostResolverRules", hostResolverParams); | 61 new JSONObject().put("HostResolverRules", hostResolverParams); |
65 builder.setExperimentalOptions(experimentalOptions.toString()); | 62 builder.setExperimentalOptions(experimentalOptions.toString()); |
66 mTestFramework = | 63 mTestFramework = |
67 new CronetTestFramework(null, commandLineArgs.toArray(args), get
Context(), builder); | 64 new CronetTestFramework(null, commandLineArgs.toArray(args), get
Context(), builder); |
68 // Start NativeTestServer. | 65 // Start NativeTestServer. |
69 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 66 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
70 } | 67 } |
71 | 68 |
72 @Override | 69 @Override |
73 protected void tearDown() throws Exception { | 70 protected void tearDown() throws Exception { |
74 NativeTestServer.shutdownNativeTestServer(); | 71 NativeTestServer.shutdownNativeTestServer(); |
75 super.tearDown(); | 72 super.tearDown(); |
76 } | 73 } |
77 | 74 |
78 @SmallTest | 75 @SmallTest |
79 @Feature({"Cronet"}) | 76 @Feature({"Cronet"}) |
80 @SuppressWarnings("deprecation") | |
81 @OnlyRunNativeCronet | |
82 public void testSdchEnabled_LegacyApi() throws Exception { | |
83 setUp(Sdch.ENABLED, Api.LEGACY); | |
84 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; | |
85 long contextAdapter = | |
86 getContextAdapter((ChromiumUrlRequestFactory) mTestFramework.mRe
questFactory); | |
87 DictionaryAddedObserver observer = | |
88 new DictionaryAddedObserver(targetUrl, contextAdapter, true /**
Legacy Api */); | |
89 | |
90 // Make a request to /sdch/index which advertises the dictionary. | |
91 TestHttpUrlRequestListener listener1 = | |
92 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory
, | |
93 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"
); | |
94 assertEquals(200, listener1.mHttpStatusCode); | |
95 assertEquals("This is an index page.\n", listener1.mResponseAsString); | |
96 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), | |
97 listener1.mResponseHeaders.get("Get-Dictionary")); | |
98 | |
99 observer.waitForDictionaryAdded(); | |
100 | |
101 // Make a request to fetch encoded response at /sdch/test. | |
102 TestHttpUrlRequestListener listener2 = | |
103 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory
, targetUrl); | |
104 assertEquals(200, listener2.mHttpStatusCode); | |
105 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2
.mResponseAsString); | |
106 } | |
107 | |
108 @SmallTest | |
109 @Feature({"Cronet"}) | |
110 @SuppressWarnings("deprecation") | |
111 @OnlyRunNativeCronet | |
112 public void testSdchDisabled_LegacyApi() throws Exception { | |
113 setUp(Sdch.DISABLED, Api.LEGACY); | |
114 // Make a request to /sdch/index. | |
115 // Since Sdch is not enabled, no dictionary should be advertised. | |
116 TestHttpUrlRequestListener listener = | |
117 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory
, | |
118 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"
); | |
119 assertEquals(200, listener.mHttpStatusCode); | |
120 assertEquals("This is an index page.\n", listener.mResponseAsString); | |
121 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary")); | |
122 } | |
123 | |
124 @SmallTest | |
125 @Feature({"Cronet"}) | |
126 @SuppressWarnings("deprecation") | |
127 @OnlyRunNativeCronet | |
128 public void testDictionaryNotFound_LegacyApi() throws Exception { | |
129 setUp(Sdch.ENABLED, Api.LEGACY); | |
130 // Make a request to /sdch/index which advertises a bad dictionary that | |
131 // does not exist. | |
132 TestHttpUrlRequestListener listener1 = | |
133 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory
, | |
134 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound"
); | |
135 assertEquals(200, listener1.mHttpStatusCode); | |
136 assertEquals("This is an index page.\n", listener1.mResponseAsString); | |
137 assertEquals(Arrays.asList("/sdch/dict/NotFound"), | |
138 listener1.mResponseHeaders.get("Get-Dictionary")); | |
139 | |
140 // Make a request to fetch /sdch/test, and make sure request succeeds. | |
141 TestHttpUrlRequestListener listener2 = startAndWaitForComplete_LegacyApi
( | |
142 mTestFramework.mRequestFactory, NativeTestServer.getSdchURL() +
"/sdch/test"); | |
143 assertEquals(200, listener2.mHttpStatusCode); | |
144 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); | |
145 } | |
146 | |
147 @SmallTest | |
148 @Feature({"Cronet"}) | |
149 @OnlyRunNativeCronet | 77 @OnlyRunNativeCronet |
150 public void testSdchEnabled() throws Exception { | 78 public void testSdchEnabled() throws Exception { |
151 setUp(Sdch.ENABLED, Api.ASYNC); | 79 setUp(Sdch.ENABLED, Api.ASYNC); |
152 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; | 80 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; |
153 long contextAdapter = | 81 long contextAdapter = |
154 getContextAdapter((CronetUrlRequestContext) mTestFramework.mCron
etEngine); | 82 getContextAdapter((CronetUrlRequestContext) mTestFramework.mCron
etEngine); |
155 DictionaryAddedObserver observer = | 83 DictionaryAddedObserver observer = |
156 new DictionaryAddedObserver(targetUrl, contextAdapter, false /**
Legacy Api */); | 84 new DictionaryAddedObserver(targetUrl, contextAdapter, false /**
Legacy Api */); |
157 | 85 |
158 // Make a request to /sdch which advertises the dictionary. | 86 // Make a request to /sdch which advertises the dictionary. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 169 } |
242 | 170 |
243 public void waitForDictionaryAdded() { | 171 public void waitForDictionaryAdded() { |
244 if (!mDictionaryAlreadyPresent) { | 172 if (!mDictionaryAlreadyPresent) { |
245 mBlock.block(); | 173 mBlock.block(); |
246 mBlock.close(); | 174 mBlock.close(); |
247 } | 175 } |
248 } | 176 } |
249 } | 177 } |
250 | 178 |
251 @SuppressWarnings("deprecation") | |
252 private long getContextAdapter(ChromiumUrlRequestFactory factory) { | |
253 return factory.getRequestContext().getUrlRequestContextAdapter(); | |
254 } | |
255 | |
256 private long getContextAdapter(CronetUrlRequestContext requestContext) { | 179 private long getContextAdapter(CronetUrlRequestContext requestContext) { |
257 return requestContext.getUrlRequestContextAdapter(); | 180 return requestContext.getUrlRequestContextAdapter(); |
258 } | 181 } |
259 | 182 |
260 @SuppressWarnings("deprecation") | |
261 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyApi( | |
262 HttpUrlRequestFactory factory, String url) throws Exception { | |
263 Map<String, String> headers = new HashMap<String, String>(); | |
264 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
265 HttpUrlRequest request = factory.createRequest( | |
266 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | |
267 request.start(); | |
268 listener.blockForComplete(); | |
269 return listener; | |
270 } | |
271 | |
272 private TestUrlRequestCallback startAndWaitForComplete(CronetEngine cronetEn
gine, String url) | 183 private TestUrlRequestCallback startAndWaitForComplete(CronetEngine cronetEn
gine, String url) |
273 throws Exception { | 184 throws Exception { |
274 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 185 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
275 UrlRequest.Builder builder = | 186 UrlRequest.Builder builder = |
276 new UrlRequest.Builder(url, callback, callback.getExecutor(), cr
onetEngine); | 187 cronetEngine.newUrlRequestBuilder(url, callback, callback.getExe
cutor()); |
277 builder.build().start(); | 188 builder.build().start(); |
278 callback.blockForDone(); | 189 callback.blockForDone(); |
279 return callback; | 190 return callback; |
280 } | 191 } |
281 | 192 |
282 // Returns whether a file contains a particular string. | 193 // Returns whether a file contains a particular string. |
283 private boolean fileContainsString(String filename, String content) throws I
OException { | 194 private boolean fileContainsString(String filename, String content) throws I
OException { |
284 BufferedReader reader = new BufferedReader(new FileReader( | 195 BufferedReader reader = new BufferedReader(new FileReader( |
285 CronetTestFramework.getTestStorage(getContext()) + "/prefs/" + f
ilename)); | 196 CronetTestFramework.getTestStorage(getContext()) + "/prefs/" + f
ilename)); |
286 String line; | 197 String line; |
287 while ((line = reader.readLine()) != null) { | 198 while ((line = reader.readLine()) != null) { |
288 if (line.contains(content)) { | 199 if (line.contains(content)) { |
289 reader.close(); | 200 reader.close(); |
290 return true; | 201 return true; |
291 } | 202 } |
292 } | 203 } |
293 reader.close(); | 204 reader.close(); |
294 return false; | 205 return false; |
295 } | 206 } |
296 } | 207 } |
OLD | NEW |