OLD | NEW |
(Empty) | |
| 1 (function() |
| 2 { |
| 3 module("I/O"); |
| 4 |
| 5 let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null); |
| 6 let file = FileUtils.getFile("TmpD", ["adblockplustests-test1.txt"]); |
| 7 let fileRenamed = FileUtils.getFile("TmpD", ["adblockplustests-test2.txt"]) |
| 8 let currentTest = -1; |
| 9 |
| 10 let tests = [ |
| 11 write.bind(null, file), |
| 12 checkExists.bind(null, file), |
| 13 read.bind(null, file), |
| 14 rename.bind(null, file, fileRenamed), |
| 15 checkExists.bind(null, fileRenamed), |
| 16 checkMissing.bind(null, file), |
| 17 read.bind(null, fileRenamed), |
| 18 copy.bind(null, fileRenamed, file), |
| 19 checkExists.bind(null, fileRenamed), |
| 20 checkExists.bind(null, file), |
| 21 read.bind(null, file), |
| 22 remove.bind(null, fileRenamed), |
| 23 checkMissing.bind(null, fileRenamed), |
| 24 checkExists.bind(null, file), |
| 25 remove.bind(null, file), |
| 26 checkMissing.bind(null, fileRenamed), |
| 27 checkMissing.bind(null, file), |
| 28 failedRead.bind(null, file) |
| 29 ]; |
| 30 |
| 31 function runNextTest() |
| 32 { |
| 33 currentTest++; |
| 34 if (currentTest < tests.length) |
| 35 tests[currentTest](); |
| 36 else |
| 37 start(); |
| 38 } |
| 39 |
| 40 function write(file) |
| 41 { |
| 42 IO.writeToFile(file, true, function dataGenerator() |
| 43 { |
| 44 for (let i = 0; i < 10000; i++) |
| 45 yield "\u1234" + i + "\uffff\x00"; |
| 46 }(), function writeCallback(e) |
| 47 { |
| 48 equal(e && e.toString(), null, "Write succeeded"); |
| 49 runNextTest(); |
| 50 }, null); |
| 51 } |
| 52 |
| 53 function read(file) |
| 54 { |
| 55 let eofReceived = false; |
| 56 let i = 0; |
| 57 IO.readFromFile(file, true, { |
| 58 process: function(line) |
| 59 { |
| 60 if (eofReceived) |
| 61 ok(false, "No lines received after EOF"); |
| 62 |
| 63 if (line === null) |
| 64 { |
| 65 eofReceived = true; |
| 66 equal(i, 10000, "10000 lines received before EOF"); |
| 67 } |
| 68 else |
| 69 { |
| 70 let expected = "\u1234" + i + "\uffff\x00"; |
| 71 if (line != expected) |
| 72 equal(line, expected, "Line " + i + " contents"); |
| 73 i++; |
| 74 } |
| 75 } |
| 76 }, function readCallback(e) |
| 77 { |
| 78 equal(e && e.toString(), null, "Read succeeded"); |
| 79 ok(eofReceived, "File processor received EOF indicator before callback was
called"); |
| 80 runNextTest(); |
| 81 }, null); |
| 82 } |
| 83 |
| 84 function failedRead(file) |
| 85 { |
| 86 IO.readFromFile(file, true, { |
| 87 process: function(line) |
| 88 { |
| 89 ok(false, "Line received for non-existing file") |
| 90 } |
| 91 }, function readCallback(e) |
| 92 { |
| 93 ok(e, "Error received reading non-existing file"); |
| 94 runNextTest(); |
| 95 }); |
| 96 } |
| 97 |
| 98 function copy(from, to) |
| 99 { |
| 100 IO.copyFile(from, to, function copyCallback(e) |
| 101 { |
| 102 equal(e && e.toString(), null, "Copy succeeded"); |
| 103 runNextTest(); |
| 104 }); |
| 105 } |
| 106 |
| 107 function rename(from, to) |
| 108 { |
| 109 IO.renameFile(from, to.leafName, function renameCallback(e) |
| 110 { |
| 111 equal(e && e.toString(), null, "Rename succeeded"); |
| 112 runNextTest(); |
| 113 }); |
| 114 } |
| 115 |
| 116 function remove(file) |
| 117 { |
| 118 IO.removeFile(file, function removeCallback(e) |
| 119 { |
| 120 equal(e && e.toString(), null, "Remove succeeded"); |
| 121 runNextTest(); |
| 122 }); |
| 123 } |
| 124 |
| 125 function checkExists(file) |
| 126 { |
| 127 IO.statFile(file, function statCallback(e, info) |
| 128 { |
| 129 equal(e && e.toString(), null, "Stat succeeded"); |
| 130 if (!e) |
| 131 { |
| 132 ok(info.exists, "File exists"); |
| 133 ok(!info.isDirectory, "File is not a directory"); |
| 134 ok(info.isFile, "File is a regular file"); |
| 135 ok(Date.now() - info.lastModified < 5000, "File modification time is rec
ent"); |
| 136 } |
| 137 runNextTest(); |
| 138 }); |
| 139 } |
| 140 |
| 141 function checkMissing(file) |
| 142 { |
| 143 IO.statFile(file, function statCallback(e, info) |
| 144 { |
| 145 equal(e && e.toString(), null, "Stat succeeded"); |
| 146 if (!e) |
| 147 { |
| 148 ok(!info.exists, "File does not exist"); |
| 149 } |
| 150 runNextTest(); |
| 151 }); |
| 152 } |
| 153 |
| 154 asyncTest("File operations", function() |
| 155 { |
| 156 runNextTest(); |
| 157 }); |
| 158 })(); |
OLD | NEW |