toolkit/components/osfile/tests/xpcshell/test_osfile_async_bytes.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:4bf195901408
1 "use strict";
2
3 Components.utils.import("resource://gre/modules/osfile.jsm");
4 Components.utils.import("resource://gre/modules/Task.jsm");
5
6 function run_test() {
7 do_test_pending();
8 run_next_test();
9 }
10
11 /**
12 * Test to ensure that {bytes:} in options to |readTo| and |write| are correctly
13 * preserved.
14 */
15 add_task(function* test_bytes() {
16 let path = OS.Path.join(OS.Constants.Path.tmpDir,
17 "test_osfile_async_bytes.tmp");
18 let file = yield OS.File.open(path, {trunc: true, read: true, write: true});
19 try {
20 try {
21 // 1. Test write, by supplying {bytes:} options smaller than the actual
22 // buffer.
23 yield file.write(new Uint8Array(2048), {bytes: 1024});
24 do_check_eq((yield file.stat()).size, 1024);
25
26 // 2. Test same for |readTo|.
27 yield file.setPosition(0, OS.File.POS_START);
28 let read = yield file.readTo(new Uint8Array(1024), {bytes: 512});
29 do_check_eq(read, 512);
30
31 // 3. Test that passing nullish values for |options| still works.
32 yield file.setPosition(0, OS.File.POS_END);
33 yield file.write(new Uint8Array(1024), null);
34 yield file.write(new Uint8Array(1024), undefined);
35 do_check_eq((yield file.stat()).size, 3072);
36 } finally {
37 yield file.close();
38 }
39 } finally {
40 yield OS.File.remove(path);
41 }
42 });
43
44 add_task(do_test_finished);

mercurial