Bug 747876 - Companion testsuite;r=taras

This commit is contained in:
David Rajchenbach-Teller 2012-06-15 17:22:40 +02:00
parent 05798f6fb6
commit a74ce62b20
5 changed files with 260 additions and 2 deletions

View File

@ -9,6 +9,8 @@ _CHROME_TEST_FILES = \
test_osfile_back.xul \
worker_test_osfile_unix.js \
worker_test_osfile_win.js \
test_osfile_front.xul \
worker_test_osfile_front.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,58 @@
<?xml version="1.0"?>
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Testing OS.File on a chrome worker thread"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload="test();">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
<script type="application/javascript">
<![CDATA[
let worker;
function test() {
ok(true, "test_osfile_front.xul: Starting test");
worker = new ChromeWorker("worker_test_osfile_front.js");
SimpleTest.waitForExplicitFinish();
ok(true, "test_osfile_front.xul: Chrome worker created");
dump("MAIN: go\n");
worker.onerror = function(error) {
error.preventDefault();
ok(false, "error "+error);
}
worker.onmessage = function(msg) {
ok(true, "MAIN: onmessage "+JSON.stringify(msg));
switch (msg.data.kind) {
case "is":
return SimpleTest.is(msg.data.a, msg.data.b, msg.data.description);
case "isnot":
return SimpleTest.isnot(msg.data.a, msg.data.b, msg.data.description);
case "ok":
return SimpleTest.ok(msg.data.condition, msg.data.description);
case "finish":
SimpleTest.finish();
return;
default:
SimpleTest.ok(false, "test_osfile_front.xul: wrong message "+JSON.stringify(msg.data));
return;
}
};
worker.postMessage(0);
ok(true, "test_osfile_front.xul: Test in progress");
};
]]>
</script>
<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display"></p>
<div id="content" style="display:none;"></div>
<pre id="test"></pre>
</body>
<label id="test-result"/>
</window>

View File

@ -0,0 +1,200 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
function log(text) {
dump("WORKER "+text+"\n");
}
function send(message) {
self.postMessage(message);
}
self.onmessage = function(msg) {
self.onmessage = function(msg) {
log("ignored message "+JSON.stringify(msg.data));
};
try {
test_init();
test_open_existing_file();
test_open_non_existing_file();
test_copy_existing_file();
test_read_write_file();
test_move_file();
} catch (x) {
log("Catching error: " + x);
log("Stack: " + x.stack);
log("Source: " + x.toSource());
ok(false, x.toString() + "\n" + x.stack);
}
finish();
};
function finish() {
send({kind: "finish"});
}
function ok(condition, description) {
send({kind: "ok", condition: condition, description:description});
}
function is(a, b, description) {
send({kind: "is", a: a, b:b, description:description});
}
function isnot(a, b, description) {
send({kind: "isnot", a: a, b:b, description:description});
}
function test_init() {
ok(true, "Starting test_init");
importScripts("resource:///modules/osfile.jsm");
}
/**
* Test that we can open an existing file.
*/
function test_open_existing_file()
{
ok(true, "Starting test_open_existing");
let file = OS.File.open("chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js");
file.close();
}
/**
* Test that opening a file that does not exist fails with the right error.
*/
function test_open_non_existing_file()
{
ok(true, "Starting test_open_non_existing");
let exn;
try {
let file = OS.File.open("/I do not exist");
} catch (x) {
exn = x;
ok(true, "test_open_non_existing_file: Exception detail " + exn);
}
ok(!!exn, "test_open_non_existing_file: Exception was raised ");
ok(exn instanceof OS.File.Error, "test_open_non_existing_file: Exception was a OS.File.Error");
ok(exn.becauseNoSuchFile, "test_open_non_existing_file: Exception confirms that the file does not exist");
}
/**
* Utility function for comparing two files.
*/
function compare_files(test, sourcePath, destPath)
{
ok(true, test + ": Comparing " + sourcePath + " and " + destPath);
let source = OS.File.open(sourcePath);
let dest = OS.File.open(destPath);
ok(true, "Files are open");
let array1 = new (ctypes.ArrayType(ctypes.char, 4096))();
let array2 = new (ctypes.ArrayType(ctypes.char, 4096))();
ok(true, "Arrays are created");
let pos = 0;
while (true) {
ok(true, "Position: "+pos);
let bytes_read1 = source.read(array1, 4096);
let bytes_read2 = dest.read(array2, 4096);
is (bytes_read1 > 0, bytes_read2 > 0,
test + ": Both files contain data or neither does " +
bytes_read1 + ", " + bytes_read2);
if (bytes_read1 == 0) {
break;
}
let bytes;
if (bytes_read1 != bytes_read2) {
// This would be surprising, but theoretically possible with a
// remote file system, I believe.
bytes = Math.min(bytes_read1, bytes_read2);
pos += bytes;
source.setPosition(pos, OS.File.POS_START);
dest.setPosition(pos, OS.File.POS_START);
} else {
bytes = bytes_read1;
pos += bytes;
}
for (let i = 0; i < bytes; ++i) {
if (array1[i] != array2[i]) {
ok(false, test + ": Files do not match at position " + i
+ " (" + array1[i] + "/ " + array2[i] + ")");
}
}
}
source.close();
dest.close();
ok(true, test + ": Comparison complete");
}
/**
* Test basic read/write through an ArrayBuffer
*/
function test_read_write_file()
{
let src_file_name = "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js";
let tmp_file_name = "test_osfile_front.tmp";
ok(true, "Starting test_read_write_file");
let source = OS.File.open(src_file_name);
let dest = OS.File.open(tmp_file_name, {write: true, trunc:true});
let buf = new ArrayBuffer(4096);
for (let bytesAvailable = source.read(buf, 4096);
bytesAvailable != 0;
bytesAvailable = source.read(buf, 4096)) {
let bytesWritten = dest.write(buf, bytesAvailable);
if (bytesWritten != bytesAvailable) {
eq(bytesWritten, bytesAvailable, "test_read_write_file: writing all bytes");
}
}
ok(true, "test_read_write_file: copy complete");
source.close();
dest.close();
compare_files("test_read_write_file", src_file_name, tmp_file_name);
OS.File.remove(tmp_file_name);
}
/**
* Test that copying a file using |copy| works.
*/
function test_copy_existing_file()
{
let src_file_name = "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js";
let tmp_file_name = "test_osfile_front.tmp";
ok(true, "Starting test_copy_existing");
OS.File.copy(src_file_name, tmp_file_name);
ok(true, "test_copy_existing: Copy complete");
compare_files("test_copy_existing", src_file_name, tmp_file_name);
ok(true, "test_copy_existing: Cleaning up");
OS.File.remove(tmp_file_name);
}
/**
* Test that moving a file works.
*/
function test_move_file()
{
ok(true, "test_move_file: Starting");
// 1. Copy file into a temporary file
let src_file_name = "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js";
let tmp_file_name = "test_osfile_front.tmp";
let tmp2_file_name = "test_osfile_front.tmp2";
OS.File.copy(src_file_name, tmp_file_name);
ok(true, "test_move_file: Copy complete");
// 2. Move
OS.File.move(tmp_file_name, tmp2_file_name);
ok(true, "test_move_file: Move complete");
// 3. Check
compare_files("test_move_file", src_file_name, tmp2_file_name);
ok(true, "test_move_file: Cleaning up");
OS.File.remove(tmp2_file_name);
}

View File

@ -43,7 +43,6 @@ function isnot(a, b, description) {
function test_init() {
ok(true, "Starting test_init");
importScripts("resource:///modules/osfile.jsm");
OS.Unix.File._init();
}
function test_open_close() {

View File

@ -43,7 +43,6 @@ function isnot(a, b, description) {
function test_init() {
ok(true, "Starting test_init");
importScripts("resource:///modules/osfile.jsm");
OS.Win.File._init();
}
function test_OpenClose() {