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