Skip to content

Commit

Permalink
Bug 819548 - Remember sys updates are available after reboot
Browse files Browse the repository at this point in the history
We then prompt for updates on startup
  • Loading branch information
rik authored and jhford committed Jan 11, 2013
1 parent ec4910d commit 243bf25
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 2 deletions.
29 changes: 29 additions & 0 deletions apps/system/js/updatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,16 @@ function SystemUpdatable() {
this.size = 0;
this.downloading = false;
this.paused = false;

this.checkKnownUpdate(function checkCallback() {
UpdateManager.checkForUpdates(true);
});

window.addEventListener('mozChromeEvent', this);
}

SystemUpdatable.KNOWN_UPDATE_FLAG = 'known-sysupdate';

SystemUpdatable.prototype.download = function() {
if (this.downloading) {
return;
Expand Down Expand Up @@ -202,6 +209,9 @@ SystemUpdatable.prototype.errorCallBack = function() {
SystemUpdatable.prototype.showApplyPrompt = function() {
var _ = navigator.mozL10n.get;

// Update will be completed after restart
this.forgetKnownUpdate();

var cancel = {
title: _('later'),
callback: this.declineInstall.bind(this)
Expand Down Expand Up @@ -229,6 +239,25 @@ SystemUpdatable.prototype.acceptInstall = function() {
this._dispatchEvent('update-prompt-apply-result', 'restart');
};

SystemUpdatable.prototype.rememberKnownUpdate = function() {
asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
};

SystemUpdatable.prototype.checkKnownUpdate = function(callback) {
if (typeof callback !== 'function') {
return;
}
asyncStorage.getItem(SystemUpdatable.KNOWN_UPDATE_FLAG, function(value) {
if (value) {
callback();
}
});
};

SystemUpdatable.prototype.forgetKnownUpdate = function() {
asyncStorage.removeItem(SystemUpdatable.KNOWN_UPDATE_FLAG);
};

SystemUpdatable.prototype._dispatchEvent = function(type, result) {
var event = document.createEvent('CustomEvent');
var data = { type: type };
Expand Down
1 change: 1 addition & 0 deletions apps/system/js/update_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ var UpdateManager = {

if (detail.type && detail.type === 'update-available') {
this.systemUpdatable.size = detail.size;
this.systemUpdatable.rememberKnownUpdate();
this.addToUpdatesQueue(this.systemUpdatable);
}
},
Expand Down
33 changes: 33 additions & 0 deletions apps/system/test/unit/mock_asyncStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

var MockasyncStorage = {
mItems: {},

setItem: function(key, value, callback) {
this.mItems[key] = value;
if (typeof callback === 'function') {
callback();
}
},

getItem: function(key, callback) {
var value = this.mItems[key] || null;
if (typeof callback === 'function') {
callback(value);
}
},

removeItem: function(key, callback) {
if (key in this.mItems) {
delete this.mItems[key];
}

if (typeof callback === 'function') {
callback();
}
},

mTeardown: function() {
this.mItems = {};
}
};
15 changes: 15 additions & 0 deletions apps/system/test/unit/mock_updatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function MockSystemUpdatable(downloadSize) {
MockSystemUpdatable.mInstancesCount = 0;
MockSystemUpdatable.mTeardown = function() {
MockSystemUpdatable.mInstancesCount = 0;
delete MockSystemUpdatable.mKnownUpdate;
};


Expand All @@ -55,3 +56,17 @@ MockSystemUpdatable.prototype.download = function() {
MockSystemUpdatable.prototype.cancelDownload = function() {
this.mCancelCalled = true;
};

MockSystemUpdatable.prototype.rememberKnownUpdate = function() {
this.mKnownUpdate = true;
};

MockSystemUpdatable.prototype.forgetKnownUpdate = function() {
delete this.mKnownUpdate;
};

MockSystemUpdatable.prototype.checkKnownUpdate = function(callback) {
if (this.mKnownUpdate && typeof callback === 'function') {
callback();
}
};
6 changes: 6 additions & 0 deletions apps/system/test/unit/mock_update_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ var MockUpdateManager = {
this.mErrorBannerRequested = true;
},

checkForUpdates: function mum_checkForUpdate(forced) {
this.mCheckForUpdatesCalledWith = forced;
},

mErrorBannerRequested: false,
mLastUpdatesAdd: null,
mLastUpdatableAdd: null,
mLastUpdatesRemoval: null,
mLastDownloadsAdd: null,
mLastDownloadsRemoval: null,
mProgressCalledWith: null,
mCheckForUpdatesCalledWith: null,
mStartedUncompressingCalled: false,
mTeardown: function mum_mTeardown() {
this.mErrorBannerRequested = false;
Expand All @@ -49,6 +54,7 @@ var MockUpdateManager = {
this.mLastDownloadsAdd = null;
this.mLastDownloadsRemoval = null;
this.mProgressCalledWith = null;
this.mCheckForUpdatesCalledWith = null;
this.mStartedUncompressingCalled = false;
}
};
31 changes: 29 additions & 2 deletions apps/system/test/unit/updatable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requireApp('system/js/updatable.js');

requireApp('system/test/unit/mock_app.js');
requireApp('system/test/unit/mock_asyncStorage.js');
requireApp('system/test/unit/mock_update_manager.js');
requireApp('system/test/unit/mock_window_manager.js');
requireApp('system/test/unit/mock_apps_mgmt.js');
Expand All @@ -18,7 +19,8 @@ var mocksForUpdatable = [
'UpdateManager',
'WindowManager',
'UtilityTray',
'ManifestHelper'
'ManifestHelper',
'asyncStorage'
];

mocksForUpdatable.forEach(function(mockName) {
Expand Down Expand Up @@ -124,6 +126,12 @@ suite('system/Updatable', function() {
assert.equal(MockUpdateManager.mLastUpdatableAdd, subject);
});

test('should remember about the update on startup', function() {
asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG] = true;
var systemUpdatable = new SystemUpdatable();
assert.equal(MockUpdateManager.mCheckForUpdatesCalledWith, true);
});

downloadAvailableSuite('app has a download available', function() {
mockApp.downloadAvailable = true;
subject = new AppUpdatable(mockApp);
Expand Down Expand Up @@ -229,6 +237,7 @@ suite('system/Updatable', function() {

suite('cancel system update download', function() {
setup(function() {
asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
subject = new SystemUpdatable(42);
subject.download();
subject._dispatchEvent = fakeDispatchEvent;
Expand Down Expand Up @@ -431,6 +440,7 @@ suite('system/Updatable', function() {

suite('update-downloaded', function() {
setup(function() {
asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
var event = new MockChromeEvent({
type: 'update-downloaded'
});
Expand All @@ -441,18 +451,27 @@ suite('system/Updatable', function() {
assert.isFalse(subject.downloading);
});

test('should reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
assert.isUndefined(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
});

testSystemApplyPrompt();
});

suite('update-prompt-apply', function() {
setup(function() {
asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
MockUtilityTray.show();
var event = new MockChromeEvent({
type: 'update-prompt-apply'
});
subject.handleEvent(event);
});

test('should reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
assert.isUndefined(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
});

testSystemApplyPrompt();
});

Expand Down Expand Up @@ -508,7 +527,7 @@ suite('system/Updatable', function() {
progress: 1234,
total: 98734
});
})
});

test('should send progress to update manager', function() {
subject.handleEvent(event);
Expand All @@ -525,6 +544,7 @@ suite('system/Updatable', function() {

suite('when the download is paused', function() {
setup(function() {
asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
event = new MockChromeEvent({
type: 'update-download-stopped',
paused: true
Expand All @@ -538,10 +558,14 @@ suite('system/Updatable', function() {
test('shouldn\'t signal "started uncompressing"', function() {
assert.isFalse(MockUpdateManager.mStartedUncompressingCalled);
});
test('should not reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
assert.isTrue(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
});
});

suite('when the download is complete', function() {
setup(function() {
asyncStorage.setItem(SystemUpdatable.KNOWN_UPDATE_FLAG, true);
event = new MockChromeEvent({
type: 'update-download-stopped',
paused: false
Expand All @@ -556,6 +580,9 @@ suite('system/Updatable', function() {
test('should signal the UpdateManager', function() {
assert.isTrue(MockUpdateManager.mStartedUncompressingCalled);
});
test('should not reset SystemUpdatable.KNOWN_UPDATE_FLAG', function() {
assert.isTrue(asyncStorage.mItems[SystemUpdatable.KNOWN_UPDATE_FLAG]);
});
});
});
});
Expand Down
15 changes: 15 additions & 0 deletions apps/system/test/unit/update_manager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,22 @@ suite('system/UpdateManager', function() {
assert.equal(UpdateManager.updatesQueue.length, initialLength);
assert.equal(MockSystemUpdatable.mInstancesCount, 1);
});

test('should remember that update is available', function() {
assert.isTrue(UpdateManager.systemUpdatable.mKnownUpdate);
});
});

suite('no system update available', function() {
setup(function() {
UpdateManager.init();
});

test('should not remember about the update', function() {
assert.isUndefined(UpdateManager.systemUpdatable.mKnownUpdate);
});
});

});

suite('UI', function() {
Expand Down

0 comments on commit 243bf25

Please sign in to comment.