Commit 039ec1ad authored by jbudorick's avatar jbudorick Committed by Commit bot

[Android] Keep extracted secondary dex files when clearing data in tests.

BUG=716029

Review-Url: https://codereview.chromium.org/2854503002
Cr-Commit-Position: refs/heads/master@{#468356}
parent 81fb77eb
...@@ -2593,6 +2593,7 @@ if (is_android) { ...@@ -2593,6 +2593,7 @@ if (is_android) {
"//third_party/android_support_test_runner:exposed_instrumentation_api_publish_java", "//third_party/android_support_test_runner:exposed_instrumentation_api_publish_java",
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/android_tools:android_support_chromium_java", "//third_party/android_tools:android_support_chromium_java",
"//third_party/android_tools:android_support_v4_java",
"//third_party/hamcrest:hamcrest_core_java", "//third_party/hamcrest:hamcrest_core_java",
"//third_party/junit", "//third_party/junit",
] ]
......
...@@ -9,6 +9,7 @@ import android.content.ContextWrapper; ...@@ -9,6 +9,7 @@ import android.content.ContextWrapper;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import org.chromium.android.support.PackageManagerWrapper; import org.chromium.android.support.PackageManagerWrapper;
import org.chromium.base.Log; import org.chromium.base.Log;
...@@ -59,9 +60,8 @@ class BaseChromiumRunnerCommon { ...@@ -59,9 +60,8 @@ class BaseChromiumRunnerCommon {
try { try {
ApplicationInfo ai = super.getApplicationInfo(packageName, flags); ApplicationInfo ai = super.getApplicationInfo(packageName, flags);
if (packageName.equals(getPackageName())) { if (packageName.equals(getPackageName())) {
ApplicationInfo appAi = File dataDir = new File(
super.getApplicationInfo(mAppContext.getPackageName(), flags); ContextCompat.getCodeCacheDir(mAppContext), "test-multidex");
File dataDir = new File(appAi.dataDir, "test-multidex");
if (!dataDir.exists() && !dataDir.mkdirs()) { if (!dataDir.exists() && !dataDir.mkdirs()) {
throw new IOException(String.format( throw new IOException(String.format(
"Unable to create test multidex directory \"%s\"", "Unable to create test multidex directory \"%s\"",
......
...@@ -91,6 +91,7 @@ android_library("chrome_java_test_support") { ...@@ -91,6 +91,7 @@ android_library("chrome_java_test_support") {
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/android_tools:android_support_design_java", "//third_party/android_tools:android_support_design_java",
"//third_party/android_tools:android_support_transition_java", "//third_party/android_tools:android_support_transition_java",
"//third_party/android_tools:android_support_v4_java",
"//third_party/android_tools:android_support_v7_appcompat_java", "//third_party/android_tools:android_support_v7_appcompat_java",
"//third_party/android_tools:android_support_v7_recyclerview_java", "//third_party/android_tools:android_support_v7_recyclerview_java",
"//third_party/jsr-305:jsr_305_javalib", "//third_party/jsr-305:jsr_305_javalib",
......
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
package org.chromium.chrome.test.util; package org.chromium.chrome.test.util;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
...@@ -37,42 +40,35 @@ public final class ApplicationData { ...@@ -37,42 +40,35 @@ public final class ApplicationData {
* *
* @param targetContext the target Context. * @param targetContext the target Context.
*/ */
public static void clearAppData(Context targetContext) { public static void clearAppData(final Context targetContext) {
final String appDir = getAppDirFromTargetContext(targetContext);
CriteriaHelper.pollInstrumentationThread( CriteriaHelper.pollInstrumentationThread(
new Criteria() { new Criteria() {
private boolean mDataRemoved; private boolean mDataRemoved;
@SuppressLint("CommitPrefEdits")
@Override @Override
public boolean isSatisfied() { public boolean isSatisfied() {
if (!mDataRemoved && !removeAppData(appDir)) { SharedPreferences multidexPrefs =
targetContext.getSharedPreferences("multidex.version", 0);
if (!mDataRemoved && !removeAppData(targetContext)) {
return false; return false;
} }
mDataRemoved = true; mDataRemoved = true;
// We have to make sure the cache directory still exists, as the framework // We have to make sure the cache directory still exists, as the framework
// will try to create it otherwise and will fail for sandbox processes with // will try to create it otherwise and will fail for sandbox processes with
// a NullPointerException. // a NullPointerException.
File cacheDir = new File(appDir, "cache"); File cacheDir = new File(ContextCompat.getDataDir(targetContext), "cache");
// Removing app data cleared out all shared prefs. Multidex uses shared
// prefs to cache hashes of the secondary dexes it has extracted; without
// them, it'll attempt to reextract the dexes the next time the tests
// start up.
multidexPrefs.edit().commit();
return cacheDir.exists() || cacheDir.mkdir(); return cacheDir.exists() || cacheDir.mkdir();
} }
}, },
MAX_CLEAR_APP_DATA_TIMEOUT_MS, CLEAR_APP_DATA_POLL_INTERVAL_MS); MAX_CLEAR_APP_DATA_TIMEOUT_MS, CLEAR_APP_DATA_POLL_INTERVAL_MS);
} }
/**
* Find the absolute path of the application data directory for the given target context.
*
* When this is invoked from tests, the target context from the instrumentation must be used.
*
* @param targetContext the target Context.
*
* @return the absolute path of the application data directory.
*/
public static String getAppDirFromTargetContext(Context targetContext) {
String cacheDir = targetContext.getCacheDir().getAbsolutePath();
return cacheDir.substring(0, cacheDir.lastIndexOf('/'));
}
/** /**
* Remove all files and directories under the given application directory, except 'lib'. * Remove all files and directories under the given application directory, except 'lib'.
* *
...@@ -80,12 +76,14 @@ public final class ApplicationData { ...@@ -80,12 +76,14 @@ public final class ApplicationData {
* *
* @return whether removal succeeded. * @return whether removal succeeded.
*/ */
private static boolean removeAppData(String appDir) { private static boolean removeAppData(final Context targetContext) {
File[] files = new File(appDir).listFiles(); File dataDir = ContextCompat.getDataDir(targetContext);
File codeCacheDir = ContextCompat.getCodeCacheDir(targetContext);
File[] files = dataDir.listFiles();
if (files == null) return true; if (files == null) return true;
for (File file : files) { for (File file : files) {
if (!(file.getName().equals("lib") if (!(file.getName().equals("lib") || file.getName().equals("incremental-install-files")
|| file.getName().equals("incremental-install-files")) || file.getName().equals(codeCacheDir.getName()))
&& !removeFile(file)) { && !removeFile(file)) {
return false; return false;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment