| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.AssetManager; | 8 import android.content.res.AssetManager; |
| 9 import android.util.Log; | 9 import android.util.Log; |
| 10 | 10 |
| 11 import org.chromium.base.PathUtils; | 11 import org.chromium.base.PathUtils; |
| 12 import org.chromium.base.annotations.SuppressFBWarnings; |
| 12 | 13 |
| 13 import java.io.File; | 14 import java.io.File; |
| 14 import java.io.FileOutputStream; | 15 import java.io.FileOutputStream; |
| 16 import java.io.IOException; |
| 15 import java.io.InputStream; | 17 import java.io.InputStream; |
| 16 import java.io.OutputStream; | 18 import java.io.OutputStream; |
| 17 | 19 |
| 18 /** | 20 /** |
| 19 * Helper class to install test files. | 21 * Helper class to install test files. |
| 20 */ | 22 */ |
| 21 public final class TestFilesInstaller { | 23 public final class TestFilesInstaller { |
| 22 private static final String TAG = "TestFilesInstaller"; | 24 private static final String TAG = "TestFilesInstaller"; |
| 23 // Name of the asset directory in which test files are stored. | 25 // Name of the asset directory in which test files are stored. |
| 24 private static final String TEST_FILE_ASSET_PATH = "test"; | 26 private static final String TEST_FILE_ASSET_PATH = "test"; |
| 25 | 27 |
| 26 /** | 28 /** |
| 27 * Installs test files if files have not been installed. | 29 * Installs test files if files have not been installed. |
| 28 */ | 30 */ |
| 29 public static void installIfNeeded(Context context) { | 31 public static void installIfNeeded(Context context) { |
| 30 if (areFilesInstalled(context)) { | 32 if (areFilesInstalled(context)) { |
| 31 return; | 33 return; |
| 32 } | 34 } |
| 33 install(context); | 35 try { |
| 36 install(context, TEST_FILE_ASSET_PATH); |
| 37 } catch (IOException e) { |
| 38 // Make the test app crash and fail early. |
| 39 throw new RuntimeException(e); |
| 40 } |
| 34 } | 41 } |
| 35 | 42 |
| 36 /** | 43 /** |
| 37 * Returns the installed path of the test files. | 44 * Returns the installed path of the test files. |
| 38 */ | 45 */ |
| 39 public static String getInstalledPath(Context context) { | 46 public static String getInstalledPath(Context context) { |
| 40 return PathUtils.getDataDirectory(context) + "/test"; | 47 return PathUtils.getDataDirectory(context) + "/" + TEST_FILE_ASSET_PATH; |
| 41 } | 48 } |
| 42 | 49 |
| 43 /** | 50 /** |
| 44 * Returns whether test files are installed. | 51 * Returns whether test files are installed. |
| 45 */ | 52 */ |
| 46 public static boolean areFilesInstalled(Context context) { | 53 public static boolean areFilesInstalled(Context context) { |
| 47 // Checking for file directory is fine even when new files are added, | 54 // Checking for file directory is fine even when new files are added, |
| 48 // because the app will be re-installed and app data will be cleared. | 55 // because the app will be re-installed and app data will be cleared. |
| 49 File directory = new File(getInstalledPath(context)); | 56 File directory = new File(getInstalledPath(context)); |
| 50 return directory.exists(); | 57 return directory.exists(); |
| 51 } | 58 } |
| 52 | 59 |
| 53 /** | 60 /** |
| 54 * Installs test files that are included in assets. | 61 * Installs test files that are included in {@code path}. |
| 55 * @params context Application context | 62 * @params context Application context |
| 63 * @params path |
| 56 */ | 64 */ |
| 57 private static void install(Context context) { | 65 private static void install(Context context, String path) throws IOException
{ |
| 58 AssetManager assetManager = context.getAssets(); | 66 AssetManager assetManager = context.getAssets(); |
| 59 try { | 67 String files[] = assetManager.list(path); |
| 60 String[] files = assetManager.list(TEST_FILE_ASSET_PATH); | 68 Log.i(TAG, "Loading " + path + " ..."); |
| 61 String destDir = getInstalledPath(context); | 69 String root = PathUtils.getDataDirectory(context); |
| 62 File destDirFile = new File(destDir); | 70 if (files.length == 0) { |
| 63 if (!destDirFile.mkdir()) { | 71 // The path is a file, so copy the file now. |
| 64 throw new IllegalStateException( | 72 copyTestFile(assetManager, path, root + "/" + path); |
| 65 "directory exists or it cannot be created."); | 73 } else { |
| 66 } | 74 // The path is a directory, so recursively handle its files, since |
| 67 Log.i(TAG, "Begin loading " + files.length + " test files."); | 75 // the directory can contain subdirectories. |
| 68 for (String fileName : files) { | 76 String fullPath = root + "/" + path; |
| 69 Log.i(TAG, "Loading " + fileName); | 77 File dir = new File(fullPath); |
| 70 String destFilePath = destDir + "/" + fileName; | 78 if (!dir.exists()) { |
| 71 if (!copyTestFile(assetManager, | 79 Log.i(TAG, "Creating directory " + fullPath + " ..."); |
| 72 TEST_FILE_ASSET_PATH + "/" + fileName, | 80 if (!dir.mkdir()) { |
| 73 destFilePath)) { | 81 throw new IOException("Directory not created."); |
| 74 Log.e(TAG, "Loading " + fileName + " failed."); | |
| 75 } | 82 } |
| 76 } | 83 } |
| 77 } catch (Exception e) { | 84 for (int i = 0; i < files.length; i++) { |
| 78 e.printStackTrace(); | 85 install(context, path + "/" + files[i]); |
| 86 } |
| 79 } | 87 } |
| 80 } | 88 } |
| 81 | |
| 82 /** | 89 /** |
| 83 * Copies a file from assets to the device's file system. | 90 * Copies a file from assets to the device's file system. |
| 84 * @param assetManager AssetManager of the application. | 91 * @param assetManager AssetManager of the application. |
| 85 * @param srcFilePath the source file path in assets. | 92 * @param srcFilePath the source file path in assets. |
| 86 * @param destFilePath the destination file path. | 93 * @param destFilePath the destination file path. |
| 87 * @throws IllegalStateException if the destination file already exists. | 94 * @throws IllegalStateException if the destination file already exists. |
| 88 */ | 95 */ |
| 89 private static boolean copyTestFile(AssetManager assetManager, | 96 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") |
| 90 String srcFilePath, | 97 private static void copyTestFile(AssetManager assetManager, |
| 91 String destFilePath) { | 98 String srcFilePath, |
| 92 OutputStream out; | 99 String destFilePath) throws IOException { |
| 93 try { | 100 File destFile = new File(destFilePath); |
| 94 File destFile = new File(destFilePath); | 101 if (destFile.exists()) { |
| 95 if (destFile.exists()) { | 102 throw new IllegalStateException(srcFilePath + " already exists"); |
| 96 throw new IllegalStateException(srcFilePath | |
| 97 + " already exists"); | |
| 98 } | |
| 99 out = new FileOutputStream(destFilePath); | |
| 100 } catch (Exception e) { | |
| 101 e.printStackTrace(); | |
| 102 return false; | |
| 103 } | 103 } |
| 104 try { | 104 OutputStream out = new FileOutputStream(destFilePath); |
| 105 InputStream in = assetManager.open(srcFilePath); | 105 InputStream in = assetManager.open(srcFilePath); |
| 106 | 106 |
| 107 byte[] buffer = new byte[1024]; | 107 byte[] buffer = new byte[1024]; |
| 108 int read; | 108 int read; |
| 109 while ((read = in.read(buffer)) != -1) { | 109 while ((read = in.read(buffer)) != -1) { |
| 110 out.write(buffer, 0, read); | 110 out.write(buffer, 0, read); |
| 111 } | |
| 112 in.close(); | |
| 113 out.flush(); | |
| 114 out.close(); | |
| 115 return true; | |
| 116 } catch (Exception e) { | |
| 117 try { | |
| 118 out.close(); | |
| 119 } catch (Exception closeException) { | |
| 120 closeException.printStackTrace(); | |
| 121 } | |
| 122 e.printStackTrace(); | |
| 123 return false; | |
| 124 } | 111 } |
| 112 in.close(); |
| 113 out.flush(); |
| 114 out.close(); |
| 125 } | 115 } |
| 126 } | 116 } |
| OLD | NEW |