test_temporary_file_service.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-2008 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 // Temporary file service unit tests.
38 //
39 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 
46 function runTest() {
47  // Get the file protocol handler.
48  var ioService = Cc["@mozilla.org/network/io-service;1"]
49  .getService(Ci.nsIIOService);
50  var fileProtocolHandler = ioService.getProtocolHandler("file")
51  .QueryInterface(Ci.nsIFileProtocolHandler);
52 
53  // Test that the temporary file service component is available.
54  var temporaryFileService;
55  try {
56  temporaryFileService =
57  Cc["@songbirdnest.com/Songbird/TemporaryFileService;1"]
58  .getService(Ci.sbITemporaryFileService);
59  } catch (ex) {}
60  assertTrue(temporaryFileService,
61  "Temporary file service component is not available.");
62 
63  // Test existence of temporary file service root directory.
64  assertTrue(temporaryFileService.rootTemporaryDirectory,
65  "Temporary file service directory is null.");
66  assertTrue(temporaryFileService.rootTemporaryDirectory.exists(),
67  "Temporary file service directory does not exist.");
68 
69  // Create temporary file and test its existence.
70  var tmpFile1 = temporaryFileService.createFile(Ci.nsIFile.NORMAL_FILE_TYPE);
71  assertTrue(tmpFile1, "Could not create temporary file.");
72  assertTrue(tmpFile1.exists(), "Temporary file does not exist.");
73 
74  // Create another temporary file and test its uniqueness.
75  var tmpFile2 = temporaryFileService.createFile(Ci.nsIFile.NORMAL_FILE_TYPE);
76  assertTrue(tmpFile2, "Could not create temporary file.");
77  assertTrue(tmpFile2.exists(), "Temporary file does not exist.");
78  assertTrue(!tmpFile2.equals(tmpFile1), "Temporary file is not unique.");
79 
80  // Create and validate a temporary file with a specified base name.
81  var fileBaseName = "test";
82  tmpFile1 = temporaryFileService.createFile(Ci.nsIFile.NORMAL_FILE_TYPE,
83  fileBaseName);
84  assertTrue(tmpFile1, "Could not create temporary file.");
85  var fileURL = fileProtocolHandler.newFileURI(tmpFile1);
86  fileURL = fileURL.QueryInterface(Ci.nsIURL);
87  assertEqual(fileURL.fileBaseName.search(fileBaseName), 0);
88 
89  // Create and validate a temporary file with a specified extension.
90  tmpFile1 = temporaryFileService.createFile(Ci.nsIFile.NORMAL_FILE_TYPE,
91  null,
92  "tst");
93  assertTrue(tmpFile1, "Could not create temporary file.");
94  assertTrue(tmpFile1.path.match(/\.tst$/), "File extension incorrect.");
95 }
96 
const Cc
function assertTrue(aTest, aMessage)
var ioService
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Advanced DataRemote unit tests.
return null
Definition: FeedWriter.js:1143
const Ci