This source file includes following definitions.
- clearAppData
- getAppDirFromTargetContext
- removeAppData
- removeFile
package org.chromium.chrome.test.util;
import android.content.Context;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import java.io.File;
import java.util.concurrent.TimeUnit;
public final class ApplicationData {
private static final long MAX_CLEAR_APP_DATA_TIMEOUT_MS =
scaleTimeout(TimeUnit.SECONDS.toMillis(3));
private static final long CLEAR_APP_DATA_POLL_INTERVAL_MS = MAX_CLEAR_APP_DATA_TIMEOUT_MS / 10;
private ApplicationData() {}
public static boolean clearAppData(Context targetContext) throws InterruptedException {
final String appDir = getAppDirFromTargetContext(targetContext);
return CriteriaHelper.pollForCriteria(
new Criteria() {
private boolean mDataRemoved = false;
@Override
public boolean isSatisfied() {
if (!mDataRemoved && !removeAppData(appDir)) {
return false;
}
mDataRemoved = true;
File cacheDir = new File(appDir, "cache");
return cacheDir.exists() || cacheDir.mkdir();
}
},
MAX_CLEAR_APP_DATA_TIMEOUT_MS, CLEAR_APP_DATA_POLL_INTERVAL_MS);
}
public static String getAppDirFromTargetContext(Context targetContext) {
String cacheDir = targetContext.getCacheDir().getAbsolutePath();
return cacheDir.substring(0, cacheDir.lastIndexOf('/'));
}
private static boolean removeAppData(String appDir) {
File[] files = new File(appDir).listFiles();
if (files == null)
return true;
for (File file : files) {
if (!file.getAbsolutePath().endsWith("/lib") && !removeFile(file))
return false;
}
return true;
}
private static boolean removeFile(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null)
return true;
for (File sub_file : files) {
if (!removeFile(sub_file))
return false;
}
}
return file.delete();
}
}