michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Tests the update part of the post-app-update dialog michael@0: michael@0: var gProvider; michael@0: var gWin; michael@0: michael@0: function waitForView(aView, aCallback) { michael@0: var view = gWin.document.getElementById(aView); michael@0: if (view.parentNode.selectedPanel == view) { michael@0: aCallback(); michael@0: return; michael@0: } michael@0: michael@0: view.addEventListener("ViewChanged", function() { michael@0: view.removeEventListener("ViewChanged", arguments.callee, false); michael@0: aCallback(); michael@0: }, false); michael@0: } michael@0: michael@0: function waitForClose(aCallback) { michael@0: gWin.addEventListener("unload", function() { michael@0: gWin.removeEventListener("unload", arguments.callee, false); michael@0: michael@0: aCallback(); michael@0: }, false); michael@0: } michael@0: michael@0: /** michael@0: * Creates 4 test add-ons. Two are disabled and two enabled. michael@0: */ michael@0: function setupUI(aFailDownloads, aFailInstalls, aCallback) { michael@0: if (gProvider) michael@0: gProvider.unregister(); michael@0: michael@0: gProvider = new MockProvider(); michael@0: michael@0: for (var i = 1; i < 5; i++) { michael@0: var addon = new MockAddon("test" + i + "@tests.mozilla.org", michael@0: "Test Add-on " + i, "extension"); michael@0: addon.version = "1.0"; michael@0: addon.userDisabled = (i > 2); michael@0: addon.appDisabled = false; michael@0: addon.isActive = !addon.userDisabled && !addon.appDisabled; michael@0: michael@0: addon.findUpdates = function(aListener, aReason, aAppVersion, aPlatformVersion) { michael@0: var newAddon = new MockAddon(this.id, this.name, "extension"); michael@0: newAddon.version = "2.0"; michael@0: var install = new MockInstall(this.name, this.type, newAddon); michael@0: install.existingAddon = this; michael@0: michael@0: install.install = function() { michael@0: this.state = AddonManager.STATE_DOWNLOADING; michael@0: this.callListeners("onDownloadStarted"); michael@0: michael@0: var self = this; michael@0: executeSoon(function() { michael@0: if (aFailDownloads) { michael@0: self.state = AddonManager.STATE_DOWNLOAD_FAILED; michael@0: self.callListeners("onDownloadFailed"); michael@0: return; michael@0: } michael@0: michael@0: self.type = self._type; michael@0: self.addon = new MockAddon(self.existingAddon.id, self.name, self.type); michael@0: self.addon.version = self.version; michael@0: self.addon.pendingOperations = AddonManager.PENDING_INSTALL; michael@0: self.addon.install = self; michael@0: michael@0: self.existingAddon.pendingUpgrade = self.addon; michael@0: self.existingAddon.pendingOperations |= AddonManager.PENDING_UPGRADE; michael@0: michael@0: self.state = AddonManager.STATE_DOWNLOADED; michael@0: self.callListeners("onDownloadEnded"); michael@0: michael@0: self.state = AddonManager.STATE_INSTALLING; michael@0: self.callListeners("onInstallStarted"); michael@0: michael@0: if (aFailInstalls) { michael@0: self.state = AddonManager.STATE_INSTALL_FAILED; michael@0: self.callListeners("onInstallFailed"); michael@0: return; michael@0: } michael@0: michael@0: self.state = AddonManager.STATE_INSTALLED; michael@0: self.callListeners("onInstallEnded"); michael@0: }); michael@0: } michael@0: michael@0: aListener.onUpdateAvailable(this, install); michael@0: michael@0: aListener.onUpdateFinished(this, AddonManager.UPDATE_STATUS_NO_ERROR); michael@0: }; michael@0: michael@0: gProvider.addAddon(addon); michael@0: } michael@0: michael@0: gWin = Services.ww.openWindow(null, michael@0: "chrome://mozapps/content/extensions/selectAddons.xul", michael@0: "", michael@0: "chrome,centerscreen,dialog,titlebar", michael@0: null); michael@0: waitForFocus(function() { michael@0: waitForView("select", function() { michael@0: var row = gWin.document.getElementById("select-rows").firstChild.nextSibling; michael@0: while (row) { michael@0: if (!row.id || row.id.indexOf("@tests.mozilla.org") < 0) { michael@0: // not a test add-on michael@0: row = row.nextSibling; michael@0: continue; michael@0: } michael@0: michael@0: if (row.id == "test2@tests.mozilla.org" || michael@0: row.id == "test4@tests.mozilla.org") { michael@0: row.disable(); michael@0: } michael@0: else { michael@0: row.keep(); michael@0: } michael@0: row = row.nextSibling; michael@0: } michael@0: michael@0: waitForView("confirm", function() { michael@0: waitForView("update", aCallback); michael@0: EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); michael@0: }); michael@0: EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); michael@0: }); michael@0: }, gWin); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function end_test() { michael@0: finish(); michael@0: } michael@0: michael@0: // Test for working updates michael@0: add_test(function working_test() { michael@0: setupUI(false, false, function() { michael@0: waitForClose(function() { michael@0: is(gWin.document.getElementById("update-progress").value, 2, "Should have finished 2 downloads"); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); michael@0: }); michael@0: }); michael@0: michael@0: // Test for failed updates michael@0: add_test(function working_test() { michael@0: setupUI(true, false, function() { michael@0: waitForView("errors", function() { michael@0: is(gWin.document.getElementById("update-progress").value, 2, "Should have finished 2 downloads"); michael@0: gWin.close(); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); michael@0: }); michael@0: }); michael@0: michael@0: // Test for failed updates michael@0: add_test(function working_test() { michael@0: setupUI(false, true, function() { michael@0: waitForView("errors", function() { michael@0: is(gWin.document.getElementById("update-progress").value, 2, "Should have finished 2 downloads"); michael@0: gWin.close(); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: EventUtils.synthesizeMouseAtCenter(gWin.document.getElementById("next"), {}, gWin); michael@0: }); michael@0: });