summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-byte-streams.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /addon-sdk/source/test/test-byte-streams.js
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'addon-sdk/source/test/test-byte-streams.js')
-rw-r--r--addon-sdk/source/test/test-byte-streams.js169
1 files changed, 0 insertions, 169 deletions
diff --git a/addon-sdk/source/test/test-byte-streams.js b/addon-sdk/source/test/test-byte-streams.js
deleted file mode 100644
index 7d45130aa..000000000
--- a/addon-sdk/source/test/test-byte-streams.js
+++ /dev/null
@@ -1,169 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-const byteStreams = require("sdk/io/byte-streams");
-const file = require("sdk/io/file");
-const { pathFor } = require("sdk/system");
-const { Loader } = require("sdk/test/loader");
-
-const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used.");
-
-// This should match the constant of the same name in byte-streams.js.
-const BUFFER_BYTE_LEN = 0x8000;
-
-exports.testWriteRead = function (assert) {
- let fname = dataFileFilename();
-
- // Write a small string less than the stream's buffer size...
- let str = "exports.testWriteRead data!";
- let stream = open(assert, fname, true);
- assert.ok(!stream.closed, "stream.closed after open should be false");
- stream.write(str);
- stream.close();
- assert.ok(stream.closed, "Stream should be closed after stream.close");
- assert.throws(() => stream.write("This shouldn't be written!"),
- STREAM_CLOSED_ERROR,
- "stream.write after close should raise error");
-
- // ... and read it.
- stream = open(assert, fname);
- assert.equal(stream.read(), str,
- "stream.read should return string written");
- assert.equal(stream.read(), "",
- "stream.read at EOS should return empty string");
- stream.close();
- assert.ok(stream.closed, "Stream should be closed after stream.close");
- assert.throws(() => stream.read(),
- STREAM_CLOSED_ERROR,
- "stream.read after close should raise error");
-
- file.remove(fname);
-};
-
-// Write a big string many times the size of the stream's buffer and read it.
-exports.testWriteReadBig = function (assert) {
- let str = "";
- let bufLen = BUFFER_BYTE_LEN;
- let fileSize = bufLen * 10;
- for (let i = 0; i < fileSize; i++)
- str += i % 10;
- let fname = dataFileFilename();
- let stream = open(assert, fname, true);
- stream.write(str);
- stream.close();
- stream = open(assert, fname);
- assert.equal(stream.read(), str,
- "stream.read should return string written");
- stream.close();
- file.remove(fname);
-};
-
-// The same, but write and read in chunks.
-exports.testWriteReadChunks = function (assert) {
- let str = "";
- let bufLen = BUFFER_BYTE_LEN;
- let fileSize = bufLen * 10;
- for (let i = 0; i < fileSize; i++)
- str += i % 10;
- let fname = dataFileFilename();
- let stream = open(assert, fname, true);
- let i = 0;
- while (i < str.length) {
- // Use a chunk length that spans buffers.
- let chunk = str.substr(i, bufLen + 1);
- stream.write(chunk);
- i += bufLen + 1;
- }
- stream.close();
- stream = open(assert, fname);
- let readStr = "";
- bufLen = BUFFER_BYTE_LEN;
- let readLen = bufLen + 1;
- do {
- var frag = stream.read(readLen);
- readStr += frag;
- } while (frag);
- stream.close();
- assert.equal(readStr, str,
- "stream.write and read in chunks should work as expected");
- file.remove(fname);
-};
-
-exports.testReadLengths = function (assert) {
- let fname = dataFileFilename();
- let str = "exports.testReadLengths data!";
- let stream = open(assert, fname, true);
- stream.write(str);
- stream.close();
-
- stream = open(assert, fname);
- assert.equal(stream.read(str.length * 1000), str,
- "stream.read with big byte length should return string " +
- "written");
- stream.close();
-
- stream = open(assert, fname);
- assert.equal(stream.read(0), "",
- "string.read with zero byte length should return empty " +
- "string");
- stream.close();
-
- stream = open(assert, fname);
- assert.equal(stream.read(-1), "",
- "string.read with negative byte length should return " +
- "empty string");
- stream.close();
-
- file.remove(fname);
-};
-
-exports.testTruncate = function (assert) {
- let fname = dataFileFilename();
- let str = "exports.testReadLengths data!";
- let stream = open(assert, fname, true);
- stream.write(str);
- stream.close();
-
- stream = open(assert, fname);
- assert.equal(stream.read(), str,
- "stream.read should return string written");
- stream.close();
-
- stream = open(assert, fname, true);
- stream.close();
-
- stream = open(assert, fname);
- assert.equal(stream.read(), "",
- "stream.read after truncate should be empty");
- stream.close();
-
- file.remove(fname);
-};
-
-exports.testUnload = function (assert) {
- let loader = Loader(module);
- let file = loader.require("sdk/io/file");
-
- let filename = dataFileFilename("temp-b");
- let stream = file.open(filename, "wb");
-
- loader.unload();
- assert.ok(stream.closed, "Stream should be closed after module unload");
-};
-
-// Returns the name of a file that should be used to test writing and reading.
-function dataFileFilename() {
- return file.join(pathFor("ProfD"), "test-byte-streams-data");
-}
-
-// Opens and returns the given file and ensures it's of the correct class.
-function open(assert, filename, forWriting) {
- let stream = file.open(filename, forWriting ? "wb" : "b");
- let klass = forWriting ? "ByteWriter" : "ByteReader";
- assert.ok(stream instanceof byteStreams[klass],
- "Opened stream should be a " + klass);
- return stream;
-}
-
-require('sdk/test').run(exports);