Uploader.init.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Plupload: Test Uploader Initialization</title>
  6. <script src="../loader.js"></script>
  7. <script type="text/javascript">
  8. QUnit.config.reorder = false;
  9. QUnit.config.autostart = false;
  10. QUnit.config.testTimeout = 10000;
  11. FileHash.init({
  12. files: ['files/01.min.js', 'files/02.min.js'],
  13. onready: function() {
  14. QUnit.start();
  15. }
  16. });
  17. o.Env.swf_url = "../../js/Moxie.swf";
  18. o.Env.xap_url = "../../js/Moxie.xap";
  19. module("plupload.Uploader", {
  20. setup: function() {
  21. $('#qunit-fixture').html('<div id="uploader" />');
  22. this.options = {
  23. browse_button: 'uploader',
  24. drop_element: 'uploader',
  25. container: 'qunit-fixture',
  26. url: "Plupload/upload.php"
  27. };
  28. this.up = null;
  29. },
  30. teardown: function() {
  31. if (this.up) {
  32. this.up.destroy();
  33. }
  34. }
  35. });
  36. // test required options
  37. test("'url' is not a required option anymore so uploader shouldn't trigger plupload.INIT_ERROR", function() {
  38. expect(1);
  39. delete this.options['url'];
  40. this.up = new plupload.Uploader(this.options);
  41. this.up.bind("Init", function() {
  42. start();
  43. ok(true, "'url' option is not required.");
  44. });
  45. this.up.bind("Error", function(up, err) {
  46. start();
  47. ok(false, "'url' option is not required.");
  48. });
  49. stop();
  50. this.up.init();
  51. });
  52. test("If both 'browse_button' and 'drop_element' are missing, uploader should trigger plupload.INIT_ERROR", function() {
  53. expect(1);
  54. delete this.options['browse_button'];
  55. delete this.options['drop_element'];
  56. this.up = new plupload.Uploader(this.options);
  57. this.up.bind("Init", function() {
  58. start();
  59. ok(false,
  60. "One must specify either 'browse_button' or 'drop_element'.");
  61. });
  62. this.up.bind("Error", function(up, err) {
  63. start();
  64. equal(err.code, plupload.INIT_ERROR,
  65. "One must specify either 'browse_button' or 'drop_element'.");
  66. });
  67. stop();
  68. this.up.init();
  69. });
  70. // test normalized options after instantiation
  71. test("Normalize required_features when passed as comma-delimited string", function() {
  72. expect(1);
  73. this.up = new plupload.Uploader(o.extend(this.options, {
  74. required_features: 'chunks,jpgresize, progress,dragdrop,multi_selection,triggerDialog,display_media'
  75. }));
  76. deepEqual(this.up.settings.required_features, {
  77. slice_blob: true,
  78. send_binary_string: true,
  79. report_upload_progress: true,
  80. select_multiple: true,
  81. summon_file_dialog: true,
  82. drag_and_drop: true,
  83. display_media: true
  84. }, "required_features properly normalized and translated to caps.");
  85. });
  86. test("Normalize required_features when passed as object", function() {
  87. expect(1);
  88. this.up = new plupload.Uploader(o.extend(this.options, {
  89. required_features: {
  90. chunks: true,
  91. jpgresize: true,
  92. progress: true,
  93. dragdrop: true,
  94. multi_selection: true,
  95. triggerDialog: true,
  96. display_media: true
  97. }
  98. }));
  99. deepEqual(this.up.settings.required_features, {
  100. slice_blob: true,
  101. send_binary_string: true,
  102. report_upload_progress: true,
  103. select_multiple: true,
  104. summon_file_dialog: true,
  105. drag_and_drop: true,
  106. display_media: true
  107. }, "required_features properly normalized and translated to caps.");
  108. });
  109. test("Consider special settings as required_features when required_features is boolean true", function() {
  110. this.up = new plupload.Uploader(o.extend(this.options, {
  111. multi_selection: true,
  112. drop_element: 'uploader',
  113. multipart: false,
  114. chunk_size: '1mb',
  115. max_file_size: '1gb',
  116. resize: {
  117. width: 100,
  118. height: 60
  119. },
  120. headers: {
  121. 'X-Test': 1
  122. },
  123. required_features: true
  124. }));
  125. deepEqual(this.up.settings.required_features, {
  126. select_multiple: true,
  127. drag_and_drop: true,
  128. slice_blob: true,
  129. send_binary_string: true,
  130. send_custom_headers: true,
  131. use_http_method: "POST"
  132. }, "required_features properly normalized and translated to caps.");
  133. });
  134. test("When required_features is undefined, consider special settings as optional", function() {
  135. this.up = new plupload.Uploader(o.extend(this.options, {
  136. multi_selection: true,
  137. drop_element: 'uploader',
  138. max_file_size: '1gb',
  139. resize: {
  140. width: 100,
  141. height: 60
  142. },
  143. headers: {
  144. 'X-Test': 1
  145. }
  146. }));
  147. deepEqual(this.up.settings.required_features, {}, "required_features must be empty.");
  148. });
  149. test("Size identifiers in string format (e.g. 1mb) converted to numerical format", function() {
  150. this.up = new plupload.Uploader(o.extend(this.options, {
  151. chunk_size: '1mb',
  152. max_file_size: '1gb'
  153. }));
  154. equal(this.up.settings.chunk_size, 1024 * 1024, "'1mb' converted to numerical bytes");
  155. });
  156. test("Test how default values are assigned to missing options", function() {
  157. this.up = new plupload.Uploader(this.options);
  158. deepEqual(this.up.settings, {
  159. browse_button: plupload.getAll('uploader'),
  160. drop_element: plupload.getAll('uploader'),
  161. container: plupload.get('qunit-fixture'),
  162. url: "Plupload/upload.php",
  163. runtimes: o.Runtime.order,
  164. http_method: "POST",
  165. max_retries: 0,
  166. multipart : true,
  167. send_file_name : true,
  168. multi_selection : true,
  169. file_data_name : 'file',
  170. flash_swf_url : 'js/Moxie.swf',
  171. silverlight_xap_url : 'js/Moxie.xap',
  172. filters : {
  173. prevent_duplicates: false,
  174. prevent_empty: true,
  175. max_file_size: 0,
  176. mime_types: []
  177. },
  178. resize: false,
  179. chunk_size: 0,
  180. required_features: {},
  181. send_chunk_number: true
  182. }, "Proper defaults set.");
  183. });
  184. test("Test how defaults are set for resize object", function() {
  185. this.up = new plupload.Uploader(o.extend(this.options, {
  186. resize: {
  187. width: 100,
  188. height: 60
  189. }
  190. }));
  191. deepEqual(this.up.settings.resize, {
  192. width: 100,
  193. height: 60,
  194. crop: false,
  195. preserve_headers: true
  196. }, "Proper defaults set for resize property.");
  197. });
  198. // test init event with restored runtime specifier
  199. test("Test runtime property", function() {
  200. expect(2);
  201. this.up = new plupload.Uploader(o.extend(this.options, {
  202. runtimes: 'html4,test',
  203. required_features: 'multi_selection'
  204. }));
  205. this.up.bind('Init', function(up, info) {
  206. equal(info.runtime, 'test', "Test second argument for Init handler.");
  207. equal(up.runtime, 'test', "Test uploader's 'runtime' property.");
  208. });
  209. this.up.bind('PostInit', function() {
  210. start();
  211. });
  212. stop();
  213. this.up.init();
  214. });
  215. // test preinit and init
  216. test("Test preinit and init callbacks", function() {
  217. var eventOrder = [];
  218. this.up = new plupload.Uploader(o.extend(this.options, {
  219. preinit: function() {
  220. eventOrder.push(1);
  221. },
  222. init: function() {
  223. start();
  224. eventOrder.push(2);
  225. }
  226. }));
  227. this.up.bind('Init', function() {
  228. eventOrder.push(3);
  229. deepEqual(eventOrder, [1, 2, 3], "preinit callback runs first, then init callback and finally Init handler.");
  230. });
  231. stop();
  232. this.up.init();
  233. });
  234. test("Test preinit and init set of event handlers", function() {
  235. var eventOrder = [];
  236. this.up = new plupload.Uploader(o.extend(this.options, {
  237. preinit: {
  238. PostInit: function() {
  239. eventOrder.push(1);
  240. }
  241. },
  242. init: {
  243. PostInit: function() {
  244. eventOrder.push(2);
  245. start();
  246. deepEqual(eventOrder, [1, 2], "preinit handlers run first and and then init handlers.");
  247. }
  248. }
  249. }));
  250. stop();
  251. this.up.init();
  252. });
  253. module("up.features must be set for backward compatibility", {
  254. setup: function() {
  255. $('#qunit-fixture').html('<div id="uploader" />');
  256. this.options = {
  257. runtimes: 'test',
  258. browse_button: 'uploader',
  259. drop_element: 'uploader',
  260. container: 'qunit-fixture',
  261. url: "Plupload/upload.php",
  262. init: {
  263. Error: function(up) {
  264. start();
  265. ok(false, "Runtime failed to initialise.");
  266. }
  267. }
  268. };
  269. this.up = null;
  270. },
  271. teardown: function() {
  272. if (this.up) {
  273. this.up.destroy();
  274. }
  275. }
  276. });
  277. test("with 'drop_element' only", function() {
  278. delete this.options.browse_button;
  279. var up = new plupload.Uploader(this.options);
  280. up.bind('PostInit', function(up) {
  281. start();
  282. deepEqual(up.features, {
  283. dragdrop: true,
  284. chunks: true,
  285. multipart: true
  286. },
  287. "up.features properly set for backward compatibility."
  288. );
  289. });
  290. stop();
  291. up.init();
  292. });
  293. test("with 'browse_button' only", function() {
  294. delete this.options.drop_element;
  295. var up = new plupload.Uploader(this.options);
  296. up.bind('PostInit', function(up) {
  297. start();
  298. deepEqual(up.features, {
  299. multi_selection: true,
  300. chunks: true,
  301. multipart: true
  302. },
  303. "up.features properly set for backward compatibility."
  304. );
  305. });
  306. stop();
  307. up.init();
  308. });
  309. test("with both", function() {
  310. var up = new plupload.Uploader(this.options);
  311. up.bind('PostInit', function(up) {
  312. start();
  313. deepEqual(up.features, {
  314. dragdrop: true,
  315. chunks: true,
  316. multipart: true,
  317. multi_selection: true
  318. },
  319. "up.features properly set for backward compatibility."
  320. );
  321. });
  322. stop();
  323. up.init();
  324. });
  325. </script>
  326. </head>
  327. <body>
  328. <div id="qunit"></div>
  329. <div id="qunit-fixture" style="position: relative; top: 0 !important; left: 0 !important; width: 100%; height: 9px;"></div>
  330. </body>
  331. </html>