2018-02-18 13:53:20 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
function run_test() {
|
2019-08-29 22:15:53 +03:00
|
|
|
test_listener_set();
|
|
|
|
test_observer_set();
|
|
|
|
test_operation_group();
|
2018-02-18 13:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_listener_set() {
|
2023-09-22 03:41:53 +03:00
|
|
|
const set = new cal.data.ListenerSet(Ci.calIOperationListener);
|
2019-08-29 22:15:53 +03:00
|
|
|
let listener1Id = null;
|
|
|
|
let listener2Id = null;
|
|
|
|
|
2023-09-22 03:41:53 +03:00
|
|
|
const listener1 = cal.createAdapter("calIOperationListener", {
|
2024-03-22 22:12:04 +03:00
|
|
|
onOperationComplete(aCalendar, aStatus, aOpType, aId) {
|
2019-08-29 22:15:53 +03:00
|
|
|
listener1Id = aId;
|
|
|
|
},
|
|
|
|
});
|
2023-09-22 03:41:53 +03:00
|
|
|
const listener2 = cal.createAdapter("calIOperationListener", {
|
2024-03-22 22:12:04 +03:00
|
|
|
onOperationComplete(aCalendar, aStatus, aOpType, aId) {
|
2019-08-29 22:15:53 +03:00
|
|
|
listener2Id = aId;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
set.add(listener1);
|
|
|
|
set.add(listener2);
|
|
|
|
set.notify("onOperationComplete", [null, null, null, "test", null]);
|
|
|
|
equal(listener1Id, "test");
|
|
|
|
equal(listener2Id, "test");
|
|
|
|
|
|
|
|
set.delete(listener2);
|
|
|
|
listener1Id = listener2Id = null;
|
|
|
|
set.notify("onOperationComplete", [null, null, null, "test2", null]);
|
|
|
|
equal(listener1Id, "test2");
|
|
|
|
strictEqual(listener2Id, null);
|
|
|
|
|
|
|
|
// Re-adding the listener may lead to an endless loop if the notify
|
|
|
|
// function uses a live list of observers.
|
|
|
|
let called = 0;
|
2023-09-22 03:41:53 +03:00
|
|
|
const listener3 = cal.createAdapter("calIOperationListener", {
|
2024-03-22 22:12:04 +03:00
|
|
|
onOperationComplete() {
|
2019-08-29 22:15:53 +03:00
|
|
|
set.delete(listener3);
|
|
|
|
if (called == 0) {
|
|
|
|
set.add(listener3);
|
|
|
|
}
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
set.add(listener3);
|
|
|
|
set.notify("onOperationComplete", [null, null, null, "test3", null]);
|
|
|
|
equal(called, 1);
|
2018-02-18 13:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_observer_set() {
|
2023-09-22 03:41:53 +03:00
|
|
|
const set = new cal.data.ObserverSet(Ci.calIObserver);
|
2019-08-29 22:15:53 +03:00
|
|
|
let listenerCountBegin1 = 0;
|
|
|
|
let listenerCountBegin2 = 0;
|
|
|
|
let listenerCountEnd1 = 0;
|
|
|
|
let listenerCountEnd2 = 0;
|
|
|
|
|
2023-09-22 03:41:53 +03:00
|
|
|
const listener1 = cal.createAdapter("calIObserver", {
|
2020-02-20 01:24:47 +03:00
|
|
|
onStartBatch() {
|
2019-08-29 22:15:53 +03:00
|
|
|
listenerCountBegin1++;
|
|
|
|
},
|
2020-02-20 01:24:47 +03:00
|
|
|
onEndBatch() {
|
2019-08-29 22:15:53 +03:00
|
|
|
listenerCountEnd1++;
|
|
|
|
},
|
|
|
|
});
|
2023-09-22 03:41:53 +03:00
|
|
|
const listener2 = cal.createAdapter("calIObserver", {
|
2020-02-20 01:24:47 +03:00
|
|
|
onStartBatch() {
|
2019-08-29 22:15:53 +03:00
|
|
|
listenerCountBegin2++;
|
|
|
|
},
|
2020-02-20 01:24:47 +03:00
|
|
|
onEndBatch() {
|
2019-08-29 22:15:53 +03:00
|
|
|
listenerCountEnd2++;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
set.add(listener1);
|
|
|
|
equal(listenerCountBegin1, 0);
|
|
|
|
equal(listenerCountEnd1, 0);
|
|
|
|
equal(set.batchCount, 0);
|
|
|
|
|
|
|
|
set.notify("onStartBatch");
|
|
|
|
equal(listenerCountBegin1, 1);
|
|
|
|
equal(listenerCountEnd1, 0);
|
|
|
|
equal(set.batchCount, 1);
|
|
|
|
|
|
|
|
set.add(listener2);
|
|
|
|
equal(listenerCountBegin1, 1);
|
|
|
|
equal(listenerCountEnd1, 0);
|
|
|
|
equal(listenerCountBegin2, 1);
|
|
|
|
equal(listenerCountEnd2, 0);
|
|
|
|
equal(set.batchCount, 1);
|
|
|
|
|
|
|
|
set.add(listener1);
|
|
|
|
equal(listenerCountBegin1, 1);
|
|
|
|
equal(listenerCountEnd1, 0);
|
|
|
|
equal(listenerCountBegin2, 1);
|
|
|
|
equal(listenerCountEnd2, 0);
|
|
|
|
equal(set.batchCount, 1);
|
|
|
|
|
|
|
|
set.notify("onEndBatch");
|
|
|
|
equal(listenerCountBegin1, 1);
|
|
|
|
equal(listenerCountEnd1, 1);
|
|
|
|
equal(listenerCountBegin2, 1);
|
|
|
|
equal(listenerCountEnd2, 1);
|
|
|
|
equal(set.batchCount, 0);
|
2018-02-18 13:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_operation_group() {
|
2019-08-29 22:15:53 +03:00
|
|
|
let calledCancel = false;
|
|
|
|
let calledOperationCancel = null;
|
|
|
|
let group = new cal.data.OperationGroup();
|
|
|
|
ok(group.id.endsWith("-0"));
|
|
|
|
ok(group.isPending);
|
|
|
|
equal(group.status, Cr.NS_OK);
|
|
|
|
ok(group.isEmpty);
|
|
|
|
|
2023-09-22 03:41:53 +03:00
|
|
|
const operation = {
|
2019-08-29 22:15:53 +03:00
|
|
|
id: 123,
|
|
|
|
isPending: true,
|
|
|
|
cancel: status => {
|
|
|
|
calledOperationCancel = status;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
group.add(operation);
|
|
|
|
ok(!group.isEmpty);
|
|
|
|
|
|
|
|
group.notifyCompleted(Cr.NS_ERROR_FAILURE);
|
|
|
|
ok(!group.isPending);
|
|
|
|
equal(group.status, Cr.NS_ERROR_FAILURE);
|
|
|
|
strictEqual(calledOperationCancel, null);
|
|
|
|
|
|
|
|
group.remove(operation);
|
|
|
|
ok(group.isEmpty);
|
|
|
|
|
|
|
|
group = new cal.data.OperationGroup(() => {
|
|
|
|
calledCancel = true;
|
|
|
|
});
|
|
|
|
ok(group.id.endsWith("-1"));
|
|
|
|
group.add(operation);
|
|
|
|
|
|
|
|
group.cancel();
|
|
|
|
equal(group.status, Ci.calIErrors.OPERATION_CANCELLED);
|
|
|
|
equal(calledOperationCancel, Ci.calIErrors.OPERATION_CANCELLED);
|
|
|
|
ok(calledCancel);
|
2018-02-18 13:53:20 +03:00
|
|
|
}
|