michael@0: "use strict"; michael@0: michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: Components.utils.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: function run_test() { michael@0: do_get_profile(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function testFiles(filename) { michael@0: return Task.spawn(function() { michael@0: const MAX_TRIES = 10; michael@0: let profileDir = OS.Constants.Path.profileDir; michael@0: let path = OS.Path.join(profileDir, filename); michael@0: michael@0: // Ensure that openUnique() uses the file name if there is no file with that name already. michael@0: let openedFile = yield OS.File.openUnique(path); michael@0: do_print("\nCreate new file: " + openedFile.path); michael@0: yield openedFile.file.close(); michael@0: let exists = yield OS.File.exists(openedFile.path); michael@0: do_check_true(exists); michael@0: do_check_eq(path, openedFile.path); michael@0: let fileInfo = yield OS.File.stat(openedFile.path); michael@0: do_check_true(fileInfo.size == 0); michael@0: michael@0: // Ensure that openUnique() creates a new file name using a HEX number, as the original name is already taken. michael@0: openedFile = yield OS.File.openUnique(path); michael@0: do_print("\nCreate unique HEX file: " + openedFile.path); michael@0: yield openedFile.file.close(); michael@0: exists = yield OS.File.exists(openedFile.path); michael@0: do_check_true(exists); michael@0: let fileInfo = yield OS.File.stat(openedFile.path); michael@0: do_check_true(fileInfo.size == 0); michael@0: michael@0: // Ensure that openUnique() generates different file names each time, using the HEX number algorithm michael@0: let filenames = new Set(); michael@0: for (let i=0; i < MAX_TRIES; i++) { michael@0: openedFile = yield OS.File.openUnique(path); michael@0: yield openedFile.file.close(); michael@0: filenames.add(openedFile.path); michael@0: } michael@0: michael@0: do_check_eq(filenames.size, MAX_TRIES); michael@0: michael@0: // Ensure that openUnique() creates a new human readable file name using, as the original name is already taken. michael@0: openedFile = yield OS.File.openUnique(path, {humanReadable : true}); michael@0: do_print("\nCreate unique Human Readable file: " + openedFile.path); michael@0: yield openedFile.file.close(); michael@0: exists = yield OS.File.exists(openedFile.path); michael@0: do_check_true(exists); michael@0: let fileInfo = yield OS.File.stat(openedFile.path); michael@0: do_check_true(fileInfo.size == 0); michael@0: michael@0: // Ensure that openUnique() generates different human readable file names each time michael@0: filenames = new Set(); michael@0: for (let i=0; i < MAX_TRIES; i++) { michael@0: openedFile = yield OS.File.openUnique(path, {humanReadable : true}); michael@0: yield openedFile.file.close(); michael@0: filenames.add(openedFile.path); michael@0: } michael@0: michael@0: do_check_eq(filenames.size, MAX_TRIES); michael@0: michael@0: let exn; michael@0: try { michael@0: for (let i=0; i < 100; i++) { michael@0: openedFile = yield OS.File.openUnique(path, {humanReadable : true}); michael@0: yield openedFile.file.close(); michael@0: } michael@0: } catch (ex) { michael@0: exn = ex; michael@0: } michael@0: michael@0: do_print("Ensure that this raises the correct error"); michael@0: do_check_true(!!exn); michael@0: do_check_true(exn instanceof OS.File.Error); michael@0: do_check_true(exn.becauseExists); michael@0: }); michael@0: } michael@0: michael@0: add_task(function test_unique() { michael@0: OS.Shared.DEBUG = true; michael@0: // Tests files with extension michael@0: yield testFiles("dummy_unique_file.txt"); michael@0: // Tests files with no extension michael@0: yield testFiles("dummy_unique_file_no_ext"); michael@0: });