| 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; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 if (setting == Sdch.ENABLED) { | 41 if (setting == Sdch.ENABLED) { |
| 42 commandLineArgs.add(CronetTestActivity.SDCH_KEY); | 42 commandLineArgs.add(CronetTestActivity.SDCH_KEY); |
| 43 commandLineArgs.add(CronetTestActivity.SDCH_ENABLE); | 43 commandLineArgs.add(CronetTestActivity.SDCH_ENABLE); |
| 44 } | 44 } |
| 45 | 45 |
| 46 String[] args = new String[commandLineArgs.size()]; | 46 String[] args = new String[commandLineArgs.size()]; |
| 47 mActivity = | 47 mActivity = |
| 48 launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineAr
gs.toArray(args)); | 48 launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineAr
gs.toArray(args)); |
| 49 long urlRequestContextAdapter = (api == Api.LEGACY) | 49 long urlRequestContextAdapter = (api == Api.LEGACY) |
| 50 ? getContextAdapter((ChromiumUrlRequestFactory) mActivity.mReque
stFactory) | 50 ? getContextAdapter((ChromiumUrlRequestFactory) mActivity.mReque
stFactory) |
| 51 : getContextAdapter((CronetUrlRequestContext) mActivity.mUrlRequ
estContext); | 51 : getContextAdapter((CronetUrlRequestContext) mActivity.mCronetE
ngine); |
| 52 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, api
== Api.LEGACY); | 52 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, api
== Api.LEGACY); |
| 53 // Start NativeTestServer. | 53 // Start NativeTestServer. |
| 54 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g
etTargetContext())); | 54 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g
etTargetContext())); |
| 55 } | 55 } |
| 56 | 56 |
| 57 @Override | 57 @Override |
| 58 protected void tearDown() throws Exception { | 58 protected void tearDown() throws Exception { |
| 59 NativeTestServer.shutdownNativeTestServer(); | 59 NativeTestServer.shutdownNativeTestServer(); |
| 60 super.tearDown(); | 60 super.tearDown(); |
| 61 } | 61 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 mActivity.mRequestFactory, NativeTestServer.getSdchURL() + "/sdc
h/test"); | 121 mActivity.mRequestFactory, NativeTestServer.getSdchURL() + "/sdc
h/test"); |
| 122 assertEquals(200, listener2.mHttpStatusCode); | 122 assertEquals(200, listener2.mHttpStatusCode); |
| 123 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); | 123 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); |
| 124 } | 124 } |
| 125 | 125 |
| 126 @SmallTest | 126 @SmallTest |
| 127 @Feature({"Cronet"}) | 127 @Feature({"Cronet"}) |
| 128 public void testSdchEnabled() throws Exception { | 128 public void testSdchEnabled() throws Exception { |
| 129 setUp(Sdch.ENABLED, Api.ASYNC); | 129 setUp(Sdch.ENABLED, Api.ASYNC); |
| 130 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; | 130 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; |
| 131 long contextAdapter = | 131 long contextAdapter = getContextAdapter((CronetUrlRequestContext) mActiv
ity.mCronetEngine); |
| 132 getContextAdapter((CronetUrlRequestContext) mActivity.mUrlReques
tContext); | |
| 133 DictionaryAddedObserver observer = | 132 DictionaryAddedObserver observer = |
| 134 new DictionaryAddedObserver(targetUrl, contextAdapter, false /**
Legacy Api */); | 133 new DictionaryAddedObserver(targetUrl, contextAdapter, false /**
Legacy Api */); |
| 135 | 134 |
| 136 // Make a request to /sdch which advertises the dictionary. | 135 // Make a request to /sdch which advertises the dictionary. |
| 137 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr
lRequestContext, | 136 TestUrlRequestListener listener1 = startAndWaitForComplete( |
| 138 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); | 137 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/
index?q=LeQxM80O"); |
| 139 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | 138 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); |
| 140 assertEquals("This is an index page.\n", listener1.mResponseAsString); | 139 assertEquals("This is an index page.\n", listener1.mResponseAsString); |
| 141 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), | 140 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), |
| 142 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | 141 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 143 | 142 |
| 144 observer.waitForDictionaryAdded(); | 143 observer.waitForDictionaryAdded(); |
| 145 | 144 |
| 146 // Make a request to fetch encoded response at /sdch/test. | 145 // Make a request to fetch encoded response at /sdch/test. |
| 147 TestUrlRequestListener listener2 = | 146 TestUrlRequestListener listener2 = |
| 148 startAndWaitForComplete(mActivity.mUrlRequestContext, targetUrl)
; | 147 startAndWaitForComplete(mActivity.mCronetEngine, targetUrl); |
| 149 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); | 148 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); |
| 150 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2
.mResponseAsString); | 149 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2
.mResponseAsString); |
| 151 | 150 |
| 152 // Wait for a bit until SimpleCache finished closing entries before | 151 // Wait for a bit until SimpleCache finished closing entries before |
| 153 // calling shutdown on the UrlRequestContext. | 152 // calling shutdown on the CronetEngine. |
| 154 // TODO(xunjieli): Remove once crbug.com/486120 is fixed. | 153 // TODO(xunjieli): Remove once crbug.com/486120 is fixed. |
| 155 Thread.sleep(5000); | 154 Thread.sleep(5000); |
| 156 mActivity.mUrlRequestContext.shutdown(); | 155 mActivity.mCronetEngine.shutdown(); |
| 157 | 156 |
| 158 // Shutting down the context will make JsonPrefStore to flush pending | 157 // Shutting down the context will make JsonPrefStore to flush pending |
| 159 // writes to disk. | 158 // writes to disk. |
| 160 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; | 159 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; |
| 161 assertTrue(fileContainsString("local_prefs.json", dictUrl)); | 160 assertTrue(fileContainsString("local_prefs.json", dictUrl)); |
| 162 | 161 |
| 163 // Test persistence. | 162 // Test persistence. |
| 164 CronetUrlRequestContext newContext = new CronetUrlRequestContext( | 163 CronetUrlRequestContext newContext = new CronetUrlRequestContext( |
| 165 getInstrumentation().getTargetContext(), mActivity.getContextCon
fig()); | 164 getInstrumentation().getTargetContext(), mActivity.getCronetEngi
neBuilder()); |
| 166 | 165 |
| 167 long newContextAdapter = getContextAdapter(newContext); | 166 long newContextAdapter = getContextAdapter(newContext); |
| 168 NativeTestServer.registerHostResolverProc(newContextAdapter, false); | 167 NativeTestServer.registerHostResolverProc(newContextAdapter, false); |
| 169 DictionaryAddedObserver newObserver = | 168 DictionaryAddedObserver newObserver = |
| 170 new DictionaryAddedObserver(targetUrl, newContextAdapter, false
/** Legacy Api */); | 169 new DictionaryAddedObserver(targetUrl, newContextAdapter, false
/** Legacy Api */); |
| 171 newObserver.waitForDictionaryAdded(); | 170 newObserver.waitForDictionaryAdded(); |
| 172 | 171 |
| 173 // Make a request to fetch encoded response at /sdch/test. | 172 // Make a request to fetch encoded response at /sdch/test. |
| 174 TestUrlRequestListener listener3 = startAndWaitForComplete(newContext, t
argetUrl); | 173 TestUrlRequestListener listener3 = startAndWaitForComplete(newContext, t
argetUrl); |
| 175 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode()); | 174 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode()); |
| 176 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3
.mResponseAsString); | 175 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3
.mResponseAsString); |
| 177 } | 176 } |
| 178 | 177 |
| 179 @SmallTest | 178 @SmallTest |
| 180 @Feature({"Cronet"}) | 179 @Feature({"Cronet"}) |
| 181 public void testSdchDisabled() throws Exception { | 180 public void testSdchDisabled() throws Exception { |
| 182 setUp(Sdch.DISABLED, Api.ASYNC); | 181 setUp(Sdch.DISABLED, Api.ASYNC); |
| 183 // Make a request to /sdch. | 182 // Make a request to /sdch. |
| 184 // Since Sdch is not enabled, no dictionary should be advertised. | 183 // Since Sdch is not enabled, no dictionary should be advertised. |
| 185 TestUrlRequestListener listener = startAndWaitForComplete(mActivity.mUrl
RequestContext, | 184 TestUrlRequestListener listener = startAndWaitForComplete( |
| 186 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); | 185 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/
index?q=LeQxM80O"); |
| 187 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 186 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 188 assertEquals("This is an index page.\n", listener.mResponseAsString); | 187 assertEquals("This is an index page.\n", listener.mResponseAsString); |
| 189 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti
onary")); | 188 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti
onary")); |
| 190 } | 189 } |
| 191 | 190 |
| 192 @SmallTest | 191 @SmallTest |
| 193 @Feature({"Cronet"}) | 192 @Feature({"Cronet"}) |
| 194 public void testDictionaryNotFound() throws Exception { | 193 public void testDictionaryNotFound() throws Exception { |
| 195 setUp(Sdch.ENABLED, Api.ASYNC); | 194 setUp(Sdch.ENABLED, Api.ASYNC); |
| 196 // Make a request to /sdch/index which advertises a bad dictionary that | 195 // Make a request to /sdch/index which advertises a bad dictionary that |
| 197 // does not exist. | 196 // does not exist. |
| 198 TestUrlRequestListener listener1 = startAndWaitForComplete(mActivity.mUr
lRequestContext, | 197 TestUrlRequestListener listener1 = startAndWaitForComplete( |
| 199 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound"); | 198 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/
index?q=NotFound"); |
| 200 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | 199 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); |
| 201 assertEquals("This is an index page.\n", listener1.mResponseAsString); | 200 assertEquals("This is an index page.\n", listener1.mResponseAsString); |
| 202 assertEquals(Arrays.asList("/sdch/dict/NotFound"), | 201 assertEquals(Arrays.asList("/sdch/dict/NotFound"), |
| 203 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | 202 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 204 | 203 |
| 205 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no
t used. | 204 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no
t used. |
| 206 TestUrlRequestListener listener2 = startAndWaitForComplete( | 205 TestUrlRequestListener listener2 = startAndWaitForComplete( |
| 207 mActivity.mUrlRequestContext, NativeTestServer.getSdchURL() + "/
sdch/test"); | 206 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/
test"); |
| 208 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); | 207 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); |
| 209 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); | 208 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); |
| 210 } | 209 } |
| 211 | 210 |
| 212 private static class DictionaryAddedObserver extends SdchObserver { | 211 private static class DictionaryAddedObserver extends SdchObserver { |
| 213 ConditionVariable mBlock = new ConditionVariable(); | 212 ConditionVariable mBlock = new ConditionVariable(); |
| 214 | 213 |
| 215 public DictionaryAddedObserver(String targetUrl, long contextAdapter, bo
olean isLegacyAPI) { | 214 public DictionaryAddedObserver(String targetUrl, long contextAdapter, bo
olean isLegacyAPI) { |
| 216 super(targetUrl, contextAdapter, isLegacyAPI); | 215 super(targetUrl, contextAdapter, isLegacyAPI); |
| 217 } | 216 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 241 HttpUrlRequestFactory factory, String url) throws Exception { | 240 HttpUrlRequestFactory factory, String url) throws Exception { |
| 242 Map<String, String> headers = new HashMap<String, String>(); | 241 Map<String, String> headers = new HashMap<String, String>(); |
| 243 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 242 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 244 HttpUrlRequest request = factory.createRequest( | 243 HttpUrlRequest request = factory.createRequest( |
| 245 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 244 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 246 request.start(); | 245 request.start(); |
| 247 listener.blockForComplete(); | 246 listener.blockForComplete(); |
| 248 return listener; | 247 return listener; |
| 249 } | 248 } |
| 250 | 249 |
| 251 private TestUrlRequestListener startAndWaitForComplete( | 250 private TestUrlRequestListener startAndWaitForComplete(CronetEngine cronetEn
gine, String url) |
| 252 UrlRequestContext requestContext, String url) throws Exception { | 251 throws Exception { |
| 253 TestUrlRequestListener listener = new TestUrlRequestListener(); | 252 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 254 UrlRequest request = requestContext.createRequest(url, listener, listene
r.getExecutor()); | 253 UrlRequest.Builder builder = new UrlRequest.Builder(url, listener, liste
ner.getExecutor()); |
| 255 request.start(); | 254 cronetEngine.executeRequest(builder); |
| 256 listener.blockForDone(); | 255 listener.blockForDone(); |
| 257 return listener; | 256 return listener; |
| 258 } | 257 } |
| 259 | 258 |
| 260 // Returns whether a file contains a particular string. | 259 // Returns whether a file contains a particular string. |
| 261 private boolean fileContainsString(String filename, String content) throws I
OException { | 260 private boolean fileContainsString(String filename, String content) throws I
OException { |
| 262 BufferedReader reader = | 261 BufferedReader reader = |
| 263 new BufferedReader(new FileReader(mActivity.getTestStorage() + "
/" + filename)); | 262 new BufferedReader(new FileReader(mActivity.getTestStorage() + "
/" + filename)); |
| 264 String line; | 263 String line; |
| 265 while ((line = reader.readLine()) != null) { | 264 while ((line = reader.readLine()) != null) { |
| 266 if (line.contains(content)) { | 265 if (line.contains(content)) { |
| 267 reader.close(); | 266 reader.close(); |
| 268 return true; | 267 return true; |
| 269 } | 268 } |
| 270 } | 269 } |
| 271 reader.close(); | 270 reader.close(); |
| 272 return false; | 271 return false; |
| 273 } | 272 } |
| 274 } | 273 } |
| OLD | NEW |