test_diffing_library.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 
53  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
54 
55  var sourceItemAddedURI = ios.newURI("file:///alpha.mp3", null, null);
56  var sourceItemAddedProperties =
57  { artistName: "A", albumName: "Alpha", trackName: "Track Alpha", trackNumber: "1", year: "2000"};
58 
59  var sourceItemNotModifiedURI = ios.newURI("file:///ecto.mp3", null, null);
60  var sourceItemNotModifiedProperties =
61  { artistName: "E", albumName: "Ecto", trackName: "Track Ecto", trackNumber: "2", year: "2004"};
62 
63  var destinationItemDeletedURI = ios.newURI("file:///beta.mp3", null, null);
64  var destinationItemDeletedProperties =
65  { artistName: "B", albumName: "Beta", trackName: "Track Beta", trackNumber: "2", year: "2001"};
66 
67  var sourceItemModifiedURI = ios.newURI("file:///cent.mp3", null, null);
68  var sourceItemModifiedProperties =
69  { artistName: "C", albumName: "Cent", trackName: "Track Cent", trackNumber: "3", year: "2002", composerName: "Centos" };
70 
71  var destinationItemModifiedURI = sourceItemModifiedURI;
72  var destinationItemModifiedProperties =
73  { artistName: "D", albumName: "Delta", trackName: "Track Delta", trackNumber: "4", year: "2002", genre: "rock" };
74 
75  var destinationItemNotModifiedURI = ios.newURI("file:///ecto.mp3", null, null);
76  var destinationItemNotModifiedProperties =
77  { artistName: "E", albumName: "Ecto", trackName: "Track Ecto", trackNumber: "2", year: "2004"};
78 
79  var diffingService = Cc["@songbirdnest.com/Songbird/Library/DiffingService;1"]
80  .getService(Ci.sbILibraryDiffingService);
81 
82  var sourceDBGUID = "test_library_source";
83  var destinationDBGUID = "test_library_destination";
84 
85  var sourceLibrary = createLibrary(sourceDBGUID, null, false);
86  var destinationLibrary = createLibrary(destinationDBGUID, null, false);
87 
88  var itemProperties = createPropertyArray();
89  appendPropertiesToArray(sourceItemAddedProperties, itemProperties);
90  var addedItem = sourceLibrary.createMediaItem(sourceItemAddedURI,
91  itemProperties);
92 
93  itemProperties = createPropertyArray();
94  appendPropertiesToArray(destinationItemDeletedProperties, itemProperties);
95  var deletedItem = destinationLibrary.createMediaItem(destinationItemDeletedURI,
96  itemProperties);
97 
98  itemProperties = createPropertyArray();
99  appendPropertiesToArray(sourceItemModifiedProperties, itemProperties);
100  var modifiedItemA = sourceLibrary.createMediaItem(sourceItemModifiedURI,
101  itemProperties);
102 
103  itemProperties = createPropertyArray();
104  appendPropertiesToArray(destinationItemModifiedProperties, itemProperties);
105  var modifiedItemB = destinationLibrary.createMediaItem(destinationItemModifiedURI,
106  itemProperties);
107  makeItACopy(modifiedItemA, modifiedItemB);
108 
109  itemProperties = createPropertyArray();
110  appendPropertiesToArray(sourceItemNotModifiedProperties, itemProperties);
111  var sourceItemNotModified =
112  sourceLibrary.createMediaItem(sourceItemNotModifiedURI,
113  itemProperties);
114 
115  itemProperties = createPropertyArray();
116  appendPropertiesToArray(destinationItemNotModifiedProperties, itemProperties);
117  var destinationItemNotModified =
118  destinationLibrary.createMediaItem(destinationItemNotModifiedURI,
119  itemProperties);
120  makeItACopy(sourceItemNotModified, destinationItemNotModified);
121  log("destinationItemNotModified = " + destinationItemNotModified.getProperty(SBProperties.originItemGuid) + "\n")
122 
123  var libraryChangeset = diffingService.createChangeset(sourceLibrary,
124  destinationLibrary);
125 
126  var sourceLists = libraryChangeset.sourceLists;
127  assertEqual(sourceLists.length, 1, "There should only be one source list!");
128 
129  var sourceList = sourceLists.queryElementAt(0, Ci.sbILibrary);
130  assertEqual(sourceList,
131  sourceLibrary,
132  "Source list should be equal to the source library!");
133 
134  var destinationList = libraryChangeset.destinationList;
135  assertEqual(destinationList.QueryInterface(Ci.sbILibrary),
136  destinationLibrary,
137  "Destination list should be equal to destination library!");
138 
139  var changes = libraryChangeset.changes;
140  log("There are " + changes.length + " changes present in the changeset.");
141 
142  var addedOps = 0;
143  var addedChange = null;
144 
145  var deletedOps = 0;
146  var deletedChange = null;
147 
148  var modifiedOps = 0;
149  var modifiedChange = null;
150 
151  var unknownOps = 0;
152 
153  var changesEnum = changes.enumerate();
154 
155  while(changesEnum.hasMoreElements()) {
156  var change = changesEnum.getNext().QueryInterface(Ci.sbILibraryChange);
157 
158  switch(change.operation) {
159  case Ci.sbIChangeOperation.ADDED:
160  addedOps++;
161  addedChange = change;
162  log("added " + change.sourceItem.contentSrc.spec + "\n");
163  break;
164  case Ci.sbIChangeOperation.DELETED:
165  deletedOps++;
166  deletedChange = change;
167  log("deleted " + change.destinationItem.contentSrc.spec + "\n");
168  break;
169  case Ci.sbIChangeOperation.MODIFIED:
170  modifiedOps++;
171  modifiedChange = change;
172  log("modified " + change.sourceItem.contentSrc.spec + "\n");
173  break;
174  default:
175  unknownOps++;
176  }
177  }
178 
179  assertEqual(addedOps, 1, "Wrong number of added operation!");
180  assertEqual(deletedOps, 1, "Wrong number of deleted operations!");
181  assertEqual(modifiedOps, 1, "Wrong number of modified operation!");
182  assertEqual(unknownOps, 0, "There should _not_ be any unknown operations!");
183 
184  var propertyChange = null;
185  var propertyChangesEnum = addedChange.properties.enumerate();
186  while(propertyChangesEnum.hasMoreElements()) {
187  propertyChange = propertyChangesEnum.getNext().QueryInterface(Ci.sbIPropertyChange);
188  if(typeof(sourceItemAddedProperties[propertyChange.id]) != 'undefined') {
189  assertEqual(sourceItemAddedProperties[propertyChange.id],
190  propertyChange.newValue,
191  "Value is not what is expected!");
192  }
193  }
194 
195  try {
196  deletedChange.properties;
197  }
198  catch(e) {
199  assertEqual(e.result,
200  Cr.NS_ERROR_NOT_AVAILABLE,
201  "There should be no property changes available for deleted operations!");
202  }
203 
204  propertyChangesEnum = modifiedChange.properties.enumerate();
205  while(propertyChangesEnum.hasMoreElements()) {
206  propertyChange = propertyChangesEnum.getNext().QueryInterface(Ci.sbIPropertyChange);
207  // According to this, the property should've been added.
208  if(typeof(sourceItemModifiedProperties[propertyChange.id]) != 'undefined' &&
209  typeof(destinationItemModifiedProperties[propertyChange.id]) == 'undefined') {
210  assertEqual(propertyChange.operation,
211  Ci.sbIChangeOperation.ADDED,
212  "Property should have been ADDED!");
213  assertEqual(propertyChange.oldValue,
214  'undefined',
215  "Old property value should be undefined!");
216  }
217  // According to this, the property should've been modified.
218  if(typeof(sourceItemModifiedProperties[propertyChange.id]) != 'undefined' &&
219  typeof(destinationItemModifiedProperties[propertyChange.id]) != 'undefined') {
220  assertEqual(propertyChange.oldValue,
221  destinationItemModifiedProperties[propertyChange.id],
222  "Old value and actual item value do not match when they should!");
223  assertEqual(propertyChange.operation,
224  Ci.sbIChangeOperation.MODIFIED,
225  "Property should have been MODIFIED!");
226  }
227 
228  if(typeof(sourceItemModifiedProperties[propertyChange.id]) == 'undefined' &&
229  typeof(destinationItemModifiedProperties[propertyChange.id]) != 'undefined') {
230  assertEqual(propertyChange.operation,
231  Ci.sbIChangeOperation.DELETED,
232  "Property should have been DELETED!");
233  assertEqual(destinationItemModifiedProperties[propertyChange.id],
234  propertyChange.oldValue);
235  assertEqual(propertyChange.newValue,
236  'undefined',
237  "Deleted property should _not_ have a newValue");
238  }
239 
240  }
241 }
const Cc
function log(s)
function createPropertyArray()
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Advanced DataRemote unit tests.
function appendPropertiesToArray(aProperties, aPropertyArray)
const char * propName
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
const Cr
var SB_NS
Test file.
const Ci
var ios
Definition: head_feeds.js:5
function makeItACopy(source, destination)