1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_unique.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +"use strict"; 1.5 + 1.6 +Components.utils.import("resource://gre/modules/osfile.jsm"); 1.7 +Components.utils.import("resource://gre/modules/Task.jsm"); 1.8 + 1.9 +function run_test() { 1.10 + do_get_profile(); 1.11 + run_next_test(); 1.12 +} 1.13 + 1.14 +function testFiles(filename) { 1.15 + return Task.spawn(function() { 1.16 + const MAX_TRIES = 10; 1.17 + let profileDir = OS.Constants.Path.profileDir; 1.18 + let path = OS.Path.join(profileDir, filename); 1.19 + 1.20 + // Ensure that openUnique() uses the file name if there is no file with that name already. 1.21 + let openedFile = yield OS.File.openUnique(path); 1.22 + do_print("\nCreate new file: " + openedFile.path); 1.23 + yield openedFile.file.close(); 1.24 + let exists = yield OS.File.exists(openedFile.path); 1.25 + do_check_true(exists); 1.26 + do_check_eq(path, openedFile.path); 1.27 + let fileInfo = yield OS.File.stat(openedFile.path); 1.28 + do_check_true(fileInfo.size == 0); 1.29 + 1.30 + // Ensure that openUnique() creates a new file name using a HEX number, as the original name is already taken. 1.31 + openedFile = yield OS.File.openUnique(path); 1.32 + do_print("\nCreate unique HEX file: " + openedFile.path); 1.33 + yield openedFile.file.close(); 1.34 + exists = yield OS.File.exists(openedFile.path); 1.35 + do_check_true(exists); 1.36 + let fileInfo = yield OS.File.stat(openedFile.path); 1.37 + do_check_true(fileInfo.size == 0); 1.38 + 1.39 + // Ensure that openUnique() generates different file names each time, using the HEX number algorithm 1.40 + let filenames = new Set(); 1.41 + for (let i=0; i < MAX_TRIES; i++) { 1.42 + openedFile = yield OS.File.openUnique(path); 1.43 + yield openedFile.file.close(); 1.44 + filenames.add(openedFile.path); 1.45 + } 1.46 + 1.47 + do_check_eq(filenames.size, MAX_TRIES); 1.48 + 1.49 + // Ensure that openUnique() creates a new human readable file name using, as the original name is already taken. 1.50 + openedFile = yield OS.File.openUnique(path, {humanReadable : true}); 1.51 + do_print("\nCreate unique Human Readable file: " + openedFile.path); 1.52 + yield openedFile.file.close(); 1.53 + exists = yield OS.File.exists(openedFile.path); 1.54 + do_check_true(exists); 1.55 + let fileInfo = yield OS.File.stat(openedFile.path); 1.56 + do_check_true(fileInfo.size == 0); 1.57 + 1.58 + // Ensure that openUnique() generates different human readable file names each time 1.59 + filenames = new Set(); 1.60 + for (let i=0; i < MAX_TRIES; i++) { 1.61 + openedFile = yield OS.File.openUnique(path, {humanReadable : true}); 1.62 + yield openedFile.file.close(); 1.63 + filenames.add(openedFile.path); 1.64 + } 1.65 + 1.66 + do_check_eq(filenames.size, MAX_TRIES); 1.67 + 1.68 + let exn; 1.69 + try { 1.70 + for (let i=0; i < 100; i++) { 1.71 + openedFile = yield OS.File.openUnique(path, {humanReadable : true}); 1.72 + yield openedFile.file.close(); 1.73 + } 1.74 + } catch (ex) { 1.75 + exn = ex; 1.76 + } 1.77 + 1.78 + do_print("Ensure that this raises the correct error"); 1.79 + do_check_true(!!exn); 1.80 + do_check_true(exn instanceof OS.File.Error); 1.81 + do_check_true(exn.becauseExists); 1.82 + }); 1.83 +} 1.84 + 1.85 +add_task(function test_unique() { 1.86 + OS.Shared.DEBUG = true; 1.87 + // Tests files with extension 1.88 + yield testFiles("dummy_unique_file.txt"); 1.89 + // Tests files with no extension 1.90 + yield testFiles("dummy_unique_file_no_ext"); 1.91 +});