iframeio_test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2006 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. goog.provide('goog.net.IframeIoTest');
  15. goog.setTestOnly('goog.net.IframeIoTest');
  16. goog.require('goog.debug');
  17. goog.require('goog.debug.DivConsole');
  18. goog.require('goog.debug.LogManager');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.TagName');
  21. goog.require('goog.events');
  22. goog.require('goog.events.EventType');
  23. goog.require('goog.log');
  24. goog.require('goog.log.Level');
  25. goog.require('goog.net.IframeIo');
  26. goog.require('goog.testing.events');
  27. goog.require('goog.testing.events.Event');
  28. goog.require('goog.testing.jsunit');
  29. goog.require('goog.userAgent');
  30. // MANUAL TESTS - The tests should be run in the browser from the Closure Test
  31. // Server
  32. // Set up a logger to track responses
  33. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.INFO);
  34. var logconsole;
  35. var testLogger = goog.log.getLogger('test');
  36. function setUpPage() {
  37. var logconsole = new goog.debug.DivConsole(document.getElementById('log'));
  38. logconsole.setCapturing(true);
  39. }
  40. /** Creates an iframeIo instance and sets up the test environment */
  41. function getTestIframeIo() {
  42. logconsole.addSeparator();
  43. logconsole.getFormatter().resetRelativeTimeStart();
  44. var io = new goog.net.IframeIo();
  45. io.setErrorChecker(checkForError);
  46. goog.events.listen(io, 'success', onSuccess);
  47. goog.events.listen(io, 'error', onError);
  48. goog.events.listen(io, 'ready', onReady);
  49. return io;
  50. }
  51. /**
  52. * Checks for error strings returned by the GSE and error variables that
  53. * the Gmail server and GFE set on certain errors.
  54. */
  55. function checkForError(doc) {
  56. var win = goog.dom.getWindow(doc);
  57. var text = doc.body.textContent || doc.body.innerText || '';
  58. var gseError = text.match(/([^\n]+)\nError ([0-9]{3})/);
  59. if (gseError) {
  60. return '(Error ' + gseError[2] + ') ' + gseError[1];
  61. } else if (win.gmail_error) {
  62. return win.gmail_error + 700;
  63. } else if (win.rc) {
  64. return 600 + win.rc % 100;
  65. } else {
  66. return null;
  67. }
  68. }
  69. /** Logs the status of an iframeIo object */
  70. function logStatus(i) {
  71. goog.log.fine(
  72. testLogger, 'Is complete/success/active: ' +
  73. [i.isComplete(), i.isSuccess(), i.isActive()].join('/'));
  74. }
  75. function onSuccess(e) {
  76. goog.log.warning(testLogger, 'Request Succeeded');
  77. logStatus(e.target);
  78. }
  79. function onError(e) {
  80. goog.log.warning(testLogger, 'Request Errored: ' + e.target.getLastError());
  81. logStatus(e.target);
  82. }
  83. function onReady(e) {
  84. goog.log.info(
  85. testLogger, 'Test finished and iframe ready, disposing test object');
  86. e.target.dispose();
  87. }
  88. function simpleGet() {
  89. var io = getTestIframeIo();
  90. goog.events.listen(io, 'complete', onSimpleTestComplete);
  91. io.send('/iframeio/ping', 'GET');
  92. }
  93. function simplePost() {
  94. var io = getTestIframeIo();
  95. goog.events.listen(io, 'complete', onSimpleTestComplete);
  96. io.send('/iframeio/ping', 'POST');
  97. }
  98. function onSimpleTestComplete(e) {
  99. goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
  100. }
  101. function abort() {
  102. var io = getTestIframeIo();
  103. goog.events.listen(io, 'complete', onAbortComplete);
  104. goog.events.listen(io, 'abort', onAbort);
  105. io.send('/iframeio/ping', 'GET');
  106. io.abort();
  107. }
  108. function onAbortComplete(e) {
  109. goog.log.info(testLogger, 'Hmm, request should have been aborted');
  110. }
  111. function onAbort(e) {
  112. goog.log.info(testLogger, 'Request aborted');
  113. }
  114. function errorGse404() {
  115. var io = getTestIframeIo();
  116. io.send('/iframeio/404', 'GET');
  117. }
  118. function jsonEcho(method) {
  119. var io = getTestIframeIo();
  120. goog.events.listen(io, 'complete', onJsonComplete);
  121. var data = {'p1': 'x', 'p2': 'y', 'p3': 'z', 'r': 10};
  122. io.send('/iframeio/jsonecho?q1=a&q2=b&q3=c&r=5', method, false, data);
  123. }
  124. function onJsonComplete(e) {
  125. goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
  126. var json = e.target.getResponseJson();
  127. goog.log.info(
  128. testLogger, 'ResponseJson:\n' + goog.debug.deepExpose(json, true));
  129. }
  130. function sendFromForm() {
  131. var io = getTestIframeIo();
  132. goog.events.listen(io, 'success', onUploadSuccess);
  133. goog.events.listen(io, 'error', onUploadError);
  134. io.sendFromForm(document.getElementById('uploadform'));
  135. }
  136. function onUploadSuccess(e) {
  137. goog.log.log(testLogger, goog.log.Level.SHOUT, 'Upload Succeeded');
  138. goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
  139. }
  140. function onUploadError(e) {
  141. goog.log.log(testLogger, goog.log.Level.SHOUT, 'Upload Errored');
  142. goog.log.info(testLogger, 'ResponseText: ' + e.target.getResponseText());
  143. }
  144. function redirect1() {
  145. var io = getTestIframeIo();
  146. io.send('/iframeio/redirect', 'GET');
  147. }
  148. function redirect2() {
  149. var io = getTestIframeIo();
  150. io.send('/iframeio/move', 'GET');
  151. }
  152. function badUrl() {
  153. var io = getTestIframeIo();
  154. io.send('http://news.bbc.co.uk', 'GET');
  155. }
  156. function localUrl1() {
  157. var io = getTestIframeIo();
  158. goog.events.listen(io, 'complete', onLocalSuccess);
  159. io.send('c:\test.txt', 'GET');
  160. }
  161. function localUrl2() {
  162. var io = getTestIframeIo();
  163. goog.events.listen(io, 'success', onLocalSuccess);
  164. io.send('//test.txt', 'GET');
  165. }
  166. function onLocalSuccess(e) {
  167. goog.log.info(
  168. testLogger, 'The file was found:\n' + e.target.getResponseText());
  169. }
  170. function getServerTime(noCache) {
  171. var io = getTestIframeIo();
  172. goog.events.listen(io, 'success', onTestCacheSuccess);
  173. io.send('/iframeio/datetime', 'GET', noCache);
  174. }
  175. function onTestCacheSuccess(e) {
  176. goog.log.info(testLogger, 'Date reported: ' + e.target.getResponseText());
  177. }
  178. function errorGmail() {
  179. var io = getTestIframeIo();
  180. goog.events.listen(io, 'error', onGmailError);
  181. io.send('/iframeio/gmailerror', 'GET');
  182. }
  183. function onGmailError(e) {
  184. goog.log.info(testLogger, 'Gmail error: ' + e.target.getLastError());
  185. }
  186. function errorGfe() {
  187. var io = getTestIframeIo();
  188. goog.events.listen(io, 'error', onGfeError);
  189. io.send('/iframeio/gfeerror', 'GET');
  190. }
  191. function onGfeError(e) {
  192. goog.log.info(testLogger, 'GFE error: ' + e.target.getLastError());
  193. }
  194. function incremental() {
  195. var io = getTestIframeIo();
  196. io.send('/iframeio/incremental', 'GET');
  197. }
  198. window['P'] = function(iframe, data) {
  199. goog.net.IframeIo.getInstanceByName(iframe.name);
  200. goog.log.info(testLogger, 'Data received - ' + data);
  201. };
  202. function postForm() {
  203. var io = getTestIframeIo();
  204. goog.events.listen(io, 'complete', onJsonComplete);
  205. io.sendFromForm(document.getElementById('testfrm'));
  206. }
  207. // UNIT TESTS - to be run via the JsUnit testRunner
  208. // TODO(user): How to unit test all of this? Creating a MockIframe could
  209. // help for the IE code path, but since the other browsers require weird
  210. // behaviors this becomes very tricky.
  211. function testGetForm() {
  212. var frm1 = goog.net.IframeIo.getForm_;
  213. var frm2 = goog.net.IframeIo.getForm_;
  214. assertEquals(frm1, frm2);
  215. }
  216. function testAddFormInputs() {
  217. var form = goog.dom.createElement(goog.dom.TagName.FORM);
  218. goog.net.IframeIo.addFormInputs_(form, {'a': 1, 'b': 2, 'c': 3});
  219. var inputs = goog.dom.getElementsByTagName(goog.dom.TagName.INPUT, form);
  220. assertEquals(3, inputs.length);
  221. for (var i = 0; i < inputs.length; i++) {
  222. assertEquals('hidden', inputs[i].type);
  223. var n = inputs[i].name;
  224. assertEquals(n == 'a' ? '1' : n == 'b' ? '2' : '3', inputs[i].value);
  225. }
  226. }
  227. function testAddFormArrayInputs() {
  228. var form = goog.dom.createElement(goog.dom.TagName.FORM);
  229. var data = {'a': ['blue', 'green'], 'b': ['red', 'pink', 'white']};
  230. goog.net.IframeIo.addFormInputs_(form, data);
  231. var inputs = goog.dom.getElementsByTagName(goog.dom.TagName.INPUT, form);
  232. assertEquals(5, inputs.length);
  233. for (var i = 0; i < inputs.length; i++) {
  234. assertEquals('hidden', inputs[i].type);
  235. var n = inputs[i].name;
  236. assertContains(inputs[i].value, data[n]);
  237. }
  238. }
  239. function testNotIgnoringResponse() {
  240. // This test can't run in IE because we can't forge the check for
  241. // iframe.readyState = 'complete'.
  242. if (goog.userAgent.IE) {
  243. return;
  244. }
  245. var iframeIo = new goog.net.IframeIo();
  246. iframeIo.send('about:blank');
  247. // Simulate the frame finishing loading.
  248. goog.testing.events.fireBrowserEvent(
  249. new goog.testing.events.Event(
  250. goog.events.EventType.LOAD, iframeIo.getRequestIframe()));
  251. assertTrue(iframeIo.isComplete());
  252. }
  253. function testIgnoreResponse() {
  254. var iframeIo = new goog.net.IframeIo();
  255. iframeIo.setIgnoreResponse(true);
  256. iframeIo.send('about:blank');
  257. // Simulate the frame finishing loading.
  258. goog.testing.events.fireBrowserEvent(
  259. new goog.testing.events.Event(
  260. goog.events.EventType.LOAD, iframeIo.getRequestIframe()));
  261. // Although the request is complete, the IframeIo isn't paying attention.
  262. assertFalse(iframeIo.isComplete());
  263. }