[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / components / cronet / android / test / javatests / src / org / chromium / net / CronetUrlTest.java
blob481cebad8cc2df09f5a9c25b6b8cf60c5010c74d
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
3 // found in the LICENSE file.
5 package org.chromium.net;
7 import android.content.Context;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.base.PathUtils;
11 import org.chromium.base.test.util.Feature;
13 import java.io.File;
14 import java.util.HashMap;
16 /**
17 * Example test that just starts the cronet sample.
19 public class CronetUrlTest extends CronetTestBase {
20 // URL used for base tests.
21 private static final String URL = "http://127.0.0.1:8000";
23 @SmallTest
24 @Feature({"Cronet"})
25 public void testLoadUrl() throws Exception {
26 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
28 // Make sure that the URL is set as expected.
29 assertEquals(URL, activity.getUrl());
30 assertEquals(200, activity.getHttpStatusCode());
33 @SmallTest
34 @Feature({"Cronet"})
35 public void testInvalidUrl() throws Exception {
36 CronetTestActivity activity = launchCronetTestAppWithUrl(
37 "127.0.0.1:8000");
39 // The load should fail.
40 assertEquals(0, activity.getHttpStatusCode());
43 @SmallTest
44 @Feature({"Cronet"})
45 public void testPostData() throws Exception {
46 String[] commandLineArgs = {
47 CronetTestActivity.POST_DATA_KEY, "test" };
48 CronetTestActivity activity =
49 launchCronetTestAppWithUrlAndCommandLineArgs(URL,
50 commandLineArgs);
52 // Make sure that the URL is set as expected.
53 assertEquals(URL, activity.getUrl());
54 assertEquals(200, activity.getHttpStatusCode());
57 @SmallTest
58 @Feature({"Cronet"})
59 public void testNetLog() throws Exception {
60 Context context = getInstrumentation().getTargetContext();
61 File directory = new File(PathUtils.getDataDirectory(context));
62 File file = File.createTempFile("cronet", "json", directory);
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());
70 // Starts a request.
71 HashMap<String, String> headers = new HashMap<String, String>();
72 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
73 HttpUrlRequest request = factory.createRequest(
74 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
75 request.start();
76 listener.blockForComplete();
77 factory.stopNetLog();
78 assertTrue(file.exists());
79 assertTrue(file.length() != 0);
80 assertTrue(file.delete());
81 assertTrue(!file.exists());
84 static class BadHttpUrlRequestListener extends TestHttpUrlRequestListener {
85 static final String THROW_TAG = "BadListener";
87 public BadHttpUrlRequestListener() {
90 @Override
91 public void onResponseStarted(HttpUrlRequest request) {
92 throw new NullPointerException(THROW_TAG);
96 @SmallTest
97 @Feature({"Cronet"})
98 public void testCalledByNativeException() throws Exception {
99 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
101 HashMap<String, String> headers = new HashMap<String, String>();
102 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener();
104 // Create request with bad listener to trigger an exception.
105 HttpUrlRequest request = activity.mRequestFactory.createRequest(
106 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
107 request.start();
108 listener.blockForComplete();
109 assertTrue(request.isCanceled());
110 assertNotNull(request.getException());
111 assertEquals(listener.THROW_TAG,
112 request.getException().getCause().getMessage());
115 @SmallTest
116 @Feature({"Cronet"})
117 public void testSetUploadDataWithNullContentType() throws Exception {
118 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
120 HashMap<String, String> headers = new HashMap<String, String>();
121 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener();
123 // Create request.
124 HttpUrlRequest request = activity.mRequestFactory.createRequest(
125 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
126 byte[] uploadData = new byte[] {1, 2, 3};
127 try {
128 request.setUploadData(null, uploadData);
129 fail("setUploadData should throw on null content type");
130 } catch (NullPointerException e) {
131 // Nothing to do here.
135 @SmallTest
136 @Feature({"Cronet"})
137 public void disabled_testQuicLoadUrl() throws Exception {
138 HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
139 // TODO(mef): Test Quic end-to-end using local QUIC server.
140 String quicURL = "https://www.google.com:443";
141 String quicNegotiatedProtocol = "quic/1+spdy/3";
142 config.enableQUIC(true);
143 config.addQuicHint("www.google.com", 443, 443);
144 config.setExperimentalQuicConnectionOptions("PACE,IW10,FOO,DEADBEEF");
146 String[] commandLineArgs = {
147 CronetTestActivity.CONFIG_KEY, config.toString() };
148 CronetTestActivity activity =
149 launchCronetTestAppWithUrlAndCommandLineArgs(null,
150 commandLineArgs);
151 activity.startNetLog();
153 HashMap<String, String> headers = new HashMap<String, String>();
154 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
156 // Try several times as first request may not use QUIC.
157 // TODO(mef): Remove loop after adding http server properties manager.
158 for (int i = 0; i < 10; ++i) {
159 HttpUrlRequest request =
160 activity.mRequestFactory.createRequest(
161 quicURL,
162 HttpUrlRequest.REQUEST_PRIORITY_MEDIUM,
163 headers,
164 listener);
165 request.start();
166 listener.blockForComplete();
167 assertEquals(200, listener.mHttpStatusCode);
168 if (listener.mNegotiatedProtocol.equals(quicNegotiatedProtocol)) {
169 break;
171 Thread.sleep(1000);
172 listener.resetComplete();
175 assertEquals(quicNegotiatedProtocol, listener.mNegotiatedProtocol);
176 activity.stopNetLog();
179 @SmallTest
180 @Feature({"Cronet"})
181 public void testLegacyLoadUrl() throws Exception {
182 HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
183 config.enableLegacyMode(true);
184 // TODO(mef) fix tests so that library isn't loaded for legacy stack
185 config.setLibraryName("cronet_tests");
187 String[] commandLineArgs = {
188 CronetTestActivity.CONFIG_KEY, config.toString() };
189 CronetTestActivity activity =
190 launchCronetTestAppWithUrlAndCommandLineArgs(URL,
191 commandLineArgs);
193 // Make sure that the URL is set as expected.
194 assertEquals(URL, activity.getUrl());
195 assertEquals(200, activity.getHttpStatusCode());
198 @SmallTest
199 @Feature({"Cronet"})
200 public void testRequestHead() throws Exception {
201 CronetTestActivity activity = launchCronetTestAppWithUrl(URL);
203 HashMap<String, String> headers = new HashMap<String, String>();
204 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
206 // Create request.
207 HttpUrlRequest request = activity.mRequestFactory.createRequest(
208 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
209 request.setHttpMethod("HEAD");
210 request.start();
211 listener.blockForComplete();
212 assertEquals(200, listener.mHttpStatusCode);
213 // HEAD requests do not get any response data and Content-Length must be
214 // ignored.
215 assertEquals(0, listener.mResponseAsBytes.length);