Chromium Code Reviews| 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 String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, config.toStri ng()}; | |
| 26 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLi neArgs); | |
| 27 | |
| 28 // Registers custom DNS mapping for legacy ChromiumUrlRequestFactory. | |
| 29 ChromiumUrlRequestFactory factory = (ChromiumUrlRequestFactory) mActivit y.mRequestFactory; | |
| 30 long legacyAdapter = factory.getRequestContext().getUrlRequestContextAda pter(); | |
| 31 assertTrue(legacyAdapter != 0); | |
| 32 NativeTestServer.registerHostResolverProc(legacyAdapter, true); | |
| 33 | |
| 34 // Registers custom DNS mapping for CronetUrlRequestContext. | |
| 35 CronetUrlRequestContext requestContext = | |
| 36 (CronetUrlRequestContext) mActivity.mUrlRequestContext; | |
| 37 long adapter = requestContext.getUrlRequestContextAdapter(); | |
| 38 assertTrue(adapter != 0); | |
| 39 NativeTestServer.registerHostResolverProc(adapter, false); | |
| 40 | |
| 41 // Starts NativeTestServer. | |
| 42 assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().g etTargetContext())); | |
| 43 } | |
| 44 | |
| 45 @Override | |
| 46 protected void tearDown() { | |
| 47 NativeTestServer.shutdownNativeTestServer(); | |
| 48 } | |
| 49 | |
| 50 @SmallTest | |
| 51 @Feature({"Cronet"}) | |
| 52 public void testSdchEnabledLegacyAPI() throws Exception { | |
| 53 setUp(true); | |
| 54 // Make a request to /sdch which advertises the dictionary. | |
| 55 TestHttpUrlRequestListener listener1 = | |
| 56 startAndWaitForCompleteLegacyAPI(NativeTestServer.getSdchURL() + "/sdch"); | |
| 57 assertEquals(200, listener1.mHttpStatusCode); | |
| 58 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener1 .mResponseAsString); | |
| 59 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), | |
| 60 listener1.mResponseHeaders.get("Get-Dictionary")); | |
| 61 | |
| 62 // TODO(xunjieli): Rather than setting an arbitrary delay here, consider | |
| 63 // implementing a SdchObserver to watch for dictionary add events once | |
| 64 // add event is implemented in SdchObserver. | |
| 65 Thread.sleep(5000); | |
| 66 | |
| 67 // Make a request to fetch encoded response at /sdch/test. | |
| 68 TestHttpUrlRequestListener listener2 = | |
| 69 startAndWaitForCompleteLegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test"); | |
| 70 assertEquals(200, listener2.mHttpStatusCode); | |
| 71 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); | |
| 72 } | |
| 73 | |
| 74 @SmallTest | |
| 75 @Feature({"Cronet"}) | |
| 76 public void testSdchDisabledLegacyAPI() throws Exception { | |
| 77 setUp(false); | |
| 78 // Make a request to /sdch. | |
| 79 // Since Sdch is not enabled, no dictionary should be advertised. | |
| 80 TestHttpUrlRequestListener listener1 = | |
| 81 startAndWaitForCompleteLegacyAPI(NativeTestServer.getSdchURL() + "/sdch"); | |
| 82 assertEquals(200, listener1.mHttpStatusCode); | |
| 83 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener1 .mResponseAsString); | |
| 84 assertEquals(null, listener1.mResponseHeaders.get("Get-Dictionary")); | |
| 85 | |
| 86 // Make a request to fetch /sdch/test. | |
|
alexraikman
2015/04/14 18:11:33
I am not sure how this check is useful. In theory,
xunjieli
2015/04/14 18:21:51
Yea. I just wanted to make sure that when sdch is
| |
| 87 TestHttpUrlRequestListener listener2 = | |
| 88 startAndWaitForCompleteLegacyAPI(NativeTestServer.getSdchURL() + "/sdch/test"); | |
| 89 assertEquals(200, listener2.mHttpStatusCode); | |
| 90 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); | |
| 91 } | |
| 92 | |
| 93 @SmallTest | |
| 94 @Feature({"Cronet"}) | |
| 95 public void testSdchEnabled() throws Exception { | |
| 96 setUp(true); | |
| 97 // Make a request to /sdch which advertises the dictionary. | |
| 98 TestUrlRequestListener listener1 = | |
| 99 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch") ; | |
| 100 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
| 101 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener1 .mResponseAsString); | |
| 102 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), | |
| 103 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | |
| 104 | |
| 105 // TODO(xunjieli): Rather than setting an arbitrary delay here, consider | |
| 106 // implementing a SdchObserver to watch for dictionary add events once | |
| 107 // add event is implemented in SdchObserver. | |
| 108 Thread.sleep(5000); | |
| 109 | |
| 110 // Make a request to fetch encoded response at /sdch/test. | |
| 111 TestUrlRequestListener listener2 = | |
| 112 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); | |
| 113 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
| 114 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); | |
| 115 } | |
| 116 | |
| 117 @SmallTest | |
| 118 @Feature({"Cronet"}) | |
| 119 public void testSdchDisabled() throws Exception { | |
| 120 setUp(false); | |
| 121 // Make a request to /sdch. | |
| 122 // Since Sdch is not enabled, no dictionary should be advertised. | |
| 123 TestUrlRequestListener listener1 = | |
| 124 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch") ; | |
| 125 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
| 126 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener1 .mResponseAsString); | |
| 127 assertEquals(null, listener1.mResponseInfo.getAllHeaders().get("Get-Dict ionary")); | |
| 128 | |
| 129 // Make a request to fetch /sdch/test. | |
|
alexraikman
2015/04/14 18:11:33
ditto
| |
| 130 TestUrlRequestListener listener2 = | |
| 131 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); | |
| 132 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
| 133 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); | |
| 134 } | |
| 135 | |
| 136 private TestHttpUrlRequestListener startAndWaitForCompleteLegacyAPI(String u rl) | |
| 137 throws Exception { | |
| 138 Map<String, String> headers = new HashMap<String, String>(); | |
| 139 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
| 140 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( | |
| 141 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | |
| 142 request.start(); | |
| 143 listener.blockForComplete(); | |
| 144 return listener; | |
| 145 } | |
| 146 | |
| 147 private TestUrlRequestListener startAndWaitForComplete(String url) throws Ex ception { | |
| 148 TestUrlRequestListener listener = new TestUrlRequestListener(); | |
| 149 UrlRequest request = | |
| 150 mActivity.mUrlRequestContext.createRequest(url, listener, listen er.getExecutor()); | |
| 151 request.start(); | |
| 152 listener.blockForDone(); | |
| 153 return listener; | |
| 154 } | |
| 155 } | |
| OLD | NEW |