From 029c9754f029fa358192afedff52b4d868ab0dcf Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 14 May 2015 15:44:43 -0700 Subject: [PATCH] Fix tests --- js/background.js | 1 + js/chromium.js | 2 +- js/storage.js | 7 +++++- libtextsecure/test/in_memory_axolotl_store.js | 4 ++-- libtextsecure/test/storage_test.js | 16 +++++++------ test/index.html | 1 + test/storage_test.js | 24 +++++++++++-------- 7 files changed, 34 insertions(+), 21 deletions(-) diff --git a/js/background.js b/js/background.js index a887dc3f..7019d549 100644 --- a/js/background.js +++ b/js/background.js @@ -16,6 +16,7 @@ ;(function() { 'use strict'; + storage.fetch(); storage.onready(function() { var messageReceiver; diff --git a/js/chromium.js b/js/chromium.js index 9f5ba48e..a7885336 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -180,7 +180,7 @@ }); }; - if (chrome.runtime) { + if (chrome.runtime.onInstalled) { chrome.runtime.onInstalled.addListener(function(options) { if (options.reason === 'install') { extension.install(); diff --git a/js/storage.js b/js/storage.js index 1a20c840..d6422c94 100644 --- a/js/storage.js +++ b/js/storage.js @@ -29,7 +29,6 @@ var ready = false; var items = new ItemCollection(); items.on('reset', function() { ready = true; }); - items.fetch({reset: true}); window.storage = { /***************************** *** Base Storage Routines *** @@ -62,6 +61,12 @@ } else { items.on('reset', callback); } + }, + + fetch: function() { + return new Promise(function(resolve) { + items.fetch({reset: true}).always(resolve); + }); } }; window.textsecure = window.textsecure || {}; diff --git a/libtextsecure/test/in_memory_axolotl_store.js b/libtextsecure/test/in_memory_axolotl_store.js index 4e00367b..c1b2f9d5 100644 --- a/libtextsecure/test/in_memory_axolotl_store.js +++ b/libtextsecure/test/in_memory_axolotl_store.js @@ -4,10 +4,10 @@ function AxolotlStore() { AxolotlStore.prototype = { getMyIdentityKey: function() { - return this.get('identityKey'); + return Promise.resolve(this.get('identityKey')); }, getMyRegistrationId: function() { - return this.get('registrationId'); + return Promise.resolve(this.get('registrationId')); }, put: function(key, value) { if (key === undefined || value === undefined || key === null || value === null) diff --git a/libtextsecure/test/storage_test.js b/libtextsecure/test/storage_test.js index bc4609c7..d7d6bd1a 100644 --- a/libtextsecure/test/storage_test.js +++ b/libtextsecure/test/storage_test.js @@ -29,16 +29,18 @@ describe("AxolotlStore", function() { pubKey: textsecure.crypto.getRandomBytes(33), privKey: textsecure.crypto.getRandomBytes(32), }; - it('retrieves my registration id', function() { + it('retrieves my registration id', function(done) { store.put('registrationId', 1337); - var reg = store.getMyRegistrationId(); - assert.strictEqual(reg, 1337); + store.getMyRegistrationId().then(function(reg) { + assert.strictEqual(reg, 1337); + }).then(done, done); }); - it('retrieves my identity key', function() { + it('retrieves my identity key', function(done) { store.put('identityKey', identityKey); - var key = store.getMyIdentityKey(); - assertEqualArrayBuffers(key.pubKey, identityKey.pubKey); - assertEqualArrayBuffers(key.privKey, identityKey.privKey); + store.getMyIdentityKey().then(function(key) { + assertEqualArrayBuffers(key.pubKey, identityKey.pubKey); + assertEqualArrayBuffers(key.privKey, identityKey.privKey); + }).then(done,done); }); it('stores identity keys', function(done) { store.putIdentityKey(identifier, testKey.pubKey).then(function() { diff --git a/test/index.html b/test/index.html index b36c5648..a9258ff3 100644 --- a/test/index.html +++ b/test/index.html @@ -117,6 +117,7 @@ + diff --git a/test/storage_test.js b/test/storage_test.js index 01f893b4..37c77fea 100644 --- a/test/storage_test.js +++ b/test/storage_test.js @@ -17,7 +17,11 @@ 'use strict'; describe("AxolotlStore", function() { - before(function() { localStorage.clear(); }); + before(function(done) { + storage.put('registrationId', 1337); + storage.put('identityKey', identityKey); + storage.fetch().then(done, done); + }); var store = textsecure.storage.axolotl; var identifier = '+5558675309'; var identityKey = { @@ -28,16 +32,16 @@ describe("AxolotlStore", function() { pubKey: textsecure.crypto.getRandomBytes(33), privKey: textsecure.crypto.getRandomBytes(32), }; - it('retrieves my registration id', function() { - textsecure.storage.put('registrationId', 1337); - var reg = store.getMyRegistrationId(); - assert.strictEqual(reg, 1337); + it('retrieves my registration id', function(done) { + store.getMyRegistrationId().then(function(reg) { + assert.strictEqual(reg, 1337); + }).then(done, done); }); - it('retrieves my identity key', function() { - textsecure.storage.put('identityKey', identityKey); - var key = store.getMyIdentityKey(); - assertEqualArrayBuffers(key.pubKey, identityKey.pubKey); - assertEqualArrayBuffers(key.privKey, identityKey.privKey); + it('retrieves my identity key', function(done) { + store.getMyIdentityKey().then(function(key) { + assertEqualArrayBuffers(key.pubKey, identityKey.pubKey); + assertEqualArrayBuffers(key.privKey, identityKey.privKey); + }).then(done,done); }); it('stores identity keys', function(done) { store.putIdentityKey(identifier, testKey.pubKey).then(function() {