Chromium Code Reviews| 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.test.suitebuilder.annotation.LargeTest; | |
| 7 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 8 | 9 |
| 10 import org.chromium.base.Log; | |
| 9 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
| 10 | 12 |
| 13 import java.io.File; | |
| 14 import java.io.FileInputStream; | |
| 15 import java.io.FileNotFoundException; | |
| 16 import java.io.IOException; | |
| 11 import java.util.HashMap; | 17 import java.util.HashMap; |
| 12 | 18 |
| 13 /** | 19 /** |
| 14 * Tests making requests using QUIC. | 20 * Tests making requests using QUIC. |
| 15 */ | 21 */ |
| 16 public class QuicTest extends CronetTestBase { | 22 public class QuicTest extends CronetTestBase { |
| 17 | 23 private static final String TAG = "cr.QuicTest"; |
| 18 private CronetTestActivity mActivity; | 24 private CronetTestActivity mActivity; |
| 19 | 25 |
| 20 @Override | 26 @Override |
| 21 protected void setUp() throws Exception { | 27 protected void setUp() throws Exception { |
| 22 super.setUp(); | 28 super.setUp(); |
| 23 // Load library first, since we need the Quic test server's URL. | 29 // Load library first, since we need the Quic test server's URL. |
| 24 System.loadLibrary("cronet_tests"); | 30 System.loadLibrary("cronet_tests"); |
| 25 QuicTestServer.startQuicTestServer(getInstrumentation().getTargetContext ()); | 31 QuicTestServer.startQuicTestServer(getInstrumentation().getTargetContext ()); |
| 26 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 32 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| 27 config.enableQUIC(true); | 33 config.enableQUIC(true); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 56 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen er); | 62 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen er); |
| 57 request.start(); | 63 request.start(); |
| 58 listener.blockForComplete(); | 64 listener.blockForComplete(); |
| 59 assertEquals(200, listener.mHttpStatusCode); | 65 assertEquals(200, listener.mHttpStatusCode); |
| 60 assertEquals( | 66 assertEquals( |
| 61 "This is a simple text file served by QUIC.\n", | 67 "This is a simple text file served by QUIC.\n", |
| 62 listener.mResponseAsString); | 68 listener.mResponseAsString); |
| 63 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); | 69 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); |
| 64 } | 70 } |
| 65 | 71 |
| 66 @SmallTest | 72 @LargeTest |
| 67 @Feature({"Cronet"}) | 73 @Feature({"Cronet"}) |
| 68 public void testQuicLoadUrl() throws Exception { | 74 public void testQuicLoadUrl() throws Exception { |
| 69 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; | 75 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
| 70 TestUrlRequestListener listener = new TestUrlRequestListener(); | 76 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 71 | 77 |
| 72 // Although the native stack races QUIC and SPDY for the first request, | 78 // Although the native stack races QUIC and SPDY for the first request, |
| 73 // since there is no http server running on the corresponding TCP port, | 79 // since there is no http server running on the corresponding TCP port, |
| 74 // QUIC will always succeed with a 200 (see | 80 // QUIC will always succeed with a 200 (see |
| 75 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). | 81 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
| 76 UrlRequest request = mActivity.mUrlRequestContext.createRequest( | 82 UrlRequest request = mActivity.mUrlRequestContext.createRequest( |
| 77 quicURL, listener, listener.getExecutor()); | 83 quicURL, listener, listener.getExecutor()); |
| 78 request.start(); | 84 request.start(); |
| 79 listener.blockForDone(); | 85 listener.blockForDone(); |
| 80 | 86 |
| 81 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 87 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 82 assertEquals("This is a simple text file served by QUIC.\n", listener.mR esponseAsString); | 88 assertEquals("This is a simple text file served by QUIC.\n", listener.mR esponseAsString); |
| 83 assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtoc ol()); | 89 assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtoc ol()); |
| 90 | |
| 91 // This test takes a long time, since the update will only be scheduled | |
| 92 // after kUpdatePrefsDelayMs in http_server_properties_manager.cc. | |
| 93 while (true) { | |
| 94 Log.i(TAG, "Still waiting for pref file update....."); | |
| 95 Thread.sleep(10000); | |
| 96 boolean contains = false; | |
| 97 try { | |
| 98 if (fileContainsString("local_prefs.json", "quic")) break; | |
| 99 } catch (FileNotFoundException e) { | |
| 100 // Ignored this exception since the file will only be created wh en updates are | |
| 101 // flushed to the disk. | |
| 102 } | |
| 103 } | |
| 104 assertTrue(fileContainsString("local_prefs.json", | |
| 105 QuicTestServer.getServerHost() + ":" + QuicTestServer.getServerP ort())); | |
| 106 mActivity.mUrlRequestContext.shutdown(); | |
| 107 | |
| 108 // Make another request using a new context but with no quic hints. | |
|
mmenke
2015/07/09 19:08:10
nit: QUIC
xunjieli
2015/07/09 19:37:02
Done.
| |
| 109 UrlRequestContextConfig config = new UrlRequestContextConfig(); | |
| 110 config.setStoragePath(mActivity.getTestStorage()); | |
| 111 config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 1000 * 10 24); | |
| 112 config.enableQUIC(true); | |
| 113 CronetUrlRequestContext newContext = | |
| 114 new CronetUrlRequestContext(getInstrumentation().getTargetContex t(), config); | |
| 115 TestUrlRequestListener listener2 = new TestUrlRequestListener(); | |
| 116 UrlRequest request2 = newContext.createRequest(quicURL, listener2, liste ner2.getExecutor()); | |
| 117 request2.start(); | |
| 118 listener2.blockForDone(); | |
| 119 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); | |
| 120 assertEquals("This is a simple text file served by QUIC.\n", listener2.m ResponseAsString); | |
| 121 assertEquals("quic/1+spdy/3", listener2.mResponseInfo.getNegotiatedProto col()); | |
| 122 } | |
| 123 | |
| 124 // Returns whether a file contains a particular string. | |
| 125 private boolean fileContainsString(String filename, String content) throws I OException { | |
| 126 File file = new File(mActivity.getTestStorage() + "/" + filename); | |
| 127 FileInputStream fileInputStream = new FileInputStream(file); | |
| 128 byte[] data = new byte[(int) file.length()]; | |
| 129 fileInputStream.read(data); | |
| 130 fileInputStream.close(); | |
| 131 return new String(data, "UTF-8").contains(content); | |
| 84 } | 132 } |
| 85 } | 133 } |
| OLD | NEW |