Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, 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 // | 4 // |
| 5 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 | 8 |
| 9 class FileTest { | 9 class FileTest { |
| 10 static Directory tempDirectory; | 10 static Directory tempDirectory; |
| 11 static int numLiveAsyncTests = 0; | 11 static int numLiveAsyncTests = 0; |
| 12 | 12 |
| 13 static void asyncTestStarted() { ++numLiveAsyncTests; } | 13 static void asyncTestStarted() { ++numLiveAsyncTests; } |
| 14 static void asyncTestDone() { | 14 static void asyncTestDone(String name) { |
| 15 --numLiveAsyncTests; | 15 --numLiveAsyncTests; |
| 16 if (numLiveAsyncTests == 0) { | 16 if (numLiveAsyncTests == 0) { |
| 17 deleteTempDirectory(); | 17 deleteTempDirectory(); |
| 18 } | 18 } |
| 19 } | 19 } |
| 20 | 20 |
| 21 static void createTempDirectory(Function doNext) { | 21 static void createTempDirectory(Function doNext) { |
| 22 tempDirectory = new Directory(''); | 22 tempDirectory = new Directory(''); |
| 23 tempDirectory.createTempHandler = doNext; | 23 tempDirectory.createTempHandler = doNext; |
| 24 tempDirectory.createTemp(); | 24 tempDirectory.createTemp(); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 // are identical. | 225 // are identical. |
| 226 Expect.equals(buffer1.length, buffer2.length); | 226 Expect.equals(buffer1.length, buffer2.length); |
| 227 for (int i = 0; i < buffer1.length; i++) { | 227 for (int i = 0; i < buffer1.length; i++) { |
| 228 Expect.equals(buffer1[i], buffer2[i]); | 228 Expect.equals(buffer1[i], buffer2[i]); |
| 229 } | 229 } |
| 230 // Delete the output file. | 230 // Delete the output file. |
| 231 final file4 = file3; | 231 final file4 = file3; |
| 232 file4.deleteHandler = () { | 232 file4.deleteHandler = () { |
| 233 file4.existsHandler = (exists) { | 233 file4.existsHandler = (exists) { |
| 234 Expect.isFalse(exists); | 234 Expect.isFalse(exists); |
| 235 asyncTestDone(); | 235 asyncTestDone("testReadWrite"); |
| 236 }; | 236 }; |
| 237 file4.exists(); | 237 file4.exists(); |
| 238 }; | 238 }; |
| 239 file4.delete(); | 239 file4.delete(); |
| 240 }; | 240 }; |
| 241 openedFile3.close(); | 241 openedFile3.close(); |
| 242 }; | 242 }; |
| 243 openedFile3.readList(buffer2, 0, 42); | 243 openedFile3.readList(buffer2, 0, 42); |
| 244 }; | 244 }; |
| 245 file3.open(); | 245 file3.open(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 file.deleteSync(); | 286 file.deleteSync(); |
| 287 } | 287 } |
| 288 | 288 |
| 289 static void testOutputStreamWriteAppend() { | 289 static void testOutputStreamWriteAppend() { |
| 290 String content = "foobar"; | 290 String content = "foobar"; |
| 291 String filename = tempDirectory.path + "/outstream_write_append"; | 291 String filename = tempDirectory.path + "/outstream_write_append"; |
| 292 File file = new File(filename); | 292 File file = new File(filename); |
| 293 file.createSync(); | 293 file.createSync(); |
| 294 List<int> buffer = content.charCodes(); | 294 List<int> buffer = content.charCodes(); |
| 295 OutputStream outStream = file.openOutputStream(); | 295 OutputStream outStream = file.openOutputStream(); |
| 296 outStream.closeHandler = () { | |
| 297 File file2 = new File(filename); | |
| 298 OutputStream appendingOutput = file2.openOutputStream(FileMode.APPEND); | |
| 299 appendingOutput.write(buffer); | |
| 300 appendingOutput.closeHandler = () { | |
| 301 File file3 = new File(filename); | |
| 302 file3.openHandler = (RandomAccessFile openedFile) { | |
| 303 openedFile.lengthHandler = (int length) { | |
| 304 Expect.equals(content.length * 2, length); | |
| 305 openedFile.closeHandler = () { | |
| 306 asyncTestDone(); | |
| 307 }; | |
| 308 openedFile.close(); | |
| 309 }; | |
| 310 openedFile.length(); | |
| 311 }; | |
| 312 file3.open(); | |
| 313 }; | |
| 314 appendingOutput.close(); | |
| 315 }; | |
| 316 asyncTestStarted(); | |
| 317 outStream.write(buffer); | 296 outStream.write(buffer); |
| 318 outStream.close(); | 297 outStream.close(); |
|
ricow1
2012/02/13 11:40:45
how do we know that the output has been written to
Søren Gjesse
2012/02/13 11:58:04
Output streams are buffering streams which will fl
| |
| 298 File file2 = new File(filename); | |
| 299 OutputStream appendingOutput = file2.openOutputStream(FileMode.APPEND); | |
| 300 appendingOutput.write(buffer); | |
| 301 appendingOutput.close(); | |
| 302 File file3 = new File(filename); | |
| 303 file3.openHandler = (RandomAccessFile openedFile) { | |
| 304 openedFile.lengthHandler = (int length) { | |
| 305 Expect.equals(content.length * 2, length); | |
| 306 openedFile.closeHandler = () { | |
| 307 file3.deleteHandler = () { | |
| 308 asyncTestDone("testOutputStreamWriteAppend"); | |
| 309 }; | |
| 310 file3.delete(); | |
| 311 }; | |
| 312 openedFile.close(); | |
| 313 }; | |
| 314 openedFile.length(); | |
| 315 }; | |
| 316 file3.open(); | |
| 317 asyncTestStarted(); | |
| 319 } | 318 } |
| 320 | 319 |
| 321 | 320 |
| 322 static void testReadWriteSync() { | 321 static void testReadWriteSync() { |
| 323 // Read a file. | 322 // Read a file. |
| 324 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 323 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 325 RandomAccessFile file = (new File(inFilename)).openSync(); | 324 RandomAccessFile file = (new File(inFilename)).openSync(); |
| 326 List<int> buffer1 = new List<int>(42); | 325 List<int> buffer1 = new List<int>(42); |
| 327 int bytes_read = 0; | 326 int bytes_read = 0; |
| 328 int bytes_written = 0; | 327 int bytes_written = 0; |
| 329 bytes_read = file.readListSync(buffer1, 0, 42); | 328 bytes_read = file.readListSync(buffer1, 0, 42); |
| 330 Expect.equals(42, bytes_read); | 329 Expect.equals(42, bytes_read); |
| 331 file.closeSync(); | 330 file.closeSync(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 }; | 374 }; |
| 376 file.createHandler = () { | 375 file.createHandler = () { |
| 377 file.openHandler = (RandomAccessFile openedFile) { | 376 file.openHandler = (RandomAccessFile openedFile) { |
| 378 openedFile.readByteHandler = (int byte) { | 377 openedFile.readByteHandler = (int byte) { |
| 379 Expect.fail("Read byte from empty file"); | 378 Expect.fail("Read byte from empty file"); |
| 380 }; | 379 }; |
| 381 openedFile.errorHandler = (String err) { | 380 openedFile.errorHandler = (String err) { |
| 382 Expect.isTrue(err.indexOf("failed") != -1); | 381 Expect.isTrue(err.indexOf("failed") != -1); |
| 383 openedFile.closeHandler = () { | 382 openedFile.closeHandler = () { |
| 384 file.deleteHandler = () { | 383 file.deleteHandler = () { |
| 385 asyncTestDone(); | 384 asyncTestDone("testReadEmptyFile"); |
| 386 }; | 385 }; |
| 387 file.delete(); | 386 file.delete(); |
| 388 }; | 387 }; |
| 389 openedFile.close(); | 388 openedFile.close(); |
| 390 }; | 389 }; |
| 391 openedFile.readByte(); | 390 openedFile.readByte(); |
| 392 }; | 391 }; |
| 393 file.open(); | 392 file.open(); |
| 394 }; | 393 }; |
| 395 asyncTestStarted(); | 394 asyncTestStarted(); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 openedFile.noPendingWriteHandler = () { | 475 openedFile.noPendingWriteHandler = () { |
| 477 openedFile.lengthHandler = (length) { | 476 openedFile.lengthHandler = (length) { |
| 478 Expect.equals(10, length); | 477 Expect.equals(10, length); |
| 479 openedFile.truncateHandler = () { | 478 openedFile.truncateHandler = () { |
| 480 openedFile.lengthHandler = (length) { | 479 openedFile.lengthHandler = (length) { |
| 481 Expect.equals(5, length); | 480 Expect.equals(5, length); |
| 482 openedFile.closeHandler = () { | 481 openedFile.closeHandler = () { |
| 483 file.deleteHandler = () { | 482 file.deleteHandler = () { |
| 484 file.existsHandler = (exists) { | 483 file.existsHandler = (exists) { |
| 485 Expect.isFalse(exists); | 484 Expect.isFalse(exists); |
| 486 asyncTestDone(); | 485 asyncTestDone("testTruncate"); |
| 487 }; | 486 }; |
| 488 file.exists(); | 487 file.exists(); |
| 489 }; | 488 }; |
| 490 file.delete(); | 489 file.delete(); |
| 491 }; | 490 }; |
| 492 openedFile.close(); | 491 openedFile.close(); |
| 493 }; | 492 }; |
| 494 openedFile.length(); | 493 openedFile.length(); |
| 495 }; | 494 }; |
| 496 openedFile.truncate(5); | 495 openedFile.truncate(5); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 openedFile.lengthHandler = (length) { | 746 openedFile.lengthHandler = (length) { |
| 748 Expect.equals(4, length); | 747 Expect.equals(4, length); |
| 749 openedFile.setPositionHandler = () { | 748 openedFile.setPositionHandler = () { |
| 750 openedFile.noPendingWriteHandler = () { | 749 openedFile.noPendingWriteHandler = () { |
| 751 openedFile.lengthHandler = (length) { | 750 openedFile.lengthHandler = (length) { |
| 752 Expect.equals(8, length); | 751 Expect.equals(8, length); |
| 753 openedFile.closeHandler = () { | 752 openedFile.closeHandler = () { |
| 754 file.deleteHandler = () { | 753 file.deleteHandler = () { |
| 755 file.existsHandler = (exists) { | 754 file.existsHandler = (exists) { |
| 756 Expect.isFalse(exists); | 755 Expect.isFalse(exists); |
| 757 asyncTestDone(); | 756 asyncTestDone("testAppend"); |
| 758 }; | 757 }; |
| 759 file.exists(); | 758 file.exists(); |
| 760 }; | 759 }; |
| 761 file.delete(); | 760 file.delete(); |
| 762 }; | 761 }; |
| 763 openedFile.close(); | 762 openedFile.close(); |
| 764 }; | 763 }; |
| 765 openedFile.length(); | 764 openedFile.length(); |
| 766 }; | 765 }; |
| 767 openedFile.writeString("asdf"); | 766 openedFile.writeString("asdf"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 803 // Main test entrypoint. | 802 // Main test entrypoint. |
| 804 static testMain() { | 803 static testMain() { |
| 805 testRead(); | 804 testRead(); |
| 806 testReadSync(); | 805 testReadSync(); |
| 807 testReadStream(); | 806 testReadStream(); |
| 808 testLength(); | 807 testLength(); |
| 809 testLengthSync(); | 808 testLengthSync(); |
| 810 testPosition(); | 809 testPosition(); |
| 811 testPositionSync(); | 810 testPositionSync(); |
| 812 testMixedSyncAndAsync(); | 811 testMixedSyncAndAsync(); |
| 813 asyncTestStarted(); | 812 |
| 814 createTempDirectory(() { | 813 createTempDirectory(() { |
| 815 testReadWrite(); | 814 testReadWrite(); |
| 816 testReadWriteSync(); | 815 testReadWriteSync(); |
| 817 testReadWriteStream(); | 816 testReadWriteStream(); |
| 818 testReadEmptyFileSync(); | 817 testReadEmptyFileSync(); |
| 819 testReadEmptyFile(); | 818 testReadEmptyFile(); |
| 820 testTruncate(); | 819 testTruncate(); |
| 821 testTruncateSync(); | 820 testTruncateSync(); |
| 822 testCloseException(); | 821 testCloseException(); |
| 823 testCloseExceptionStream(); | 822 testCloseExceptionStream(); |
| 824 testBufferOutOfBoundsException(); | 823 testBufferOutOfBoundsException(); |
| 825 testAppend(); | 824 testAppend(); |
| 826 testAppendSync(); | 825 testAppendSync(); |
| 827 testWriteAppend(); | 826 testWriteAppend(); |
| 828 testOutputStreamWriteAppend(); | 827 testOutputStreamWriteAppend(); |
| 829 asyncTestDone(); | |
| 830 }); | 828 }); |
| 831 } | 829 } |
| 832 } | 830 } |
| 833 | 831 |
| 834 main() { | 832 main() { |
| 835 FileTest.testMain(); | 833 FileTest.testMain(); |
| 836 } | 834 } |
| OLD | NEW |