|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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 |
|
6 const Cc = Components.classes; |
|
7 const Ci = Components.interfaces; |
|
8 |
|
9 function run_test() |
|
10 { |
|
11 // Create the base directory. |
|
12 let base = Cc['@mozilla.org/file/directory_service;1'] |
|
13 .getService(Ci.nsIProperties) |
|
14 .get('TmpD', Ci.nsILocalFile); |
|
15 base.append('renameTesting'); |
|
16 if (base.exists()) { |
|
17 base.remove(true); |
|
18 } |
|
19 base.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8)); |
|
20 |
|
21 // Create a sub directory under the base. |
|
22 let subdir = base.clone(); |
|
23 subdir.append('subdir'); |
|
24 subdir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8)); |
|
25 |
|
26 // Create a file under the sub directory. |
|
27 let tempFile = subdir.clone(); |
|
28 tempFile.append('file0.txt'); |
|
29 tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0777', 8)); |
|
30 |
|
31 // Test renameTo in the base directory |
|
32 tempFile.renameTo(null, 'file1.txt'); |
|
33 do_check_true(exists(subdir, 'file1.txt')); |
|
34 |
|
35 // Test moving across directories |
|
36 tempFile = subdir.clone(); |
|
37 tempFile.append('file1.txt'); |
|
38 tempFile.renameTo(base, ''); |
|
39 do_check_true(exists(base, 'file1.txt')); |
|
40 |
|
41 // Test moving across directories and renaming at the same time |
|
42 tempFile = base.clone(); |
|
43 tempFile.append('file1.txt'); |
|
44 tempFile.renameTo(subdir, 'file2.txt'); |
|
45 do_check_true(exists(subdir, 'file2.txt')); |
|
46 |
|
47 // Test moving a directory |
|
48 subdir.renameTo(base, 'renamed'); |
|
49 do_check_true(exists(base, 'renamed')); |
|
50 let renamed = base.clone(); |
|
51 renamed.append('renamed'); |
|
52 do_check_true(exists(renamed, 'file2.txt')); |
|
53 |
|
54 base.remove(true); |
|
55 } |
|
56 |
|
57 function exists(parent, filename) { |
|
58 let file = parent.clone(); |
|
59 file.append(filename); |
|
60 return file.exists(); |
|
61 } |