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.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
8 | 9 |
9 import org.chromium.base.PathUtils; | 10 import org.chromium.base.PathUtils; |
10 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
11 | 12 |
12 import java.io.File; | 13 import java.io.File; |
13 import java.util.HashMap; | 14 import java.util.HashMap; |
14 | 15 |
15 /** | 16 /** |
16 * Example test that just starts the cronet sample. | 17 * Example test that just starts the cronet sample. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 commandLineArgs); | 50 commandLineArgs); |
50 | 51 |
51 // Make sure that the URL is set as expected. | 52 // Make sure that the URL is set as expected. |
52 assertEquals(URL, activity.getUrl()); | 53 assertEquals(URL, activity.getUrl()); |
53 assertEquals(200, activity.getHttpStatusCode()); | 54 assertEquals(200, activity.getHttpStatusCode()); |
54 } | 55 } |
55 | 56 |
56 @SmallTest | 57 @SmallTest |
57 @Feature({"Cronet"}) | 58 @Feature({"Cronet"}) |
58 public void testNetLog() throws Exception { | 59 public void testNetLog() throws Exception { |
59 CronetTestActivity activity = launchCronetTestApp(); | 60 Context context = getInstrumentation().getTargetContext(); |
60 File directory = new File(PathUtils.getDataDirectory( | 61 File directory = new File(PathUtils.getDataDirectory(context)); |
61 getInstrumentation().getTargetContext())); | |
62 File file = File.createTempFile("cronet", "json", directory); | 62 File file = File.createTempFile("cronet", "json", directory); |
63 activity.mRequestFactory.startNetLogToFile(file.getPath()); | 63 HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory( |
| 64 context, |
| 65 new UrlRequestContextConfig().setLibraryName("cronet_tests")); |
| 66 // 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 |
| 68 // context is not fully initialized. See crbug.com/470196. |
| 69 factory.startNetLogToFile(file.getPath()); |
64 // Starts a request. | 70 // Starts a request. |
65 activity.startWithURL(URL); | 71 HashMap<String, String> headers = new HashMap<String, String>(); |
66 Thread.sleep(5000); | 72 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
67 activity.mRequestFactory.stopNetLog(); | 73 HttpUrlRequest request = factory.createRequest( |
| 74 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 75 request.start(); |
| 76 listener.blockForComplete(); |
| 77 factory.stopNetLog(); |
68 assertTrue(file.exists()); | 78 assertTrue(file.exists()); |
69 assertTrue(file.length() != 0); | 79 assertTrue(file.length() != 0); |
70 assertTrue(file.delete()); | 80 assertTrue(file.delete()); |
71 assertTrue(!file.exists()); | 81 assertTrue(!file.exists()); |
72 } | 82 } |
73 | 83 |
74 static class BadHttpUrlRequestListener extends TestHttpUrlRequestListener { | 84 static class BadHttpUrlRequestListener extends TestHttpUrlRequestListener { |
75 static final String THROW_TAG = "BadListener"; | 85 static final String THROW_TAG = "BadListener"; |
76 | 86 |
77 public BadHttpUrlRequestListener() { | 87 public BadHttpUrlRequestListener() { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 208 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
199 request.setHttpMethod("HEAD"); | 209 request.setHttpMethod("HEAD"); |
200 request.start(); | 210 request.start(); |
201 listener.blockForComplete(); | 211 listener.blockForComplete(); |
202 assertEquals(200, listener.mHttpStatusCode); | 212 assertEquals(200, listener.mHttpStatusCode); |
203 // HEAD requests do not get any response data and Content-Length must be | 213 // HEAD requests do not get any response data and Content-Length must be |
204 // ignored. | 214 // ignored. |
205 assertEquals(0, listener.mResponseAsBytes.length); | 215 assertEquals(0, listener.mResponseAsBytes.length); |
206 } | 216 } |
207 } | 217 } |
OLD | NEW |