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 |
(...skipping 24 matching lines...) Expand all Loading... |
35 } catch (IOException e) { | 35 } catch (IOException e) { |
36 // Make the test app crash and fail early. | 36 // Make the test app crash and fail early. |
37 throw new RuntimeException(e); | 37 throw new RuntimeException(e); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 /** | 41 /** |
42 * Returns the installed path of the test files. | 42 * Returns the installed path of the test files. |
43 */ | 43 */ |
44 public static String getInstalledPath(Context context) { | 44 public static String getInstalledPath(Context context) { |
45 return PathUtils.getDataDirectory(context) + "/" + TEST_FILE_ASSET_PATH; | 45 return PathUtils.getDataDirectory() + "/" + TEST_FILE_ASSET_PATH; |
46 } | 46 } |
47 | 47 |
48 /** | 48 /** |
49 * Returns whether test files are installed. | 49 * Returns whether test files are installed. |
50 */ | 50 */ |
51 private static boolean areFilesInstalled(Context context) { | 51 private static boolean areFilesInstalled(Context context) { |
52 // Checking for file directory is fine even when new files are added, | 52 // Checking for file directory is fine even when new files are added, |
53 // because the app will be re-installed and app data will be cleared. | 53 // because the app will be re-installed and app data will be cleared. |
54 File directory = new File(getInstalledPath(context)); | 54 File directory = new File(getInstalledPath(context)); |
55 return directory.exists(); | 55 return directory.exists(); |
56 } | 56 } |
57 | 57 |
58 /** | 58 /** |
59 * Installs test files that are included in {@code path}. | 59 * Installs test files that are included in {@code path}. |
60 * @params context Application context | 60 * @params context Application context |
61 * @params path | 61 * @params path |
62 */ | 62 */ |
63 private static void install(Context context, String path) throws IOException
{ | 63 private static void install(Context context, String path) throws IOException
{ |
64 AssetManager assetManager = context.getAssets(); | 64 AssetManager assetManager = context.getAssets(); |
65 String files[] = assetManager.list(path); | 65 String files[] = assetManager.list(path); |
66 Log.i(TAG, "Loading " + path + " ..."); | 66 Log.i(TAG, "Loading " + path + " ..."); |
67 String root = PathUtils.getDataDirectory(context); | 67 String root = PathUtils.getDataDirectory(); |
68 if (files.length == 0) { | 68 if (files.length == 0) { |
69 // The path is a file, so copy the file now. | 69 // The path is a file, so copy the file now. |
70 copyTestFile(context, path, root + "/" + path); | 70 copyTestFile(context, path, root + "/" + path); |
71 } else { | 71 } else { |
72 // The path is a directory, so recursively handle its files, since | 72 // The path is a directory, so recursively handle its files, since |
73 // the directory can contain subdirectories. | 73 // the directory can contain subdirectories. |
74 String fullPath = root + "/" + path; | 74 String fullPath = root + "/" + path; |
75 File dir = new File(fullPath); | 75 File dir = new File(fullPath); |
76 if (!dir.exists()) { | 76 if (!dir.exists()) { |
77 Log.i(TAG, "Creating directory " + fullPath + " ..."); | 77 Log.i(TAG, "Creating directory " + fullPath + " ..."); |
(...skipping 19 matching lines...) Expand all Loading... |
97 File destFile = new File(destFilePath); | 97 File destFile = new File(destFilePath); |
98 if (destFile.exists()) { | 98 if (destFile.exists()) { |
99 throw new IllegalStateException(srcFilePath + " already exists."); | 99 throw new IllegalStateException(srcFilePath + " already exists."); |
100 } | 100 } |
101 | 101 |
102 if (!FileUtils.extractAsset(context, srcFilePath, destFile)) { | 102 if (!FileUtils.extractAsset(context, srcFilePath, destFile)) { |
103 throw new IOException("Could not extract asset " + srcFilePath + "."
); | 103 throw new IOException("Could not extract asset " + srcFilePath + "."
); |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
OLD | NEW |