test_temporary_file_factory.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  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 //
35 // Temporary file factory unit tests.
36 //
37 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 
44 function runTest() {
45  // Get the file protocol handler.
46  var ioService = Cc["@mozilla.org/network/io-service;1"]
47  .getService(Ci.nsIIOService);
48  var fileProtocolHandler = ioService.getProtocolHandler("file")
49  .QueryInterface(Ci.nsIFileProtocolHandler);
50 
51  // Test that the temporary file factory component is available.
52  var temporaryFileFactory;
53  try {
54  temporaryFileFactory =
55  Cc["@songbirdnest.com/Songbird/TemporaryFileFactory;1"]
56  .getService(Ci.sbITemporaryFileFactory);
57  } catch (ex) {}
58  assertTrue(temporaryFileFactory,
59  "Temporary file factory component is not available.");
60 
61  // Test existence of temporary file factory root directory.
62  var rootTemporaryDirectory = temporaryFileFactory.rootTemporaryDirectory;
63  assertTrue(rootTemporaryDirectory,
64  "Temporary file factory directory is null.");
65  assertTrue(rootTemporaryDirectory.exists(),
66  "Temporary file factory directory does not exist.");
67 
68  // Create temporary file and test its existence.
69  var tmpFile1 = temporaryFileFactory.createFile(Ci.nsIFile.NORMAL_FILE_TYPE);
70  assertTrue(tmpFile1, "Could not create temporary file.");
71  assertTrue(tmpFile1.exists(), "Temporary file does not exist.");
72 
73  // Create another temporary file and test its uniqueness.
74  var tmpFile2 = temporaryFileFactory.createFile(Ci.nsIFile.NORMAL_FILE_TYPE);
75  assertTrue(tmpFile2, "Could not create temporary file.");
76  assertTrue(tmpFile2.exists(), "Temporary file does not exist.");
77  assertTrue(!tmpFile2.equals(tmpFile1), "Temporary file is not unique.");
78 
79  // Create and validate a temporary file with a specified base name.
80  var fileBaseName = "test";
81  tmpFile1 = temporaryFileFactory.createFile(Ci.nsIFile.NORMAL_FILE_TYPE,
82  fileBaseName);
83  assertTrue(tmpFile1, "Could not create temporary file.");
84  var fileURL = fileProtocolHandler.newFileURI(tmpFile1);
85  fileURL = fileURL.QueryInterface(Ci.nsIURL);
86  assertEqual(fileURL.fileBaseName.search(fileBaseName), 0);
87 
88  // Create and validate a temporary file with a specified extension.
89  tmpFile1 = temporaryFileFactory.createFile(Ci.nsIFile.NORMAL_FILE_TYPE,
90  null,
91  "tst");
92  assertTrue(tmpFile1, "Could not create temporary file.");
93  assertTrue(tmpFile1.path.match(/\.tst$/), "File extension incorrect.");
94 
95  // Clear the temporary files and test that the root temporary directory no
96  // longer exists.
97  temporaryFileFactory.clear();
98  assertTrue(!rootTemporaryDirectory.exists(),
99  "Failed to clear temporary files.");
100 }
101 
const Cc
function assertTrue(aTest, aMessage)
var ioService
function assertEqual(aExpected, aActual, aMessage)
return null
Definition: FeedWriter.js:1143
const Ci
function runTest()
Advanced DataRemote unit tests.