Make castv2 performance test work.
[chromium-blink-merge.git] / components / cronet / android / test / src / org / chromium / net / CronetTestActivity.java
blob897515f2b151d1cd54b5ad5befe5a4bf3cde86d7
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.app.Activity;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.os.Environment;
11 import android.util.Log;
13 import org.chromium.base.annotations.SuppressFBWarnings;
14 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory;
16 import java.io.ByteArrayInputStream;
17 import java.io.InputStream;
19 import java.nio.channels.Channels;
20 import java.nio.channels.ReadableByteChannel;
21 import java.util.HashMap;
23 /**
24 * Activity for managing the Cronet Test.
26 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
27 public class CronetTestActivity extends Activity {
28 private static final String TAG = "CronetTestActivity";
30 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
31 public static final String POST_DATA_KEY = "postData";
32 public static final String CONFIG_KEY = "config";
33 public static final String LIBRARY_INIT_KEY = "libraryInit";
34 /**
35 * Skip library initialization.
37 public static final String LIBRARY_INIT_SKIP = "skip";
39 /**
40 * Initialize Cronet Async API only.
42 public static final String LIBRARY_INIT_CRONET_ONLY = "cronetOnly";
44 public CronetURLStreamHandlerFactory mStreamHandlerFactory;
45 public UrlRequestContext mUrlRequestContext;
46 HttpUrlRequestFactory mRequestFactory;
47 @SuppressFBWarnings("URF_UNREAD_FIELD")
48 HistogramManager mHistogramManager;
50 String mUrl;
52 boolean mLoading = false;
54 int mHttpStatusCode = 0;
56 class TestHttpUrlRequestListener implements HttpUrlRequestListener {
57 public TestHttpUrlRequestListener() {
60 @Override
61 public void onResponseStarted(HttpUrlRequest request) {
62 mHttpStatusCode = request.getHttpStatusCode();
65 @Override
66 public void onRequestComplete(HttpUrlRequest request) {
67 mLoading = false;
71 @Override
72 protected void onCreate(final Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
75 String initString = getCommandLineArg(LIBRARY_INIT_KEY);
76 if (LIBRARY_INIT_SKIP.equals(initString)) {
77 return;
80 mUrlRequestContext = initRequestContext();
81 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(this, getContextConfig());
82 mHistogramManager = HistogramManager.createHistogramManager();
84 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) {
85 return;
88 mRequestFactory = initRequestFactory();
89 String appUrl = getUrlFromIntent(getIntent());
90 if (appUrl != null) {
91 startWithURL(appUrl);
95 UrlRequestContextConfig getContextConfig() {
96 UrlRequestContextConfig config = new UrlRequestContextConfig();
97 config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY,
98 100 * 1024)
99 .enableSPDY(true)
100 .enableQUIC(true);
102 // Override config if it is passed from the launcher.
103 String configString = getCommandLineArg(CONFIG_KEY);
104 if (configString != null) {
105 try {
106 Log.i(TAG, "Using Config: " + configString);
107 config = new UrlRequestContextConfig(configString);
108 } catch (org.json.JSONException e) {
109 Log.e(TAG, "Invalid Config.", e);
110 finish();
111 return null;
115 // Setting this here so it isn't overridden on the command line
116 config.setLibraryName("cronet_tests");
117 return config;
120 // Helper function to initialize request context. Also used in testing.
121 public UrlRequestContext initRequestContext() {
122 return UrlRequestContext.createContext(this, getContextConfig());
125 // Helper function to initialize request factory. Also used in testing.
126 public HttpUrlRequestFactory initRequestFactory() {
127 return HttpUrlRequestFactory.createFactory(this, getContextConfig());
130 private static String getUrlFromIntent(Intent intent) {
131 return intent != null ? intent.getDataString() : null;
134 private String getCommandLineArg(String key) {
135 Intent intent = getIntent();
136 Bundle extras = intent.getExtras();
137 Log.i(TAG, "Cronet extras: " + extras);
138 if (extras != null) {
139 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY);
140 if (commandLine != null) {
141 for (int i = 0; i < commandLine.length; ++i) {
142 Log.i(TAG,
143 "Cronet commandLine[" + i + "]=" + commandLine[i]);
144 if (commandLine[i].equals(key)) {
145 return commandLine[++i];
150 return null;
153 private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) {
154 String postData = getCommandLineArg(POST_DATA_KEY);
155 if (postData != null) {
156 InputStream dataStream = new ByteArrayInputStream(
157 postData.getBytes());
158 ReadableByteChannel dataChannel = Channels.newChannel(dataStream);
159 request.setUploadChannel("text/plain", dataChannel,
160 postData.length());
161 request.setHttpMethod("POST");
165 public void startWithURL(String url) {
166 Log.i(TAG, "Cronet started: " + url);
167 mUrl = url;
168 mLoading = true;
170 HashMap<String, String> headers = new HashMap<String, String>();
171 HttpUrlRequestListener listener = new TestHttpUrlRequestListener();
172 HttpUrlRequest request = mRequestFactory.createRequest(
173 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
174 applyCommandLineToHttpUrlRequest(request);
175 request.start();
178 public String getUrl() {
179 return mUrl;
182 public boolean isLoading() {
183 return mLoading;
186 public int getHttpStatusCode() {
187 return mHttpStatusCode;
190 public void startNetLog() {
191 mRequestFactory.startNetLogToFile(
192 Environment.getExternalStorageDirectory().getPath()
193 + "/cronet_sample_netlog.json");
196 public void stopNetLog() {
197 mRequestFactory.stopNetLog();