michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let Cu = Components.utils; michael@0: michael@0: Components.utils.import("resource://gre/modules/SharedPreferences.jsm"); michael@0: Components.utils.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIMessageListenerManager); michael@0: let deferred = 0; michael@0: let shouldPass = true; michael@0: let reqId = 0; michael@0: function getRequestId(increment) { michael@0: reqId += increment; michael@0: return "Request" + reqId; michael@0: } michael@0: michael@0: let paymentSuccess = { michael@0: receiveMessage: function(aMsg) { michael@0: let msg = aMsg.json; michael@0: if (shouldPass) { michael@0: do_check_eq(msg.requestId, getRequestId(0)); michael@0: } else { michael@0: do_throw("Test should not have passed"); michael@0: } michael@0: deferred.resolve(); michael@0: } michael@0: } michael@0: michael@0: let paymentFailed = { michael@0: receiveMessage: function(aMsg) { michael@0: let msg = aMsg.json; michael@0: if (shouldPass) { michael@0: do_throw("Test should not have failed: " + msg.errorMsg); michael@0: } else { michael@0: do_check_eq(msg.requestId, getRequestId(0)); michael@0: do_check_eq(msg.errorMsg, "FAILED CORRECTLY"); michael@0: } michael@0: deferred.resolve(); michael@0: } michael@0: } michael@0: michael@0: add_task(function test_get_set() { michael@0: let ui = Cc["@mozilla.org/payment/ui-glue;1"].getService(Ci.nsIPaymentUIGlue); michael@0: deferred = Promise.defer(); michael@0: let id = getRequestId(1); michael@0: ui.confirmPaymentRequest(id, michael@0: [{ wrappedJSObject: { type: "Fake Provider" } }], michael@0: function(aRequestId, type) { michael@0: do_check_eq(type, "Fake Provider"); michael@0: deferred.resolve(); michael@0: }, michael@0: function(id, msg) { michael@0: do_throw("confirmPaymentRequest should not have failed"); michael@0: deferred.resolve(); michael@0: }); michael@0: yield deferred.promise; michael@0: }); michael@0: michael@0: add_task(function test_default() { michael@0: ppmm.addMessageListener("Payment:Success", paymentSuccess); michael@0: ppmm.addMessageListener("Payment:Failed", paymentFailed); michael@0: michael@0: let ui = Cc["@mozilla.org/payment/ui-glue;1"].getService(Ci.nsIPaymentUIGlue); michael@0: deferred = Promise.defer(); michael@0: let id = getRequestId(1); michael@0: shouldPass = true; michael@0: ui.showPaymentFlow(id, michael@0: { michael@0: uri: "chrome://roboextender/content/paymentsUI.html", michael@0: jwt: "#pass" michael@0: }, michael@0: function(id, msg) { michael@0: do_throw("confirmPaymentRequest should not have failed"); michael@0: deferred.resolve(); michael@0: }); michael@0: yield deferred.promise; michael@0: michael@0: deferred = Promise.defer(); michael@0: let id = getRequestId(1); michael@0: shouldPass = false; michael@0: ui.showPaymentFlow(id, michael@0: { michael@0: uri: "chrome://roboextender/content/paymentsUI.html", michael@0: jwt: "#fail" michael@0: }, michael@0: function(id, msg) { michael@0: do_throw("confirmPaymentRequest should not have failed"); michael@0: deferred.resolve(); michael@0: }); michael@0: michael@0: yield deferred.promise; michael@0: michael@0: ppmm.removeMessageListener("Payment:Success", paymentSuccess); michael@0: ppmm.removeMessageListener("Payment:Failed", paymentFailed); michael@0: }); michael@0: michael@0: run_next_test();