| 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; | |
| 8 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
| 9 | 8 |
| 10 import org.json.JSONException; | 9 import org.json.JSONException; |
| 11 import org.json.JSONObject; | 10 import org.json.JSONObject; |
| 12 | 11 |
| 13 import org.chromium.base.test.util.Feature; | 12 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.net.impl.CronetUrlRequestContext; | 13 import org.chromium.net.impl.CronetUrlRequestContext; |
| 15 | 14 |
| 16 import java.io.BufferedReader; | 15 import java.io.BufferedReader; |
| 17 import java.io.FileReader; | 16 import java.io.FileReader; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 63 } |
| 65 | 64 |
| 66 @SmallTest | 65 @SmallTest |
| 67 @Feature({"Cronet"}) | 66 @Feature({"Cronet"}) |
| 68 @OnlyRunNativeCronet | 67 @OnlyRunNativeCronet |
| 69 public void testSdchEnabled() throws Exception { | 68 public void testSdchEnabled() throws Exception { |
| 70 setUp(Sdch.ENABLED); | 69 setUp(Sdch.ENABLED); |
| 71 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; | 70 String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test"; |
| 72 long contextAdapter = | 71 long contextAdapter = |
| 73 getContextAdapter((CronetUrlRequestContext) mTestFramework.mCron
etEngine); | 72 getContextAdapter((CronetUrlRequestContext) mTestFramework.mCron
etEngine); |
| 74 DictionaryAddedObserver observer = new DictionaryAddedObserver(targetUrl
, contextAdapter); | 73 SdchObserver observer = new SdchObserver(targetUrl, contextAdapter); |
| 75 | 74 |
| 76 // Make a request to /sdch which advertises the dictionary. | 75 // Make a request to /sdch which advertises the dictionary. |
| 77 TestUrlRequestCallback callback1 = startAndWaitForComplete(mTestFramewor
k.mCronetEngine, | 76 TestUrlRequestCallback callback1 = startAndWaitForComplete(mTestFramewor
k.mCronetEngine, |
| 78 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); | 77 NativeTestServer.getSdchURL() + "/sdch/index?q=LeQxM80O"); |
| 79 assertEquals(200, callback1.mResponseInfo.getHttpStatusCode()); | 78 assertEquals(200, callback1.mResponseInfo.getHttpStatusCode()); |
| 80 assertEquals("This is an index page.\n", callback1.mResponseAsString); | 79 assertEquals("This is an index page.\n", callback1.mResponseAsString); |
| 81 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), | 80 assertEquals(Arrays.asList("/sdch/dict/LeQxM80O"), |
| 82 callback1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | 81 callback1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 83 | 82 |
| 84 observer.waitForDictionaryAdded(); | 83 observer.waitForDictionaryAdded(); |
| 85 | 84 |
| 86 // Make a request to fetch encoded response at /sdch/test. | 85 // Make a request to fetch encoded response at /sdch/test. |
| 87 TestUrlRequestCallback callback2 = | 86 TestUrlRequestCallback callback2 = |
| 88 startAndWaitForComplete(mTestFramework.mCronetEngine, targetUrl)
; | 87 startAndWaitForComplete(mTestFramework.mCronetEngine, targetUrl)
; |
| 89 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); | 88 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); |
| 90 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback2
.mResponseAsString); | 89 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback2
.mResponseAsString); |
| 91 | 90 |
| 92 mTestFramework.mCronetEngine.shutdown(); | 91 mTestFramework.mCronetEngine.shutdown(); |
| 93 | 92 |
| 94 // Shutting down the context will make JsonPrefStore to flush pending | 93 // Shutting down the context will make JsonPrefStore to flush pending |
| 95 // writes to disk. | 94 // writes to disk. |
| 96 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; | 95 String dictUrl = NativeTestServer.getSdchURL() + "/sdch/dict/LeQxM80O"; |
| 97 assertTrue(fileContainsString("local_prefs.json", dictUrl)); | 96 assertTrue(fileContainsString("local_prefs.json", dictUrl)); |
| 98 | 97 |
| 99 // Test persistence. | 98 // Test persistence. |
| 100 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder( | 99 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder( |
| 101 null, mTestFramework.getCronetEngineBuilder()); | 100 null, mTestFramework.getCronetEngineBuilder()); |
| 102 CronetUrlRequestContext newContext = (CronetUrlRequestContext) mTestFram
ework.mCronetEngine; | 101 CronetUrlRequestContext newContext = (CronetUrlRequestContext) mTestFram
ework.mCronetEngine; |
| 103 long newContextAdapter = getContextAdapter(newContext); | 102 long newContextAdapter = getContextAdapter(newContext); |
| 104 DictionaryAddedObserver newObserver = | 103 SdchObserver newObserver = new SdchObserver(targetUrl, newContextAdapter
); |
| 105 new DictionaryAddedObserver(targetUrl, newContextAdapter); | |
| 106 newObserver.waitForDictionaryAdded(); | 104 newObserver.waitForDictionaryAdded(); |
| 107 | 105 |
| 108 // Make a request to fetch encoded response at /sdch/test. | 106 // Make a request to fetch encoded response at /sdch/test. |
| 109 TestUrlRequestCallback callback3 = startAndWaitForComplete(newContext, t
argetUrl); | 107 TestUrlRequestCallback callback3 = startAndWaitForComplete(newContext, t
argetUrl); |
| 110 assertEquals(200, callback3.mResponseInfo.getHttpStatusCode()); | 108 assertEquals(200, callback3.mResponseInfo.getHttpStatusCode()); |
| 111 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback3
.mResponseAsString); | 109 assertEquals("The quick brown fox jumps over the lazy dog.\n", callback3
.mResponseAsString); |
| 112 } | 110 } |
| 113 | 111 |
| 114 @SmallTest | 112 @SmallTest |
| 115 @Feature({"Cronet"}) | 113 @Feature({"Cronet"}) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 139 assertEquals(Arrays.asList("/sdch/dict/NotFound"), | 137 assertEquals(Arrays.asList("/sdch/dict/NotFound"), |
| 140 callback1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); | 138 callback1.mResponseInfo.getAllHeaders().get("Get-Dictionary")); |
| 141 | 139 |
| 142 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no
t used. | 140 // Make a request to fetch /sdch/test, and make sure Sdch encoding is no
t used. |
| 143 TestUrlRequestCallback callback2 = startAndWaitForComplete( | 141 TestUrlRequestCallback callback2 = startAndWaitForComplete( |
| 144 mTestFramework.mCronetEngine, NativeTestServer.getSdchURL() + "/
sdch/test"); | 142 mTestFramework.mCronetEngine, NativeTestServer.getSdchURL() + "/
sdch/test"); |
| 145 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); | 143 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); |
| 146 assertEquals("Sdch is not used.\n", callback2.mResponseAsString); | 144 assertEquals("Sdch is not used.\n", callback2.mResponseAsString); |
| 147 } | 145 } |
| 148 | 146 |
| 149 private static class DictionaryAddedObserver extends SdchObserver { | |
| 150 private final ConditionVariable mBlock; | |
| 151 | |
| 152 public DictionaryAddedObserver(String targetUrl, long contextAdapter) { | |
| 153 super(targetUrl, contextAdapter); | |
| 154 mBlock = new ConditionVariable(); | |
| 155 } | |
| 156 | |
| 157 @Override | |
| 158 public void onDictionaryAdded() { | |
| 159 mBlock.open(); | |
| 160 } | |
| 161 | |
| 162 public void waitForDictionaryAdded() { | |
| 163 if (!mDictionaryAlreadyPresent) { | |
| 164 mBlock.block(); | |
| 165 mBlock.close(); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 private long getContextAdapter(CronetUrlRequestContext requestContext) { | 147 private long getContextAdapter(CronetUrlRequestContext requestContext) { |
| 171 return requestContext.getUrlRequestContextAdapter(); | 148 return requestContext.getUrlRequestContextAdapter(); |
| 172 } | 149 } |
| 173 | 150 |
| 174 private TestUrlRequestCallback startAndWaitForComplete(CronetEngine cronetEn
gine, String url) | 151 private TestUrlRequestCallback startAndWaitForComplete(CronetEngine cronetEn
gine, String url) |
| 175 throws Exception { | 152 throws Exception { |
| 176 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 153 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 177 UrlRequest.Builder builder = | 154 UrlRequest.Builder builder = |
| 178 cronetEngine.newUrlRequestBuilder(url, callback, callback.getExe
cutor()); | 155 cronetEngine.newUrlRequestBuilder(url, callback, callback.getExe
cutor()); |
| 179 builder.build().start(); | 156 builder.build().start(); |
| 180 callback.blockForDone(); | 157 callback.blockForDone(); |
| 181 return callback; | 158 return callback; |
| 182 } | 159 } |
| 183 | 160 |
| 184 // Returns whether a file contains a particular string. | 161 // Returns whether a file contains a particular string. |
| 185 private boolean fileContainsString(String filename, String content) throws I
OException { | 162 private boolean fileContainsString(String filename, String content) throws I
OException { |
| 186 BufferedReader reader = new BufferedReader(new FileReader( | 163 BufferedReader reader = new BufferedReader(new FileReader( |
| 187 CronetTestFramework.getTestStorage(getContext()) + "/prefs/" + f
ilename)); | 164 CronetTestFramework.getTestStorage(getContext()) + "/prefs/" + f
ilename)); |
| 188 String line; | 165 String line; |
| 189 while ((line = reader.readLine()) != null) { | 166 while ((line = reader.readLine()) != null) { |
| 190 if (line.contains(content)) { | 167 if (line.contains(content)) { |
| 191 reader.close(); | 168 reader.close(); |
| 192 return true; | 169 return true; |
| 193 } | 170 } |
| 194 } | 171 } |
| 195 reader.close(); | 172 reader.close(); |
| 196 return false; | 173 return false; |
| 197 } | 174 } |
| 198 } | 175 } |
| OLD | NEW |