Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.content.Context; | 7 import android.content.Context; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 9 |
| 10 import org.chromium.base.PathUtils; | 10 import org.chromium.base.PathUtils; |
| 11 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
| 12 | 12 |
| 13 import java.io.File; | 13 import java.io.File; |
| 14 import java.util.HashMap; | 14 import java.util.HashMap; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Example test that just starts the cronet sample. | 17 * Example test that just starts the cronet sample. |
|
xunjieli
2015/10/22 18:41:24
optional nit: If you don't mind, maybe you could u
pauljensen
2015/10/23 11:35:50
Done.
| |
| 18 */ | 18 */ |
| 19 public class CronetUrlTest extends CronetTestBase { | 19 public class CronetUrlTest extends CronetTestBase { |
| 20 // URL used for base tests. | 20 // URL used for base tests. |
| 21 private static final String URL = "http://127.0.0.1:8000"; | 21 private static final String URL = "http://127.0.0.1:8000"; |
| 22 | 22 |
| 23 @SmallTest | 23 @SmallTest |
| 24 @Feature({"Cronet"}) | 24 @Feature({"Cronet"}) |
| 25 public void testLoadUrl() throws Exception { | 25 public void testLoadUrl() throws Exception { |
| 26 CronetTestActivity activity = launchCronetTestAppWithUrl(URL); | 26 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; |
| 27 | 27 |
| 28 // Make sure that the URL is set as expected. | 28 // Make sure that the URL is set as expected. |
| 29 assertEquals(URL, activity.getUrl()); | 29 assertEquals(URL, testFramework.getUrl()); |
| 30 assertEquals(200, activity.getHttpStatusCode()); | 30 assertEquals(200, testFramework.getHttpStatusCode()); |
| 31 } | 31 } |
| 32 | 32 |
| 33 @SmallTest | 33 @SmallTest |
| 34 @Feature({"Cronet"}) | 34 @Feature({"Cronet"}) |
| 35 public void testInvalidUrl() throws Exception { | 35 public void testInvalidUrl() throws Exception { |
| 36 CronetTestActivity activity = launchCronetTestAppWithUrl( | 36 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl("127 .0.0.1:8000"); |
| 37 "127.0.0.1:8000"); | |
| 38 | 37 |
| 39 // The load should fail. | 38 // The load should fail. |
| 40 assertEquals(0, activity.getHttpStatusCode()); | 39 assertEquals(0, testFramework.getHttpStatusCode()); |
| 41 } | 40 } |
| 42 | 41 |
| 43 @SmallTest | 42 @SmallTest |
| 44 @Feature({"Cronet"}) | 43 @Feature({"Cronet"}) |
| 45 public void testPostData() throws Exception { | 44 public void testPostData() throws Exception { |
| 46 String[] commandLineArgs = { | 45 String[] commandLineArgs = {CronetTestFramework.POST_DATA_KEY, "test"}; |
| 47 CronetTestActivity.POST_DATA_KEY, "test" }; | 46 CronetTestFramework testFramework = |
| 48 CronetTestActivity activity = | 47 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); |
| 49 launchCronetTestAppWithUrlAndCommandLineArgs(URL, | |
| 50 commandLineArgs); | |
| 51 | 48 |
| 52 // Make sure that the URL is set as expected. | 49 // Make sure that the URL is set as expected. |
| 53 assertEquals(URL, activity.getUrl()); | 50 assertEquals(URL, testFramework.getUrl()); |
| 54 assertEquals(200, activity.getHttpStatusCode()); | 51 assertEquals(200, testFramework.getHttpStatusCode()); |
| 55 } | 52 } |
| 56 | 53 |
| 57 @SmallTest | 54 @SmallTest |
| 58 @Feature({"Cronet"}) | 55 @Feature({"Cronet"}) |
| 59 public void testNetLog() throws Exception { | 56 public void testNetLog() throws Exception { |
| 60 Context context = getInstrumentation().getTargetContext(); | 57 Context context = getContext(); |
| 61 File directory = new File(PathUtils.getDataDirectory(context)); | 58 File directory = new File(PathUtils.getDataDirectory(context)); |
| 62 File file = File.createTempFile("cronet", "json", directory); | 59 File file = File.createTempFile("cronet", "json", directory); |
| 63 HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory( | 60 HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory( |
| 64 context, | 61 context, |
| 65 new UrlRequestContextConfig().setLibraryName("cronet_tests")); | 62 new UrlRequestContextConfig().setLibraryName("cronet_tests")); |
| 66 // Start NetLog immediately after the request context is created to make | 63 // Start NetLog immediately after the request context is created to make |
| 67 // sure that the call won't crash the app even when the native request | 64 // sure that the call won't crash the app even when the native request |
| 68 // context is not fully initialized. See crbug.com/470196. | 65 // context is not fully initialized. See crbug.com/470196. |
| 69 factory.startNetLogToFile(file.getPath(), false); | 66 factory.startNetLogToFile(file.getPath(), false); |
| 70 // Starts a request. | 67 // Starts a request. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 89 | 86 |
| 90 @Override | 87 @Override |
| 91 public void onResponseStarted(HttpUrlRequest request) { | 88 public void onResponseStarted(HttpUrlRequest request) { |
| 92 throw new NullPointerException(THROW_TAG); | 89 throw new NullPointerException(THROW_TAG); |
| 93 } | 90 } |
| 94 } | 91 } |
| 95 | 92 |
| 96 @SmallTest | 93 @SmallTest |
| 97 @Feature({"Cronet"}) | 94 @Feature({"Cronet"}) |
| 98 public void testCalledByNativeException() throws Exception { | 95 public void testCalledByNativeException() throws Exception { |
| 99 CronetTestActivity activity = launchCronetTestAppWithUrl(URL); | 96 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; |
| 100 | 97 |
| 101 HashMap<String, String> headers = new HashMap<String, String>(); | 98 HashMap<String, String> headers = new HashMap<String, String>(); |
| 102 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | 99 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); |
| 103 | 100 |
| 104 // Create request with bad listener to trigger an exception. | 101 // Create request with bad listener to trigger an exception. |
| 105 HttpUrlRequest request = activity.mRequestFactory.createRequest( | 102 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 106 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 103 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 107 request.start(); | 104 request.start(); |
| 108 listener.blockForComplete(); | 105 listener.blockForComplete(); |
| 109 assertTrue(request.isCanceled()); | 106 assertTrue(request.isCanceled()); |
| 110 assertNotNull(request.getException()); | 107 assertNotNull(request.getException()); |
| 111 assertEquals(BadHttpUrlRequestListener.THROW_TAG, | 108 assertEquals(BadHttpUrlRequestListener.THROW_TAG, |
| 112 request.getException().getCause().getMessage()); | 109 request.getException().getCause().getMessage()); |
| 113 } | 110 } |
| 114 | 111 |
| 115 @SmallTest | 112 @SmallTest |
| 116 @Feature({"Cronet"}) | 113 @Feature({"Cronet"}) |
| 117 public void testSetUploadDataWithNullContentType() throws Exception { | 114 public void testSetUploadDataWithNullContentType() throws Exception { |
| 118 CronetTestActivity activity = launchCronetTestAppWithUrl(URL); | 115 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; |
| 119 | 116 |
| 120 HashMap<String, String> headers = new HashMap<String, String>(); | 117 HashMap<String, String> headers = new HashMap<String, String>(); |
| 121 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | 118 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); |
| 122 | 119 |
| 123 // Create request. | 120 // Create request. |
| 124 HttpUrlRequest request = activity.mRequestFactory.createRequest( | 121 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 125 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 122 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 126 byte[] uploadData = new byte[] {1, 2, 3}; | 123 byte[] uploadData = new byte[] {1, 2, 3}; |
| 127 try { | 124 try { |
| 128 request.setUploadData(null, uploadData); | 125 request.setUploadData(null, uploadData); |
| 129 fail("setUploadData should throw on null content type"); | 126 fail("setUploadData should throw on null content type"); |
| 130 } catch (NullPointerException e) { | 127 } catch (NullPointerException e) { |
| 131 // Nothing to do here. | 128 // Nothing to do here. |
| 132 } | 129 } |
| 133 } | 130 } |
| 134 | 131 |
| 135 @SmallTest | 132 @SmallTest |
| 136 @Feature({"Cronet"}) | 133 @Feature({"Cronet"}) |
| 137 public void testLegacyLoadUrl() throws Exception { | 134 public void testLegacyLoadUrl() throws Exception { |
| 138 HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig(); | 135 HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig(); |
| 139 config.enableLegacyMode(true); | 136 config.enableLegacyMode(true); |
| 140 // TODO(mef) fix tests so that library isn't loaded for legacy stack | 137 // TODO(mef) fix tests so that library isn't loaded for legacy stack |
| 141 config.setLibraryName("cronet_tests"); | 138 config.setLibraryName("cronet_tests"); |
| 142 | 139 |
| 143 String[] commandLineArgs = { | 140 String[] commandLineArgs = {CronetTestFramework.CONFIG_KEY, config.toStr ing()}; |
| 144 CronetTestActivity.CONFIG_KEY, config.toString() }; | 141 CronetTestFramework testFramework = |
| 145 CronetTestActivity activity = | 142 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); |
| 146 launchCronetTestAppWithUrlAndCommandLineArgs(URL, | |
| 147 commandLineArgs); | |
| 148 | 143 |
| 149 // Make sure that the URL is set as expected. | 144 // Make sure that the URL is set as expected. |
| 150 assertEquals(URL, activity.getUrl()); | 145 assertEquals(URL, testFramework.getUrl()); |
| 151 assertEquals(200, activity.getHttpStatusCode()); | 146 assertEquals(200, testFramework.getHttpStatusCode()); |
| 152 } | 147 } |
| 153 | 148 |
| 154 @SmallTest | 149 @SmallTest |
| 155 @Feature({"Cronet"}) | 150 @Feature({"Cronet"}) |
| 156 public void testRequestHead() throws Exception { | 151 public void testRequestHead() throws Exception { |
| 157 CronetTestActivity activity = launchCronetTestAppWithUrl(URL); | 152 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; |
| 158 | 153 |
| 159 HashMap<String, String> headers = new HashMap<String, String>(); | 154 HashMap<String, String> headers = new HashMap<String, String>(); |
| 160 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 155 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 161 | 156 |
| 162 // Create request. | 157 // Create request. |
| 163 HttpUrlRequest request = activity.mRequestFactory.createRequest( | 158 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 164 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 159 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 165 request.setHttpMethod("HEAD"); | 160 request.setHttpMethod("HEAD"); |
| 166 request.start(); | 161 request.start(); |
| 167 listener.blockForComplete(); | 162 listener.blockForComplete(); |
| 168 assertEquals(200, listener.mHttpStatusCode); | 163 assertEquals(200, listener.mHttpStatusCode); |
| 169 // HEAD requests do not get any response data and Content-Length must be | 164 // HEAD requests do not get any response data and Content-Length must be |
| 170 // ignored. | 165 // ignored. |
| 171 assertEquals(0, listener.mResponseAsBytes.length); | 166 assertEquals(0, listener.mResponseAsBytes.length); |
| 172 } | 167 } |
| 173 } | 168 } |
| OLD | NEW |