michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const file = require("sdk/io/file"); michael@0: const { pathFor } = require("sdk/system"); michael@0: const { Loader } = require("sdk/test/loader"); michael@0: michael@0: const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used."); michael@0: michael@0: // This should match the constant of the same name in text-streams.js. michael@0: const BUFFER_BYTE_LEN = 0x8000; michael@0: michael@0: exports.testWriteRead = function (assert) { michael@0: let fname = dataFileFilename(); michael@0: michael@0: // Write a small string less than the stream's buffer size... michael@0: let str = "exports.testWriteRead data!"; michael@0: let stream = file.open(fname, "w"); michael@0: assert.ok(!stream.closed, "stream.closed after open should be false"); michael@0: stream.write(str); michael@0: stream.close(); michael@0: assert.ok(stream.closed, "stream.closed after close should be true"); michael@0: assert.throws(function () stream.close(), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.close after already closed should raise error"); michael@0: assert.throws(function () stream.write("This shouldn't be written!"), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.write after close should raise error"); michael@0: michael@0: // ... and read it. michael@0: stream = file.open(fname); michael@0: assert.ok(!stream.closed, "stream.closed after open should be false"); michael@0: assert.equal(stream.read(), str, michael@0: "stream.read should return string written"); michael@0: assert.equal(stream.read(), "", michael@0: "stream.read at EOS should return empty string"); michael@0: stream.close(); michael@0: assert.ok(stream.closed, "stream.closed after close should be true"); michael@0: assert.throws(function () stream.close(), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.close after already closed should raise error"); michael@0: assert.throws(function () stream.read(), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.read after close should raise error"); michael@0: michael@0: // Write a big string many times the size of the stream's buffer and read it. michael@0: // Since it comes after the previous test, this also ensures that the file is michael@0: // truncated when it's opened for writing. michael@0: str = ""; michael@0: let bufLen = BUFFER_BYTE_LEN; michael@0: let fileSize = bufLen * 10; michael@0: for (let i = 0; i < fileSize; i++) michael@0: str += i % 10; michael@0: stream = file.open(fname, "w"); michael@0: stream.write(str); michael@0: stream.close(); michael@0: stream = file.open(fname); michael@0: assert.equal(stream.read(), str, michael@0: "stream.read should return string written"); michael@0: stream.close(); michael@0: michael@0: // The same, but write and read in chunks. michael@0: stream = file.open(fname, "w"); michael@0: let i = 0; michael@0: while (i < str.length) { michael@0: // Use a chunk length that spans buffers. michael@0: let chunk = str.substr(i, bufLen + 1); michael@0: stream.write(chunk); michael@0: i += bufLen + 1; michael@0: } michael@0: stream.close(); michael@0: stream = file.open(fname); michael@0: let readStr = ""; michael@0: bufLen = BUFFER_BYTE_LEN; michael@0: let readLen = bufLen + 1; michael@0: do { michael@0: var frag = stream.read(readLen); michael@0: readStr += frag; michael@0: } while (frag); michael@0: stream.close(); michael@0: assert.equal(readStr, str, michael@0: "stream.write and read in chunks should work as expected"); michael@0: michael@0: // Read the same file, passing in strange numbers of bytes to read. michael@0: stream = file.open(fname); michael@0: assert.equal(stream.read(fileSize * 100), str, michael@0: "stream.read with big byte length should return string " + michael@0: "written"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(fname); michael@0: assert.equal(stream.read(0), "", michael@0: "string.read with zero byte length should return empty " + michael@0: "string"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(fname); michael@0: assert.equal(stream.read(-1), "", michael@0: "string.read with negative byte length should return " + michael@0: "empty string"); michael@0: stream.close(); michael@0: michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: exports.testWriteAsync = function (assert, done) { michael@0: let fname = dataFileFilename(); michael@0: let str = "exports.testWriteAsync data!"; michael@0: let stream = file.open(fname, "w"); michael@0: assert.ok(!stream.closed, "stream.closed after open should be false"); michael@0: michael@0: // Write. michael@0: stream.writeAsync(str, function (err) { michael@0: assert.equal(this, stream, "|this| should be the stream object"); michael@0: assert.equal(err, undefined, michael@0: "stream.writeAsync should not cause error"); michael@0: assert.ok(stream.closed, "stream.closed after write should be true"); michael@0: assert.throws(function () stream.close(), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.close after already closed should raise error"); michael@0: assert.throws(function () stream.writeAsync("This shouldn't work!"), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.writeAsync after close should raise error"); michael@0: michael@0: // Read. michael@0: stream = file.open(fname, "r"); michael@0: assert.ok(!stream.closed, "stream.closed after open should be false"); michael@0: let readStr = stream.read(); michael@0: assert.equal(readStr, str, michael@0: "string.read should yield string written"); michael@0: stream.close(); michael@0: file.remove(fname); michael@0: done(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testUnload = function (assert) { michael@0: let loader = Loader(module); michael@0: let file = loader.require("sdk/io/file"); michael@0: michael@0: let filename = dataFileFilename("temp"); michael@0: let stream = file.open(filename, "w"); michael@0: michael@0: loader.unload(); michael@0: assert.ok(stream.closed, "stream should be closed after module unload"); michael@0: }; michael@0: michael@0: // Returns the name of a file that should be used to test writing and reading. michael@0: function dataFileFilename() { michael@0: return file.join(pathFor("ProfD"), "test-text-streams-data"); michael@0: } michael@0: michael@0: require('sdk/test').run(exports);