Chromium Code Reviews| Index: components/cronet/android/test/src/org/chromium/cronet_test_apk/CronetTestActivity.java |
| diff --git a/components/cronet/android/test/src/org/chromium/cronet_test_apk/CronetTestActivity.java b/components/cronet/android/test/src/org/chromium/cronet_test_apk/CronetTestActivity.java |
| index bf0cbc787e9aedcb1b1b6be5017c961db75ec5ec..e39f22e920031177a245dd56a0d52b5fda5b2149 100644 |
| --- a/components/cronet/android/test/src/org/chromium/cronet_test_apk/CronetTestActivity.java |
| +++ b/components/cronet/android/test/src/org/chromium/cronet_test_apk/CronetTestActivity.java |
| @@ -6,10 +6,12 @@ package org.chromium.cronet_test_apk; |
| import android.app.Activity; |
| import android.content.Intent; |
| +import android.content.res.AssetManager; |
| import android.os.Bundle; |
| import android.os.Environment; |
| import android.util.Log; |
| +import org.chromium.base.PathUtils; |
| import org.chromium.net.ChromiumUrlRequestFactory; |
| import org.chromium.net.HttpUrlRequest; |
| import org.chromium.net.HttpUrlRequestFactory; |
| @@ -17,7 +19,10 @@ import org.chromium.net.HttpUrlRequestFactoryConfig; |
| import org.chromium.net.HttpUrlRequestListener; |
| import java.io.ByteArrayInputStream; |
| +import java.io.File; |
| +import java.io.FileOutputStream; |
| import java.io.InputStream; |
| +import java.io.OutputStream; |
| import java.nio.channels.Channels; |
| import java.nio.channels.ReadableByteChannel; |
| @@ -62,6 +67,11 @@ public class CronetTestActivity extends Activity { |
| protected void onCreate(final Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| + if (!loadTestFiles()) { |
| + Log.e(TAG, "Loading test files failed"); |
| + return; |
| + } |
| + |
| try { |
| System.loadLibrary("cronet_tests"); |
| } catch (UnsatisfiedLinkError e) { |
| @@ -101,6 +111,50 @@ public class CronetTestActivity extends Activity { |
| } |
| } |
| + private boolean loadTestFiles() { |
| + String testFilePath = "test"; |
| + String toPath = PathUtils.getDataDirectory(getApplicationContext()); |
| + AssetManager assetManager = getAssets(); |
| + try { |
| + String[] files = assetManager.list(testFilePath); |
| + Log.i(TAG, "Begin loading " + files.length + " test files."); |
| + for (String file : files) { |
| + Log.i(TAG, "Loading " + file); |
| + if (!copyTestFile(assetManager, |
| + testFilePath + "/" + file, |
| + toPath + "/" + file)) { |
| + return false; |
| + } |
| + } |
| + return true; |
| + } catch (Exception e) { |
| + e.printStackTrace(); |
| + return false; |
| + } |
| + } |
| + |
| + // Helper function to copy a file to a destination. |
| + private static boolean copyTestFile(AssetManager assetManager, String testFile, |
|
mmenke
2014/09/18 18:31:45
nit: While Java style guide allows longer lines,
xunjieli
2014/09/18 22:45:15
Done.
|
| + String testFileCopy) { |
| + try { |
| + InputStream in = assetManager.open(testFile); |
| + new File(testFileCopy).createNewFile(); |
| + OutputStream out = new FileOutputStream(testFileCopy); |
| + byte[] buffer = new byte[1024]; |
| + int read; |
| + while ((read = in.read(buffer)) != -1) { |
| + out.write(buffer, 0, read); |
| + } |
| + in.close(); |
| + out.flush(); |
| + out.close(); |
| + return true; |
| + } catch (Exception e) { |
| + e.printStackTrace(); |
| + return false; |
| + } |
| + } |
| + |
| private static String getUrlFromIntent(Intent intent) { |
| return intent != null ? intent.getDataString() : null; |
| } |