1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Metrics Service.
16 * The Initial Developer of the Original Code is Google Inc.
17 * Portions created by the Initial Developer are Copyright (C) 2006
18 * the Initial Developer. All Rights Reserved.
21 * Brian Ryner <bryner@brianryner.com>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 // Unit test for the MetricsEventItem object
39 function run_test () {
40 for (var i
= 0; i
< tests
.length
&& tests
[i
][0]; ++i
) {
41 if (!tests
[i
][0].call()) {
42 do_throw(tests
[i
][1]);
48 [ test_create
, "createEventItem failed" ],
49 [ test_properties
, "properties set/get failed" ],
50 [ test_append_count
, "appendChild/childCount failed" ],
51 [ test_childat
, "childAt failed" ],
52 [ test_indexof
, "indexOf failed" ],
53 [ test_insert
, "insertChildAt failed" ],
54 [ test_remove
, "removeChildAt failed" ],
55 [ test_replace
, "replaceChildAt failed" ],
56 [ test_clear
, "clearChildren failed" ],
60 function test_create() {
61 var item
= createEventItem(EventNS
, "test");
62 do_check_neq(item
, null);
64 do_check_eq(item
.itemNamespace
, EventNS
);
65 do_check_eq(item
.itemName
, "test");
66 do_check_eq(item
.properties
, null);
67 do_check_eq(item
.childCount
, 0);
71 function test_properties() {
72 var item
= createEventItem(EventNS
, "test");
76 QueryInterface: function(iid
) {
77 if (iid
.equals(Components
.interfaces
.nsIPropertyBag
) ||
78 iid
.equals(Components
.interfaces
.nsISupports
)) {
83 item
.properties
= properties
;
85 // XPConnect has created a PropertyBag wrapper for us, so we can't
86 // actually check equality between properties and item.properties.
88 var month
= item
.properties
.getProperty("month");
89 do_check_eq(typeof(month
), "string");
90 do_check_eq(month
, "April");
92 var year
= item
.properties
.getProperty("year");
93 do_check_eq(typeof(year
), "number");
94 do_check_eq(year
, 2006);
96 var day
= item
.properties
.getProperty("day");
97 do_check_eq(day
, undefined);
102 function test_append_count() {
103 var item
= createEventItem(EventNS
, "test");
104 var children
= buildItemChildren(item
);
105 do_check_eq(item
.childCount
, children
.length
);
107 // test input validation
109 item
.appendChild(null);
110 do_throw("appendChild(null) should fail");
113 do_check_eq(item
.childCount
, children
.length
);
117 function test_childat() {
118 var item
= createEventItem(EventNS
, "test");
119 var children
= buildItemChildren(item
);
121 // test all of the valid children with chilAt().
122 compareItemChildren(item
, children
);
124 // test input validation
127 do_throw("childAt(-1)");
130 item
.childAt(children
.length
);
131 do_throw("childAt(children.length)");
137 function test_indexof() {
138 var item
= createEventItem(EventNS
, "test");
139 var children
= buildItemChildren(item
);
140 for (var i
= 0; i
< children
.length
; ++i
) {
141 do_check_eq(item
.indexOf(children
[i
]), i
);
144 do_check_eq(item
.indexOf(createEventItem(EventNS
, "nothere")), -1);
145 do_check_eq(item
.indexOf(null), -1);
150 function test_insert() {
151 var item
= createEventItem(EventNS
, "test");
152 var children
= buildItemChildren(item
);
155 var newChild
= createEventItem(EventNS
, "newchild");
156 item
.insertChildAt(newChild
, 1);
157 children
.splice(1, 0, newChild
);
158 compareItemChildren(item
, children
);
160 // test inserting at the end
161 newChild
= createEventItem(EventNS
, "newchild2");
162 item
.insertChildAt(newChild
, item
.childCount
);
163 children
.push(newChild
);
164 compareItemChildren(item
, children
);
166 // test input validation
168 item
.insertChildAt(newChild
, -1);
169 do_throw("insertChildAt(-1)");
171 compareItemChildren(item
, children
);
174 item
.insertChildAt(newChild
, item
.childCount
+ 1);
175 do_throw("insertChildAt past end");
177 compareItemChildren(item
, children
);
180 item
.insertChildAt(null, item
.childCount
);
181 do_throw("insertChildAt(null) should fail");
183 compareItemChildren(item
, children
);
188 function test_remove() {
189 var item
= createEventItem(EventNS
, "test");
190 var children
= buildItemChildren(item
);
192 item
.removeChildAt(3);
193 children
.splice(3, 1);
194 compareItemChildren(item
, children
);
196 // test input validation
198 item
.removeChildAt(-1);
199 do_throw("removeChildAt(-1)");
201 compareItemChildren(item
, children
);
204 item
.removeChildAt(item
.childCount
);
205 do_throw("removeChildAt past end");
207 compareItemChildren(item
, children
);
212 function test_replace() {
213 var item
= createEventItem(EventNS
, "test");
214 var children
= buildItemChildren(item
);
216 var newChild
= createEventItem(EventNS
, "newchild");
217 item
.replaceChildAt(newChild
, 6);
218 children
[6] = newChild
;
219 compareItemChildren(item
, children
);
221 // test input validation
223 item
.replaceChildAt(newChild
, -1);
224 do_throw("replaceChildAt(-1)");
226 compareItemChildren(item
, children
);
229 item
.replaceChildAt(newChild
, item
.childCount
);
230 do_throw("replaceChildAt past end");
232 compareItemChildren(item
, children
);
237 function test_clear() {
238 var item
= createEventItem(EventNS
, "test");
239 buildItemChildren(item
);
241 item
.clearChildren();
242 compareItemChildren(item
, []);
244 item
.clearChildren();
245 compareItemChildren(item
, []);