Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/SdchTest.java

Issue 1417973002: [Cronet] Switch from InstrumentationTest to junit (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address Helen's comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 11
12 import java.io.BufferedReader; 12 import java.io.BufferedReader;
13 import java.io.FileReader; 13 import java.io.FileReader;
14 import java.io.IOException; 14 import java.io.IOException;
15 import java.util.ArrayList; 15 import java.util.ArrayList;
16 import java.util.Arrays; 16 import java.util.Arrays;
17 import java.util.HashMap; 17 import java.util.HashMap;
18 import java.util.List; 18 import java.util.List;
19 import java.util.Map; 19 import java.util.Map;
20 20
21 /** 21 /**
22 * Tests Sdch support. 22 * Tests Sdch support.
23 */ 23 */
24 public class SdchTest extends CronetTestBase { 24 public class SdchTest extends CronetTestBase {
25 private CronetTestActivity mActivity; 25 private CronetTestFramework mTestFramework;
26 26
27 private enum Sdch { 27 private enum Sdch {
28 ENABLED, 28 ENABLED,
29 DISABLED, 29 DISABLED,
30 } 30 }
31 31
32 private enum Api { 32 private enum Api {
33 LEGACY, 33 LEGACY,
34 ASYNC, 34 ASYNC,
35 } 35 }
36 36
37 private void setUp(Sdch setting, Api api) { 37 private void setUp(Sdch setting, Api api) {
38 List<String> commandLineArgs = new ArrayList<String>(); 38 List<String> commandLineArgs = new ArrayList<String>();
39 commandLineArgs.add(CronetTestActivity.CACHE_KEY); 39 commandLineArgs.add(CronetTestFramework.CACHE_KEY);
40 commandLineArgs.add(CronetTestActivity.CACHE_DISK); 40 commandLineArgs.add(CronetTestFramework.CACHE_DISK);
41 if (setting == Sdch.ENABLED) { 41 if (setting == Sdch.ENABLED) {
42 commandLineArgs.add(CronetTestActivity.SDCH_KEY); 42 commandLineArgs.add(CronetTestFramework.SDCH_KEY);
43 commandLineArgs.add(CronetTestActivity.SDCH_ENABLE); 43 commandLineArgs.add(CronetTestFramework.SDCH_ENABLE);
44 } 44 }
45 45
46 String[] args = new String[commandLineArgs.size()]; 46 String[] args = new String[commandLineArgs.size()];
47 mActivity = 47 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(
48 launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineAr gs.toArray(args)); 48 null, commandLineArgs.toArray(args));
49 long urlRequestContextAdapter = (api == Api.LEGACY) 49 long urlRequestContextAdapter = (api == Api.LEGACY)
50 ? getContextAdapter((ChromiumUrlRequestFactory) mActivity.mReque stFactory) 50 ? getContextAdapter((ChromiumUrlRequestFactory) mTestFramework.m RequestFactory)
51 : getContextAdapter((CronetUrlRequestContext) mActivity.mCronetE ngine); 51 : getContextAdapter((CronetUrlRequestContext) mTestFramework.mCr onetEngine);
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(getContext()));
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 }
62 62
63 @SmallTest 63 @SmallTest
64 @Feature({"Cronet"}) 64 @Feature({"Cronet"})
65 public void testSdchEnabled_LegacyApi() throws Exception { 65 public void testSdchEnabled_LegacyApi() throws Exception {
66 setUp(Sdch.ENABLED, Api.LEGACY); 66 setUp(Sdch.ENABLED, Api.LEGACY);
67 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; 67 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test";
68 long contextAdapter = 68 long contextAdapter =
69 getContextAdapter((ChromiumUrlRequestFactory) mActivity.mRequest Factory); 69 getContextAdapter((ChromiumUrlRequestFactory) mTestFramework.mRe questFactory);
70 DictionaryAddedObserver observer = 70 DictionaryAddedObserver observer =
71 new DictionaryAddedObserver(targetUrl, contextAdapter, true /** Legacy Api */); 71 new DictionaryAddedObserver(targetUrl, contextAdapter, true /** Legacy Api */);
72 72
73 // Make a request to /sdch/index which advertises the dictionary. 73 // Make a request to /sdch/index which advertises the dictionary.
74 TestHttpUrlRequestListener listener1 = 74 TestHttpUrlRequestListener listener1 =
75 startAndWaitForComplete_LegacyApi(mActivity.mRequestFactory, 75 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory ,
76 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O" ); 76 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O" );
77 assertEquals(200, listener1.mHttpStatusCode); 77 assertEquals(200, listener1.mHttpStatusCode);
78 assertEquals("This is an index page.\n", listener1.mResponseAsString); 78 assertEquals("This is an index page.\n", listener1.mResponseAsString);
79 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 79 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
80 listener1.mResponseHeaders.get("Get-Dictionary")); 80 listener1.mResponseHeaders.get("Get-Dictionary"));
81 81
82 observer.waitForDictionaryAdded(); 82 observer.waitForDictionaryAdded();
83 83
84 // Make a request to fetch encoded response at /sdch/test. 84 // Make a request to fetch encoded response at /sdch/test.
85 TestHttpUrlRequestListener listener2 = 85 TestHttpUrlRequestListener listener2 =
86 startAndWaitForComplete_LegacyApi(mActivity.mRequestFactory, tar getUrl); 86 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory , targetUrl);
87 assertEquals(200, listener2.mHttpStatusCode); 87 assertEquals(200, listener2.mHttpStatusCode);
88 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 88 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
89 } 89 }
90 90
91 @SmallTest 91 @SmallTest
92 @Feature({"Cronet"}) 92 @Feature({"Cronet"})
93 public void testSdchDisabled_LegacyApi() throws Exception { 93 public void testSdchDisabled_LegacyApi() throws Exception {
94 setUp(Sdch.DISABLED, Api.LEGACY); 94 setUp(Sdch.DISABLED, Api.LEGACY);
95 // Make a request to /sdch/index. 95 // Make a request to /sdch/index.
96 // Since Sdch is not enabled, no dictionary should be advertised. 96 // Since Sdch is not enabled, no dictionary should be advertised.
97 TestHttpUrlRequestListener listener = 97 TestHttpUrlRequestListener listener =
98 startAndWaitForComplete_LegacyApi(mActivity.mRequestFactory, 98 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory ,
99 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O" ); 99 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O" );
100 assertEquals(200, listener.mHttpStatusCode); 100 assertEquals(200, listener.mHttpStatusCode);
101 assertEquals("This is an index page.\n", listener.mResponseAsString); 101 assertEquals("This is an index page.\n", listener.mResponseAsString);
102 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary")); 102 assertEquals(null, listener.mResponseHeaders.get("Get-Dictionary"));
103 } 103 }
104 104
105 @SmallTest 105 @SmallTest
106 @Feature({"Cronet"}) 106 @Feature({"Cronet"})
107 public void testDictionaryNotFound_LegacyApi() throws Exception { 107 public void testDictionaryNotFound_LegacyApi() throws Exception {
108 setUp(Sdch.ENABLED, Api.LEGACY); 108 setUp(Sdch.ENABLED, Api.LEGACY);
109 // Make a request to /sdch/index which advertises a bad dictionary that 109 // Make a request to /sdch/index which advertises a bad dictionary that
110 // does not exist. 110 // does not exist.
111 TestHttpUrlRequestListener listener1 = 111 TestHttpUrlRequestListener listener1 =
112 startAndWaitForComplete_LegacyApi(mActivity.mRequestFactory, 112 startAndWaitForComplete_LegacyApi(mTestFramework.mRequestFactory ,
113 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound" ); 113 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound" );
114 assertEquals(200, listener1.mHttpStatusCode); 114 assertEquals(200, listener1.mHttpStatusCode);
115 assertEquals("This is an index page.\n", listener1.mResponseAsString); 115 assertEquals("This is an index page.\n", listener1.mResponseAsString);
116 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 116 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
117 listener1.mResponseHeaders.get("Get-Dictionary")); 117 listener1.mResponseHeaders.get("Get-Dictionary"));
118 118
119 // Make a request to fetch /sdch/test, and make sure request succeeds. 119 // Make a request to fetch /sdch/test, and make sure request succeeds.
120 TestHttpUrlRequestListener listener2 = startAndWaitForComplete_LegacyApi ( 120 TestHttpUrlRequestListener listener2 = startAndWaitForComplete_LegacyApi (
121 mActivity.mRequestFactory, NativeTestServer.getSdchURL() + "/sdc h/test"); 121 mTestFramework.mRequestFactory, NativeTestServer.getSdchURL() + "/sdch/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 = getContextAdapter((CronetUrlRequestContext) mActiv ity.mCronetEngine); 131 long contextAdapter =
132 getContextAdapter((CronetUrlRequestContext) mTestFramework.mCron etEngine);
132 DictionaryAddedObserver observer = 133 DictionaryAddedObserver observer =
133 new DictionaryAddedObserver(targetUrl, contextAdapter, false /** Legacy Api */); 134 new DictionaryAddedObserver(targetUrl, contextAdapter, false /** Legacy Api */);
134 135
135 // Make a request to /sdch which advertises the dictionary. 136 // Make a request to /sdch which advertises the dictionary.
136 TestUrlRequestListener listener1 = startAndWaitForComplete( 137 TestUrlRequestListener listener1 = startAndWaitForComplete(mTestFramewor k.mCronetEngine,
137 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/ index?q=LeQxM80O"); 138 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
138 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 139 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
139 assertEquals("This is an index page.\n", listener1.mResponseAsString); 140 assertEquals("This is an index page.\n", listener1.mResponseAsString);
140 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), 141 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"),
141 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 142 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
142 143
143 observer.waitForDictionaryAdded(); 144 observer.waitForDictionaryAdded();
144 145
145 // Make a request to fetch encoded response at /sdch/test. 146 // Make a request to fetch encoded response at /sdch/test.
146 TestUrlRequestListener listener2 = 147 TestUrlRequestListener listener2 =
147 startAndWaitForComplete(mActivity.mCronetEngine, targetUrl); 148 startAndWaitForComplete(mTestFramework.mCronetEngine, targetUrl) ;
148 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); 149 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
149 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString); 150 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener2 .mResponseAsString);
150 151
151 // Wait for a bit until SimpleCache finished closing entries before 152 // Wait for a bit until SimpleCache finished closing entries before
152 // calling shutdown on the CronetEngine. 153 // calling shutdown on the CronetEngine.
153 // TODO(xunjieli): Remove once crbug.com/486120 is fixed. 154 // TODO(xunjieli): Remove once crbug.com/486120 is fixed.
154 Thread.sleep(5000); 155 Thread.sleep(5000);
155 mActivity.mCronetEngine.shutdown(); 156 mTestFramework.mCronetEngine.shutdown();
156 157
157 // Shutting down the context will make JsonPrefStore to flush pending 158 // Shutting down the context will make JsonPrefStore to flush pending
158 // writes to disk. 159 // writes to disk.
159 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; 160 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O";
160 assertTrue(fileContainsString("local_prefs.json", dictUrl)); 161 assertTrue(fileContainsString("local_prefs.json", dictUrl));
161 162
162 // Test persistence. 163 // Test persistence.
163 CronetUrlRequestContext newContext = 164 CronetUrlRequestContext newContext =
164 new CronetUrlRequestContext(mActivity.getCronetEngineBuilder()); 165 new CronetUrlRequestContext(mTestFramework.getCronetEngineBuilde r());
165 166
166 long newContextAdapter = getContextAdapter(newContext); 167 long newContextAdapter = getContextAdapter(newContext);
167 NativeTestServer.registerHostResolverProc(newContextAdapter, false); 168 NativeTestServer.registerHostResolverProc(newContextAdapter, false);
168 DictionaryAddedObserver newObserver = 169 DictionaryAddedObserver newObserver =
169 new DictionaryAddedObserver(targetUrl, newContextAdapter, false /** Legacy Api */); 170 new DictionaryAddedObserver(targetUrl, newContextAdapter, false /** Legacy Api */);
170 newObserver.waitForDictionaryAdded(); 171 newObserver.waitForDictionaryAdded();
171 172
172 // Make a request to fetch encoded response at /sdch/test. 173 // Make a request to fetch encoded response at /sdch/test.
173 TestUrlRequestListener listener3 = startAndWaitForComplete(newContext, t argetUrl); 174 TestUrlRequestListener listener3 = startAndWaitForComplete(newContext, t argetUrl);
174 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode()); 175 assertEquals(200, listener3.mResponseInfo.getHttpStatusCode());
175 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3 .mResponseAsString); 176 assertEquals("The quick brown fox jumps over the lazy dog.\n", listener3 .mResponseAsString);
176 } 177 }
177 178
178 @SmallTest 179 @SmallTest
179 @Feature({"Cronet"}) 180 @Feature({"Cronet"})
180 public void testSdchDisabled() throws Exception { 181 public void testSdchDisabled() throws Exception {
181 setUp(Sdch.DISABLED, Api.ASYNC); 182 setUp(Sdch.DISABLED, Api.ASYNC);
182 // Make a request to /sdch. 183 // Make a request to /sdch.
183 // Since Sdch is not enabled, no dictionary should be advertised. 184 // Since Sdch is not enabled, no dictionary should be advertised.
184 TestUrlRequestListener listener = startAndWaitForComplete( 185 TestUrlRequestListener listener = startAndWaitForComplete(mTestFramework .mCronetEngine,
185 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/ index?q=LeQxM80O"); 186 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O");
186 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 187 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
187 assertEquals("This is an index page.\n", listener.mResponseAsString); 188 assertEquals("This is an index page.\n", listener.mResponseAsString);
188 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary")); 189 assertEquals(null, listener.mResponseInfo.getAllHeaders().get("Get-Dicti onary"));
189 } 190 }
190 191
191 @SmallTest 192 @SmallTest
192 @Feature({"Cronet"}) 193 @Feature({"Cronet"})
193 public void testDictionaryNotFound() throws Exception { 194 public void testDictionaryNotFound() throws Exception {
194 setUp(Sdch.ENABLED, Api.ASYNC); 195 setUp(Sdch.ENABLED, Api.ASYNC);
195 // Make a request to /sdch/index which advertises a bad dictionary that 196 // Make a request to /sdch/index which advertises a bad dictionary that
196 // does not exist. 197 // does not exist.
197 TestUrlRequestListener listener1 = startAndWaitForComplete( 198 TestUrlRequestListener listener1 = startAndWaitForComplete(mTestFramewor k.mCronetEngine,
198 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/ index?q=NotFound"); 199 NativeTestServer.getSdchURL() + "/sdch/index?q=NotFound");
199 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode()); 200 assertEquals(200, listener1.mResponseInfo.getHttpStatusCode());
200 assertEquals("This is an index page.\n", listener1.mResponseAsString); 201 assertEquals("This is an index page.\n", listener1.mResponseAsString);
201 assertEquals(Arrays.asList("/sdch/dict/NotFound"), 202 assertEquals(Arrays.asList("/sdch/dict/NotFound"),
202 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); 203 listener1.mResponseInfo.getAllHeaders().get("Get-Dictionary"));
203 204
204 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used. 205 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no t used.
205 TestUrlRequestListener listener2 = startAndWaitForComplete( 206 TestUrlRequestListener listener2 = startAndWaitForComplete(
206 mActivity.mCronetEngine, NativeTestServer.getSdchURL() + "/sdch/ test"); 207 mTestFramework.mCronetEngine, NativeTestServer.getSdchURL() + "/ sdch/test");
207 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); 208 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode());
208 assertEquals("Sdch is not used.\n", listener2.mResponseAsString); 209 assertEquals("Sdch is not used.\n", listener2.mResponseAsString);
209 } 210 }
210 211
211 private static class DictionaryAddedObserver extends SdchObserver { 212 private static class DictionaryAddedObserver extends SdchObserver {
212 ConditionVariable mBlock = new ConditionVariable(); 213 ConditionVariable mBlock = new ConditionVariable();
213 214
214 public DictionaryAddedObserver(String targetUrl, long contextAdapter, bo olean isLegacyAPI) { 215 public DictionaryAddedObserver(String targetUrl, long contextAdapter, bo olean isLegacyAPI) {
215 super(targetUrl, contextAdapter, isLegacyAPI); 216 super(targetUrl, contextAdapter, isLegacyAPI);
216 } 217 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 TestUrlRequestListener listener = new TestUrlRequestListener(); 253 TestUrlRequestListener listener = new TestUrlRequestListener();
253 UrlRequest.Builder builder = 254 UrlRequest.Builder builder =
254 new UrlRequest.Builder(url, listener, listener.getExecutor(), cr onetEngine); 255 new UrlRequest.Builder(url, listener, listener.getExecutor(), cr onetEngine);
255 builder.build().start(); 256 builder.build().start();
256 listener.blockForDone(); 257 listener.blockForDone();
257 return listener; 258 return listener;
258 } 259 }
259 260
260 // Returns whether a file contains a particular string. 261 // Returns whether a file contains a particular string.
261 private boolean fileContainsString(String filename, String content) throws I OException { 262 private boolean fileContainsString(String filename, String content) throws I OException {
262 BufferedReader reader = 263 BufferedReader reader = new BufferedReader(
263 new BufferedReader(new FileReader(mActivity.getTestStorage() + " /" + filename)); 264 new FileReader(mTestFramework.getTestStorage() + "/" + filename) );
264 String line; 265 String line;
265 while ((line = reader.readLine()) != null) { 266 while ((line = reader.readLine()) != null) {
266 if (line.contains(content)) { 267 if (line.contains(content)) {
267 reader.close(); 268 reader.close();
268 return true; 269 return true;
269 } 270 }
270 } 271 }
271 reader.close(); 272 reader.close();
272 return false; 273 return false;
273 } 274 }
274 } 275 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698