| 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; |
| 11 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; | 11 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; |
| 12 import org.json.JSONException; |
| 13 import org.json.JSONObject; |
| 12 | 14 |
| 13 import java.io.BufferedReader; | 15 import java.io.BufferedReader; |
| 14 import java.io.FileReader; | 16 import java.io.FileReader; |
| 15 import java.io.IOException; | 17 import java.io.IOException; |
| 16 import java.util.ArrayList; | 18 import java.util.ArrayList; |
| 17 import java.util.Arrays; | 19 import java.util.Arrays; |
| 18 import java.util.HashMap; | 20 import java.util.HashMap; |
| 19 import java.util.List; | 21 import java.util.List; |
| 20 import java.util.Map; | 22 import java.util.Map; |
| 21 | 23 |
| 22 /** | 24 /** |
| 23 * Tests Sdch support. | 25 * Tests Sdch support. |
| 24 */ | 26 */ |
| 25 public class SdchTest extends CronetTestBase { | 27 public class SdchTest extends CronetTestBase { |
| 26 private CronetTestFramework mTestFramework; | 28 private CronetTestFramework mTestFramework; |
| 27 | 29 |
| 28 private enum Sdch { | 30 private enum Sdch { |
| 29 ENABLED, | 31 ENABLED, |
| 30 DISABLED, | 32 DISABLED, |
| 31 } | 33 } |
| 32 | 34 |
| 33 private enum Api { | 35 private enum Api { |
| 34 LEGACY, | 36 LEGACY, |
| 35 ASYNC, | 37 ASYNC, |
| 36 } | 38 } |
| 37 | 39 |
| 38 @SuppressWarnings("deprecation") | 40 @SuppressWarnings("deprecation") |
| 39 private void setUp(Sdch setting, Api api) { | 41 private void setUp(Sdch setting, Api api) throws JSONException { |
| 40 List<String> commandLineArgs = new ArrayList<String>(); | 42 List<String> commandLineArgs = new ArrayList<String>(); |
| 41 commandLineArgs.add(CronetTestFramework.CACHE_KEY); | 43 commandLineArgs.add(CronetTestFramework.CACHE_KEY); |
| 42 commandLineArgs.add(CronetTestFramework.CACHE_DISK); | 44 commandLineArgs.add(CronetTestFramework.CACHE_DISK); |
| 43 if (setting == Sdch.ENABLED) { | 45 if (setting == Sdch.ENABLED) { |
| 44 commandLineArgs.add(CronetTestFramework.SDCH_KEY); | 46 commandLineArgs.add(CronetTestFramework.SDCH_KEY); |
| 45 commandLineArgs.add(CronetTestFramework.SDCH_ENABLE); | 47 commandLineArgs.add(CronetTestFramework.SDCH_ENABLE); |
| 46 } | 48 } |
| 47 | 49 |
| 48 if (api == Api.LEGACY) { | 50 if (api == Api.LEGACY) { |
| 49 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); | 51 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); |
| 50 commandLineArgs.add(CronetTestFramework.LibraryInitType.LEGACY); | 52 commandLineArgs.add(CronetTestFramework.LibraryInitType.LEGACY); |
| 51 } else { | 53 } else { |
| 52 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); | 54 commandLineArgs.add(CronetTestFramework.LIBRARY_INIT_KEY); |
| 53 commandLineArgs.add(CronetTestFramework.LibraryInitType.CRONET); | 55 commandLineArgs.add(CronetTestFramework.LibraryInitType.CRONET); |
| 54 } | 56 } |
| 55 | 57 |
| 56 String[] args = new String[commandLineArgs.size()]; | 58 String[] args = new String[commandLineArgs.size()]; |
| 57 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs( | 59 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 58 null, commandLineArgs.toArray(args)); | 60 JSONObject hostResolverParams = CronetTestUtil.generateHostResolverRules
(); |
| 59 registerHostResolver(mTestFramework, api == Api.LEGACY); | 61 JSONObject experimentalOptions = |
| 62 new JSONObject().put("HostResolverRules", hostResolverParams); |
| 63 builder.setExperimentalOptions(experimentalOptions.toString()); |
| 64 mTestFramework = |
| 65 new CronetTestFramework(null, commandLineArgs.toArray(args), get
Context(), builder); |
| 60 // Start NativeTestServer. | 66 // Start NativeTestServer. |
| 61 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 67 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 62 } | 68 } |
| 63 | 69 |
| 64 @Override | 70 @Override |
| 65 protected void tearDown() throws Exception { | 71 protected void tearDown() throws Exception { |
| 66 NativeTestServer.shutdownNativeTestServer(); | 72 NativeTestServer.shutdownNativeTestServer(); |
| 67 super.tearDown(); | 73 super.tearDown(); |
| 68 } | 74 } |
| 69 | 75 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 callback1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | 162 callback1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 157 | 163 |
| 158 observer.waitForDictionaryAdded(); | 164 observer.waitForDictionaryAdded(); |
| 159 | 165 |
| 160 // Make a request to fetch encoded response at /sdch/test. | 166 // Make a request to fetch encoded response at /sdch/test. |
| 161 TestUrlRequestCallback callback2 = | 167 TestUrlRequestCallback callback2 = |
| 162 startAndWaitForComplete(mTestFramework.mCronetEngine, targetUrl)
; | 168 startAndWaitForComplete(mTestFramework.mCronetEngine, targetUrl)
; |
| 163 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); | 169 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); |
| 164 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback2
.mResponseAsString); | 170 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback2
.mResponseAsString); |
| 165 | 171 |
| 166 // Wait for a bit until SimpleCache finished closing entries before | |
| 167 // calling shutdown on the CronetEngine. | |
| 168 // TODO(xunjieli): Remove once crbug.com/486120 is fixed. | |
| 169 Thread.sleep(5000); | |
| 170 mTestFramework.mCronetEngine.shutdown(); | 172 mTestFramework.mCronetEngine.shutdown(); |
| 171 | 173 |
| 172 // Shutting down the context will make JsonPrefStore to flush pending | 174 // Shutting down the context will make JsonPrefStore to flush pending |
| 173 // writes to disk. | 175 // writes to disk. |
| 174 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; | 176 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; |
| 175 assertTrue(fileContainsString("local_prefs.json", dictUrl)); | 177 assertTrue(fileContainsString("local_prefs.json", dictUrl)); |
| 176 | 178 |
| 177 // Test persistence. | 179 // Test persistence. |
| 178 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder( | 180 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder( |
| 179 null, mTestFramework.getCronetEngineBuilder()); | 181 null, mTestFramework.getCronetEngineBuilder()); |
| 180 CronetUrlRequestContext newContext = (CronetUrlRequestContext) mTestFram
ework.mCronetEngine; | 182 CronetUrlRequestContext newContext = (CronetUrlRequestContext) mTestFram
ework.mCronetEngine; |
| 181 long newContextAdapter = getContextAdapter(newContext); | 183 long newContextAdapter = getContextAdapter(newContext); |
| 182 registerHostResolver(mTestFramework); | |
| 183 DictionaryAddedObserver newObserver = | 184 DictionaryAddedObserver newObserver = |
| 184 new DictionaryAddedObserver(targetUrl, newContextAdapter, false
/** Legacy Api */); | 185 new DictionaryAddedObserver(targetUrl, newContextAdapter, false
/** Legacy Api */); |
| 185 newObserver.waitForDictionaryAdded(); | 186 newObserver.waitForDictionaryAdded(); |
| 186 | 187 |
| 187 // Make a request to fetch encoded response at /sdch/test. | 188 // Make a request to fetch encoded response at /sdch/test. |
| 188 TestUrlRequestCallback callback3 = startAndWaitForComplete(newContext, t
argetUrl); | 189 TestUrlRequestCallback callback3 = startAndWaitForComplete(newContext, t
argetUrl); |
| 189 assertEquals(200, callback3.mResponseInfo.getHttpStatusCode()); | 190 assertEquals(200, callback3.mResponseInfo.getHttpStatusCode()); |
| 190 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback3
.mResponseAsString); | 191 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback3
.mResponseAsString); |
| 191 } | 192 } |
| 192 | 193 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 while ((line = reader.readLine()) != null) { | 285 while ((line = reader.readLine()) != null) { |
| 285 if (line.contains(content)) { | 286 if (line.contains(content)) { |
| 286 reader.close(); | 287 reader.close(); |
| 287 return true; | 288 return true; |
| 288 } | 289 } |
| 289 } | 290 } |
| 290 reader.close(); | 291 reader.close(); |
| 291 return false; | 292 return false; |
| 292 } | 293 } |
| 293 } | 294 } |
| OLD | NEW |