fileReaderSyncErrors_worker.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Delegates "is" evaluation back to main thread.
  3. */
  4. function is(actual, expected, message) {
  5. var rtnObj = new Object();
  6. rtnObj.actual = actual;
  7. rtnObj.expected = expected;
  8. rtnObj.message = message;
  9. postMessage(rtnObj);
  10. }
  11. /**
  12. * Tries to write to property.
  13. */
  14. function writeProperty(file, property) {
  15. var oldValue = file[property];
  16. file[property] = -1;
  17. is(file[property], oldValue, "Property " + property + " should be readonly.");
  18. }
  19. /**
  20. * Passes junk arguments to FileReaderSync methods and expects an exception to
  21. * be thrown.
  22. */
  23. function fileReaderJunkArgument(blob) {
  24. var fileReader = new FileReaderSync();
  25. try {
  26. fileReader.readAsBinaryString(blob);
  27. is(false, true, "Should have thrown an exception calling readAsBinaryString.");
  28. } catch(ex) {
  29. is(true, true, "Should have thrown an exception.");
  30. }
  31. try {
  32. fileReader.readAsDataURL(blob);
  33. is(false, true, "Should have thrown an exception calling readAsDataURL.");
  34. } catch(ex) {
  35. is(true, true, "Should have thrown an exception.");
  36. }
  37. try {
  38. fileReader.readAsArrayBuffer(blob);
  39. is(false, true, "Should have thrown an exception calling readAsArrayBuffer.");
  40. } catch(ex) {
  41. is(true, true, "Should have thrown an exception.");
  42. }
  43. try {
  44. fileReader.readAsText(blob);
  45. is(false, true, "Should have thrown an exception calling readAsText.");
  46. } catch(ex) {
  47. is(true, true, "Should have thrown an exception.");
  48. }
  49. }
  50. onmessage = function(event) {
  51. var file = event.data;
  52. // Test read only properties.
  53. writeProperty(file, "size");
  54. writeProperty(file, "type");
  55. writeProperty(file, "name");
  56. // Bad types.
  57. fileReaderJunkArgument(undefined);
  58. fileReaderJunkArgument(-1);
  59. fileReaderJunkArgument(1);
  60. fileReaderJunkArgument(new Object());
  61. fileReaderJunkArgument("hello");
  62. // Post undefined to indicate that testing has finished.
  63. postMessage(undefined);
  64. };