xpcom/tests/unit/test_file_renameTo.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/unit/test_file_renameTo.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,61 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +const Cc = Components.classes;
    1.10 +const Ci = Components.interfaces;
    1.11 +
    1.12 +function run_test()
    1.13 +{
    1.14 +  // Create the base directory.
    1.15 +  let base = Cc['@mozilla.org/file/directory_service;1']
    1.16 +             .getService(Ci.nsIProperties)
    1.17 +             .get('TmpD', Ci.nsILocalFile);
    1.18 +  base.append('renameTesting');
    1.19 +  if (base.exists()) {
    1.20 +    base.remove(true);
    1.21 +  }
    1.22 +  base.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8));
    1.23 +
    1.24 +  // Create a sub directory under the base.
    1.25 +  let subdir = base.clone();
    1.26 +  subdir.append('subdir');
    1.27 +  subdir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0777', 8));
    1.28 +
    1.29 +  // Create a file under the sub directory.
    1.30 +  let tempFile = subdir.clone();
    1.31 +  tempFile.append('file0.txt');
    1.32 +  tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0777', 8));
    1.33 +
    1.34 +  // Test renameTo in the base directory
    1.35 +  tempFile.renameTo(null, 'file1.txt');
    1.36 +  do_check_true(exists(subdir, 'file1.txt'));
    1.37 +
    1.38 +  // Test moving across directories
    1.39 +  tempFile = subdir.clone();
    1.40 +  tempFile.append('file1.txt');
    1.41 +  tempFile.renameTo(base, '');
    1.42 +  do_check_true(exists(base, 'file1.txt'));
    1.43 +
    1.44 +  // Test moving across directories and renaming at the same time
    1.45 +  tempFile = base.clone();
    1.46 +  tempFile.append('file1.txt');
    1.47 +  tempFile.renameTo(subdir, 'file2.txt');
    1.48 +  do_check_true(exists(subdir, 'file2.txt'));
    1.49 +
    1.50 +  // Test moving a directory
    1.51 +  subdir.renameTo(base, 'renamed');
    1.52 +  do_check_true(exists(base, 'renamed'));
    1.53 +  let renamed = base.clone();
    1.54 +  renamed.append('renamed');
    1.55 +  do_check_true(exists(renamed, 'file2.txt'));
    1.56 +
    1.57 +  base.remove(true);
    1.58 +}
    1.59 +
    1.60 +function exists(parent, filename) {
    1.61 +  let file = parent.clone();
    1.62 +  file.append(filename);
    1.63 +  return file.exists();
    1.64 +}

mercurial