test_guidarray_nullsorting.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  var databaseGUID = "test_guidarray_nullsorting";
34  var library = createLibrary(databaseGUID);
35  library.clear();
36 
37  // Set up a library with 20 items, 10 of which have a null content length
38  // and the rest have contents lengths 0 - 9
39  var items = [];
40 
41  var array = Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/GUIDArray;1"]
42  .createInstance(Ci.sbILocalDatabaseGUIDArray);
43  array.databaseGUID = databaseGUID;
44  array.baseTable = "media_items";
45  array.propertyCache =
46  library.QueryInterface(Ci.sbILocalDatabaseLibrary).propertyCache;
47 
48 /*
49  XXXsteve Going to disable this test since we now automagically fill in the
50  contentLength for new items. We can re-enable this either when we add the
51  null support to setProperty or if we move the setting of this property to
52  the metadata scanner
53 
54  for (var i = 0; i < 20; i++) {
55  var item = library.createMediaItem(newURI("file://foo/" + i));
56  if (i >= 10) {
57  item.setProperty("http://songbirdnest.com/data/1.0#contentLength", i - 10);
58  }
59  items.push(item.guid);
60  }
61 
62  // We build the array to match an ascending sort, so just test
63  array.addSort("http://songbirdnest.com/data/1.0#contentLength", true);
64  assertArraySame(array, items);
65 
66  // Reversing the sort should cause the nulls to go to the bottom (because
67  // they are small) but remain in the same order. The non-null items should
68  // move to the top and be reversed.
69  array.clearSorts();
70  array.addSort("http://songbirdnest.com/data/1.0#contentLength", false);
71 
72  items = swap(items, 10);
73  items = reverseRange(items, 0, 10);
74  assertArraySame(array, items);
75 */
76 
77  // Now test on a new property using different null sort configurations
78  library.clear();
79  var numberInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Number;1"]
80  .createInstance(Ci.sbINumberPropertyInfo);
81  numberInfo.id = "http://songbirdnest.com/data/1.0#testNumber";
82  numberInfo.nullSort = Ci.sbIPropertyInfo.SORT_NULL_SMALL;
83 
84  var propMan = Cc["@songbirdnest.com/Songbird/Properties/PropertyManager;1"]
85  .getService(Ci.sbIPropertyManager);
86  propMan.addPropertyInfo(numberInfo);
87 
88  // Set up a library with 20 items, 10 of which have a null content length
89  // and the rest have contents lengths 0 - 9
90  items = [];
91  for (var i = 0; i < 20; i++) {
92  var item = library.createMediaItem(newURI("file://foo/" + i));
93  if (i >= 10) {
94  item.setProperty("http://songbirdnest.com/data/1.0#testNumber", i - 10);
95  }
96  items.push(item.guid);
97  }
98 
99  array.clearSorts();
100  array.addSort("http://songbirdnest.com/data/1.0#testNumber", true);
101  assertArraySame(array, items);
102 
103  // Reversing the sort should cause the nulls to go to the bottom (because
104  // they are small) but remain in the same order. The non-null items should
105  // move to the top and be reversed.
106  array.clearSorts();
107  array.addSort("http://songbirdnest.com/data/1.0#testNumber", false);
108 
109  var sortNullSmall = swap(items, 10);
110  sortNullSmall = reverseRange(sortNullSmall, 0, 10);
111  assertArraySame(array, sortNullSmall);
112 
113  // Change property to sort nulls big
114  numberInfo.nullSort = Ci.sbIPropertyInfo.SORT_NULL_BIG;
115 
116  // Now the nulls should be at the bottom
117  array.clearSorts();
118  array.addSort("http://songbirdnest.com/data/1.0#testNumber", true);
119 
120  var sortNullBig = swap(items, 10);
121  assertArraySame(array, sortNullBig);
122 
123  // Reversung the sort moves the nulls to the top and reverses the non-null
124  // items
125  array.clearSorts();
126  array.addSort("http://songbirdnest.com/data/1.0#testNumber", false);
127 
128  sortNullBig = reverseRange(items, 10, 10);
129  assertArraySame(array, sortNullBig);
130 
131  // Change the property to sort nulls always first
132  numberInfo.nullSort = Ci.sbIPropertyInfo.SORT_NULL_FIRST;
133 
134  // Always sorting nulls first will match our original data
135  array.clearSorts();
136  array.addSort("http://songbirdnest.com/data/1.0#testNumber", true);
137 
138  assertArraySame(array, items);
139 
140  // Reversing the sort should only reverse the non-null items, nulls should
141  // stay on top
142  array.clearSorts();
143  array.addSort("http://songbirdnest.com/data/1.0#testNumber", false);
144 
145  var sortNullFirst = reverseRange(items, 10, 10);
146  assertArraySame(array, sortNullFirst);
147 
148  // Change the property to sort nulls always last
149  numberInfo.nullSort = Ci.sbIPropertyInfo.SORT_NULL_LAST;
150 
151  // Now the nulls should be at the bottom and the non-null should be in the
152  // original order
153  array.clearSorts();
154  array.addSort("http://songbirdnest.com/data/1.0#testNumber", true);
155 
156  var sortNullLast = swap(items, 10);
157  assertArraySame(array, sortNullLast);
158 
159  // Reversing the sort should only reverse the non-null items
160  array.clearSorts();
161  array.addSort("http://songbirdnest.com/data/1.0#testNumber", false);
162 
163  sortNullLast = reverseRange(sortNullLast, 0, 10);
164  assertArraySame(array, sortNullLast);
165 
166 }
167 
168 function assertArraySame(guidArray, guids) {
169 
170  if (guidArray.length != guids.length) {
171  fail("different lengths, " + guidArray.length + " != " + guids.length);
172  }
173 
174  for (var i = 0; i < guidArray.length; i++) {
175  if (guidArray.getGuidByIndex(i) != guids[i]) {
176  fail("different items at " + i + ", " + guidArray.getGuidByIndex(i) + " != " + guids[i]);
177  }
178  }
179 
180 }
181 
182 function reverseRange(a, start, length) {
183 
184  var before = a.slice(0, start);
185  var range = a.slice(start, start + length);
186  var after = a.slice(start + length);
187  range.reverse();
188  return before.concat(range).concat(after);
189 }
190 
191 function swap(a, length) {
192 
193  var before = a.slice(0, length);
194  var after = a.slice(length);
195 
196  return after.concat(before);
197 }
198 
function start(ch)
function swap(a, length)
function reverseRange(a, start, length)
const Cc
function fail(aMessage)
inArray array
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
const Ci
function runTest()
Test file.
function assertArraySame(guidArray, guids)
_getSelectedPageStyle s i
function range(x, y)
Definition: httpd.js:138