| 1 |
(function () { |
| 2 |
|
| 3 |
let BACKUP = {}; |
| 4 |
|
| 5 |
QUnit.config.autostart = false; |
| 6 |
QUnit.config.testTimeout = 6400; |
| 7 |
|
| 8 |
// disable storage persistence |
| 9 |
// unit tests shouldn't be able to affect your Badger's storage |
| 10 |
chrome.storage.local.set = (_, callback) => { |
| 11 |
if (callback) { |
| 12 |
setTimeout(function () { |
| 13 |
callback(null); |
| 14 |
}, 0); |
| 15 |
} |
| 16 |
}; |
| 17 |
|
| 18 |
// make it seem like there is nothing in storage |
| 19 |
// unit tests shouldn't read from your Badger's storage either |
| 20 |
chrome.storage.local.get = (keys, callback) => { |
| 21 |
// callback has to be async |
| 22 |
setTimeout(function () { |
| 23 |
callback({ |
| 24 |
// don't open the new user intro page or load seed data |
| 25 |
private_storage: { |
| 26 |
badgerVersion: chrome.runtime.getManifest().version, |
| 27 |
doneLoadingSeed: true, |
| 28 |
} |
| 29 |
}); |
| 30 |
}, 0); |
| 31 |
}; |
| 32 |
|
| 33 |
// reset state between tests |
| 34 |
// to prevent tests affecting each other via side effects |
| 35 |
QUnit.testStart(() => { |
| 36 |
// back up settings and heuristic learning |
| 37 |
// TODO any other state we should reset? tabData? |
| 38 |
badger.storage.KEYS.forEach(store_name => { |
| 39 |
let store = badger.storage.getStore(store_name); |
| 40 |
BACKUP[store_name] = store.getItemClones(); |
| 41 |
}); |
| 42 |
}); |
| 43 |
|
| 44 |
QUnit.testDone(() => { |
| 45 |
// restore original settings and heuristic learning |
| 46 |
badger.storage.KEYS.forEach(store_name => { |
| 47 |
let store = badger.storage.getStore(store_name); |
| 48 |
store._store = BACKUP[store_name]; |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
// kick off tests when we have what we need from Badger |
| 53 |
(function () { |
| 54 |
|
| 55 |
const WAIT_INTERVAL = 10, |
| 56 |
MAX_WAIT = 1000; |
| 57 |
|
| 58 |
let elapsed = 0; |
| 59 |
|
| 60 |
function wait_for_badger() { |
| 61 |
elapsed += WAIT_INTERVAL; |
| 62 |
|
| 63 |
if (elapsed >= MAX_WAIT) { |
| 64 |
// give up |
| 65 |
QUnit.start(); |
| 66 |
} |
| 67 |
|
| 68 |
if (typeof badger == "object" && badger.INITIALIZED) { |
| 69 |
QUnit.start(); |
| 70 |
} else { |
| 71 |
setTimeout(wait_for_badger, WAIT_INTERVAL); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
setTimeout(wait_for_badger, WAIT_INTERVAL); |
| 76 |
|
| 77 |
}()); |
| 78 |
|
| 79 |
}()); |