mobile/android/tests/background/junit3/src/common/TestUtils.java

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:175291cbb05c
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 package org.mozilla.gecko.background.common;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.mozilla.gecko.background.helpers.AndroidSyncTestCase;
16 import org.mozilla.gecko.sync.Utils;
17
18 import android.os.Bundle;
19
20 public class TestUtils extends AndroidSyncTestCase {
21 protected static void assertStages(String[] all, String[] sync, String[] skip, String[] expected) {
22 final Set<String> sAll = new HashSet<String>();
23 for (String s : all) {
24 sAll.add(s);
25 }
26 List<String> sSync = null;
27 if (sync != null) {
28 sSync = new ArrayList<String>();
29 for (String s : sync) {
30 sSync.add(s);
31 }
32 }
33 List<String> sSkip = null;
34 if (skip != null) {
35 sSkip = new ArrayList<String>();
36 for (String s : skip) {
37 sSkip.add(s);
38 }
39 }
40 List<String> stages = new ArrayList<String>(Utils.getStagesToSync(sAll, sSync, sSkip));
41 Collections.sort(stages);
42 List<String> exp = new ArrayList<String>();
43 for (String e : expected) {
44 exp.add(e);
45 }
46 assertEquals(exp, stages);
47 }
48
49 public void testGetStagesToSync() {
50 final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" };
51 assertStages(all, null, null, all);
52 assertStages(all, new String[] { "sync1" }, null, new String[] { "sync1" });
53 assertStages(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" });
54 assertStages(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" });
55 }
56
57 protected static void assertStagesFromBundle(String[] all, String[] sync, String[] skip, String[] expected) {
58 final Set<String> sAll = new HashSet<String>();
59 for (String s : all) {
60 sAll.add(s);
61 }
62 final Bundle bundle = new Bundle();
63 Utils.putStageNamesToSync(bundle, sync, skip);
64
65 Collection<String> ss = Utils.getStagesToSyncFromBundle(sAll, bundle);
66 List<String> stages = new ArrayList<String>(ss);
67 Collections.sort(stages);
68 List<String> exp = new ArrayList<String>();
69 for (String e : expected) {
70 exp.add(e);
71 }
72 assertEquals(exp, stages);
73 }
74
75 public void testGetStagesToSyncFromBundle() {
76 final String[] all = new String[] { "other1", "other2", "skip1", "skip2", "sync1", "sync2" };
77 assertStagesFromBundle(all, null, null, all);
78 assertStagesFromBundle(all, new String[] { "sync1" }, null, new String[] { "sync1" });
79 assertStagesFromBundle(all, null, new String[] { "skip1", "skip2" }, new String[] { "other1", "other2", "sync1", "sync2" });
80 assertStagesFromBundle(all, new String[] { "sync1", "sync2" }, new String[] { "skip1", "skip2" }, new String[] { "sync1", "sync2" });
81 }
82
83 public static void deleteDirectoryRecursively(final File dir) throws IOException {
84 if (!dir.isDirectory()) {
85 throw new IllegalStateException("Given directory, " + dir + ", is not a directory!");
86 }
87
88 for (File f : dir.listFiles()) {
89 if (f.isDirectory()) {
90 deleteDirectoryRecursively(f);
91 } else if (!f.delete()) {
92 // Since this method is for testing, we assume we should be able to do this.
93 throw new IOException("Could not delete file, " + f.getAbsolutePath() + ". Permissions?");
94 }
95 }
96
97 if (!dir.delete()) {
98 throw new IOException("Could not delete dir, " + dir.getAbsolutePath() + ".");
99 }
100 }
101
102 public void testDeleteDirectoryRecursively() throws Exception {
103 final String TEST_DIR = getApplicationContext().getCacheDir().getAbsolutePath() +
104 "-testDeleteDirectory-" + System.currentTimeMillis();
105
106 // Non-existent directory.
107 final File nonexistent = new File("nonexistentDirectory"); // Hopefully. ;)
108 assertFalse(nonexistent.exists());
109 try {
110 deleteDirectoryRecursively(nonexistent);
111 fail("deleteDirectoryRecursively on a nonexistent directory should throw Exception");
112 } catch (IllegalStateException e) { }
113
114 // Empty dir.
115 File dir = mkdir(TEST_DIR);
116 deleteDirectoryRecursively(dir);
117 assertFalse(dir.exists());
118
119 // Filled dir.
120 dir = mkdir(TEST_DIR);
121 populateDir(dir);
122 deleteDirectoryRecursively(dir);
123 assertFalse(dir.exists());
124
125 // Filled dir with empty dir.
126 dir = mkdir(TEST_DIR);
127 populateDir(dir);
128 File subDir = new File(TEST_DIR + File.separator + "subDir");
129 assertTrue(subDir.mkdir());
130 deleteDirectoryRecursively(dir);
131 assertFalse(subDir.exists()); // For short-circuiting errors.
132 assertFalse(dir.exists());
133
134 // Filled dir with filled dir.
135 dir = mkdir(TEST_DIR);
136 populateDir(dir);
137 subDir = new File(TEST_DIR + File.separator + "subDir");
138 assertTrue(subDir.mkdir());
139 populateDir(subDir);
140 deleteDirectoryRecursively(dir);
141 assertFalse(subDir.exists()); // For short-circuiting errors.
142 assertFalse(dir.exists());
143 }
144
145 private File mkdir(final String name) {
146 final File dir = new File(name);
147 assertTrue(dir.mkdir());
148 return dir;
149 }
150
151 private void populateDir(final File dir) throws IOException {
152 assertTrue(dir.isDirectory());
153 final String dirPath = dir.getAbsolutePath();
154 for (int i = 0; i < 3; i++) {
155 final File f = new File(dirPath + File.separator + i);
156 assertTrue(f.createNewFile()); // Throws IOException if file could not be created.
157 }
158 }
159 }

mercurial