1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/common/TestUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.common; 1.8 + 1.9 +import java.io.File; 1.10 +import java.io.IOException; 1.11 +import java.util.ArrayList; 1.12 +import java.util.Collection; 1.13 +import java.util.Collections; 1.14 +import java.util.HashSet; 1.15 +import java.util.List; 1.16 +import java.util.Set; 1.17 + 1.18 +import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; 1.19 +import org.mozilla.gecko.sync.Utils; 1.20 + 1.21 +import android.os.Bundle; 1.22 + 1.23 +public class TestUtils extends AndroidSyncTestCase { 1.24 + protected static void assertStages(String[] all, String[] sync, String[] skip, String[] expected) { 1.25 + final Set<String> sAll = new HashSet<String>(); 1.26 + for (String s : all) { 1.27 + sAll.add(s); 1.28 + } 1.29 + List<String> sSync = null; 1.30 + if (sync != null) { 1.31 + sSync = new ArrayList<String>(); 1.32 + for (String s : sync) { 1.33 + sSync.add(s); 1.34 + } 1.35 + } 1.36 + List<String> sSkip = null; 1.37 + if (skip != null) { 1.38 + sSkip = new ArrayList<String>(); 1.39 + for (String s : skip) { 1.40 + sSkip.add(s); 1.41 + } 1.42 + } 1.43 + List<String> stages = new ArrayList<String>(Utils.getStagesToSync(sAll, sSync, sSkip)); 1.44 + Collections.sort(stages); 1.45 + List<String> exp = new ArrayList<String>(); 1.46 + for (String e : expected) { 1.47 + exp.add(e); 1.48 + } 1.49 + assertEquals(exp, stages); 1.50 + } 1.51 + 1.52 + public void testGetStagesToSync() { 1.53 + final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" }; 1.54 + assertStages(all, null, null, all); 1.55 + assertStages(all, new String[] { "sync1" }, null, new String[] { "sync1" }); 1.56 + assertStages(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" }); 1.57 + assertStages(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" }); 1.58 + } 1.59 + 1.60 + protected static void assertStagesFromBundle(String[] all, String[] sync, String[] skip, String[] expected) { 1.61 + final Set<String> sAll = new HashSet<String>(); 1.62 + for (String s : all) { 1.63 + sAll.add(s); 1.64 + } 1.65 + final Bundle bundle = new Bundle(); 1.66 + Utils.putStageNamesToSync(bundle, sync, skip); 1.67 + 1.68 + Collection<String> ss = Utils.getStagesToSyncFromBundle(sAll, bundle); 1.69 + List<String> stages = new ArrayList<String>(ss); 1.70 + Collections.sort(stages); 1.71 + List<String> exp = new ArrayList<String>(); 1.72 + for (String e : expected) { 1.73 + exp.add(e); 1.74 + } 1.75 + assertEquals(exp, stages); 1.76 + } 1.77 + 1.78 + public void testGetStagesToSyncFromBundle() { 1.79 + final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" }; 1.80 + assertStagesFromBundle(all, null, null, all); 1.81 + assertStagesFromBundle(all, new String[] { "sync1" }, null, new String[] { "sync1" }); 1.82 + assertStagesFromBundle(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" }); 1.83 + assertStagesFromBundle(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" }); 1.84 + } 1.85 + 1.86 + public static void deleteDirectoryRecursively(final File dir) throws IOException { 1.87 + if (!dir.isDirectory()) { 1.88 + throw new IllegalStateException("Given directory, " + dir + ", is not a directory!"); 1.89 + } 1.90 + 1.91 + for (File f : dir.listFiles()) { 1.92 + if (f.isDirectory()) { 1.93 + deleteDirectoryRecursively(f); 1.94 + } else if (!f.delete()) { 1.95 + // Since this method is for testing, we assume we should be able to do this. 1.96 + throw new IOException("Could not delete file, " + f.getAbsolutePath() + ". Permissions?"); 1.97 + } 1.98 + } 1.99 + 1.100 + if (!dir.delete()) { 1.101 + throw new IOException("Could not delete dir, " + dir.getAbsolutePath() + "."); 1.102 + } 1.103 + } 1.104 + 1.105 + public void testDeleteDirectoryRecursively() throws Exception { 1.106 + final String TEST_DIR = getApplicationContext().getCacheDir().getAbsolutePath() + 1.107 + "-testDeleteDirectory-" + System.currentTimeMillis(); 1.108 + 1.109 + // Non-existent directory. 1.110 + final File nonexistent = new File("nonexistentDirectory"); // Hopefully. ;) 1.111 + assertFalse(nonexistent.exists()); 1.112 + try { 1.113 + deleteDirectoryRecursively(nonexistent); 1.114 + fail("deleteDirectoryRecursively on a nonexistent directory should throw Exception"); 1.115 + } catch (IllegalStateException e) { } 1.116 + 1.117 + // Empty dir. 1.118 + File dir = mkdir(TEST_DIR); 1.119 + deleteDirectoryRecursively(dir); 1.120 + assertFalse(dir.exists()); 1.121 + 1.122 + // Filled dir. 1.123 + dir = mkdir(TEST_DIR); 1.124 + populateDir(dir); 1.125 + deleteDirectoryRecursively(dir); 1.126 + assertFalse(dir.exists()); 1.127 + 1.128 + // Filled dir with empty dir. 1.129 + dir = mkdir(TEST_DIR); 1.130 + populateDir(dir); 1.131 + File subDir = new File(TEST_DIR + File.separator + "subDir"); 1.132 + assertTrue(subDir.mkdir()); 1.133 + deleteDirectoryRecursively(dir); 1.134 + assertFalse(subDir.exists()); // For short-circuiting errors. 1.135 + assertFalse(dir.exists()); 1.136 + 1.137 + // Filled dir with filled dir. 1.138 + dir = mkdir(TEST_DIR); 1.139 + populateDir(dir); 1.140 + subDir = new File(TEST_DIR + File.separator + "subDir"); 1.141 + assertTrue(subDir.mkdir()); 1.142 + populateDir(subDir); 1.143 + deleteDirectoryRecursively(dir); 1.144 + assertFalse(subDir.exists()); // For short-circuiting errors. 1.145 + assertFalse(dir.exists()); 1.146 + } 1.147 + 1.148 + private File mkdir(final String name) { 1.149 + final File dir = new File(name); 1.150 + assertTrue(dir.mkdir()); 1.151 + return dir; 1.152 + } 1.153 + 1.154 + private void populateDir(final File dir) throws IOException { 1.155 + assertTrue(dir.isDirectory()); 1.156 + final String dirPath = dir.getAbsolutePath(); 1.157 + for (int i = 0; i < 3; i++) { 1.158 + final File f = new File(dirPath + File.separator + i); 1.159 + assertTrue(f.createNewFile()); // Throws IOException if file could not be created. 1.160 + } 1.161 + } 1.162 +}