test_directory_enumerator.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2009 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27  */
28 
34 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 //
37 // Directory enumerator unit tests.
38 //
39 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 
46 function runTest() {
47  // Start running the tests.
49 }
50 
51 
57  //
58  // Directory enumerator tests configuration.
59  //
60  // testDir Test directory structure to enumerate.
61  // object Specifies a directory with the name of the field.
62  // null Specifies a file with the name of the field.
63  // enumerationTestList List of enumeration tests.
64  // name Name of test.
65  // enumConfig List of enumeration attribute names and values.
66  // expectedResult Expected result of the enumeration.
67  //
68 
69  testDir:
70  {
71  test1:
72  {
73  test11: null,
74  test12: null,
75  test13: null
76  },
77  test2:
78  {
79  test21:
80  {
81  test211: null,
82  test212: null
83  }
84  },
85  test3:
86  {
87  test31:
88  {
89  test311: null,
90  test312: null
91  },
92  test32:
93  {
94  test321:
95  {
96  test3211: null
97  }
98  }
99  },
100  test4: {}
101  },
102 
103  enumerationTestList:
104  [
105  // Test default settings.
106  {
107  name: "default",
108  enumConfig: {},
109  expectedResult:
110  [
111  "test1", "test11", "test12", "test13",
112  "test2", "test21", "test211", "test212",
113  "test3", "test31", "test311", "test312",
114  "test32", "test321", "test3211",
115  "test4"
116  ]
117  },
118 
119  // Test max depth setting.
120  {
121  name: "max depth",
122  enumConfig: { maxDepth: 2 },
123  expectedResult: [ "test1", "test11", "test12", "test13",
124  "test2", "test21",
125  "test3", "test31", "test32",
126  "test4" ]
127  },
128 
129  // Test files only setting.
130  {
131  name: "files only",
132  enumConfig: { filesOnly: true },
133  expectedResult: [ "test11", "test12", "test13",
134  "test211", "test212",
135  "test311", "test312", "test3211" ]
136  },
137 
138  // Test directories only setting.
139  {
140  name: "directories only",
141  enumConfig: { directoriesOnly: true },
142  expectedResult: [ "test1", "test2", "test21",
143  "test3", "test31", "test32", "test321",
144  "test4" ]
145  }
146  ],
147 
148 
149  //
150  // Directory enumerator tests fields.
151  //
152  // _testList List of tests to run.
153  // _nextTestIndex Index of next test to run.
154  // _testPendingIndicated True if a pending test has been indicated.
155  // _allTestsComplete True if all tests have completed.
156  //
157 
158  _testList: null,
159  _nextTestIndex: 0,
160  _testPendingIndicated: false,
161  _allTestsComplete: false,
162 
163 
168  start: function testDirectoryEnumerator_start() {
169  var _this = this;
170  var func;
171 
172  // Initialize the list of tests.
173  this._testList = [];
174  func = function() { return _this._testExistence(); };
175  this._testList.push(func);
176  for (var i = 0; i < this.enumerationTestList.length; i++) {
177  // Create a function closure for the enumeration test. The "let" keyword
178  // must be used instead of "var" so that the enumerationTest scope is
179  // limited to each iteration of the loop and each function has a different
180  // value for the enumeration test.
181  let enumerationTest = this.enumerationTestList[i];
182  func = function() { return _this._testEnumeration(enumerationTest); };
183 
184  // Add the enumeration test to the test list.
185  this._testList.push(func);
186  }
187 
188  // Start running the tests.
189  this._nextTestIndex = 0;
190  this._runNextTest();
191  },
192 
193 
198  _runNextTest: function testDirectoryEnumerator__runNextTest() {
199  // Run tests until complete or test is pending.
200  while (1) {
201  // Finish test if no more tests to run.
202  if (this._nextTestIndex >= this._testList.length) {
203  this._allTestsComplete = true;
204  if (this._testPendingIndicated) {
205  testFinished();
206  }
207  break;
208  }
209 
210  // Run the next test. Indicate if tests are pending.
211  if (!this._testList[this._nextTestIndex++]()) {
212  // If not all tests have completed and a pending test has not been
213  // indicated, indicate a pending test.
214  if (!this._allTestsComplete && !this._testPendingIndicated) {
215  this._testPendingIndicated = true;
216  testPending();
217  }
218  break;
219  }
220  }
221  },
222 
223 
228  _testExistence: function testDirectoryEnumerator__testExistence() {
229  // Log progress.
230  dump("Running existence test.");
231 
232  // Test that the directory enumerator component is available.
233  var directoryEnumerator;
234  try {
235  directoryEnumerator =
236  Cc["@songbirdnest.com/Songbird/DirectoryEnumerator;1"]
237  .createInstance(Ci.sbIDirectoryEnumerator);
238  } catch (ex) {}
239  assertTrue(directoryEnumerator,
240  "Directory enumerator component is not available.");
241 
242  // Test is complete.
243  return true;
244  },
245 
246 
253  _testEnumeration:
254  function testDirectoryEnumerator__testEnumeration(aTestConfig) {
255  // Log progress.
256  dump("Running enumeration test " + aTestConfig.name + "\n");
257 
258  // Create the test directory.
259  var testDir = this._createTestDir(this.testDir);
260 
261  // Create a directory enumerator.
262  var directoryEnumerator =
263  Cc["@songbirdnest.com/Songbird/DirectoryEnumerator;1"]
264  .createInstance(Ci.sbIDirectoryEnumerator);
265 
266  // Configure the directory enumerator.
267  var enumConfig = aTestConfig.enumConfig;
268  for (var config in enumConfig) {
269  directoryEnumerator[config] = enumConfig[config];
270  }
271 
272  // Enumerate the test directory and check results.
273  var result = [];
274  directoryEnumerator.enumerate(testDir);
275  while (directoryEnumerator.hasMoreElements()) {
276  result.push(directoryEnumerator.getNext().leafName);
277  }
278  dump("Result: " + result + "\n");
279  assertSetsEqual(result, aTestConfig.expectedResult);
280 
281  // Delete the test directory.
282  testDir.remove(true);
283 
284  // Test is complete.
285  return true;
286  },
287 
288 
298  _createTestDir:
299  function testDirectoryEnumerator__createTestDir(aTestDirStruct) {
300  // Create a temporary test directory.
301  var temporaryFileService =
302  Cc["@songbirdnest.com/Songbird/TemporaryFileService;1"]
303  .getService(Ci.sbITemporaryFileService);
304  var testDir = temporaryFileService.createFile(Ci.nsIFile.DIRECTORY_TYPE);
305 
306  // Fill test directory with specified structure.
307  this._addTestSubDir(aTestDirStruct, testDir);
308 
309  return testDir;
310  },
311 
312  _addTestSubDir:
313  function testDirectoryEnumerator__createTestSubdir(aTestDirStruct,
314  aTestDir) {
315  // Fill test directory with specified structure.
316  for (var name in aTestDirStruct) {
317  // Get the next entry in the structure.
318  var entry = aTestDirStruct[name];
319 
320  // Add the next file or directory. A null entry specifies a file with the
321  // same name as the directory structure object field.
322  if (entry) {
323  // Create and fill in a test directory.
324  var dir = aTestDir.clone();
325  dir.append(name);
326  dir.create(Ci.nsIFile.DIRECTORY_TYPE, aTestDir.permissions);
327  this._addTestSubDir(entry, dir);
328  } else {
329  // Create a test file.
330  var file = aTestDir.clone();
331  file.append(name);
332  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, aTestDir.permissions);
333  }
334  }
335  }
336 };
337 
338 
function start(ch)
classDescription entry
Definition: FeedWriter.js:1427
const Cc
function test4(win)
function test2()
function testFinished()
function assertTrue(aTest, aMessage)
function assertSetsEqual(s1, s2)
var testDirectoryEnumerator
function test1()
function test3()
function runTest()
Advanced DataRemote unit tests.
var _this
return null
Definition: FeedWriter.js:1143
const Ci
_getSelectedPageStyle s i
var file
function testPending()