OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Testing file input stream, VM-only, standalone test. | 4 // Testing file input stream, VM-only, standalone test. |
5 | 5 |
6 import "dart:io"; | 6 import "dart:io"; |
7 import "dart:isolate"; | 7 import "dart:isolate"; |
8 | 8 |
9 void testOpenOutputStreamSync() { | 9 void testOpenOutputStreamSync() { |
10 Directory tempDirectory = new Directory('').createTempSync(); | 10 Directory tempDirectory = new Directory('').createTempSync(); |
11 | 11 |
12 // Create a port for waiting on the final result of this test. | 12 // Create a port for waiting on the final result of this test. |
13 ReceivePort done = new ReceivePort(); | 13 ReceivePort done = new ReceivePort(); |
14 done.receive((message, replyTo) { | 14 done.receive((message, replyTo) { |
15 tempDirectory.deleteSync(); | 15 tempDirectory.deleteSync(); |
16 done.close(); | 16 done.close(); |
17 }); | 17 }); |
18 | 18 |
19 String fileName = "${tempDirectory.path}/test"; | 19 String fileName = "${tempDirectory.path}/test"; |
20 File file = new File(fileName); | 20 File file = new File(fileName); |
21 file.createSync(); | 21 file.createSync(); |
22 OutputStream x = file.openOutputStream(); | 22 IOSink x = file.openWrite(); |
23 x.write([65, 66, 67]); | 23 var data = [65, 66, 67]; |
24 Expect.isFalse(x.closed); | 24 x.add(data); |
25 x.close(); | 25 x.close(); |
26 Expect.isTrue(x.closed); | 26 x.done.then((_) { |
27 x.onClosed = () { | 27 Expect.listEquals(file.readAsBytesSync(), data); |
28 Expect.isTrue(x.closed); | |
29 file.deleteSync(); | 28 file.deleteSync(); |
30 done.toSendPort().send("done"); | 29 done.toSendPort().send("done"); |
31 }; | |
32 } | |
33 | |
34 | |
35 void testOutputStreamNoPendingWrite() { | |
36 var tempDirectory; | |
37 | |
38 // Create a port for waiting on the final result of this test. | |
39 ReceivePort done = new ReceivePort(); | |
40 done.receive((message, replyTo) { | |
41 tempDirectory.delete(recursive: true).then((ignore) => done.close()); | |
42 }); | |
43 | |
44 new Directory('').createTemp().then((temp) { | |
45 tempDirectory = temp; | |
46 String fileName = "${tempDirectory.path}/test"; | |
47 File file = new File(fileName); | |
48 file.create().then((ignore) { | |
49 OutputStream stream = file.openOutputStream(); | |
50 final total = 100; | |
51 var count = 0; | |
52 stream.onNoPendingWrites = () { | |
53 stream.write([count++]); | |
54 if (count == total) { | |
55 stream.close(); | |
56 } | |
57 stream.onClosed = () { | |
58 List buffer = new List<int>.fixedLength(total); | |
59 File fileSync = new File(fileName); | |
60 var openedFile = fileSync.openSync(); | |
61 openedFile.readListSync(buffer, 0, total); | |
62 for (var i = 0; i < total; i++) { | |
63 Expect.equals(i, buffer[i]); | |
64 } | |
65 openedFile.closeSync(); | |
66 fileSync.deleteSync(); | |
67 done.toSendPort().send("done"); | |
68 }; | |
69 }; | |
70 }); | |
71 }); | 30 }); |
72 } | 31 } |
73 | 32 |
74 | 33 |
75 void testOutputStreamFlush() { | |
76 Directory tempDirectory = new Directory('').createTempSync(); | |
77 | |
78 // Create a port for waiting on the final result of this test. | |
79 ReceivePort done = new ReceivePort(); | |
80 done.receive((message, replyTo) { | |
81 tempDirectory.deleteSync(); | |
82 done.close(); | |
83 }); | |
84 | |
85 String fileName = "${tempDirectory.path}/test"; | |
86 File file = new File(fileName); | |
87 file.createSync(); | |
88 OutputStream x = file.openOutputStream(); | |
89 x.write([65, 66, 67]); | |
90 x.flush(); | |
91 x.write([68, 69, 70]); | |
92 x.flush(); | |
93 x.write([71, 72, 73]); | |
94 x.onClosed = () { | |
95 file.deleteSync(); | |
96 done.toSendPort().send("done"); | |
97 }; | |
98 x.close(); | |
99 x.onError = (e) => Expect.fail("No error expected"); | |
100 } | |
101 | |
102 main() { | 34 main() { |
103 testOpenOutputStreamSync(); | 35 testOpenOutputStreamSync(); |
104 testOutputStreamNoPendingWrite(); | |
105 testOutputStreamFlush(); | |
106 } | 36 } |
OLD | NEW |