test_playbackhistoryservice.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 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
26 
27 function runTest() {
28 
29  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
30 
31  var testItemURL = ios.newURI("file:///alpha.mp3", null, null);
32  var testItemProperties =
33  SBProperties.createArray({ artistName: "A",
34  albumName: "Alpha",
35  trackName: "Track Alpha",
36  trackNumber: "1",
37  year: "2000" });
38 
39  var history = Cc["@songbirdnest.com/Songbird/PlaybackHistoryService;1"]
40  .getService(Ci.sbIPlaybackHistoryService);
41 
42  history.clear();
43 
44  var entry = Cc["@songbirdnest.com/Songbird/PlaybackHistoryEntry;1"]
45  .createInstance(Ci.sbIPlaybackHistoryEntry);
46 
47  var library = createLibrary("test_playbackhistoryservice", null, false);
48 
49  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
50  .getService(Ci.sbILibraryManager);
51  libraryManager.registerLibrary(library, false);
52 
53  var item = library.createMediaItem(testItemURL, testItemProperties);
54 
55  var itemPlayedAt = new Date();
56  var itemPlayDuration = 1000 * 1000 * 1000;
57 
58  entry.init(item, itemPlayedAt, itemPlayDuration, null);
59 
60  assertEqual(entry.item, item);
61  assertEqual(entry.timestamp, itemPlayedAt.getTime());
62  assertEqual(entry.duration, itemPlayDuration);
63 
64  history.addEntry(entry);
65 
66  var itemPlayedAt2 = new Date().getTime() + 10000;
67  var itemPlayDuration2 = 1000 * 1000 * 999;
68 
69  var annotations = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
70  .createInstance(Ci.sbIPropertyArray);
71  annotations.appendProperty("http://songbirdnest.com/data/1.0#scrobbled",
72  "scrobbled");
73 
74  var entry2 = history.createEntry(item,
75  itemPlayedAt2,
76  itemPlayDuration2,
77  annotations);
78  history.addEntry(entry2);
79 
80  entry2.setAnnotation("http://songbirdnest.com/data/1.0#iheartthis", "true");
81 
82  var getByAnnotationArray = history.getEntriesByAnnotation("http://songbirdnest.com/data/1.0#iheartthis", "true");
83  assertEqual(getByAnnotationArray.length, 1);
84  var getByAnnotationEntry = getByAnnotationArray.queryElementAt(0, Ci.sbIPlaybackHistoryEntry);
85  assertEqual(getByAnnotationEntry.getAnnotation("http://songbirdnest.com/data/1.0#iheartthis"),
86  "true");
87 
88  entry2.setAnnotation("http://songbirdnest.com/data/1.0#ashamedDoNotScrobble", "true");
89 
90  var annotations2 = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
91  .createInstance(Ci.sbIPropertyArray);
92  annotations2.appendProperty("http://songbirdnest.com/data/1.0#iheartthis",
93  "true");
94  annotations2.appendProperty("http://songbirdnest.com/data/1.0#ashamedDoNotScrobble",
95  "true");
96 
97  var getByAnnotationsArray = history.getEntriesByAnnotations(annotations2, 1);
98  assertEqual(getByAnnotationsArray.length, 1);
99  var getByAnnotationsEntry = getByAnnotationsArray.queryElementAt(0, Ci.sbIPlaybackHistoryEntry);
100  assertEqual(getByAnnotationsEntry.getAnnotation("http://songbirdnest.com/data/1.0#iheartthis"),
101  "true");
102  assertEqual(getByAnnotationsEntry.getAnnotation("http://songbirdnest.com/data/1.0#ashamedDoNotScrobble"),
103  "true");
104 
105  entry2.removeAnnotation("http://songbirdnest.com/data/1.0#iheartthis");
106 
107  log("The playback history service has " + history.entryCount + " entries.");
108  assertEqual(history.entryCount, 2);
109 
110  var entriesArray = [entry2, entry];
111  var enumEntries = history.entries;
112 
113  // I hate you javascript scoping.
114  {
115  let i = 0;
116  while(enumEntries.hasMoreElements()) {
117  let entry = enumEntries.getNext()
118  .QueryInterface(Ci.sbIPlaybackHistoryEntry);
119  assertEqual(entry.item, entriesArray[i].item);
120  assertEqual(entry.timestamp, entriesArray[i].timestamp);
121  assertEqual(entry.duration, entriesArray[i].duration);
122 
123  ++i;
124  }
125  }
126 
127  {
128  let entries = history.getEntriesByTimestamp(itemPlayedAt, itemPlayedAt + 1);
129  assertEqual(entries.length, 1);
130 
131  let entry = entries.queryElementAt(0, Ci.sbIPlaybackHistoryEntry);
132  assertEqual(entry.item, item);
133  assertEqual(entry.timestamp, itemPlayedAt.getTime());
134  assertEqual(entry.duration, itemPlayDuration);
135  }
136 
137  var entry_fromGetEntry = history.getEntryByIndex(0);
138  assertEqual(entry_fromGetEntry.item, item);
139  assertEqual(entry_fromGetEntry.timestamp, itemPlayedAt2);
140  assertEqual(entry_fromGetEntry.duration, itemPlayDuration2);
141 
142  var entries = history.getEntriesByIndex(0, 2);
143  assertEqual(entries.length, 2);
144 
145  history.removeEntry(entry);
146  log("After removing one entry, the history service has " + history.entryCount + " entry");
147  assertEqual(history.entryCount, 1);
148 
149  var remainingEntry = entries.queryElementAt(0, Ci.sbIPlaybackHistoryEntry);
150  assertEqual(remainingEntry.item, item);
151  assertEqual(remainingEntry.timestamp, itemPlayedAt2);
152  assertEqual(remainingEntry.duration, itemPlayDuration2);
153 
154  return;
155 }
classDescription entry
Definition: FeedWriter.js:1427
const Cc
var history
function log(s)
function assertEqual(aExpected, aActual, aMessage)
var libraryManager
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
const Ci
var ios
Definition: head_feeds.js:5
_getSelectedPageStyle s i
function runTest()
Advanced DataRemote unit tests.