1 var eventClass = C.classes["@mozilla.org/calendar/event;1"];
2 var eventIID = C.interfaces.calIEvent;
3
4 var attendeeClass = C.classes["@mozilla.org/calendar/attendee;1"];
5 var attendeeIID = C.interfaces.calIAttendee;
6
7 dump("* Creating event.\n");
8 var e = eventClass.createInstance(eventIID);
9 dump("* Creating attendee.\n");
10 var a1 = attendeeClass.createInstance(attendeeIID);
11 dump("* Testing attendee set/get.\n");
12 var properties = ["id", "commonName", "rsvp", "role", "participationStatus",
13 "userType"];
14 var values = ["myid", "mycn", true, attendeeIID.ROLE_CHAIR,
15 attendeeIID.PARTSTAT_DECLINED,
16 attendeeIID.CUTYPE_RESOURCE];
17 if (properties.length != values.length)
18 throw "Test bug: mismatched properties and values arrays!";
19
20 for (var i = 0; i < properties.length; i++) {
21 a1[properties[i]] = values[i];
22 if (a1[properties[i]] != values[i]) {
23 throw "FAILED! " + properties[i] + " not set to " + values[i];
24 }
25 }
26
27 dump("* Adding attendee to event.\n");
28 e.addAttendee(a1);
29 dump("* Adding 2nd attendee to event.\n");
30 var a2 = attendeeClass.createInstance(attendeeIID);
31 a2.id = "myid2";
32 e.addAttendee(a2);
33
34 dump("* Finding by ID.\n");
35
findById
36 function findById(id, a) {
37 var foundAttendee = e.getAttendeeById(id);
38 if (foundAttendee != a) {
39 throw "FAILED! wrong attendee returned for + '" +
40 id + "' (got " + foundAttendee + ", expected " + a + ")";
41 }
42 }
43
44 findById("myid", a1);
45 findById("myid2", a2);
46
47 dump("* Searching getAttendees results\n");
48 var found1, found2;
49
findAttendeesInResults
50 function findAttendeesInResults(expectedCount) {
51 if (expectedCount == undefined)
52 throw "TEST BUG: expectedCount not passed to findAttendeesInResults";
53 var countObj = {};
54 dump(" Getting all attendees.\n");
55 var allAttendees = e.getAttendees(countObj);
56 if (countObj.value != allAttendees.length) {
57 throw "FAILED! out count (" + countObj.value + ") != .length (" +
58 allAttendees.length + ")";
59 }
60
61 if (allAttendees.length != expectedCount) {
62 throw "FAILED! expected to get back " + expectedCount +
63 " attendees, got " + allAttendees.length;
64 }
65
66 found1 = false, found2 = false;
67 for (var i = 0; i < expectedCount; i++) {
68 if (allAttendees[i] == a1)
69 found1 = true;
70 else if (allAttendees[i] == a2)
71 found2 = true;
72 else {
73 throw "FAILED! unknown attendee " + allAttendees[i] +
74 " (we added " + a1 + " and " + a2 + + ")";
75 }
76 }
77 }
78 findAttendeesInResults(2);
79 if (!found1)
80 throw "FAILED! didn't find attendee1 (" + a1 + ") in results";
81 if (!found2)
82 throw "FAILED! didn't find attendee2 (" + a2 + ") in results";
83
84 dump("* Removing attendee.\n");
85 e.removeAttendee(a1);
86 if (e.getAttendeeById(a1.id) != null)
87 throw "FAILED! got back removed attendee " + a1 + " by id";
88 findById("myid2", a2);
89 found1 = false, found2 = false;
90 findAttendeesInResults(1);
91 if (found1)
92 throw "FAILED! found removed attendee " + a1 + " in getAttendees results";
93 if (!found2) {
94 throw "FAILED! didn't find remaining attendee " + a2 +
95 " in getAttendees results";
96 }
97
98 dump("* Readding attendee.\n");
99 e.addAttendee(a1);
100 findById("myid", a1);
101 findById("myid2", a2);
102 findAttendeesInResults(2);
103 if (!found1)
104 throw "FAILED! didn't find attendee1 (" + a1 + ") in results";
105 if (!found2)
106 throw "FAILED! didn't find attendee2 (" + a2 + ") in results";
107
108 dump("* Making attendee immutable.\n");
109 a1.makeImmutable();
testImmutability
110 function testImmutability(a) {
111 if (a.isMutable) {
112 throw "FAILED! Attendee " + a +
113 " should be immutable, but claims otherwise";
114 }
115 for (var i = 0; i < properties.length; i++) {
116 var old = a[properties[i]];
117 var threw;
118 try {
119 a[properties[i]] = old + 1;
120 threw = false;
121 } catch (e) {
122 threw = true;
123 }
124 if (!threw) {
125 throw "FAILED! no error thrown setting " + properties[i] +
126 "on immutable attendee " + a;
127 }
128 if (a[properties[i]] != old) {
129 throw "FAILED! setting " + properties[i] + " on " + a +
130 " threw, but changed value anyway!";
131 }
132 }
133 }
134
135 testImmutability(a1);
136 dump("* Testing cascaded immutability (event -> attendee).\n");
137 e.makeImmutable();
138 testImmutability(a2);
139
140 dump("* Testing cloning\n");
141 var ec = e.clone();
142 var clonedatts = ec.getAttendees({});
143 var atts = e.getAttendees({});
144 if (atts.length != clonedatts.length) {
145 throw "FAILED! cloned event has " + clonedatts.length +
146 "attendees, original had " + atts.length;
147 }
148 for (i = 0; i < clonedatts.length; i++) {
149 if (atts[i] == clonedatts[i])
150 throw "FAILED! attendee " + atts[i].id + " shared with clone!";
151 if (atts[i].id != clonedatts[i].id) {
152 throw "FAILED! id mismatch on index " + i + ": " +
153 atts[i].id + " != " + clonedatts[i].id;
154 }
155 }
156 dump("PASSED!\n");