test_diffing_medialists.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 
30 var SB_NS = "http://songbirdnest.com/data/1.0#";
31 
32 function createPropertyArray() {
33  return Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
34  .createInstance(Ci.sbIMutablePropertyArray);
35 }
36 
37 function appendPropertiesToArray(aProperties, aPropertyArray) {
38  for (var propName in aProperties) {
39  aPropertyArray.appendProperty(SB_NS + propName, aProperties[propName]);
40  }
41  return;
42 }
43 
44 function makeItACopy(source, destination)
45 {
46  destination.setProperty(SBProperties.originItemGuid, source.guid);
47  destination.setProperty(SBProperties.originLibraryGuid, source.library.guid);
48 }
49 
50 function runTest () {
51  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
52  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
53 
54  var itemAURI = ios.newURI("file:///alpha.mp3", null, null);
55  var itemAProperties =
56  { artistName: "A", albumName: "Alpha", trackName: "Track Alpha", trackNumber: "1", year: "2000"};
57 
58  var itemBURI = ios.newURI("file:///ecto.mp3", null, null);
59  var itemBProperties =
60  { artistName: "E", albumName: "Ecto", trackName: "Track Ecto", trackNumber: "2", year: "2004"};
61 
62  var itemCURI = ios.newURI("file:///beta.mp3", null, null);
63  var itemCProperties =
64  { artistName: "B", albumName: "Beta", trackName: "Track Beta", trackNumber: "2", year: "2001"};
65 
66  var itemDURI = ios.newURI("file:///cent.mp3", null, null);
67  var itemDProperties =
68  { artistName: "C", albumName: "Cent", trackName: "Track Cent", trackNumber: "3", year: "2002", composerName: "Centos" };
69 
70  var itemEURI = ios.newURI("file:///femto.mp3", null, null);
71  var itemEProperties =
72  { artistName: "F", albumName: "Femto", trackName: "Track Femto", trackNumber: "7", year: "1998", genre: "electronic" };
73 
74  var itemEEURI = ios.newURI("file:///femto.mp3", null, null);
75  var itemEEProperties =
76  { artistName: "Full", albumName: "Femto", trackName: "Track Femto Super", trackNumber: "7", year: "1998", genre: "techno" };
77 
78  var itemFURI = ios.newURI("file:///delta.mp3", null, null);
79  var itemFProperties =
80  { artistName: "D", albumName: "Delta", trackName: "Track Delta", trackNumber: "5", year: "2008"};
81 
82 
83  var diffingService = Cc["@songbirdnest.com/Songbird/Library/DiffingService;1"]
84  .getService(Ci.sbILibraryDiffingService);
85 
86  var sourceDBGUID = "test_medialist_source";
87  var destinationDBGUID = "test_medialist_destination";
88 
89  var sourceLibrary = createLibrary(sourceDBGUID, null, false);
90  var destinationLibrary = createLibrary(destinationDBGUID, null, false);
91 
92  var itemProperties = createPropertyArray();
93  appendPropertiesToArray(itemAProperties, itemProperties);
94  var itemA = sourceLibrary.createMediaItem(itemAURI,
95  itemProperties);
96 
97  itemProperties = createPropertyArray();
98  appendPropertiesToArray(itemBProperties, itemProperties);
99  var itemB = destinationLibrary.createMediaItem(itemBURI,
100  itemProperties);
101 
102  itemProperties = createPropertyArray();
103  appendPropertiesToArray(itemCProperties, itemProperties);
104  var itemC = sourceLibrary.createMediaItem(itemCURI,
105  itemProperties);
106 
107  itemProperties = createPropertyArray();
108  appendPropertiesToArray(itemDProperties, itemProperties);
109  var itemD = destinationLibrary.createMediaItem(itemDURI,
110  itemProperties);
111 
112  itemProperties = createPropertyArray();
113  appendPropertiesToArray(itemEProperties, itemProperties);
114  var itemE = sourceLibrary.createMediaItem(itemEURI,
115  itemProperties);
116 
117  itemProperties = createPropertyArray();
118  appendPropertiesToArray(itemEEProperties, itemProperties);
119  var itemEE = destinationLibrary.createMediaItem(itemEEURI,
120  itemProperties);
121  makeItACopy(itemE, itemEE);
122 
123  itemProperties = createPropertyArray();
124  appendPropertiesToArray(itemFProperties, itemProperties);
125  var itemF = destinationLibrary.createMediaItem(itemFURI,
126  itemProperties);
127 
128  var itemsAdded = [itemB, itemE];
129  var itemsAddedProperties = [itemBProperties, itemEProperties];
130 
131  var itemsDeleted = [itemD, itemD];
132  var itemsDeletedProperties = [itemDProperties, itemDProperties];
133 
134  var sourceMediaList = sourceLibrary.createMediaList("simple");
135  var destinationMediaList = destinationLibrary.createMediaList("simple");
136 
137  sourceMediaList.name = "The List";
138  destinationMediaList.name = "The Wrong Name";
139 
140  sourceMediaList.add(itemA);
141  sourceMediaList.add(itemB);
142  sourceMediaList.add(itemC);
143  sourceMediaList.add(itemE);
144  sourceMediaList.add(itemF);
145  sourceMediaList.add(itemE);
146 
147  destinationMediaList.add(itemA);
148  destinationMediaList.add(itemC);
149  destinationMediaList.add(itemD);
150  destinationMediaList.add(itemEE);
151  destinationMediaList.add(itemF);
152  destinationMediaList.add(itemD);
153 
154  var libraryChangeset = diffingService.createChangeset(sourceMediaList,
155  destinationMediaList);
156 
157  var sourceLists = libraryChangeset.sourceLists;
158  assertEqual(sourceLists.length, 1, "There should only be one source list!");
159 
160  var sourceList = sourceLists.queryElementAt(0, Ci.sbIMediaList);
161  assertEqual(sourceList,
162  sourceMediaList,
163  "Source list should be equal to the source media list!");
164 
165  var destinationList = libraryChangeset.destinationList;
166  assertEqual(destinationList.QueryInterface(Ci.sbIMediaList),
167  destinationMediaList,
168  "Destination list should be equal to destination media list!");
169 
170  var changes = libraryChangeset.changes;
171  log("There are " + changes.length + " changes present in the changeset.");
172 
173  var addedOps = 0;
174  var addedChanges = [];
175 
176  var deletedOps = 0;
177  var deletedChanges = [];
178 
179  var movedOps = 0;
180  var movedChanges = [];
181 
182  var modifiedOps = 0;
183  var unknownOps = 0;
184 
185  var changesEnum = changes.enumerate();
186 
187  while(changesEnum.hasMoreElements()) {
188  var change = changesEnum.getNext().QueryInterface(Ci.sbILibraryChange);
189 
190  switch(change.operation) {
191  case Ci.sbIChangeOperation.ADDED:
192  addedOps++;
193  addedChanges.push(change);
194  break;
195  case Ci.sbIChangeOperation.DELETED:
196  deletedOps++;
197  deletedChanges.push(change);
198  break;
199  case Ci.sbIChangeOperation.MODIFIED:
200  modifiedOps++;
201  break;
202  case Ci.sbIChangeOperation.MOVED:
203  movedOps++;
204  movedChanges.push(change);
205  break;
206  default:
207  unknownOps++;
208  }
209  }
210 
211  log("There are " + addedOps + " added operations.");
212  assertEqual(addedOps, 2, "There should be 2 added operations");
213 
214  log("There are " + deletedOps + " deleted operations.");
215  assertEqual(deletedOps, 2, "There should be 2 deleted operations");
216 
217  log("There are " + modifiedOps + " modified operations.");
218  assertEqual(modifiedOps, 1, "There should 1 modified operations");
219 
220  log("There are " + movedOps + " moved operations.");
221  assertEqual(movedOps, 6, "There should be 6 moved operations.");
222 
223  var i = 0;
224  var propertyChange = null;
225  var propertyChangesEnum = null;
226 
227  for(i = 0; i < addedChanges.length; ++i) {
228  propertyChangesEnum = addedChanges[i].properties.enumerate();
229  while(propertyChangesEnum.hasMoreElements()) {
230  propertyChange = propertyChangesEnum.getNext().QueryInterface(Ci.sbIPropertyChange);
231  if(typeof(itemsAddedProperties[i][propertyChange.id]) != 'undefined') {
232  assertEqual(itemsAddedProperties[i][propertyChange.id],
233  propertyChange.newValue,
234  "Value is not what is expected!");
235  }
236  }
237  }
238 
239  for(i = 0; i < deletedChanges.length; ++i) {
240  assertEqual(deletedChanges[i].destinationItem,
241  itemsDeleted[i],
242  "Items should be equal!");
243  try {
244  propertyChangesEnum = deletedChanges[i].properties;
245  }
246  catch(e) {
247  assertEqual(e.result,
248  Cr.NS_ERROR_NOT_AVAILABLE,
249  "There should be no property changes available for deleted operations!");
250  }
251  }
252 
253  for(i = 0; i < movedChanges.length; ++i) {
254 
255  propertyChangesEnum = movedChanges[i].properties.enumerate();
256 
257  var totalPropertyChanges = 0;
258  while(propertyChangesEnum.hasMoreElements()) {
259  propertyChange = propertyChangesEnum.getNext().QueryInterface(Ci.sbIPropertyChange);
260 
261  assertEqual(propertyChange.id,
262  SB_NS + "ordinal",
263  "The property id should always be 'http://songbirdnest.com/data/1.0#ordinal'");
264 
265  assertEqual(propertyChange.newValue,
266  i,
267  "The ordinal of the item is not correct!");
268 
269  ++totalPropertyChanges;
270  }
271 
272  assertEqual(totalPropertyChanges,
273  1,
274  "There should only be one property change for moved operations!");
275  }
276 
277 }
const Cc
function appendPropertiesToArray(aProperties, aPropertyArray)
function log(s)
function assertEqual(aExpected, aActual, aMessage)
function makeItACopy(source, destination)
const char * propName
function createPropertyArray()
function runTest()
Advanced DataRemote unit tests.
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
const Cr
const Ci
var ios
Definition: head_feeds.js:5
var SB_NS
Test file.
_getSelectedPageStyle s i