test_file_renameTo.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. var Cc = Components.classes;
  6. var Ci = Components.interfaces;
  7. function run_test()
  8. {
  9. // Create the base directory.
  10. let base = Cc['@mozilla.org/file/directory_service;1']
  11. .getService(Ci.nsIProperties)
  12. .get('TmpD', Ci.nsILocalFile);
  13. base.append('renameTesting');
  14. if (base.exists()) {
  15. base.remove(true);
  16. }
  17. base.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8));
  18. // Create a sub directory under the base.
  19. let subdir = base.clone();
  20. subdir.append('subdir');
  21. subdir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8));
  22. // Create a file under the sub directory.
  23. let tempFile = subdir.clone();
  24. tempFile.append('file0.txt');
  25. tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0777', 8));
  26. // Test renameTo in the base directory
  27. tempFile.renameTo(null, 'file1.txt');
  28. do_check_true(exists(subdir, 'file1.txt'));
  29. // Test moving across directories
  30. tempFile = subdir.clone();
  31. tempFile.append('file1.txt');
  32. tempFile.renameTo(base, '');
  33. do_check_true(exists(base, 'file1.txt'));
  34. // Test moving across directories and renaming at the same time
  35. tempFile = base.clone();
  36. tempFile.append('file1.txt');
  37. tempFile.renameTo(subdir, 'file2.txt');
  38. do_check_true(exists(subdir, 'file2.txt'));
  39. // Test moving a directory
  40. subdir.renameTo(base, 'renamed');
  41. do_check_true(exists(base, 'renamed'));
  42. let renamed = base.clone();
  43. renamed.append('renamed');
  44. do_check_true(exists(renamed, 'file2.txt'));
  45. base.remove(true);
  46. }
  47. function exists(parent, filename) {
  48. let file = parent.clone();
  49. file.append(filename);
  50. return file.exists();
  51. }