test_file2.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.importGlobalProperties(['File']);
  5. const Ci = Components.interfaces;
  6. function run_test() {
  7. // throw if anything goes wrong
  8. // find the current directory path
  9. var file = Components.classes["@mozilla.org/file/directory_service;1"]
  10. .getService(Ci.nsIProperties)
  11. .get("CurWorkD", Ci.nsIFile);
  12. file.append("xpcshell.ini");
  13. // should be able to construct a file
  14. var f1 = File.createFromFileName(file.path);
  15. // and with nsIFiles
  16. var f2 = File.createFromNsIFile(file);
  17. // do some tests
  18. do_check_true(f1 instanceof File, "Should be a DOM File");
  19. do_check_true(f2 instanceof File, "Should be a DOM File");
  20. do_check_true(f1.name == "xpcshell.ini", "Should be the right file");
  21. do_check_true(f2.name == "xpcshell.ini", "Should be the right file");
  22. do_check_true(f1.type == "", "Should be the right type");
  23. do_check_true(f2.type == "", "Should be the right type");
  24. var threw = false;
  25. try {
  26. // Needs a ctor argument
  27. var f7 = File();
  28. } catch (e) {
  29. threw = true;
  30. }
  31. do_check_true(threw, "No ctor arguments should throw");
  32. var threw = false;
  33. try {
  34. // Needs a valid ctor argument
  35. var f7 = File(Date(132131532));
  36. } catch (e) {
  37. threw = true;
  38. }
  39. do_check_true(threw, "Passing a random object should fail");
  40. var threw = false
  41. try {
  42. // Directories fail
  43. var dir = Components.classes["@mozilla.org/file/directory_service;1"]
  44. .getService(Ci.nsIProperties)
  45. .get("CurWorkD", Ci.nsIFile);
  46. var f7 = File.createFromNsIFile(dir)
  47. } catch (e) {
  48. threw = true;
  49. }
  50. do_check_true(threw, "Can't create a File object for a directory");
  51. }