test_moverename.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
28 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
29 Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
30 
31 // This test relies on a tree of sample files
32 var gTestFiles = newAppRelativeFile("testharness/watchfolders/files/moverename");
33 
38 function runTest () {
39 
40  // Start with an empty library
41  var library = LibraryUtils.mainLibrary;
42  library.clear();
43  // The library wont actually be empty, since
44  // the download list and stuff like that come back
45  var originalLibraryLength = library.length;
46 
47  // Start watching the temp folder, since we are going to make
48  // changes there
50  sleep(15000);
51 
52  // Get the test files set up
53  var testFolder = getCopyOfFolder(gTestFiles, "_temp_moverename_files");
54  log('using test folder ' + testFolder.path);
55 
56  // That should cause watch folders to add the files...
57  sleep(15000);
58 
59  // Mark each item with its original url, so we
60  // can tell they update correctly
61  library.enumerateAllItems({
62  onEnumerationBegin: function(list) {
63  return Ci.sbIMediaListEnumerationListener.CONTINUE;
64  },
65  onEnumeratedItem: function(list, item) {
66  dump("Found media item:" + item.contentSrc.spec + "\n");
67  // Remember the old path so we can verify things changed ok
68  item.setProperty(SBProperties.originURL, item.contentSrc.spec);
69  return Ci.sbIMediaListEnumerationListener.CONTINUE;
70  },
71  onEnumerationEnd: function(list, status) {
72  }
73  });
74 
75  assertEqual(library.length, originalLibraryLength + 5);
76 
77  // Here's what we expect to happen...
78  var root = newFileURI(testFolder).spec.toLowerCase();
79  var map = {};
80  map[root + "individualmovedfile.mp3"] = root + "dir3/individualmovedfile.mp3";
81  map[root + "renamedfile.mp3"] = root + "renamedfile2.mp3";
82  map[root + "dir1/dir2/movedfile1.mp3"] = root + "dir3/dir1/dir2/movedfile1.mp3";
83  map[root + "dir1/dir2/movedfile2.mp3"] = root + "dir3/dir1/dir2/movedfile2.mp3";
84  map[root + "deletedfile.mp3"] = null;
85  map["null"] = root + "dir3/newfile.mp3";
86 
87  var dir1 = testFolder.clone();
88  dir1.append("dir1");
89  var dir3 = testFolder.clone();
90  dir3.append("dir3");
91  var individualMovedFile = testFolder.clone();
92  individualMovedFile.append("individualMovedFile.mp3");
93  var renamedFile = testFolder.clone();
94  renamedFile.append("renamedFile.mp3");
95  var deletedFile = testFolder.clone();
96  deletedFile.append("deletedFile.mp3");
97  var newFile = dir3.clone();
98  newFile.append("random.txt");
99 
100  // Perform the move/renames
101  dir1.moveTo(dir3, null);
102  individualMovedFile.moveTo(dir3, null);
103  renamedFile.moveTo(renamedFile.parent, "renamedFile2.mp3");
104  deletedFile.remove(false);
105  newFile.copyTo(newFile.parent, "newFile.mp3");
106 
107  // Wait for watchfolders to kick in...
108  sleep(15000);
109 
110  // Now verify that the original media items still exist,
111  // and that the contentSrcs have been updated as expected
112  var count = 0;
113  library.enumerateAllItems({
114  onEnumerationBegin: function(list) {
115  return Ci.sbIMediaListEnumerationListener.CONTINUE;
116  },
117  onEnumeratedItem: function(list, item) {
118  var newSpec = item.getProperty(SBProperties.contentURL).toLowerCase();
119  var oldSpec = item.getProperty(SBProperties.originURL);
120  if (oldSpec) oldSpec = oldSpec.toLowerCase()
121  dump("Change: '" + oldSpec + "' -> '" + newSpec + "'\n");
122 
123  if (oldSpec in map) {
124  if (newSpec == map[oldSpec]) {
125  count++;
126  } else {
127  dump("FAIL: Expected '" + map[oldSpec] +
128  "' but got '" + newSpec + "'\n");
129  }
130  delete map[oldSpec]; // Delete, so we can't double count
131  }
132  return Ci.sbIMediaListEnumerationListener.CONTINUE;
133  },
134  onEnumerationEnd: function(list, status) {
135  }
136  });
137  assertEqual(count, 5);
138 
139  // Shut down watch folders
141  library.clear();
142 }
function sleep(ms, suppressOutput)
function newAppRelativeFile(path)
var testFolder
function log(s)
function setWatchFolder(file)
var gTestFiles
function assertEqual(aExpected, aActual, aMessage)
var count
Definition: test_bug7406.js:32
function newFileURI(file)
return null
Definition: FeedWriter.js:1143
function getTempFolder()
const Ci
Javascript wrappers for common library tasks.
function getCopyOfFolder(folder, tempName)
Copy the given folder to tempName, returning an nsIFile for the new location.
function runTest()
Advanced DataRemote unit tests.