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); | |
Elly Fong-Jones
2015/04/16 18:43:00
it seems like this test is going to be inherently
xunjieli
2015/04/16 19:36:57
Unfortunately I can't come up with a better soluti
| |
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 | |
87 @SmallTest | |
88 @Feature({"Cronet"}) | |
89 public void testSdchEnabled() throws Exception { | |
90 setUp(true); | |
91 // Make a request to /sdch which advertises the dictionary. | |
92 TestUrlRequestListener listener1 = | |
93 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch") ; | |
94 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
95 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener1 .mResponseAsString); | |
96 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), | |
97 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | |
98 | |
99 // TODO(xunjieli): Rather than setting an arbitrary delay here, consider | |
100 // implementing a SdchObserver to watch for dictionary add events once | |
101 // add event is implemented in SdchObserver. | |
102 Thread.sleep(5000); | |
103 | |
104 // Make a request to fetch encoded response at /sdch/test. | |
105 TestUrlRequestListener listener2 = | |
106 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch/t est"); | |
107 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
108 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); | |
109 } | |
110 | |
111 @SmallTest | |
112 @Feature({"Cronet"}) | |
113 public void testSdchDisabled() throws Exception { | |
114 setUp(false); | |
115 // Make a request to /sdch. | |
116 // Since Sdch is not enabled, no dictionary should be advertised. | |
117 TestUrlRequestListener listener1 = | |
118 startAndWaitForComplete(NativeTestServer.getSdchURL() + "/sdch") ; | |
119 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); | |
120 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener1 .mResponseAsString); | |
121 assertEquals(null, listener1.mResponseInfo.getAllHeaders().get("Get-Dict ionary")); | |
122 } | |
123 | |
124 private TestHttpUrlRequestListener startAndWaitForCompleteLegacyAPI(String u rl) | |
125 throws Exception { | |
126 Map<String, String> headers = new HashMap<String, String>(); | |
127 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
128 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( | |
129 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | |
130 request.start(); | |
131 listener.blockForComplete(); | |
132 return listener; | |
133 } | |
134 | |
135 private TestUrlRequestListener startAndWaitForComplete(String url) throws Ex ception { | |
136 TestUrlRequestListener listener = new TestUrlRequestListener(); | |
137 UrlRequest request = | |
138 mActivity.mUrlRequestContext.createRequest(url, listener, listen er.getExecutor()); | |
139 request.start(); | |
140 listener.blockForDone(); | |
141 return listener; | |
142 } | |
143 } | |
OLD | NEW |