test_medialistview.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-2008 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 
31 function runTest () {
32 
33  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34  Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
35 
36  var library = createLibrary("test_medialistview");
37 
38  // Tests for getUnfilteredIndex
39 
40  // For the library, the unfiltered index on a view should match the index
41  // of the item in the library
42  var list = library;
43 
44  var view = list.createView();
45 
46  view.filterConstraint = LibraryUtils.createConstraint([
47  [
48  [SBProperties.artistName, ["AC/DC"]]
49  ]
50  ]);
51 
52  for (var i = 0; i < view.length; i++) {
53  var viewItem = view.getItemByIndex(i);
54  var listIndex = view.getUnfilteredIndex(i);
55  assertEqual(listIndex, list.indexOf(viewItem, 0));
56  }
57 
58  // For a simple media list, set up a small media list, filter it and check
59  // the unfiltered index
60  var item1 = library.createMediaItem(newURI("file:///foo/1.mp3"));
61  item1.setProperty(SBProperties.artistName, "The Fall");
62 
63  var item2 = library.createMediaItem(newURI("file:///foo/2.mp3"));
64  item2.setProperty(SBProperties.artistName, "The Dirtbombs");
65 
66  var item3 = library.createMediaItem(newURI("file:///foo/3.mp3"));
67  item3.setProperty(SBProperties.artistName, "The Dirtbombs");
68 
69  var item4 = library.createMediaItem(newURI("file:///foo/4.mp3"));
70  item4.setProperty(SBProperties.artistName, "Air");
71 
72  list = library.createMediaList("simple");
73  list.add(item1);
74  list.add(item2);
75  list.add(item3);
76  list.add(item4);
77 
78  view = list.createView();
79  for (var i = 0; i < view.length; i++) {
80  var viewItem = view.getItemByIndex(i);
81  var listIndex = view.getUnfilteredIndex(i);
82  assertEqual(listIndex, list.indexOf(viewItem, 0));
83  }
84 
85  view.filterConstraint = LibraryUtils.createConstraint([
86  [
87  [SBProperties.artistName, ["The Dirtbombs"]]
88  ]
89  ]);
90 
91  for (var i = 0; i < view.length; i++) {
92  var viewItem = view.getItemByIndex(i);
93  var listIndex = view.getUnfilteredIndex(i);
94  assertEqual(listIndex, list.indexOf(viewItem, 0));
95  }
96 
97  // Tests for getIndexForItem
98  var item = library.getItemByGuid("3E59BD68-AD99-11DB-9321-C22AB7121F49");
99 
100  // Create a new view each time so we have no cached data
101  view = library.createView();
102  assertEqual(view.getIndexForItem(item), 69);
103  forceCache(view);
104  assertEqual(view.getIndexForItem(item), 69);
105 
106  view.filterConstraint = LibraryUtils.createConstraint([
107  [
108  [SBProperties.artistName, ["AC/DC"]]
109  ]
110  ]);
111 
112  assertEqual(view.getIndexForItem(item), 4);
113  forceCache(view);
114  assertEqual(view.getIndexForItem(item), 4);
115 
116  view = list.createView();
117  assertEqual(view.getIndexForItem(item3), 2);
118  forceCache(view);
119  assertEqual(view.getIndexForItem(item3), 2);
120 
121  view.filterConstraint = LibraryUtils.createConstraint([
122  [
123  [SBProperties.artistName, ["The Dirtbombs"]]
124  ]
125  ]);
126 
127  assertEqual(view.getIndexForItem(item3), 1);
128  forceCache(view);
129  assertEqual(view.getIndexForItem(item3), 1);
130 
131  try {
132  view.getIndexForItem(item1);
133  fail("NS_ERROR_NOT_AVAILABLE expected");
134  }
135  catch(e) {
136  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
137  }
138 
139  // test index item uid
140  view = library.createView();
141  view.setSort(SBProperties.createArray([
142  [SBProperties.artistName, "a"]
143  ]));
144 
145  var items = [];
146  var uids = [];
147  for (var i = 0; i < view.length; i++) {
148  items.push(view.getItemByIndex(i));
149  uids.push(view.getViewItemUIDForIndex(i));
150  }
151 
152  view.setSort(SBProperties.createArray([
153  [SBProperties.trackName, "a"]
154  ]));
155 
156  for (var i = 0; i < uids.length; i++) {
157  var index = view.getIndexForViewItemUID(uids[i]);
158  var item = view.getItemByIndex(index);
159  assertTrue(item.equals(items[i]));
160  }
161 
162  view = list.createView();
163  view.setSort(SBProperties.createArray([
164  [SBProperties.artistName, "a"]
165  ]));
166 
167  var items = [];
168  var uids = [];
169  for (var i = 0; i < view.length; i++) {
170  items.push(view.getItemByIndex(i));
171  uids.push(view.getViewItemUIDForIndex(i));
172  }
173 
174  view.setSort(SBProperties.createArray([
175  [SBProperties.artistName, "d"]
176  ]));
177 
178  for (var i = 0; i < uids.length; i++) {
179  var index = view.getIndexForViewItemUID(uids[i]);
180  var item = view.getItemByIndex(index);
181  assertTrue(item.equals(items[i]));
182  }
183 
184  try {
185  view.getViewItemUIDForIndex(100000);
186  fail("NS_ERROR_INVALID_ARG expected");
187  }
188  catch(e) {
189  assertEqual(e.result, Cr.NS_ERROR_INVALID_ARG);
190  }
191 
192  try {
193  view.getIndexForViewItemUID("foo");
194  fail("'foo' is not a valid viewItemUID, NS_ERROR_NOT_AVAILABLE expected");
195  }
196  catch(e) {
197  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
198  }
199 
200  try {
201  var uid = view.getViewItemUIDForIndex(2);
202  var listIndex = view.getUnfilteredIndex(2);
203  view.mediaList.removeByIndex(listIndex);
204  view.getIndexForViewItemUID(uid);
205  }
206  catch(e) {
207  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
208  }
209 }
210 
211 function forceCache(view) {
212 
213  for (let i = 0; i < view.length; i++) {
214  view.getItemByIndex(i);
215  }
216 
217 }
218 
function fail(aMessage)
function runTest()
Test file.
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
const Cr
Javascript wrappers for common library tasks.
_getSelectedPageStyle s i
function forceCache(view)