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
3 // found in the LICENSE file.
5 package org
.chromium
.net
;
7 import android
.content
.Context
;
8 import android
.content
.res
.AssetManager
;
9 import android
.util
.Log
;
11 import org
.chromium
.base
.PathUtils
;
12 import org
.chromium
.base
.annotations
.SuppressFBWarnings
;
15 import java
.io
.FileOutputStream
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
18 import java
.io
.OutputStream
;
21 * Helper class to install test files.
23 public final class TestFilesInstaller
{
24 private static final String TAG
= "TestFilesInstaller";
25 // Name of the asset directory in which test files are stored.
26 private static final String TEST_FILE_ASSET_PATH
= "test";
29 * Installs test files if files have not been installed.
31 public static void installIfNeeded(Context context
) {
32 if (areFilesInstalled(context
)) {
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
);
44 * Returns the installed path of the test files.
46 public static String
getInstalledPath(Context context
) {
47 return PathUtils
.getDataDirectory(context
) + "/" + TEST_FILE_ASSET_PATH
;
51 * Returns whether test files are installed.
53 public static boolean areFilesInstalled(Context context
) {
54 // Checking for file directory is fine even when new files are added,
55 // because the app will be re-installed and app data will be cleared.
56 File directory
= new File(getInstalledPath(context
));
57 return directory
.exists();
61 * Installs test files that are included in {@code path}.
62 * @params context Application context
65 private static void install(Context context
, String path
) throws IOException
{
66 AssetManager assetManager
= context
.getAssets();
67 String files
[] = assetManager
.list(path
);
68 Log
.i(TAG
, "Loading " + path
+ " ...");
69 String root
= PathUtils
.getDataDirectory(context
);
70 if (files
.length
== 0) {
71 // The path is a file, so copy the file now.
72 copyTestFile(assetManager
, path
, root
+ "/" + path
);
74 // The path is a directory, so recursively handle its files, since
75 // the directory can contain subdirectories.
76 String fullPath
= root
+ "/" + path
;
77 File dir
= new File(fullPath
);
79 Log
.i(TAG
, "Creating directory " + fullPath
+ " ...");
81 throw new IOException("Directory not created.");
84 for (int i
= 0; i
< files
.length
; i
++) {
85 install(context
, path
+ "/" + files
[i
]);
90 * Copies a file from assets to the device's file system.
91 * @param assetManager AssetManager of the application.
92 * @param srcFilePath the source file path in assets.
93 * @param destFilePath the destination file path.
94 * @throws IllegalStateException if the destination file already exists.
96 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE")
97 private static void copyTestFile(AssetManager assetManager
,
99 String destFilePath
) throws IOException
{
100 File destFile
= new File(destFilePath
);
101 if (destFile
.exists()) {
102 throw new IllegalStateException(srcFilePath
+ " already exists");
104 OutputStream out
= new FileOutputStream(destFilePath
);
105 InputStream in
= assetManager
.open(srcFilePath
);
107 byte[] buffer
= new byte[1024];
109 while ((read
= in
.read(buffer
)) != -1) {
110 out
.write(buffer
, 0, read
);