Bug 902560: FakeProfileTestCase deletes testing directory if it exists. r=nalexander

This commit is contained in:
Michael Comella 2013-10-10 16:44:22 -07:00
Родитель c5472b3652
Коммит e2fb6cbcd3
2 изменённых файлов: 85 добавлений и 5 удалений

Просмотреть файл

@ -3,6 +3,8 @@
package org.mozilla.gecko.background.common;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -77,4 +79,81 @@ public class TestUtils extends AndroidSyncTestCase {
assertStagesFromBundle(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" });
assertStagesFromBundle(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" });
}
public static void deleteDirectoryRecursively(final File dir) throws IOException {
if (!dir.isDirectory()) {
throw new IllegalStateException("Given directory, " + dir + ", is not a directory!");
}
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
deleteDirectoryRecursively(f);
} else if (!f.delete()) {
// Since this method is for testing, we assume we should be able to do this.
throw new IOException("Could not delete file, " + f.getAbsolutePath() + ". Permissions?");
}
}
if (!dir.delete()) {
throw new IOException("Could not delete dir, " + dir.getAbsolutePath() + ".");
}
}
public void testDeleteDirectory() throws Exception {
final String TEST_DIR = getApplicationContext().getCacheDir().getAbsolutePath() +
"-testDeleteDirectory-" + System.currentTimeMillis();
// Non-existent directory.
final File nonexistent = new File("nonexistentDirectory"); // Hopefully. ;)
assertFalse(nonexistent.exists());
try {
deleteDirectoryRecursively(nonexistent);
fail("deleteDirectoryRecursively on a nonexistent directory should throw Exception");
} catch (IllegalStateException e) { }
// Empty dir.
File dir = mkdir(TEST_DIR);
deleteDirectoryRecursively(dir);
assertFalse(dir.exists());
// Filled dir.
dir = mkdir(TEST_DIR);
populateDir(dir);
deleteDirectoryRecursively(dir);
assertFalse(dir.exists());
// Filled dir with empty dir.
dir = mkdir(TEST_DIR);
populateDir(dir);
File subDir = new File(TEST_DIR + File.separator + "subDir");
assertTrue(subDir.mkdir());
deleteDirectoryRecursively(dir);
assertFalse(subDir.exists()); // For short-circuiting errors.
assertFalse(dir.exists());
// Filled dir with filled dir.
dir = mkdir(TEST_DIR);
populateDir(dir);
subDir = new File(TEST_DIR + File.separator + "subDir");
assertTrue(subDir.mkdir());
populateDir(subDir);
deleteDirectoryRecursively(dir);
assertFalse(subDir.exists()); // For short-circuiting errors.
assertFalse(dir.exists());
}
private File mkdir(final String name) {
final File dir = new File(name);
assertTrue(dir.mkdir());
return dir;
}
private void populateDir(final File dir) throws IOException {
assertTrue(dir.isDirectory());
final String dirPath = dir.getAbsolutePath();
for (int i = 0; i < 3; i++) {
final File f = new File(dirPath + File.separator + i);
assertTrue(f.createNewFile()); // Throws IOException if file could not be created.
}
}
}

Просмотреть файл

@ -9,6 +9,8 @@ import android.app.Activity;
import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
import org.mozilla.gecko.background.common.TestUtils;
public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCase2<Activity> {
protected Context context;
@ -26,6 +28,9 @@ public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCas
context = getInstrumentation().getTargetContext();
File cache = context.getCacheDir();
fakeProfileDirectory = new File(cache.getAbsolutePath() + getCacheSuffix());
if (fakeProfileDirectory.exists()) {
TestUtils.deleteDirectoryRecursively(fakeProfileDirectory);
}
if (!fakeProfileDirectory.mkdir()) {
throw new IllegalStateException("Could not create temporary directory.");
}
@ -33,11 +38,7 @@ public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCas
@Override
protected void tearDown() throws Exception {
// We don't check return values.
for (File child : fakeProfileDirectory.listFiles()) {
child.delete();
}
fakeProfileDirectory.delete();
TestUtils.deleteDirectoryRecursively(fakeProfileDirectory);
super.tearDown();
}
}