component-blob.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  5. Components.utils.importGlobalProperties(['Blob', 'File']);
  6. const Cc = Components.classes;
  7. const Ci = Components.interfaces;
  8. const Cu = Components.utils;
  9. function do_check_true(cond, text) {
  10. // we don't have the test harness' utilities in this scope, so we need this
  11. // little helper. In the failure case, the exception is propagated to the
  12. // caller in the main run_test() function, and the test fails.
  13. if (!cond)
  14. throw "Failed check: " + text;
  15. }
  16. function BlobComponent() {
  17. this.wrappedJSObject = this;
  18. }
  19. BlobComponent.prototype =
  20. {
  21. doTest: function() {
  22. // throw if anything goes wrong
  23. let testContent = "<a id=\"a\"><b id=\"b\">hey!<\/b><\/a>";
  24. // should be able to construct a file
  25. var f1 = new Blob([testContent], {"type" : "text/xml"});
  26. // do some tests
  27. do_check_true(f1 instanceof Ci.nsIDOMBlob, "Should be a DOM Blob");
  28. do_check_true(!(f1 instanceof File), "Should not be a DOM File");
  29. do_check_true(f1.type == "text/xml", "Wrong type");
  30. do_check_true(f1.size == testContent.length, "Wrong content size");
  31. var f2 = new Blob();
  32. do_check_true(f2.size == 0, "Wrong size");
  33. do_check_true(f2.type == "", "Wrong type");
  34. var threw = false;
  35. try {
  36. // Needs a valid ctor argument
  37. var f2 = new Blob(Date(132131532));
  38. } catch (e) {
  39. threw = true;
  40. }
  41. do_check_true(threw, "Passing a random object should fail");
  42. return true;
  43. },
  44. // nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm
  45. classDescription: "Blob in components scope code",
  46. classID: Components.ID("{06215993-a3c2-41e3-bdfd-0a3a2cc0b65c}"),
  47. contractID: "@mozilla.org/tests/component-blob;1",
  48. // nsIClassInfo
  49. flags: 0,
  50. getInterfaces: function getInterfaces(aCount) {
  51. var interfaces = [Components.interfaces.nsIClassInfo];
  52. aCount.value = interfaces.length;
  53. return interfaces;
  54. },
  55. getScriptableHelper: function getScriptableHelper() {
  56. return null;
  57. },
  58. // nsISupports
  59. QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIClassInfo])
  60. };
  61. var gComponentsArray = [BlobComponent];
  62. this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);