Bug 714202 - Fix implementation of AddonsReconciler.pruneChangesBeforeDate(); r=rnewman

This commit is contained in:
Gregory Szorc 2011-12-30 13:55:19 -08:00
parent d555d02e96
commit d042d50888
2 changed files with 44 additions and 7 deletions

View File

@ -547,13 +547,9 @@ AddonsReconciler.prototype = {
pruneChangesBeforeDate: function pruneChangesBeforeDate(date) {
this._ensureStateLoaded();
while (this._changes.length > 0) {
if (this._changes[0][0] >= date) {
return;
}
delete this._changes[0];
}
this._changes = this._changes.filter(function test_age(change) {
return change[0] >= date;
});
},
/**

View File

@ -150,3 +150,44 @@ add_test(function test_load_state_future_version() {
run_next_test();
});
});
add_test(function test_prune_changes_before_date() {
_("Ensure that old changes are pruned properly.");
let reconciler = new AddonsReconciler();
reconciler._ensureStateLoaded();
reconciler._changes = [];
let now = new Date();
const HOUR_MS = 1000 * 60 * 60;
_("Ensure pruning an empty changes array works.");
reconciler.pruneChangesBeforeDate(now);
do_check_eq(0, reconciler._changes.length);
let old = new Date(now.getTime() - HOUR_MS);
let young = new Date(now.getTime() - 1000);
reconciler._changes.push([old, CHANGE_INSTALLED, "foo"]);
reconciler._changes.push([young, CHANGE_INSTALLED, "bar"]);
do_check_eq(2, reconciler._changes.length);
_("Ensure pruning with an old time won't delete anything.");
let threshold = new Date(old.getTime() - 1);
reconciler.pruneChangesBeforeDate(threshold);
do_check_eq(2, reconciler._changes.length);
_("Ensure pruning a single item works.");
let threshold = new Date(young.getTime() - 1000);
reconciler.pruneChangesBeforeDate(threshold);
do_check_eq(1, reconciler._changes.length);
do_check_neq(undefined, reconciler._changes[0]);
do_check_eq(young, reconciler._changes[0][0]);
do_check_eq("bar", reconciler._changes[0][2]);
_("Ensure pruning all changes works.");
reconciler._changes.push([old, CHANGE_INSTALLED, "foo"]);
reconciler.pruneChangesBeforeDate(now);
do_check_eq(0, reconciler._changes.length);
run_next_test();
});