test_guidarray_prefix.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_prefix";
34  var library = createLibrary(databaseGUID);
35  var listId = library.QueryInterface(Ci.sbILocalDatabaseLibrary)
36  .getMediaItemIdForGuid("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
37 
38  var array = Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/GUIDArray;1"]
39  .createInstance(Ci.sbILocalDatabaseGUIDArray);
40  array.databaseGUID = databaseGUID;
41  array.propertyCache =
42  library.QueryInterface(Ci.sbILocalDatabaseLibrary).propertyCache;
43 
44  array.baseTable = "media_items";
45  array.addSort("http://songbirdnest.com/data/1.0#albumName", true);
46 
47  var tests = [
48  "A",
49  "B",
50  "F",
51  "From",
52  "Back",
53  "Life",
54  "qqqqqqq"
55  ];
56 
57  for (var i = 0; i < tests.length; i++) {
58  var index = findFirstIndexByPrefix(array, tests[i]);
59  if (index < 0) {
60  try {
61  array.getFirstIndexByPrefix(tests[i]);
62  fail("NS_ERROR_NOT_AVAILABLE not thrown");
63  }
64  catch(e) {
65  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
66  }
67  }
68  else {
69  assertEqual(array.getFirstIndexByPrefix(tests[i]), index);
70  }
71  }
72 
73  array.addFilter("http://songbirdnest.com/data/1.0#genre",
74  new StringArrayEnumerator(["rock"]),
75  false);
76 
77  for (var i = 0; i < tests.length; i++) {
78  var index = findFirstIndexByPrefix(array, tests[i]);
79  if (index < 0) {
80  try {
81  array.getFirstIndexByPrefix(tests[i]);
82  fail("NS_ERROR_NOT_AVAILABLE not thrown");
83  }
84  catch(e) {
85  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
86  }
87  }
88  else {
89  assertEqual(array.getFirstIndexByPrefix(tests[i]), index);
90  }
91  }
92 
93  // Test on a simple media list
94  array = Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/GUIDArray;1"]
95  .createInstance(Ci.sbILocalDatabaseGUIDArray);
96  array.databaseGUID = databaseGUID;
97  array.propertyCache =
98  library.QueryInterface(Ci.sbILocalDatabaseLibrary).propertyCache;
99 
100  array.baseTable = "simple_media_lists";
101  array.baseConstraintColumn = "media_item_id";
102  array.baseConstraintValue = listId;
103 
104  array.addSort("http://songbirdnest.com/data/1.0#albumName", true);
105 
106  var tests = [
107  "A",
108  "B",
109  "F",
110  "From",
111  "Back",
112  "Life",
113  "qqqqqqq"
114  ];
115 
116  for (var i = 0; i < tests.length; i++) {
117  var index = findFirstIndexByPrefix(array, tests[i]);
118  if (index < 0) {
119  try {
120  array.getFirstIndexByPrefix(tests[i]);
121  fail("NS_ERROR_NOT_AVAILABLE not thrown");
122  }
123  catch(e) {
124  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
125  }
126  }
127  else {
128  assertEqual(array.getFirstIndexByPrefix(tests[i]), index);
129  }
130  }
131 
132  array.addFilter("http://songbirdnest.com/data/1.0#genre",
133  new StringArrayEnumerator(["rock"]),
134  false);
135 
136  for (var i = 0; i < tests.length; i++) {
137  var index = findFirstIndexByPrefix(array, tests[i]);
138  if (index < 0) {
139  try {
140  array.getFirstIndexByPrefix(tests[i]);
141  fail("NS_ERROR_NOT_AVAILABLE not thrown");
142  }
143  catch(e) {
144  assertEqual(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
145  }
146  }
147  else {
148  assertEqual(array.getFirstIndexByPrefix(tests[i]), index);
149  }
150  }
151 
152  array.clearSorts();
153  array.clearFilters();
154 
155  // Special test when sorted by ordinal
156  array.addSort("http://songbirdnest.com/data/1.0#ordinal", true);
157 
158  // The ordinals of the freshly loaded data start as equal to the index
159  // number, so test with that
160  for (let i = 0; i < array.length; i++) {
161  assertEqual(i, array.getFirstIndexByPrefix(i));
162  }
163 
164  array.addFilter("http://songbirdnest.com/data/1.0#artistName",
165  new StringArrayEnumerator(["a-ha"]),
166  false);
167 
168  for (let i = 10; i < array.length; i++) {
169  assertEqual(i - 10, array.getFirstIndexByPrefix(i));
170  }
171 
172 }
173 
174 function findFirstIndexByPrefix(array, prefix) {
175 
176  var length = array.length;
177  var re = new RegExp("^" + prefix);
178  for (var i = 0; i < length; i++) {
179  var value = array.getSortPropertyValueByIndex(i);
180  if (value.match(re)) {
181  return i;
182  }
183  }
184 
185  return -1;
186 }
187 
const Cc
function fail(aMessage)
inArray array
function assertEqual(aExpected, aActual, aMessage)
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function findFirstIndexByPrefix(array, prefix)
countRef value
Definition: FeedWriter.js:1423
const Cr
function StringArrayEnumerator(aArray)
const Ci
_getSelectedPageStyle s i
function runTest()
Test file.