OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net; |
| 6 |
| 7 import android.test.suitebuilder.annotation.SmallTest; |
| 8 |
| 9 import org.chromium.base.test.util.Feature; |
| 10 |
| 11 import java.util.Arrays; |
| 12 import java.util.HashMap; |
| 13 import java.util.Map; |
| 14 |
| 15 /** |
| 16 * Tests Sdch support. |
| 17 */ |
| 18 public class SdchTest extends CronetTestBase { |
| 19 private CronetTestActivity mActivity; |
| 20 |
| 21 private void setUp(boolean enableSdch) { |
| 22 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| 23 config.enableSDCH(enableSdch); |
| 24 config.setLibraryName("cronet_tests"); |
| 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 |
| 30 // Registers custom DNS mapping for legacy ChromiumUrlRequestFactory. |
| 31 ChromiumUrlRequestFactory factory = (ChromiumUrlRequestFactory) mActivit
y.mRequestFactory; |
| 32 long legacyAdapter = factory.getRequestContext().getUrlRequestContextAda
pterForTesting(); |
| 33 assertTrue(legacyAdapter != 0); |
| 34 NativeTestServer.registerHostResolverProc(legacyAdapter, true); |
| 35 |
| 36 // Registers custom DNS mapping for CronetUrlRequestContext. |
| 37 CronetUrlRequestContext requestContext = |
| 38 (CronetUrlRequestContext) mActivity.mUrlRequestContext; |
| 39 long adapter = requestContext.getUrlRequestContextAdapter(); |
| 40 assertTrue(adapter != 0); |
| 41 NativeTestServer.registerHostResolverProc(adapter, false); |
| 42 |
| 43 // Starts NativeTestServer. |
| 44 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g
etTargetContext())); |
| 45 } |
| 46 |
| 47 @Override |
| 48 protected void tearDown() throws Exception { |
| 49 NativeTestServer.shutdownNativeTestServer(); |
| 50 mActivity.stopNetLog(); |
| 51 super.tearDown(); |
| 52 } |
| 53 |
| 54 @SmallTest |
| 55 @Feature({"Cronet"}) |
| 56 public void testSdchEnabled_LegacyAPI() throws Exception { |
| 57 setUp(true); |
| 58 // Make a request to /sdch/index which advertises the dictionary. |
| 59 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI
( |
| 60 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); |
| 61 assertEquals(200, listener1.mHttpStatusCode); |
| 62 assertEquals("This is an index page.\n", listener1.mResponseAsString); |
| 63 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), |
| 64 listener1.mResponseHeaders.get("Get-Dictionary")); |
| 65 |
| 66 waitForDictionaryAdded("LeQxM80O", true); |
| 67 |
| 68 // Make a request to fetch encoded response at /sdch/test. |
| 69 TestHttpUrlRequestListener listener2 = |
| 70 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL()
+ "/sdch/test"); |
| 71 assertEquals(200, listener2.mHttpStatusCode); |
| 72 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2
.mResponseAsString); |
| 73 } |
| 74 |
| 75 @SmallTest |
| 76 @Feature({"Cronet"}) |
| 77 public void testSdchDisabled_LegacyAPI() throws Exception { |
| 78 setUp(false); |
| 79 // Make a request to /sdch/index. |
| 80 // Since Sdch is not enabled, no dictionary should be advertised. |
| 81 TestHttpUrlRequestListener listener = startAndWaitForComplete_LegacyAPI( |
| 82 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); |
| 83 assertEquals(200, listener.mHttpStatusCode); |
| 84 assertEquals("This is an index page.\n", listener.mResponseAsString); |
| 85 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary")); |
| 86 } |
| 87 |
| 88 @SmallTest |
| 89 @Feature({"Cronet"}) |
| 90 public void testDictionaryNotFound_LegacyAPI() throws Exception { |
| 91 setUp(true); |
| 92 // Make a request to /sdch/index which advertises a bad dictionary that |
| 93 // does not exist. |
| 94 TestHttpUrlRequestListener listener1 = startAndWaitForComplete_LegacyAPI
( |
| 95 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound"); |
| 96 assertEquals(200, listener1.mHttpStatusCode); |
| 97 assertEquals("This is an index page.\n", listener1.mResponseAsString); |
| 98 assertEquals(Arrays.asList("/sdch/dict/NotFound"), |
| 99 listener1.mResponseHeaders.get("Get-Dictionary")); |
| 100 |
| 101 waitForDictionaryAdded("NotFound", true); |
| 102 |
| 103 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no
t used. |
| 104 TestHttpUrlRequestListener listener2 = |
| 105 startAndWaitForComplete_LegacyAPI(NativeTestServer.getSdchURL()
+ "/sdch/test"); |
| 106 assertEquals(200, listener2.mHttpStatusCode); |
| 107 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); |
| 108 } |
| 109 |
| 110 @SmallTest |
| 111 @Feature({"Cronet"}) |
| 112 public void testSdchEnabled() throws Exception { |
| 113 setUp(true); |
| 114 // Make a request to /sdch which advertises the dictionary. |
| 115 TestUrlRequestListener listener1 = |
| 116 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i
ndex?q=LeQxM80O"); |
| 117 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); |
| 118 assertEquals("This is an index page.\n", listener1.mResponseAsString); |
| 119 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), |
| 120 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 121 |
| 122 waitForDictionaryAdded("LeQxM80O", false); |
| 123 |
| 124 // Make a request to fetch encoded response at /sdch/test. |
| 125 TestUrlRequestListener listener2 = |
| 126 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t
est"); |
| 127 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); |
| 128 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2
.mResponseAsString); |
| 129 } |
| 130 |
| 131 @SmallTest |
| 132 @Feature({"Cronet"}) |
| 133 public void testSdchDisabled() throws Exception { |
| 134 setUp(false); |
| 135 // Make a request to /sdch. |
| 136 // Since Sdch is not enabled, no dictionary should be advertised. |
| 137 TestUrlRequestListener listener = |
| 138 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i
ndex?q=LeQxM80O"); |
| 139 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 140 assertEquals("This is an index page.\n", listener.mResponseAsString); |
| 141 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti
onary")); |
| 142 } |
| 143 |
| 144 @SmallTest |
| 145 @Feature({"Cronet"}) |
| 146 public void testDictionaryNotFound() throws Exception { |
| 147 setUp(true); |
| 148 // Make a request to /sdch/index which advertises a bad dictionary that |
| 149 // does not exist. |
| 150 TestUrlRequestListener listener1 = |
| 151 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/i
ndex?q=NotFound"); |
| 152 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); |
| 153 assertEquals("This is an index page.\n", listener1.mResponseAsString); |
| 154 assertEquals(Arrays.asList("/sdch/dict/NotFound"), |
| 155 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 156 |
| 157 waitForDictionaryAdded("NotFound", false); |
| 158 |
| 159 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no
t used. |
| 160 TestUrlRequestListener listener2 = |
| 161 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t
est"); |
| 162 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); |
| 163 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); |
| 164 } |
| 165 |
| 166 /** |
| 167 * Helper method to wait for dictionary to be fetched and added to the list
of available |
| 168 * dictionaries. |
| 169 */ |
| 170 private void waitForDictionaryAdded(String dict, boolean isLegacyAPI) throws
Exception { |
| 171 // Since Http cache is enabled, making a request to the dictionary expli
citly will |
| 172 // be served from the cache. |
| 173 String url = NativeTestServer.getSdchURL() + "/sdch/dict/" + dict; |
| 174 if (isLegacyAPI) { |
| 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 } |
| 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 } |
| 198 |
| 199 private TestHttpUrlRequestListener startAndWaitForComplete_LegacyAPI(String
url) |
| 200 throws Exception { |
| 201 Map<String, String> headers = new HashMap<String, String>(); |
| 202 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 203 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( |
| 204 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 205 request.start(); |
| 206 listener.blockForComplete(); |
| 207 return listener; |
| 208 } |
| 209 |
| 210 private TestUrlRequestListener startAndWaitForComplete(String url) throws Ex
ception { |
| 211 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 212 UrlRequest request = |
| 213 mActivity.mUrlRequestContext.createRequest(url, listener, listen
er.getExecutor()); |
| 214 request.start(); |
| 215 listener.blockForDone(); |
| 216 return listener; |
| 217 } |
| 218 } |
OLD | NEW |