test-runtime.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ;(function(Basic, x, Runtime, Url, File, Blob, FormData, Env) {
  2. var type = "test", extensions = {};
  3. function TestRuntime(options) {
  4. var I = this
  5. , True = Runtime.capTrue
  6. , False = Runtime.capFalse
  7. ;
  8. Runtime.call(this, options, type, {
  9. access_binary: False,
  10. access_image_binary: False,
  11. display_media: False,
  12. do_cors: True,
  13. drag_and_drop: True,
  14. return_response_headers: True,
  15. return_response_type: True,
  16. report_upload_progress: True,
  17. resize_image: False,
  18. select_file: True,
  19. select_folder: False,
  20. select_multiple: True,
  21. send_binary_string: True,
  22. send_custom_headers: True,
  23. send_multipart: True,
  24. slice_blob: True,
  25. stream_upload: True,
  26. summon_file_dialog: False,
  27. upload_filesize: True,
  28. use_http_method: True
  29. });
  30. Basic.extend(this, {
  31. init : function() {
  32. this.trigger("Init");
  33. },
  34. destroy: (function(destroy) { // extend default destroy method
  35. return function() {
  36. destroy.call(I);
  37. destroy = I = null;
  38. };
  39. }(this.destroy))
  40. });
  41. Basic.extend(this.getShim(), {
  42. Blob: function() {
  43. },
  44. FileInput: function() {
  45. var _files = [];
  46. Basic.extend(this, {
  47. init: function() {
  48. // ready event is perfectly asynchronous
  49. this.trigger({
  50. type: 'ready',
  51. async: true
  52. });
  53. },
  54. setFiles: function(files) {
  55. var comp = this, I = this.getRuntime();
  56. Basic.each(this.files, function(file) {
  57. file = new File(I.uid, file);
  58. file.relativePath = '/fakepath';
  59. comp.files.push(file);
  60. });
  61. if (comp.files.length) {
  62. comp.trigger('change');
  63. }
  64. },
  65. destroy: function() {
  66. _files = null;
  67. }
  68. });
  69. },
  70. XMLHttpRequest: function() {
  71. var _state = XMLHttpRequest.OPENED, _status = 0, _meta;
  72. Basic.extend(this, {
  73. send: function(meta, data) {
  74. var target = this
  75. , upSpeed = 200 * 1024 // kb/s
  76. , downSpeed = 100 * 1024
  77. , upSize = 100 * 1024
  78. , downSize = 10 * 1024
  79. , uploaded = 0
  80. , downloaded = 0
  81. , interval = 50 // ms
  82. , upDelta = interval / 1000 * upSpeed
  83. , downDelta = interval / 1000 * downSpeed
  84. ;
  85. _meta = meta;
  86. if (data instanceof Blob) {
  87. upSize = data.size;
  88. } else if (data instanceof FormData && data.hasBlob()) {
  89. upSize = data.getBlob().size;
  90. }
  91. function updateUpProgress() {
  92. if (_state === XMLHttpRequest.DONE) {
  93. return;
  94. }
  95. uploaded += Math.floor(upDelta);
  96. if (uploaded < upSize) {
  97. target.trigger({
  98. type: 'UploadProgress',
  99. loaded: uploaded,
  100. total: upSize
  101. });
  102. setTimeout(updateUpProgress, interval);
  103. } else {
  104. target.trigger({
  105. type: 'UploadProgress',
  106. loaded: upSize,
  107. total: upSize
  108. });
  109. updateDownProgress();
  110. }
  111. }
  112. function updateDownProgress() {
  113. if (_state === XMLHttpRequest.DONE) {
  114. return;
  115. }
  116. downloaded += Math.floor(downDelta);
  117. if (downloaded < downSize) {
  118. target.trigger({
  119. type: 'Progress',
  120. loaded: downloaded,
  121. total: downSize
  122. });
  123. setTimeout(updateDownProgress, interval);
  124. } else {
  125. target.trigger({
  126. type: 'Progress',
  127. loaded: downSize,
  128. total: downSize
  129. });
  130. _state = XMLHttpRequest.DONE;
  131. // if meta.url is set to http code, trigger error with that code and exit
  132. var errCodes = ['404'];
  133. var m = meta.url.match(new RegExp('('+errCodes.join('|')+')$'));
  134. if (m) {
  135. _status = m[1];
  136. } else {
  137. _status = 200;
  138. }
  139. target.trigger('Load');
  140. }
  141. }
  142. target.trigger('LoadStart');
  143. _state = XMLHttpRequest.LOADING;
  144. updateUpProgress();
  145. },
  146. getStatus: function() {
  147. return _status;
  148. },
  149. getResponse: function(responseType) {
  150. var response = JSON && JSON.stringify(_meta) || '{"jsonrpc" : "2.0", "result" : null, "id" : "id"}';
  151. if (_state !== XMLHttpRequest.DONE) {
  152. return Basic.inArray(responseType, ["", "text"]) !== -1 ? '' : null;
  153. }
  154. return responseType === 'json' && JSON ? JSON.parse(response) : response;
  155. },
  156. getAllResponseHeaders: function() {
  157. var now = new Date()
  158. , headers = [
  159. "Cache-Control:no-store, no-cache, must-revalidate",
  160. "Cache-Control:post-check=0, pre-check=0",
  161. "Connection:Keep-Alive",
  162. "Content-Length:49",
  163. "Content-Type:text/html",
  164. "Date:" + now.toUTCString(),
  165. "Expires:Mon, 26 Jul 1997 05:00:00 GMT",
  166. "Keep-Alive:timeout=15, max=96",
  167. "Last-Modified:" + now.toUTCString(),
  168. "Pragma:no-cache",
  169. "Server:Apache/2.0.64 (Unix) PHP/5.3.5 DAV/2",
  170. "X-Powered-By:PHP/5.3.5"
  171. ];
  172. if (_state > XMLHttpRequest.OPENED) {
  173. return headers.join('\r\n');
  174. }
  175. return '';
  176. },
  177. abort: function() {
  178. _state = XMLHttpRequest.DONE;
  179. _status = 0;
  180. this.trigger('Abort');
  181. },
  182. destroy: function() {
  183. _status = 0;
  184. }
  185. });
  186. XMLHttpRequest.UNSENT = 0;
  187. XMLHttpRequest.OPENED = 1;
  188. XMLHttpRequest.HEADERS_RECEIVED = 2;
  189. XMLHttpRequest.LOADING = 3;
  190. XMLHttpRequest.DONE = 4;
  191. }
  192. });
  193. }
  194. Runtime.addConstructor(type, TestRuntime);
  195. }(
  196. moxie.core.utils.Basic,
  197. moxie.core.Exceptions,
  198. moxie.runtime.Runtime,
  199. moxie.core.utils.Url,
  200. moxie.file.File,
  201. moxie.file.Blob,
  202. moxie.xhr.FormData,
  203. moxie.core.Exceptions,
  204. moxie.core.utils.Env
  205. ));