image/test/unit/image_load_helpers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/test/unit/image_load_helpers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*
     1.5 + * Helper structures to track callbacks from image and channel loads.
     1.6 + */
     1.7 +
     1.8 +// START_REQUEST and STOP_REQUEST are used by ChannelListener, and
     1.9 +// stored in ChannelListener.requestStatus.
    1.10 +const START_REQUEST = 0x01;
    1.11 +const STOP_REQUEST = 0x02;
    1.12 +const DATA_AVAILABLE = 0x04;
    1.13 +
    1.14 +// One bit per callback that imageListener below implements. Stored in
    1.15 +// ImageListener.state.
    1.16 +const SIZE_AVAILABLE = 0x01;
    1.17 +const FRAME_UPDATE = 0x02;
    1.18 +const FRAME_COMPLETE = 0x04;
    1.19 +const LOAD_COMPLETE = 0x08;
    1.20 +const DECODE_COMPLETE = 0x10;
    1.21 +
    1.22 +// An implementation of imgIScriptedNotificationObserver with the ability to
    1.23 +// call specified functions on onStartRequest and onStopRequest.
    1.24 +function ImageListener(start_callback, stop_callback)
    1.25 +{
    1.26 +  this.sizeAvailable = function onSizeAvailable(aRequest)
    1.27 +  {
    1.28 +    do_check_false(this.synchronous);
    1.29 +
    1.30 +    this.state |= SIZE_AVAILABLE;
    1.31 +
    1.32 +    if (this.start_callback)
    1.33 +      this.start_callback(this, aRequest);
    1.34 +  }
    1.35 +  this.frameComplete = function onFrameComplete(aRequest)
    1.36 +  {
    1.37 +    do_check_false(this.synchronous);
    1.38 +
    1.39 +    this.state |= FRAME_COMPLETE;
    1.40 +  }
    1.41 +  this.decodeComplete = function onDecodeComplete(aRequest)
    1.42 +  {
    1.43 +    do_check_false(this.synchronous);
    1.44 +
    1.45 +    this.state |= DECODE_COMPLETE;
    1.46 +  }
    1.47 +  this.loadComplete = function onLoadcomplete(aRequest)
    1.48 +  {
    1.49 +    do_check_false(this.synchronous);
    1.50 +
    1.51 +    try {
    1.52 +      aRequest.requestDecode();
    1.53 +    } catch (e) {
    1.54 +      do_print("requestDecode threw " + e);
    1.55 +    }
    1.56 +
    1.57 +    this.state |= LOAD_COMPLETE;
    1.58 +
    1.59 +    if (this.stop_callback)
    1.60 +      this.stop_callback(this, aRequest);
    1.61 +  }
    1.62 +  this.frameUpdate = function onFrameUpdate(aRequest)
    1.63 +  {
    1.64 +  }
    1.65 +  this.isAnimated = function onIsAnimated()
    1.66 +  {
    1.67 +  }
    1.68 +
    1.69 +  // Initialize the synchronous flag to true to start. This must be set to
    1.70 +  // false before exiting to the event loop!
    1.71 +  this.synchronous = true;
    1.72 +
    1.73 +  // A function to call when onStartRequest is called.
    1.74 +  this.start_callback = start_callback;
    1.75 +
    1.76 +  // A function to call when onStopRequest is called.
    1.77 +  this.stop_callback = stop_callback;
    1.78 +
    1.79 +  // The image load/decode state.
    1.80 +  // A bitfield that tracks which callbacks have been called. Takes the bits
    1.81 +  // defined above.
    1.82 +  this.state = 0;
    1.83 +}
    1.84 +
    1.85 +function NS_FAILED(val)
    1.86 +{
    1.87 +  return !!(val & 0x80000000);
    1.88 +}
    1.89 +
    1.90 +function ChannelListener()
    1.91 +{
    1.92 +  this.onStartRequest = function onStartRequest(aRequest, aContext)
    1.93 +  {
    1.94 +    if (this.outputListener)
    1.95 +      this.outputListener.onStartRequest(aRequest, aContext);
    1.96 +
    1.97 +    this.requestStatus |= START_REQUEST;
    1.98 +  }
    1.99 +
   1.100 +  this.onDataAvailable = function onDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount)
   1.101 +  {
   1.102 +    if (this.outputListener)
   1.103 +      this.outputListener.onDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount);
   1.104 +
   1.105 +    this.requestStatus |= DATA_AVAILABLE;
   1.106 +  }
   1.107 +
   1.108 +  this.onStopRequest = function onStopRequest(aRequest, aContext, aStatusCode)
   1.109 +  {
   1.110 +    if (this.outputListener)
   1.111 +      this.outputListener.onStopRequest(aRequest, aContext, aStatusCode);
   1.112 +
   1.113 +    // If we failed (or were canceled - failure is implied if canceled),
   1.114 +    // there's no use tracking our state, since it's meaningless.
   1.115 +    if (NS_FAILED(aStatusCode))
   1.116 +      this.requestStatus = 0;
   1.117 +    else
   1.118 +      this.requestStatus |= STOP_REQUEST;
   1.119 +  }
   1.120 +
   1.121 +  // A listener to pass the notifications we get to.
   1.122 +  this.outputListener = null;
   1.123 +
   1.124 +  // The request's status. A bitfield that holds one or both of START_REQUEST
   1.125 +  // and STOP_REQUEST, according to which callbacks have been called on the
   1.126 +  // associated request.
   1.127 +  this.requestStatus = 0;
   1.128 +}

mercurial