2014-06-25 09:12:07 +04:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
2014-01-06 16:32:57 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-12-11 23:19:38 +03:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2017-09-29 01:49:04 +03:00
|
|
|
function run_test() {
|
2014-01-06 16:32:57 +04:00
|
|
|
// Create the base directory.
|
2017-12-11 23:19:38 +03:00
|
|
|
let base = Services.dirsvc.get("TmpD", Ci.nsIFile);
|
2017-09-29 01:49:04 +03:00
|
|
|
base.append("renameTesting");
|
2014-01-06 16:32:57 +04:00
|
|
|
if (base.exists()) {
|
|
|
|
base.remove(true);
|
|
|
|
}
|
2017-09-29 01:49:04 +03:00
|
|
|
base.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0777", 8));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
// Create a sub directory under the base.
|
|
|
|
let subdir = base.clone();
|
2017-09-29 01:49:04 +03:00
|
|
|
subdir.append("subdir");
|
|
|
|
subdir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0777", 8));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
// Create a file under the sub directory.
|
|
|
|
let tempFile = subdir.clone();
|
2017-09-29 01:49:04 +03:00
|
|
|
tempFile.append("file0.txt");
|
|
|
|
tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0777", 8));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
// Test renameTo in the base directory
|
2017-09-29 01:49:04 +03:00
|
|
|
tempFile.renameTo(null, "file1.txt");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(exists(subdir, "file1.txt"));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
// Test moving across directories
|
|
|
|
tempFile = subdir.clone();
|
2017-09-29 01:49:04 +03:00
|
|
|
tempFile.append("file1.txt");
|
|
|
|
tempFile.renameTo(base, "");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(exists(base, "file1.txt"));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
// Test moving across directories and renaming at the same time
|
|
|
|
tempFile = base.clone();
|
2017-09-29 01:49:04 +03:00
|
|
|
tempFile.append("file1.txt");
|
|
|
|
tempFile.renameTo(subdir, "file2.txt");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(exists(subdir, "file2.txt"));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
// Test moving a directory
|
2017-09-29 01:49:04 +03:00
|
|
|
subdir.renameTo(base, "renamed");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(exists(base, "renamed"));
|
2014-01-06 16:32:57 +04:00
|
|
|
let renamed = base.clone();
|
2017-09-29 01:49:04 +03:00
|
|
|
renamed.append("renamed");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(exists(renamed, "file2.txt"));
|
2014-01-06 16:32:57 +04:00
|
|
|
|
|
|
|
base.remove(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exists(parent, filename) {
|
|
|
|
let file = parent.clone();
|
|
|
|
file.append(filename);
|
|
|
|
return file.exists();
|
|
|
|
}
|