Chromium Code Reviews| Index: components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java |
| diff --git a/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java b/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java |
| index 5303e6ec9ae18f96f8aa5c9f02e62b3fff4f625b..b6ff4f5ec37a4b4eaefd51a0a9bbe96c3c15a09f 100644 |
| --- a/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java |
| +++ b/components/cronet/android/test/src/org/chromium/net/TestFilesInstaller.java |
| @@ -12,6 +12,7 @@ import org.chromium.base.PathUtils; |
| import java.io.File; |
| import java.io.FileOutputStream; |
| +import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.OutputStream; |
| @@ -30,14 +31,19 @@ public final class TestFilesInstaller { |
| if (areFilesInstalled(context)) { |
| return; |
| } |
| - install(context); |
| + try { |
| + install(context, TEST_FILE_ASSET_PATH); |
| + } catch (IOException e) { |
| + // Make the test app crash and fail early. |
| + throw new RuntimeException(e); |
| + } |
| } |
| /** |
| * Returns the installed path of the test files. |
| */ |
| public static String getInstalledPath(Context context) { |
| - return PathUtils.getDataDirectory(context) + "/test"; |
| + return PathUtils.getDataDirectory(context) + "/" + TEST_FILE_ASSET_PATH; |
| } |
| /** |
| @@ -51,34 +57,30 @@ public final class TestFilesInstaller { |
| } |
| /** |
| - * Installs test files that are included in assets. |
| + * Installs test files that are included in {@code path}. |
| * @params context Application context |
| + * @params path |
| */ |
| - private static void install(Context context) { |
| + private static void install(Context context, String path) throws IOException { |
| AssetManager assetManager = context.getAssets(); |
| - try { |
| - String[] files = assetManager.list(TEST_FILE_ASSET_PATH); |
| - String destDir = getInstalledPath(context); |
| - File destDirFile = new File(destDir); |
| - if (!destDirFile.mkdir()) { |
| - throw new IllegalStateException( |
| - "directory exists or it cannot be created."); |
| + String files[] = null; |
| + Log.i(TAG, "Loading " + path + " ..."); |
| + String root = PathUtils.getDataDirectory(context); |
| + files = assetManager.list(path); |
|
mef
2015/03/31 20:56:58
suggest: move this up to files[] declaration.
xunjieli
2015/03/31 21:20:26
Done.
|
| + if (files.length == 0) { |
|
mef
2015/03/31 20:56:58
this logic is a bit unclear. Why files.length woul
xunjieli
2015/03/31 21:20:26
When the path is a file instead of a directory. Ad
|
| + copyTestFile(assetManager, path, root + "/" + path); |
| + } else { |
| + String fullPath = root + "/" + path; |
| + File dir = new File(fullPath); |
| + if (!dir.exists()) { |
| + Log.i(TAG, "Creating directory " + fullPath + " ..."); |
| + dir.mkdir(); |
| } |
| - Log.i(TAG, "Begin loading " + files.length + " test files."); |
| - for (String fileName : files) { |
| - Log.i(TAG, "Loading " + fileName); |
| - String destFilePath = destDir + "/" + fileName; |
| - if (!copyTestFile(assetManager, |
| - TEST_FILE_ASSET_PATH + "/" + fileName, |
| - destFilePath)) { |
| - Log.e(TAG, "Loading " + fileName + " failed."); |
| - } |
| + for (int i = 0; i < files.length; i++) { |
| + install(context, path + "/" + files[i]); |
|
mef
2015/03/31 20:56:58
Is this recursive install?
xunjieli
2015/03/31 21:20:26
Yes. We need to copy subdirectories and the files
|
| } |
| - } catch (Exception e) { |
| - e.printStackTrace(); |
| } |
| } |
| - |
| /** |
| * Copies a file from assets to the device's file system. |
| * @param assetManager AssetManager of the application. |
| @@ -86,41 +88,24 @@ public final class TestFilesInstaller { |
| * @param destFilePath the destination file path. |
| * @throws IllegalStateException if the destination file already exists. |
| */ |
| - private static boolean copyTestFile(AssetManager assetManager, |
| - String srcFilePath, |
| - String destFilePath) { |
| + private static void copyTestFile(AssetManager assetManager, |
| + String srcFilePath, |
| + String destFilePath) throws IOException { |
| OutputStream out; |
| - try { |
| - File destFile = new File(destFilePath); |
| - if (destFile.exists()) { |
| - throw new IllegalStateException(srcFilePath |
| - + " already exists"); |
| - } |
| - out = new FileOutputStream(destFilePath); |
| - } catch (Exception e) { |
| - e.printStackTrace(); |
| - return false; |
| + File destFile = new File(destFilePath); |
| + if (destFile.exists()) { |
| + throw new IllegalStateException(srcFilePath + " already exists"); |
| } |
| - try { |
| - InputStream in = assetManager.open(srcFilePath); |
| + out = new FileOutputStream(destFilePath); |
| + InputStream in = assetManager.open(srcFilePath); |
| - 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) { |
| - try { |
| - out.close(); |
| - } catch (Exception closeException) { |
| - closeException.printStackTrace(); |
| - } |
| - e.printStackTrace(); |
| - return false; |
| + byte[] buffer = new byte[1024]; |
| + int read; |
| + while ((read = in.read(buffer)) != -1) { |
| + out.write(buffer, 0, read); |
| } |
| + in.close(); |
| + out.flush(); |
| + out.close(); |
| } |
| } |