michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: function run_test() michael@0: { michael@0: // Create the base directory. michael@0: let base = Cc['@mozilla.org/file/directory_service;1'] michael@0: .getService(Ci.nsIProperties) michael@0: .get('TmpD', Ci.nsILocalFile); michael@0: base.append('renameTesting'); michael@0: if (base.exists()) { michael@0: base.remove(true); michael@0: } michael@0: base.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8)); michael@0: michael@0: // Create a sub directory under the base. michael@0: let subdir = base.clone(); michael@0: subdir.append('subdir'); michael@0: subdir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8)); michael@0: michael@0: // Create a file under the sub directory. michael@0: let tempFile = subdir.clone(); michael@0: tempFile.append('file0.txt'); michael@0: tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0777', 8)); michael@0: michael@0: // Test renameTo in the base directory michael@0: tempFile.renameTo(null, 'file1.txt'); michael@0: do_check_true(exists(subdir, 'file1.txt')); michael@0: michael@0: // Test moving across directories michael@0: tempFile = subdir.clone(); michael@0: tempFile.append('file1.txt'); michael@0: tempFile.renameTo(base, ''); michael@0: do_check_true(exists(base, 'file1.txt')); michael@0: michael@0: // Test moving across directories and renaming at the same time michael@0: tempFile = base.clone(); michael@0: tempFile.append('file1.txt'); michael@0: tempFile.renameTo(subdir, 'file2.txt'); michael@0: do_check_true(exists(subdir, 'file2.txt')); michael@0: michael@0: // Test moving a directory michael@0: subdir.renameTo(base, 'renamed'); michael@0: do_check_true(exists(base, 'renamed')); michael@0: let renamed = base.clone(); michael@0: renamed.append('renamed'); michael@0: do_check_true(exists(renamed, 'file2.txt')); michael@0: michael@0: base.remove(true); michael@0: } michael@0: michael@0: function exists(parent, filename) { michael@0: let file = parent.clone(); michael@0: file.append(filename); michael@0: return file.exists(); michael@0: }