Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://gre/modules/AddonManager.jsm"); |
michael@0 | 7 | Cu.import("resource://services-sync/addonsreconciler.js"); |
michael@0 | 8 | Cu.import("resource://services-sync/engines/addons.js"); |
michael@0 | 9 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 10 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 11 | |
michael@0 | 12 | loadAddonTestFunctions(); |
michael@0 | 13 | startupManager(); |
michael@0 | 14 | |
michael@0 | 15 | function run_test() { |
michael@0 | 16 | initTestLogging("Trace"); |
michael@0 | 17 | Log.repository.getLogger("Sync.AddonsReconciler").level = Log.Level.Trace; |
michael@0 | 18 | Log.repository.getLogger("Sync.AddonsReconciler").level = |
michael@0 | 19 | Log.Level.Trace; |
michael@0 | 20 | |
michael@0 | 21 | Svc.Prefs.set("engine.addons", true); |
michael@0 | 22 | Service.engineManager.register(AddonsEngine); |
michael@0 | 23 | |
michael@0 | 24 | run_next_test(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | add_test(function test_defaults() { |
michael@0 | 28 | _("Ensure new objects have reasonable defaults."); |
michael@0 | 29 | |
michael@0 | 30 | let reconciler = new AddonsReconciler(); |
michael@0 | 31 | |
michael@0 | 32 | do_check_false(reconciler._listening); |
michael@0 | 33 | do_check_eq("object", typeof(reconciler.addons)); |
michael@0 | 34 | do_check_eq(0, Object.keys(reconciler.addons).length); |
michael@0 | 35 | do_check_eq(0, reconciler._changes.length); |
michael@0 | 36 | do_check_eq(0, reconciler._listeners.length); |
michael@0 | 37 | |
michael@0 | 38 | run_next_test(); |
michael@0 | 39 | }); |
michael@0 | 40 | |
michael@0 | 41 | add_test(function test_load_state_empty_file() { |
michael@0 | 42 | _("Ensure loading from a missing file results in defaults being set."); |
michael@0 | 43 | |
michael@0 | 44 | let reconciler = new AddonsReconciler(); |
michael@0 | 45 | |
michael@0 | 46 | reconciler.loadState(null, function(error, loaded) { |
michael@0 | 47 | do_check_eq(null, error); |
michael@0 | 48 | do_check_false(loaded); |
michael@0 | 49 | |
michael@0 | 50 | do_check_eq("object", typeof(reconciler.addons)); |
michael@0 | 51 | do_check_eq(0, Object.keys(reconciler.addons).length); |
michael@0 | 52 | do_check_eq(0, reconciler._changes.length); |
michael@0 | 53 | |
michael@0 | 54 | run_next_test(); |
michael@0 | 55 | }); |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | add_test(function test_install_detection() { |
michael@0 | 59 | _("Ensure that add-on installation results in appropriate side-effects."); |
michael@0 | 60 | |
michael@0 | 61 | let reconciler = new AddonsReconciler(); |
michael@0 | 62 | reconciler.startListening(); |
michael@0 | 63 | |
michael@0 | 64 | let before = new Date(); |
michael@0 | 65 | let addon = installAddon("test_bootstrap1_1"); |
michael@0 | 66 | let after = new Date(); |
michael@0 | 67 | |
michael@0 | 68 | do_check_eq(1, Object.keys(reconciler.addons).length); |
michael@0 | 69 | do_check_true(addon.id in reconciler.addons); |
michael@0 | 70 | let record = reconciler.addons[addon.id]; |
michael@0 | 71 | |
michael@0 | 72 | const KEYS = ["id", "guid", "enabled", "installed", "modified", "type", |
michael@0 | 73 | "scope", "foreignInstall"]; |
michael@0 | 74 | for each (let key in KEYS) { |
michael@0 | 75 | do_check_true(key in record); |
michael@0 | 76 | do_check_neq(null, record[key]); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | do_check_eq(addon.id, record.id); |
michael@0 | 80 | do_check_eq(addon.syncGUID, record.guid); |
michael@0 | 81 | do_check_true(record.enabled); |
michael@0 | 82 | do_check_true(record.installed); |
michael@0 | 83 | do_check_true(record.modified >= before && record.modified <= after); |
michael@0 | 84 | do_check_eq("extension", record.type); |
michael@0 | 85 | do_check_false(record.foreignInstall); |
michael@0 | 86 | |
michael@0 | 87 | do_check_eq(1, reconciler._changes.length); |
michael@0 | 88 | let change = reconciler._changes[0]; |
michael@0 | 89 | do_check_true(change[0] >= before && change[1] <= after); |
michael@0 | 90 | do_check_eq(CHANGE_INSTALLED, change[1]); |
michael@0 | 91 | do_check_eq(addon.id, change[2]); |
michael@0 | 92 | |
michael@0 | 93 | uninstallAddon(addon); |
michael@0 | 94 | |
michael@0 | 95 | run_next_test(); |
michael@0 | 96 | }); |
michael@0 | 97 | |
michael@0 | 98 | add_test(function test_uninstall_detection() { |
michael@0 | 99 | _("Ensure that add-on uninstallation results in appropriate side-effects."); |
michael@0 | 100 | |
michael@0 | 101 | let reconciler = new AddonsReconciler(); |
michael@0 | 102 | reconciler.startListening(); |
michael@0 | 103 | |
michael@0 | 104 | reconciler._addons = {}; |
michael@0 | 105 | reconciler._changes = []; |
michael@0 | 106 | |
michael@0 | 107 | let addon = installAddon("test_bootstrap1_1"); |
michael@0 | 108 | let id = addon.id; |
michael@0 | 109 | let guid = addon.syncGUID; |
michael@0 | 110 | |
michael@0 | 111 | reconciler._changes = []; |
michael@0 | 112 | uninstallAddon(addon); |
michael@0 | 113 | |
michael@0 | 114 | do_check_eq(1, Object.keys(reconciler.addons).length); |
michael@0 | 115 | do_check_true(id in reconciler.addons); |
michael@0 | 116 | |
michael@0 | 117 | let record = reconciler.addons[id]; |
michael@0 | 118 | do_check_false(record.installed); |
michael@0 | 119 | |
michael@0 | 120 | do_check_eq(1, reconciler._changes.length); |
michael@0 | 121 | let change = reconciler._changes[0]; |
michael@0 | 122 | do_check_eq(CHANGE_UNINSTALLED, change[1]); |
michael@0 | 123 | do_check_eq(id, change[2]); |
michael@0 | 124 | |
michael@0 | 125 | run_next_test(); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | add_test(function test_load_state_future_version() { |
michael@0 | 129 | _("Ensure loading a file from a future version results in no data loaded."); |
michael@0 | 130 | |
michael@0 | 131 | const FILENAME = "TEST_LOAD_STATE_FUTURE_VERSION"; |
michael@0 | 132 | |
michael@0 | 133 | let reconciler = new AddonsReconciler(); |
michael@0 | 134 | |
michael@0 | 135 | // First we populate our new file. |
michael@0 | 136 | let state = {version: 100, addons: {foo: {}}, changes: [[1, 1, "foo"]]}; |
michael@0 | 137 | let cb = Async.makeSyncCallback(); |
michael@0 | 138 | |
michael@0 | 139 | // jsonSave() expects an object with ._log, so we give it a reconciler |
michael@0 | 140 | // instance. |
michael@0 | 141 | Utils.jsonSave(FILENAME, reconciler, state, cb); |
michael@0 | 142 | Async.waitForSyncCallback(cb); |
michael@0 | 143 | |
michael@0 | 144 | reconciler.loadState(FILENAME, function(error, loaded) { |
michael@0 | 145 | do_check_eq(null, error); |
michael@0 | 146 | do_check_false(loaded); |
michael@0 | 147 | |
michael@0 | 148 | do_check_eq("object", typeof(reconciler.addons)); |
michael@0 | 149 | do_check_eq(1, Object.keys(reconciler.addons).length); |
michael@0 | 150 | do_check_eq(1, reconciler._changes.length); |
michael@0 | 151 | |
michael@0 | 152 | run_next_test(); |
michael@0 | 153 | }); |
michael@0 | 154 | }); |
michael@0 | 155 | |
michael@0 | 156 | add_test(function test_prune_changes_before_date() { |
michael@0 | 157 | _("Ensure that old changes are pruned properly."); |
michael@0 | 158 | |
michael@0 | 159 | let reconciler = new AddonsReconciler(); |
michael@0 | 160 | reconciler._ensureStateLoaded(); |
michael@0 | 161 | reconciler._changes = []; |
michael@0 | 162 | |
michael@0 | 163 | let now = new Date(); |
michael@0 | 164 | const HOUR_MS = 1000 * 60 * 60; |
michael@0 | 165 | |
michael@0 | 166 | _("Ensure pruning an empty changes array works."); |
michael@0 | 167 | reconciler.pruneChangesBeforeDate(now); |
michael@0 | 168 | do_check_eq(0, reconciler._changes.length); |
michael@0 | 169 | |
michael@0 | 170 | let old = new Date(now.getTime() - HOUR_MS); |
michael@0 | 171 | let young = new Date(now.getTime() - 1000); |
michael@0 | 172 | reconciler._changes.push([old, CHANGE_INSTALLED, "foo"]); |
michael@0 | 173 | reconciler._changes.push([young, CHANGE_INSTALLED, "bar"]); |
michael@0 | 174 | do_check_eq(2, reconciler._changes.length); |
michael@0 | 175 | |
michael@0 | 176 | _("Ensure pruning with an old time won't delete anything."); |
michael@0 | 177 | let threshold = new Date(old.getTime() - 1); |
michael@0 | 178 | reconciler.pruneChangesBeforeDate(threshold); |
michael@0 | 179 | do_check_eq(2, reconciler._changes.length); |
michael@0 | 180 | |
michael@0 | 181 | _("Ensure pruning a single item works."); |
michael@0 | 182 | let threshold = new Date(young.getTime() - 1000); |
michael@0 | 183 | reconciler.pruneChangesBeforeDate(threshold); |
michael@0 | 184 | do_check_eq(1, reconciler._changes.length); |
michael@0 | 185 | do_check_neq(undefined, reconciler._changes[0]); |
michael@0 | 186 | do_check_eq(young, reconciler._changes[0][0]); |
michael@0 | 187 | do_check_eq("bar", reconciler._changes[0][2]); |
michael@0 | 188 | |
michael@0 | 189 | _("Ensure pruning all changes works."); |
michael@0 | 190 | reconciler._changes.push([old, CHANGE_INSTALLED, "foo"]); |
michael@0 | 191 | reconciler.pruneChangesBeforeDate(now); |
michael@0 | 192 | do_check_eq(0, reconciler._changes.length); |
michael@0 | 193 | |
michael@0 | 194 | run_next_test(); |
michael@0 | 195 | }); |