test_propertyinfo.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-2009 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 testTextInfo() {
32  var textInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Text;1"]
33  .createInstance(Ci.sbITextPropertyInfo);
34 
35  assertEqual(textInfo.id, "");
36  assertEqual(textInfo.type, "text");
37 
38  //Should fail.
39  try {
40  textInfo.type = "mytype";
41  }
42  catch(err) {
43  assertEqual(err.result, Cr.NS_ERROR_ALREADY_INITIALIZED);
44  }
45 
46  textInfo.id = "TextInfo";
47  assertEqual(textInfo.id, "TextInfo");
48 
49  //Should fail.
50  try {
51  textInfo.id = "MyTextInfo";
52  }
53  catch(err) {
54  assertEqual(err.result, Cr.NS_ERROR_ALREADY_INITIALIZED);
55  }
56 
57  textInfo.localizationKey = "My localization key";
58  assertEqual(textInfo.localizationKey, "My localization key");
59 
60  textInfo.displayName = "My Display Name";
61  assertEqual(textInfo.displayName, "My Display Name");
62 
63  var sample = "some text";
64  assertEqual(textInfo.validate(sample), true);
65  assertEqual(textInfo.format(sample), "some text");
66 
67  textInfo.minLength = 10;
68  sample = "too short";
69  assertEqual(textInfo.validate(sample), false);
70 
71  textInfo.maxLength = 10;
72  sample = "way way way too long";
73  assertEqual(textInfo.validate(sample), false);
74 
75  sample = "just right";
76  assertEqual(textInfo.validate(sample), true);
77 
78  textInfo.enforceLowercase = true;
79  sample = "JUST RIGHT";
80  assertEqual(textInfo.validate(sample), true);
81  assertEqual(textInfo.format(sample), "just right");
82 
83  //before validating we always compress the whitespace because
84  //format always does so.
85  sample = " whitespace";
86  assertEqual(textInfo.validate(sample), false);
87 
88  var formatted = textInfo.format(sample);
89  assertEqual(formatted, "whitespace");
90  assertEqual(textInfo.validate(formatted), true);
91 
92  return;
93 }
94 
95 function testNumberInfo() {
96  var numberInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Number;1"]
97  .createInstance(Ci.sbINumberPropertyInfo);
98 
99  numberInfo.id = "NumberInfo";
100  assertEqual(numberInfo.type, "number");
101 
102  var sample = "not a number";
103  assertEqual(numberInfo.validate(sample), false);
104  try {
105  numberInfo.format(sample);
106  }
107  catch(err) {
108  assertEqual(err.result, Cr.NS_ERROR_INVALID_ARG);
109  }
110 
111  sample = "1";
112  assertEqual(numberInfo.validate(sample), true);
113  assertEqual(numberInfo.format(sample), "1");
114  assertEqual(numberInfo.makeSearchable(sample), "+0000000000000000001");
115 
116  numberInfo.radix = Ci.sbINumberPropertyInfo.RADIX_16;
117  sample = "0x00aad";
118  assertEqual(numberInfo.validate(sample), true);
119  assertEqual(numberInfo.format(sample), "0xAAD");
120  assertEqual(numberInfo.makeSearchable(sample), "0000000000000AAD");
121 
122  numberInfo.radix = Ci.sbINumberPropertyInfo.RADIX_8;
123  sample = "0644";
124  assertEqual(numberInfo.validate(sample), true);
125  assertEqual(numberInfo.format(sample), "0644");
126  assertEqual(numberInfo.makeSearchable(sample), "0000000000000000000644");
127 
128  numberInfo.radix = Ci.sbINumberPropertyInfo.RADIX_10;
129  sample = "1000";
130  numberInfo.minValue = 1001;
131  assertEqual(numberInfo.validate(sample), false);
132  try {
133  numberInfo.format(sample)
134  }
135  catch(err) {
136  assertEqual(err.result, Cr.NS_ERROR_INVALID_ARG);
137  }
138 
139  sample = "1002";
140  numberInfo.maxValue = 1003;
141  assertEqual(numberInfo.validate(sample), true);
142 
143  sample = "1001";
144  assertEqual(numberInfo.validate(sample), true);
145 
146  sample = "1003";
147  assertEqual(numberInfo.validate(sample), true);
148 
149  sample = "1004";
150  assertEqual(numberInfo.validate(sample), false);
151 
152 }
153 
155 var numberInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Number;1"]
156  .createInstance(Ci.sbINumberPropertyInfo);
157 
158  numberInfo.id = "FloatingInfo";
159  assertEqual(numberInfo.type, "number");
160 
161  numberInfo.radix = Ci.sbINumberPropertyInfo.FLOAT;
162 
163  var sample = "123 not a number";
164  assertEqual(numberInfo.validate(sample), false);
165  try {
166  numberInfo.format(sample);
167  }
168  catch(err) {
169  assertEqual(err.result, Cr.NS_ERROR_INVALID_ARG);
170  }
171 
172  /* The replace(",", ".") in the following lines prevents
173  * the test from failing if the machine has set the comma
174  * as decimal separator. So in the following test
175  * numberInfo.format(sample) will return "20,99" on such
176  * a machine. With the replacement it changes to "20.99"
177  * and is valid again. To make this visible this is also
178  * used in all log-calls.
179  */
180 
181  sample = "20.99";
182  assertEqual(numberInfo.validate(sample), true);
183 
184  var formatted_sample = numberInfo.format(sample).replace(",", ".");
185  var searchable_sample = numberInfo.makeSearchable(sample).replace(",", ".");
186  log(formatted_sample);
187  log(searchable_sample);
188 
189  assertEqual(formatted_sample, "20.99");
190 
191  var eps = 1e-10; // nearly zero, because floating points don't compare well.
192 
193  var delta = Math.abs(searchable_sample - sample);
194  assertTrue(delta < eps, "make sortable doesn't perturb the value");
195 
196  sample = "0.99";
197  assertEqual(numberInfo.validate(sample), true);
198 
199  formatted_sample = numberInfo.format(sample).replace(",", ".");
200  searchable_sample = numberInfo.makeSearchable(sample).replace(",", ".");
201  log(formatted_sample);
202  log(searchable_sample);
203 
204  assertEqual(formatted_sample, "0.99");
205  var delta = Math.abs(searchable_sample - sample);
206  assertTrue(delta < eps, "make sortable doesn't perturb the value");
207 
208  sample = "12347120349029834.1234341235";
209  assertEqual(numberInfo.validate(sample), true);
210 
211  formatted_sample = numberInfo.format(sample).replace(",", ".");
212  searchable_sample = numberInfo.makeSearchable(sample).replace(",", ".");
213  log(formatted_sample);
214  log(searchable_sample);
215 
216  assertEqual(parseFloat(formatted_sample), 1.23471e+016);
217  var delta = Math.abs(searchable_sample - sample);
218  assertTrue(delta < eps, true, "make sortable doesn't perturb the value");
219 }
220 
221 function testUriInfo() {
222  var uriInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/URI;1"]
223  .createInstance(Ci.sbIURIPropertyInfo);
224 
225  uriInfo.id = "URIInfo";
226  assertEqual(uriInfo.type, "uri");
227 
228  var sample = "totally not a valid url";
229  assertEqual(uriInfo.validate(sample), false);
230 
231  sample = "choohooo://vaguely valid/";
232  assertEqual(uriInfo.validate(sample), true);
233 
234  sample = "http://songbirdnest.com/aus/blog";
235  assertEqual(uriInfo.validate(sample), true);
236 
237  sample = "file:///Users/Me/Music";
238  uriInfo.constrainScheme = "http";
239  assertEqual(uriInfo.validate(sample), false);
240 
241  sample = "http://shoot.the.tv";
242  assertEqual(uriInfo.validate(sample), true);
243 
244  sample = "HTTp://PoorLy.FormaTTEd.URL.com/ArRrRg";
245  assertEqual(uriInfo.format(sample), "http://poorly.formatted.url.com/ArRrRg");
246 }
247 
248 function testDatetimeInfo() {
249  var datetimeInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Datetime;1"]
250  .createInstance(Ci.sbIDatetimePropertyInfo);
251 
252  datetimeInfo.id = "DatetimeInfo";
253  assertEqual(datetimeInfo.type, "datetime");
254 
255  var sample = "0";
256  datetimeInfo.timeType = Ci.sbIDatetimePropertyInfo.TIMETYPE_DATETIME;
257 
258  assertEqual(datetimeInfo.validate(sample), true);
259  log(datetimeInfo.format(sample));
260 
261  sample = "12431235123412499";
262  assertEqual(datetimeInfo.validate(sample), true);
263  log(datetimeInfo.format(sample));
264 }
265 
266 function testDurationInfo() {
267  var durationInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Duration;1"]
268  .createInstance(Ci.sbIDurationPropertyInfo);
269 
270  durationInfo.id = "DurationInfo";
271  assertEqual(durationInfo.type, "duration");
272 
273  var sample = "0";
274 
275  assertEqual(durationInfo.validate(sample), true);
276  log(durationInfo.format(sample));
277 
278  sample = "12431235123412499";
279  assertEqual(durationInfo.validate(sample), true);
280  log(durationInfo.format(sample));
281 
282  durationInfo.durationWithMilliseconds = true;
283  log(durationInfo.format(sample));
284 }
285 
286 function testBooleanInfo() {
287  var booleanInfo = Cc["@songbirdnest.com/Songbird/Properties/Info/Boolean;1"]
288  .createInstance(Ci.sbIBooleanPropertyInfo);
289 
290  booleanInfo.id = "BooleanInfo";
291  assertEqual(booleanInfo.type, "boolean");
292 
293  var sample = "not a boolean";
294  assertEqual(booleanInfo.validate(sample), false);
295  try {
296  booleanInfo.format(sample);
297  }
298  catch(err) {
299  assertEqual(err.result, Cr.NS_ERROR_INVALID_ARG);
300  }
301 
302  sample = "0";
303  assertEqual(booleanInfo.validate(sample), true);
304  assertEqual(booleanInfo.format(sample), "");
305  assertEqual(booleanInfo.makeSearchable(sample), "0");
306 
307  sample = "1";
308  assertEqual(booleanInfo.validate(sample), true);
309  assertEqual(booleanInfo.format(sample), "");
310  assertEqual(booleanInfo.makeSearchable(sample), "1");
311 
312  sample = "";
313  assertEqual(booleanInfo.validate(sample), true);
314  assertEqual(booleanInfo.format(sample), "");
315  assertEqual(booleanInfo.makeSearchable(sample), "");
316 
317  var tvpi = booleanInfo.QueryInterface(Ci.sbITreeViewPropertyInfo);
318  assertEqual(tvpi.getCellValue(null), "0");
319  assertEqual(tvpi.getCellValue(""), "0");
320  assertEqual(tvpi.getCellValue("0"), "0");
321  assertEqual(tvpi.getCellValue("1"), "1");
322 
323  assertEqual(tvpi.getCellProperties(null), "checkbox unchecked");
324  assertEqual(tvpi.getCellProperties(""), "checkbox unchecked");
325  assertEqual(tvpi.getCellProperties("0"), "checkbox unchecked");
326  assertEqual(tvpi.getCellProperties("1"), "checkbox checked");
327 }
328 
329 function runTest () {
330 
331  log("Testing TextPropertyInfo...");
332  testTextInfo();
333  log("OK");
334 
335  log("Testing NumberPropertyInfo...");
336  testNumberInfo();
338  log("OK");
339 
340  log("Testing URIPropertyInfo...");
341  testUriInfo();
342  log("OK");
343 
344  log("Testing DatetimePropertyInfo...");
346  log("OK");
347 
348  log("Testing DurationPropertyInfo...");
350  log("OK");
351 
352  log("Testing BooleanPropertyInfo...");
353  testBooleanInfo();
354  log("OK");
355 }
const Cc
function testDatetimeInfo()
function testNumberInfoFloatingPoint()
function log(s)
function testNumberInfo()
function assertTrue(aTest, aMessage)
function testBooleanInfo()
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Advanced DataRemote unit tests.
function testUriInfo()
return null
Definition: FeedWriter.js:1143
function testTextInfo()
Test file.
function testDurationInfo()
const Cr
const Ci