summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/unit/test_nsIMutableArray.js
blob: b491aee96401f7a64971cac569aaf5220424cb87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var Ci = Components.interfaces;
var Cr = Components.results;
var Cc = Components.classes;
var CC = Components.Constructor;

var MutableArray = CC("@mozilla.org/array;1", "nsIMutableArray");
var SupportsString = CC("@mozilla.org/supports-string;1", "nsISupportsString");

function create_n_element_array(n)
{
  var arr = new MutableArray();
  for (let i=0; i<n; i++) {
    let str = new SupportsString();
    str.data = "element " + i;
    arr.appendElement(str, false);
  }
  return arr;
}

function test_appending_null_actually_inserts()
{
  var arr = new MutableArray();
  do_check_eq(0, arr.length);
  arr.appendElement(null, false);
  do_check_eq(1, arr.length);
}

function test_object_gets_appended()
{
  var arr = new MutableArray();
  var str = new SupportsString();
  str.data = "hello";
  arr.appendElement(str, false);
  do_check_eq(1, arr.length);
  var obj = arr.queryElementAt(0, Ci.nsISupportsString);
  do_check_eq(str, obj);
}

function test_insert_at_beginning()
{
  var arr = create_n_element_array(5);
  // just a sanity check
  do_check_eq(5, arr.length);
  var str = new SupportsString();
  str.data = "hello";
  arr.insertElementAt(str, 0, false);
  do_check_eq(6, arr.length);
  var obj = arr.queryElementAt(0, Ci.nsISupportsString);
  do_check_eq(str, obj);
  // check the data of all the other objects
  for (let i=1; i<arr.length; i++) {
    let obj = arr.queryElementAt(i, Ci.nsISupportsString);
    do_check_eq("element " + (i-1), obj.data);
  }
}

function test_replace_element()
{
  var arr = create_n_element_array(5);
  // just a sanity check
  do_check_eq(5, arr.length);
  var str = new SupportsString();
  str.data = "hello";
  // replace first element
  arr.replaceElementAt(str, 0, false);
  do_check_eq(5, arr.length);
  var obj = arr.queryElementAt(0, Ci.nsISupportsString);
  do_check_eq(str, obj);
  // replace last element
  arr.replaceElementAt(str, arr.length - 1, false);
  do_check_eq(5, arr.length);
  obj = arr.queryElementAt(arr.length - 1, Ci.nsISupportsString);
  do_check_eq(str, obj);
  // replace after last element, should insert empty elements
  arr.replaceElementAt(str, 9, false);
  do_check_eq(10, arr.length);
  obj = arr.queryElementAt(9, Ci.nsISupportsString);
  do_check_eq(str, obj);
  // AFAIK there's no way to check the empty elements, since you can't QI them.
}

function test_clear()
{
  var arr = create_n_element_array(5);
  // just a sanity check
  do_check_eq(5, arr.length);
  arr.clear();
  do_check_eq(0, arr.length);
}

function test_enumerate()
{
  var arr = create_n_element_array(5);
  do_check_eq(5, arr.length);
  var en = arr.enumerate();
  var i = 0;
  while (en.hasMoreElements()) {
    let str = en.getNext();
    do_check_true(str instanceof Ci.nsISupportsString);
    do_check_eq(str.data, "element " + i);
    i++;
  }
  do_check_eq(arr.length, i);
}

function test_nssupportsarray_interop() {
  // Tests to check that an nsSupportsArray instance can behave like an
  // nsIArray.
  let test = Components.classes["@mozilla.org/supports-array;1"]
             .createInstance(Ci.nsISupportsArray);

  let str = new SupportsString();
  str.data = "element";
  test.AppendElement(str);

  // Now query to an nsIArray.
  let iarray = test.QueryInterface(Ci.nsIArray);
  do_check_neq(iarray, null);

  // Make sure |nsIArray.length| works.
  do_check_eq(iarray.length, 1);

  // Make sure |nsIArray.queryElementAt| works.
  let elm = iarray.queryElementAt(0, Ci.nsISupportsString);
  do_check_eq(elm.data, "element");

  // Make sure |nsIArray.indexOf| works.
  let idx = iarray.indexOf(0, str);
  do_check_eq(idx, 0);

  // Make sure |nsIArray.enumerate| works.
  let en = iarray.enumerate();
  do_check_neq(en, null);
  let i = 0;
  while (en.hasMoreElements()) {
    let str = en.getNext();
    do_check_true(str instanceof Ci.nsISupportsString);
    do_check_eq(str.data, "element");
    i++;
  }

  do_check_eq(iarray.length, i);
}

function test_nsiarrayextensions() {
  // Tests to check that the extensions that make an nsArray act like an
  // nsISupportsArray for iteration purposes works.
  // Note: we do not want to QI here, just want to make sure the magic glue
  // works as a drop-in replacement.

  let fake_nsisupports_array = create_n_element_array(5);

  // Check that |Count| works.
  do_check_eq(5, fake_nsisupports_array.Count());

  for (let i = 0; i < fake_nsisupports_array.Count(); i++) {
    // Check that the generic |GetElementAt| works.
    let elm = fake_nsisupports_array.GetElementAt(i);
    do_check_neq(elm, null);
    let str = elm.QueryInterface(Ci.nsISupportsString);
    do_check_neq(str, null);
    do_check_eq(str.data, "element " + i);
  }
}

var tests = [
  test_appending_null_actually_inserts,
  test_object_gets_appended,
  test_insert_at_beginning,
  test_replace_element,
  test_clear,
  test_enumerate,
  test_nssupportsarray_interop,
  test_nsiarrayextensions,
];

function run_test() {
  for (var i = 0; i < tests.length; i++)
    tests[i]();
}