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

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

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

Powered by Google App Engine
This is Rietveld 408576698