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