common_temporaryFileBlob.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var data = new Array(256).join("1234567890ABCDEF");
  2. function createXHR() {
  3. var xhr = new XMLHttpRequest();
  4. xhr.open("POST", "temporaryFileBlob.sjs");
  5. xhr.responseType = 'blob';
  6. xhr.send({toString: function() { return data; }});
  7. return xhr;
  8. }
  9. function test_simple() {
  10. info("Simple test");
  11. var xhr = createXHR();
  12. xhr.onloadend = function() {
  13. ok(xhr.response instanceof Blob, "We have a blob!");
  14. is(xhr.response.size, data.length, "Data length matches");
  15. var fr = new FileReader();
  16. fr.readAsText(xhr.response);
  17. fr.onload = function() {
  18. is(fr.result, data, "Data content matches");
  19. next();
  20. }
  21. }
  22. }
  23. function test_abort() {
  24. info("Aborting during onloading");
  25. var xhr = createXHR();
  26. xhr.onprogress = function() {
  27. xhr.abort();
  28. }
  29. xhr.onloadend = function() {
  30. ok(!xhr.response, "We should not have a Blob!");
  31. next();
  32. }
  33. }
  34. function test_reuse() {
  35. info("Reuse test");
  36. var xhr = createXHR();
  37. var count = 0;
  38. xhr.onloadend = function() {
  39. ok(xhr.response instanceof Blob, "We have a blob!");
  40. is(xhr.response.size, data.length, "Data length matches");
  41. var fr = new FileReader();
  42. fr.readAsText(xhr.response);
  43. fr.onload = function() {
  44. is(fr.result, data, "Data content matches");
  45. if (++count > 2) {
  46. next();
  47. return;
  48. }
  49. xhr.open("POST", "temporaryFileBlob.sjs");
  50. xhr.responseType = 'blob';
  51. xhr.send({toString: function() { return data; }});
  52. }
  53. }
  54. }
  55. function test_worker_generic(test) {
  56. var w = new Worker('worker_temporaryFileBlob.js');
  57. w.onmessage = function(e) {
  58. if (e.data.type == 'info') {
  59. info(e.data.msg);
  60. } else if (e.data.type == 'check') {
  61. ok(e.data.what, e.data.msg);
  62. } else if (e.data.type == 'finish') {
  63. next();
  64. } else {
  65. ok(false, 'Something wrong happened');
  66. }
  67. }
  68. w.postMessage(test);
  69. }
  70. function test_worker() {
  71. info("XHR in workers");
  72. test_worker_generic('simple');
  73. }
  74. function test_worker_abort() {
  75. info("XHR in workers");
  76. test_worker_generic('abort');
  77. }
  78. function test_worker_reuse() {
  79. info("XHR in workers");
  80. test_worker_generic('reuse');
  81. }