Path: blob/trunk/third_party/closure/goog/net/mockiframeio.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Mock of IframeIo for unit testing.16*/1718goog.provide('goog.net.MockIFrameIo');19goog.require('goog.events.EventTarget');20goog.require('goog.json');21goog.require('goog.net.ErrorCode');22goog.require('goog.net.EventType');23goog.require('goog.net.IframeIo');24goog.forwardDeclare('goog.testing.TestQueue');25262728/**29* Mock implementation of goog.net.IframeIo. This doesn't provide a mock30* implementation for all cases, but it's not too hard to add them as needed.31* @param {goog.testing.TestQueue} testQueue Test queue for inserting test32* events.33* @constructor34* @extends {goog.events.EventTarget}35* @final36* @deprecated Use goog.testing.net.MockIFrameIo instead.37*/38goog.net.MockIFrameIo = function(testQueue) {39goog.events.EventTarget.call(this);4041/**42* Queue of events write to43* @type {goog.testing.TestQueue}44* @private45*/46this.testQueue_ = testQueue;4748};49goog.inherits(goog.net.MockIFrameIo, goog.events.EventTarget);505152/**53* Whether MockIFrameIo is active.54* @type {boolean}55* @private56*/57goog.net.MockIFrameIo.prototype.active_ = false;585960/**61* Last content.62* @type {string}63* @private64*/65goog.net.MockIFrameIo.prototype.lastContent_ = '';666768/**69* Last error code.70* @type {goog.net.ErrorCode}71* @private72*/73goog.net.MockIFrameIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;747576/**77* Last error message.78* @type {string}79* @private80*/81goog.net.MockIFrameIo.prototype.lastError_ = '';828384/**85* Last custom error.86* @type {Object}87* @private88*/89goog.net.MockIFrameIo.prototype.lastCustomError_ = null;909192/**93* Last URI.94* @type {goog.Uri}95* @private96*/97goog.net.MockIFrameIo.prototype.lastUri_ = null;9899100/** @private {Function} */101goog.net.MockIFrameIo.prototype.errorChecker_;102103104/** @private {boolean} */105goog.net.MockIFrameIo.prototype.success_;106107108/** @private {boolean} */109goog.net.MockIFrameIo.prototype.complete_;110111112/**113* Simulates the iframe send.114*115* @param {goog.Uri|string} uri Uri of the request.116* @param {string=} opt_method Default is GET, POST uses a form to submit the117* request.118* @param {boolean=} opt_noCache Append a timestamp to the request to avoid119* caching.120* @param {Object|goog.structs.Map=} opt_data Map of key-value pairs.121*/122goog.net.MockIFrameIo.prototype.send = function(123uri, opt_method, opt_noCache, opt_data) {124if (this.active_) {125throw Error('[goog.net.IframeIo] Unable to send, already active.');126}127128this.testQueue_.enqueue(['s', uri, opt_method, opt_noCache, opt_data]);129this.complete_ = false;130this.active_ = true;131};132133134/**135* Simulates the iframe send from a form.136* @param {Element} form Form element used to send the request to the server.137* @param {string=} opt_uri Uri to set for the destination of the request, by138* default the uri will come from the form.139* @param {boolean=} opt_noCache Append a timestamp to the request to avoid140* caching.141*/142goog.net.MockIFrameIo.prototype.sendFromForm = function(143form, opt_uri, opt_noCache) {144if (this.active_) {145throw Error('[goog.net.IframeIo] Unable to send, already active.');146}147148this.testQueue_.enqueue(['s', form, opt_uri, opt_noCache]);149this.complete_ = false;150this.active_ = true;151};152153154/**155* Simulates aborting the current Iframe request.156* @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use -157* defaults to ABORT.158*/159goog.net.MockIFrameIo.prototype.abort = function(opt_failureCode) {160if (this.active_) {161this.testQueue_.enqueue(['a', opt_failureCode]);162this.complete_ = false;163this.active_ = false;164this.success_ = false;165this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;166this.dispatchEvent(goog.net.EventType.ABORT);167this.simulateReady();168}169};170171172/**173* Simulates receive of incremental data.174* @param {Object} data Data.175*/176goog.net.MockIFrameIo.prototype.simulateIncrementalData = function(data) {177this.dispatchEvent(new goog.net.IframeIo.IncrementalDataEvent(data));178};179180181/**182* Simulates the iframe is done.183* @param {goog.net.ErrorCode} errorCode The error code for any error that184* should be simulated.185*/186goog.net.MockIFrameIo.prototype.simulateDone = function(errorCode) {187if (errorCode) {188this.success_ = false;189this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;190this.lastError_ = this.getLastError();191this.dispatchEvent(goog.net.EventType.ERROR);192} else {193this.success_ = true;194this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;195this.dispatchEvent(goog.net.EventType.SUCCESS);196}197this.complete_ = true;198this.dispatchEvent(goog.net.EventType.COMPLETE);199};200201202/**203* Simulates the IFrame is ready for the next request.204*/205goog.net.MockIFrameIo.prototype.simulateReady = function() {206this.dispatchEvent(goog.net.EventType.READY);207};208209210/**211* @return {boolean} True if transfer is complete.212*/213goog.net.MockIFrameIo.prototype.isComplete = function() {214return this.complete_;215};216217218/**219* @return {boolean} True if transfer was successful.220*/221goog.net.MockIFrameIo.prototype.isSuccess = function() {222return this.success_;223};224225226/**227* @return {boolean} True if a transfer is in progress.228*/229goog.net.MockIFrameIo.prototype.isActive = function() {230return this.active_;231};232233234/**235* Returns the last response text (i.e. the text content of the iframe).236* Assumes plain text!237* @return {string} Result from the server.238*/239goog.net.MockIFrameIo.prototype.getResponseText = function() {240return this.lastContent_;241};242243244/**245* Parses the content as JSON. This is a safe parse and may throw an error246* if the response is malformed.247* @return {Object} The parsed content.248*/249goog.net.MockIFrameIo.prototype.getResponseJson = function() {250return goog.json.parse(this.lastContent_);251};252253254/**255* Get the uri of the last request.256* @return {goog.Uri} Uri of last request.257*/258goog.net.MockIFrameIo.prototype.getLastUri = function() {259return this.lastUri_;260};261262263/**264* Gets the last error code.265* @return {goog.net.ErrorCode} Last error code.266*/267goog.net.MockIFrameIo.prototype.getLastErrorCode = function() {268return this.lastErrorCode_;269};270271272/**273* Gets the last error message.274* @return {string} Last error message.275*/276goog.net.MockIFrameIo.prototype.getLastError = function() {277return goog.net.ErrorCode.getDebugMessage(this.lastErrorCode_);278};279280281/**282* Gets the last custom error.283* @return {Object} Last custom error.284*/285goog.net.MockIFrameIo.prototype.getLastCustomError = function() {286return this.lastCustomError_;287};288289290/**291* Sets the callback function used to check if a loaded IFrame is in an error292* state.293* @param {Function} fn Callback that expects a document object as it's single294* argument.295*/296goog.net.MockIFrameIo.prototype.setErrorChecker = function(fn) {297this.errorChecker_ = fn;298};299300301/**302* Gets the callback function used to check if a loaded IFrame is in an error303* state.304* @return {Function} A callback that expects a document object as it's single305* argument.306*/307goog.net.MockIFrameIo.prototype.getErrorChecker = function() {308return this.errorChecker_;309};310311312