toolkit/content/tests/browser/common/mockTransfer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/browser/common/mockTransfer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,67 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Cc["@mozilla.org/moz/jssubscript-loader;1"]
     1.9 +  .getService(Ci.mozIJSSubScriptLoader)
    1.10 +  .loadSubScript("chrome://mochikit/content/tests/SimpleTest/MockObjects.js", this);
    1.11 +
    1.12 +var mockTransferCallback;
    1.13 +
    1.14 +/**
    1.15 + * This "transfer" object implementation continues the currently running test
    1.16 + * when the download is completed, reporting true for success or false for
    1.17 + * failure as the first argument of the testRunner.continueTest function.
    1.18 + */
    1.19 +function MockTransfer() {
    1.20 +  this._downloadIsSuccessful = true;
    1.21 +}
    1.22 +
    1.23 +MockTransfer.prototype = {
    1.24 +  QueryInterface: XPCOMUtils.generateQI([
    1.25 +    Ci.nsIWebProgressListener,
    1.26 +    Ci.nsIWebProgressListener2,
    1.27 +    Ci.nsITransfer,
    1.28 +  ]),
    1.29 +
    1.30 +  /* nsIWebProgressListener */
    1.31 +  onStateChange: function MTFC_onStateChange(aWebProgress, aRequest,
    1.32 +                                             aStateFlags, aStatus) {
    1.33 +    // If at least one notification reported an error, the download failed.
    1.34 +    if (!Components.isSuccessCode(aStatus))
    1.35 +      this._downloadIsSuccessful = false;
    1.36 +
    1.37 +    // If the download is finished
    1.38 +    if ((aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) &&
    1.39 +        (aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK))
    1.40 +      // Continue the test, reporting the success or failure condition.
    1.41 +      mockTransferCallback(this._downloadIsSuccessful);
    1.42 +  },
    1.43 +  onProgressChange: function () {},
    1.44 +  onLocationChange: function () {},
    1.45 +  onStatusChange: function MTFC_onStatusChange(aWebProgress, aRequest, aStatus,
    1.46 +                                               aMessage) {
    1.47 +    // If at least one notification reported an error, the download failed.
    1.48 +    if (!Components.isSuccessCode(aStatus))
    1.49 +      this._downloadIsSuccessful = false;
    1.50 +  },
    1.51 +  onSecurityChange: function () {},
    1.52 +
    1.53 +  /* nsIWebProgressListener2 */
    1.54 +  onProgressChange64: function () {},
    1.55 +  onRefreshAttempted: function () {},
    1.56 +
    1.57 +  /* nsITransfer */
    1.58 +  init: function() {},
    1.59 +  setSha256Hash: function() {},
    1.60 +  setSignatureInfo: function() {}
    1.61 +};
    1.62 +
    1.63 +// Create an instance of a MockObjectRegisterer whose methods can be used to
    1.64 +// temporarily replace the default "@mozilla.org/transfer;1" object factory with
    1.65 +// one that provides the mock implementation above. To activate the mock object
    1.66 +// factory, call the "register" method. Starting from that moment, all the
    1.67 +// transfer objects that are requested will be mock objects, until the
    1.68 +// "unregister" method is called.
    1.69 +var mockTransferRegisterer =
    1.70 +  new MockObjectRegisterer("@mozilla.org/transfer;1",  MockTransfer);

mercurial