Home | History | Annotate | Download | only in cronet_test_apk
      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.
      4 
      5 package org.chromium.cronet_test_apk;
      6 
      7 import android.app.Activity;
      8 import android.content.Intent;
      9 import android.content.res.AssetManager;
     10 import android.os.Bundle;
     11 import android.os.Environment;
     12 import android.util.Log;
     13 
     14 import org.chromium.base.PathUtils;
     15 import org.chromium.net.ChromiumUrlRequestFactory;
     16 import org.chromium.net.HttpUrlRequest;
     17 import org.chromium.net.HttpUrlRequestFactory;
     18 import org.chromium.net.HttpUrlRequestFactoryConfig;
     19 import org.chromium.net.HttpUrlRequestListener;
     20 
     21 import java.io.ByteArrayInputStream;
     22 import java.io.FileOutputStream;
     23 import java.io.InputStream;
     24 import java.io.OutputStream;
     25 
     26 import java.nio.channels.Channels;
     27 import java.nio.channels.ReadableByteChannel;
     28 import java.util.HashMap;
     29 
     30 /**
     31  * Activity for managing the Cronet Test.
     32  */
     33 public class CronetTestActivity extends Activity {
     34     private static final String TAG = "CronetTestActivity";
     35 
     36     public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
     37     public static final String POST_DATA_KEY = "postData";
     38     public static final String CONFIG_KEY = "config";
     39 
     40     ChromiumUrlRequestFactory mChromiumRequestFactory;
     41     HttpUrlRequestFactory mRequestFactory;
     42 
     43     String mUrl;
     44 
     45     boolean mLoading = false;
     46 
     47     int mHttpStatusCode = 0;
     48 
     49     class TestHttpUrlRequestListener implements HttpUrlRequestListener {
     50         public TestHttpUrlRequestListener() {
     51         }
     52 
     53         @Override
     54         public void onResponseStarted(HttpUrlRequest request) {
     55             mHttpStatusCode = request.getHttpStatusCode();
     56         }
     57 
     58         @Override
     59         public void onRequestComplete(HttpUrlRequest request) {
     60             mLoading = false;
     61         }
     62     }
     63 
     64     @Override
     65     protected void onCreate(final Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67 
     68         if (!loadTestFiles()) {
     69             Log.e(TAG, "Loading test files failed");
     70             return;
     71         }
     72 
     73         try {
     74             System.loadLibrary("cronet_tests");
     75         } catch (UnsatisfiedLinkError e) {
     76             Log.e(TAG, "libcronet_test initialization failed.", e);
     77             finish();
     78             return;
     79         }
     80 
     81         HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
     82         config.enableHttpCache(HttpUrlRequestFactoryConfig.HttpCache.IN_MEMORY,
     83                                100 * 1024)
     84               .enableSPDY(true)
     85               .enableQUIC(true);
     86 
     87         // Override config if it is passed from the launcher.
     88         String configString = getCommandLineArg(CONFIG_KEY);
     89         if (configString != null) {
     90             try {
     91                 Log.i(TAG, "Using Config: " + configString);
     92                 config = new HttpUrlRequestFactoryConfig(configString);
     93             } catch (org.json.JSONException e) {
     94                 Log.e(TAG, "Invalid Config.", e);
     95                 finish();
     96                 return;
     97             }
     98         }
     99 
    100         // Setting this here so it isn't overridden on the command line
    101         config.setLibraryName("cronet_tests");
    102 
    103         mRequestFactory = HttpUrlRequestFactory.createFactory(
    104                 getApplicationContext(), config);
    105 
    106         mChromiumRequestFactory = new ChromiumUrlRequestFactory(
    107                 getApplicationContext(), config);
    108 
    109         String appUrl = getUrlFromIntent(getIntent());
    110         if (appUrl != null) {
    111             startWithURL(appUrl);
    112         }
    113     }
    114 
    115     private boolean loadTestFiles() {
    116         String testFilePath = "test";
    117         String toPath = PathUtils.getDataDirectory(getApplicationContext());
    118         AssetManager assetManager = getAssets();
    119         try {
    120             String[] files = assetManager.list(testFilePath);
    121             Log.i(TAG, "Begin loading " + files.length + " test files.");
    122             for (String file : files) {
    123                 Log.i(TAG, "Loading " + file);
    124                 if (!copyTestFile(assetManager,
    125                                   testFilePath + "/" + file,
    126                                   toPath + "/" + file)) {
    127                     return false;
    128                 }
    129             }
    130             return true;
    131         } catch (Exception e) {
    132             e.printStackTrace();
    133             return false;
    134         }
    135     }
    136 
    137     // Helper function to copy a file to a destination.
    138     private static boolean copyTestFile(AssetManager assetManager,
    139                                         String testFile,
    140                                         String testFileCopy) {
    141         try {
    142             InputStream in = assetManager.open(testFile);
    143             OutputStream out = new FileOutputStream(testFileCopy);
    144             byte[] buffer = new byte[1024];
    145             int read;
    146             while ((read = in.read(buffer)) != -1) {
    147               out.write(buffer, 0, read);
    148             }
    149             in.close();
    150             out.flush();
    151             out.close();
    152             return true;
    153         } catch (Exception e) {
    154             e.printStackTrace();
    155             return false;
    156         }
    157     }
    158 
    159     private static String getUrlFromIntent(Intent intent) {
    160         return intent != null ? intent.getDataString() : null;
    161     }
    162 
    163     private String getCommandLineArg(String key) {
    164         Intent intent = getIntent();
    165         Bundle extras = intent.getExtras();
    166         Log.i(TAG, "Cronet extras: " + extras);
    167         if (extras != null) {
    168             String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY);
    169             if (commandLine != null) {
    170                 for (int i = 0; i < commandLine.length; ++i) {
    171                     Log.i(TAG,
    172                             "Cronet commandLine[" + i + "]=" + commandLine[i]);
    173                     if (commandLine[i].equals(key)) {
    174                         return commandLine[++i];
    175                     }
    176                 }
    177             }
    178         }
    179         return null;
    180     }
    181 
    182     private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) {
    183         String postData = getCommandLineArg(POST_DATA_KEY);
    184         if (postData != null) {
    185             InputStream dataStream = new ByteArrayInputStream(
    186                     postData.getBytes());
    187             ReadableByteChannel dataChannel = Channels.newChannel(dataStream);
    188             request.setUploadChannel("text/plain", dataChannel,
    189                     postData.length());
    190             request.setHttpMethod("POST");
    191         }
    192     }
    193 
    194     public void startWithURL(String url) {
    195         Log.i(TAG, "Cronet started: " + url);
    196         mUrl = url;
    197         mLoading = true;
    198 
    199         HashMap<String, String> headers = new HashMap<String, String>();
    200         HttpUrlRequestListener listener = new TestHttpUrlRequestListener();
    201         HttpUrlRequest request = mRequestFactory.createRequest(
    202                 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
    203         applyCommandLineToHttpUrlRequest(request);
    204         request.start();
    205     }
    206 
    207     public String getUrl() {
    208         return mUrl;
    209     }
    210 
    211     public boolean isLoading() {
    212         return mLoading;
    213     }
    214 
    215     public int getHttpStatusCode() {
    216         return mHttpStatusCode;
    217     }
    218 
    219     public void startNetLog() {
    220         mChromiumRequestFactory.getRequestContext().startNetLogToFile(
    221                 Environment.getExternalStorageDirectory().getPath() +
    222                         "/cronet_sample_netlog.json");
    223     }
    224 
    225     public void stopNetLog() {
    226         mChromiumRequestFactory.getRequestContext().stopNetLog();
    227     }
    228 }
    229