1 // Copyright 2013 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.chrome.test.util;
7 import android.content.Context;
9 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
11 import org.chromium.content.browser.test.util.Criteria;
12 import org.chromium.content.browser.test.util.CriteriaHelper;
15 import java.util.concurrent.TimeUnit;
18 * A utility class to do operations on the application data directory, such as clearing the
21 public final class ApplicationData {
23 private static final long MAX_CLEAR_APP_DATA_TIMEOUT_MS =
24 scaleTimeout(TimeUnit.SECONDS.toMillis(3));
25 private static final long CLEAR_APP_DATA_POLL_INTERVAL_MS = MAX_CLEAR_APP_DATA_TIMEOUT_MS / 10;
27 private ApplicationData() {}
30 * Clear all files in target contexts application directory, except the directory 'lib'.
31 * The timeout is specified in |MAX_CLEAR_APP_DATA_TIMEOUT_MS|.
33 * When deleting all files is complete, the 'cache' directory is recreated, to ensure that the
34 * framework does not try to create it for sandbox processes and fail.
36 * When this is invoked from tests, the target context from the instrumentation must be used.
38 * @param targetContext the target Context.
40 * @return Whether clearing the application data was successful.
42 public static boolean clearAppData(Context targetContext) throws InterruptedException {
43 final String appDir = getAppDirFromTargetContext(targetContext);
44 return CriteriaHelper.pollForCriteria(
46 private boolean mDataRemoved = false;
49 public boolean isSatisfied() {
50 if (!mDataRemoved && !removeAppData(appDir)) {
54 // We have to make sure the cache directory still exists, as the framework
55 // will try to create it otherwise and will fail for sandbox processes with
56 // a NullPointerException.
57 File cacheDir = new File(appDir, "cache");
58 return cacheDir.exists() || cacheDir.mkdir();
61 MAX_CLEAR_APP_DATA_TIMEOUT_MS, CLEAR_APP_DATA_POLL_INTERVAL_MS);
65 * Find the absolute path of the application data directory for the given target context.
67 * When this is invoked from tests, the target context from the instrumentation must be used.
69 * @param targetContext the target Context.
71 * @return the absolute path of the application data directory.
73 public static String getAppDirFromTargetContext(Context targetContext) {
74 String cacheDir = targetContext.getCacheDir().getAbsolutePath();
75 return cacheDir.substring(0, cacheDir.lastIndexOf('/'));
79 * Remove all files and directories under the given application directory, except 'lib'.
81 * @param appDir the application directory to remove.
83 * @return whether removal succeeded.
85 private static boolean removeAppData(String appDir) {
86 File[] files = new File(appDir).listFiles();
89 for (File file : files) {
90 if (!file.getAbsolutePath().endsWith("/lib") && !removeFile(file))
97 * Remove the given file or directory.
99 * @param file the file or directory to remove.
101 * @return whether removal succeeded.
103 private static boolean removeFile(File file) {
104 if (file.isDirectory()) {
105 File[] files = file.listFiles();
108 for (File sub_file : files) {
109 if (!removeFile(sub_file))
113 return file.delete();