mockiframeio.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Mock of IframeIo for unit testing.
  16. */
  17. goog.provide('goog.testing.net.MockIFrameIo');
  18. goog.require('goog.events.EventTarget');
  19. goog.require('goog.json');
  20. goog.require('goog.net.ErrorCode');
  21. goog.require('goog.net.EventType');
  22. goog.require('goog.net.IframeIo');
  23. goog.require('goog.testing.TestQueue');
  24. /**
  25. * Mock implementation of goog.net.IframeIo. This doesn't provide a mock
  26. * implementation for all cases, but it's not too hard to add them as needed.
  27. * @param {goog.testing.TestQueue} testQueue Test queue for inserting test
  28. * events.
  29. * @constructor
  30. * @extends {goog.events.EventTarget}
  31. * @final
  32. */
  33. goog.testing.net.MockIFrameIo = function(testQueue) {
  34. goog.events.EventTarget.call(this);
  35. /**
  36. * Queue of events write to
  37. * @type {goog.testing.TestQueue}
  38. * @private
  39. */
  40. this.testQueue_ = testQueue;
  41. };
  42. goog.inherits(goog.testing.net.MockIFrameIo, goog.events.EventTarget);
  43. /**
  44. * Whether MockIFrameIo is active.
  45. * @type {boolean}
  46. * @private
  47. */
  48. goog.testing.net.MockIFrameIo.prototype.active_ = false;
  49. /**
  50. * Last content.
  51. * @type {string}
  52. * @private
  53. */
  54. goog.testing.net.MockIFrameIo.prototype.lastContent_ = '';
  55. /**
  56. * Last error code.
  57. * @type {goog.net.ErrorCode}
  58. * @private
  59. */
  60. goog.testing.net.MockIFrameIo.prototype.lastErrorCode_ =
  61. goog.net.ErrorCode.NO_ERROR;
  62. /**
  63. * Last error message.
  64. * @type {string}
  65. * @private
  66. */
  67. goog.testing.net.MockIFrameIo.prototype.lastError_ = '';
  68. /**
  69. * Last custom error.
  70. * @type {Object}
  71. * @private
  72. */
  73. goog.testing.net.MockIFrameIo.prototype.lastCustomError_ = null;
  74. /**
  75. * Last URI.
  76. * @type {goog.Uri}
  77. * @private
  78. */
  79. goog.testing.net.MockIFrameIo.prototype.lastUri_ = null;
  80. /** @private {Function} */
  81. goog.testing.net.MockIFrameIo.prototype.errorChecker_;
  82. /** @private {boolean} */
  83. goog.testing.net.MockIFrameIo.prototype.success_;
  84. /** @private {boolean} */
  85. goog.testing.net.MockIFrameIo.prototype.complete_;
  86. /**
  87. * Simulates the iframe send.
  88. *
  89. * @param {goog.Uri|string} uri Uri of the request.
  90. * @param {string=} opt_method Default is GET, POST uses a form to submit the
  91. * request.
  92. * @param {boolean=} opt_noCache Append a timestamp to the request to avoid
  93. * caching.
  94. * @param {Object|goog.structs.Map=} opt_data Map of key-value pairs.
  95. */
  96. goog.testing.net.MockIFrameIo.prototype.send = function(
  97. uri, opt_method, opt_noCache, opt_data) {
  98. if (this.active_) {
  99. throw Error('[goog.net.IframeIo] Unable to send, already active.');
  100. }
  101. this.testQueue_.enqueue(['s', uri, opt_method, opt_noCache, opt_data]);
  102. this.complete_ = false;
  103. this.active_ = true;
  104. };
  105. /**
  106. * Simulates the iframe send from a form.
  107. * @param {Element} form Form element used to send the request to the server.
  108. * @param {string=} opt_uri Uri to set for the destination of the request, by
  109. * default the uri will come from the form.
  110. * @param {boolean=} opt_noCache Append a timestamp to the request to avoid
  111. * caching.
  112. */
  113. goog.testing.net.MockIFrameIo.prototype.sendFromForm = function(
  114. form, opt_uri, opt_noCache) {
  115. if (this.active_) {
  116. throw Error('[goog.net.IframeIo] Unable to send, already active.');
  117. }
  118. this.testQueue_.enqueue(['s', form, opt_uri, opt_noCache]);
  119. this.complete_ = false;
  120. this.active_ = true;
  121. };
  122. /**
  123. * Simulates aborting the current Iframe request.
  124. * @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use -
  125. * defaults to ABORT.
  126. */
  127. goog.testing.net.MockIFrameIo.prototype.abort = function(opt_failureCode) {
  128. if (this.active_) {
  129. this.testQueue_.enqueue(['a', opt_failureCode]);
  130. this.complete_ = false;
  131. this.active_ = false;
  132. this.success_ = false;
  133. this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;
  134. this.dispatchEvent(goog.net.EventType.ABORT);
  135. this.simulateReady();
  136. }
  137. };
  138. /**
  139. * Simulates receive of incremental data.
  140. * @param {Object} data Data.
  141. */
  142. goog.testing.net.MockIFrameIo.prototype.simulateIncrementalData =
  143. function(data) {
  144. this.dispatchEvent(new goog.net.IframeIo.IncrementalDataEvent(data));
  145. };
  146. /**
  147. * Simulates the iframe is done.
  148. * @param {goog.net.ErrorCode} errorCode The error code for any error that
  149. * should be simulated.
  150. */
  151. goog.testing.net.MockIFrameIo.prototype.simulateDone = function(errorCode) {
  152. if (errorCode) {
  153. this.success_ = false;
  154. this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;
  155. this.lastError_ = this.getLastError();
  156. this.dispatchEvent(goog.net.EventType.ERROR);
  157. } else {
  158. this.success_ = true;
  159. this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
  160. this.dispatchEvent(goog.net.EventType.SUCCESS);
  161. }
  162. this.complete_ = true;
  163. this.dispatchEvent(goog.net.EventType.COMPLETE);
  164. };
  165. /**
  166. * Simulates the IFrame is ready for the next request.
  167. */
  168. goog.testing.net.MockIFrameIo.prototype.simulateReady = function() {
  169. this.dispatchEvent(goog.net.EventType.READY);
  170. };
  171. /**
  172. * @return {boolean} True if transfer is complete.
  173. */
  174. goog.testing.net.MockIFrameIo.prototype.isComplete = function() {
  175. return this.complete_;
  176. };
  177. /**
  178. * @return {boolean} True if transfer was successful.
  179. */
  180. goog.testing.net.MockIFrameIo.prototype.isSuccess = function() {
  181. return this.success_;
  182. };
  183. /**
  184. * @return {boolean} True if a transfer is in progress.
  185. */
  186. goog.testing.net.MockIFrameIo.prototype.isActive = function() {
  187. return this.active_;
  188. };
  189. /**
  190. * Returns the last response text (i.e. the text content of the iframe).
  191. * Assumes plain text!
  192. * @return {string} Result from the server.
  193. */
  194. goog.testing.net.MockIFrameIo.prototype.getResponseText = function() {
  195. return this.lastContent_;
  196. };
  197. /**
  198. * Parses the content as JSON. This is a safe parse and may throw an error
  199. * if the response is malformed.
  200. * @return {Object} The parsed content.
  201. */
  202. goog.testing.net.MockIFrameIo.prototype.getResponseJson = function() {
  203. return goog.json.parse(this.lastContent_);
  204. };
  205. /**
  206. * Get the uri of the last request.
  207. * @return {goog.Uri} Uri of last request.
  208. */
  209. goog.testing.net.MockIFrameIo.prototype.getLastUri = function() {
  210. return this.lastUri_;
  211. };
  212. /**
  213. * Gets the last error code.
  214. * @return {goog.net.ErrorCode} Last error code.
  215. */
  216. goog.testing.net.MockIFrameIo.prototype.getLastErrorCode = function() {
  217. return this.lastErrorCode_;
  218. };
  219. /**
  220. * Gets the last error message.
  221. * @return {string} Last error message.
  222. */
  223. goog.testing.net.MockIFrameIo.prototype.getLastError = function() {
  224. return goog.net.ErrorCode.getDebugMessage(this.lastErrorCode_);
  225. };
  226. /**
  227. * Gets the last custom error.
  228. * @return {Object} Last custom error.
  229. */
  230. goog.testing.net.MockIFrameIo.prototype.getLastCustomError = function() {
  231. return this.lastCustomError_;
  232. };
  233. /**
  234. * Sets the callback function used to check if a loaded IFrame is in an error
  235. * state.
  236. * @param {Function} fn Callback that expects a document object as it's single
  237. * argument.
  238. */
  239. goog.testing.net.MockIFrameIo.prototype.setErrorChecker = function(fn) {
  240. this.errorChecker_ = fn;
  241. };
  242. /**
  243. * Gets the callback function used to check if a loaded IFrame is in an error
  244. * state.
  245. * @return {Function} A callback that expects a document object as it's single
  246. * argument.
  247. */
  248. goog.testing.net.MockIFrameIo.prototype.getErrorChecker = function() {
  249. return this.errorChecker_;
  250. };