test_propertyarray.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 
27 function assertPropertyData(property, id, value) {
28  assertNotEqual(property, null);
29  assertNotEqual(property, undefined);
30  assertEqual(property.id, id);
31  assertEqual(property.value, value);
32 }
33 
34 function dumpArray(array) {
35  var count = array.length;
36  log ("dumpArray: " + array + " (count = " + count + ")");
37  for (var index = 0; index < count; index++) {
38  var prop = array.getPropertyAt(index);
39  log("array[" + index + "] = {id: " + prop.id + ", value: " +
40  prop.value + "}");
41  }
42 }
43 
44 function runTest() {
45  var propertyArray =
46  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"].
47  createInstance(Ci.sbIMutablePropertyArray);
48 
49  // Test appendProperty.
50  for (var index = 0; index < 15; index++) {
51  var id = "Property" + index;
52  var value = "Value" + index;
53  propertyArray.appendProperty(id, value);
54  }
55 
56  // Test length.
57  assertEqual(propertyArray.length, 15);
58 
59  var propertyArray2 =
60  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"].
61  createInstance(Ci.sbIMutablePropertyArray);
62  for (var index = 10; index < 20; index++) {
63  var id = "Property" + index;
64  var value = "Value" + index;
65  propertyArray2.appendProperty(id, value);
66  }
67 
68  // Test appendProperties without skipping duplicates.
69  var propertyArray3 =
70  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"].
71  createInstance(Ci.sbIMutablePropertyArray);
72  propertyArray3.appendProperties(propertyArray, false);
73  propertyArray3.appendProperties(propertyArray2, false);
74  assertEqual(propertyArray3.length, 25);
75 
76  // Test appendProperties skipping duplicates.
77  propertyArray3.clear();
78  propertyArray3.appendProperties(propertyArray, true);
79  propertyArray3.appendProperties(propertyArray2, true);
80  assertEqual(propertyArray3.length, 20);
81 
82  // Test queryElementAt.
83  var property = propertyArray.queryElementAt(0, Ci.sbIProperty);
84  assertPropertyData(property, "Property0", "Value0");
85 
86  property = propertyArray.queryElementAt(1, Ci.nsISupports);
87  assertPropertyData(property.QueryInterface(Ci.sbIProperty), "Property1",
88  "Value1");
89 
90  var qiException;
91  try {
92  property = propertyArray.queryElementAt(2, Ci.nsIFile);
93  } catch (e) {
94  qiException = true;
95  }
96  assertEqual(qiException, true);
97 
98  // Test getPropertyAt.
99  property = propertyArray.getPropertyAt(5);
100  assertPropertyData(property, "Property5", "Value5");
101 
102  // Test indexOf.
103  var index = propertyArray.indexOf(0, property);
104  assertEqual(index, 5);
105 
106  // Test enumerate.
107  var enumerator = propertyArray.enumerate();
108  var count = 0;
109  while (enumerator.hasMoreElements()) {
110  var property = enumerator.getNext().QueryInterface(Ci.sbIProperty);
111  assertPropertyData(property, "Property" + count, "Value" + count);
112  count++;
113  }
114  assertEqual(count, 15);
115 
116  // Test replaceElementAt.
117  var factory = Cc["@songbirdnest.com/Songbird/Properties/PropertyFactory;1"].
118  createInstance(Ci.sbIPropertyFactory);
119  var newProperty = factory.createProperty("NewProperty", "NewValue");
120 
121  propertyArray.replaceElementAt(newProperty, 9, false);
122 
123  var replaceException;
124  try {
125  propertyArray.replaceElementAt(newProperty, 13, true);
126  } catch (e) {
127  replaceException = true;
128  }
129  assertEqual(replaceException, true);
130 
131  var testProperty = propertyArray.getPropertyAt(9);
132  assertPropertyData(testProperty, "NewProperty", "NewValue");
133 
134  assertEqual(propertyArray.length, 15);
135 
136  // Test insertElementAt.
137  newProperty = factory.createProperty("SuperNewProperty", "SuperNewValue");
138 
139  propertyArray.insertElementAt(newProperty, 10, false);
140 
141  var insertException;
142  try {
143  propertyArray.insertElementAt(newProperty, 13, true);
144  } catch (e) {
145  insertException = true;
146  }
147  assertEqual(insertException, true);
148 
149  testProperty = propertyArray.getPropertyAt(9);
150  assertPropertyData(testProperty, "NewProperty", "NewValue");
151 
152  testProperty = propertyArray.getPropertyAt(11);
153  assertPropertyData(testProperty, "Property10", "Value10");
154 
155  testProperty = propertyArray.getPropertyAt(10);
156  assertPropertyData(testProperty, "SuperNewProperty", "SuperNewValue");
157 
158  assertEqual(propertyArray.length, 16);
159 
160  // Test removeElementAt.
161  propertyArray.removeElementAt(10);
162 
163  testProperty = propertyArray.getPropertyAt(9);
164  assertPropertyData(testProperty, "NewProperty", "NewValue");
165 
166  testProperty = propertyArray.getPropertyAt(10);
167  assertPropertyData(testProperty, "Property10", "Value10");
168 
169  testProperty = propertyArray.getPropertyAt(11);
170  assertPropertyData(testProperty, "Property11", "Value11");
171 
172  assertEqual(propertyArray.length, 15);
173 
174  // Test clear.
175  propertyArray.clear();
176  assertEqual(propertyArray.length, 0);
177 
178  var newTestProperty;
179  var exception;
180  try {
181  newTestProperty = propertyArray.getPropertyAt(6);
182  } catch (e) {
183  exception = true;
184  }
185  assertEqual(newTestProperty, undefined);
186  assertEqual(exception, true);
187 
188  // Test serialize
189  var propertyArray =
190  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"].
191  createInstance(Ci.sbIMutablePropertyArray);
192 
193  for (var index = 0; index < 10; index++) {
194  var id = "Property" + index;
195  var value = "Value" + index;
196  propertyArray.appendProperty(id, value);
197  }
198  var pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
199  pipe.init(false, false, 0, 0xffffffff, null);
200 
201  var oos = Cc["@mozilla.org/binaryoutputstream;1"]
202  .createInstance(Ci.nsIObjectOutputStream);
203  oos.setOutputStream(pipe.outputStream);
204  oos.writeObject(propertyArray, true);
205  oos.close();
206 
207  var ois = Cc["@mozilla.org/binaryinputstream;1"]
208  .createInstance(Ci.nsIObjectInputStream);
209  ois.setInputStream(pipe.inputStream);
210  propertyArray = ois.readObject(true)
211  ois.close();
212 
213  for (var index = 0; index < 10; index++) {
214  var property = propertyArray.queryElementAt(index, Ci.sbIProperty);
215  assertPropertyData(property, "Property" + index, "Value" + index);
216  }
217 }
218 
219 function getTempFile(name) {
220  var file = Cc["@mozilla.org/file/directory_service;1"]
221  .getService(Ci.nsIProperties)
222  .get("TmpD", Ci.nsIFile);
223  file.append(name);
224  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0664);
225  var registerFileForDelete = Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
226  .getService(Ci.nsPIExternalAppLauncher);
227  registerFileForDelete.deleteTemporaryFileOnExit(file);
228  return file;
229 }
230 
const Cc
function dumpArray(array)
function assertNotEqual(aExpected, aActual, aMessage)
inArray array
function log(s)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function assertEqual(aExpected, aActual, aMessage)
var count
Definition: test_bug7406.js:32
function runTest()
Advanced DataRemote unit tests.
return null
Definition: FeedWriter.js:1143
function getTempFile(name)
countRef value
Definition: FeedWriter.js:1423
function assertPropertyData(property, id, value)
const Ci
var file