OLD | NEW |
(Empty) | |
| 1 (function() |
| 2 { |
| 3 module("Filter storage read/write", { |
| 4 setup: function() |
| 5 { |
| 6 prepareFilterComponents.call(this); |
| 7 preparePrefs.call(this); |
| 8 }, |
| 9 teardown: function() |
| 10 { |
| 11 restoreFilterComponents.call(this); |
| 12 restorePrefs.call(this); |
| 13 } |
| 14 }); |
| 15 |
| 16 let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null); |
| 17 let {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", null); |
| 18 |
| 19 let filtersData = (function() |
| 20 { |
| 21 let lines = ["# Adblock Plus preferences", "version=4"]; |
| 22 for (let i = 0; i < 40000; i++) |
| 23 { |
| 24 lines.push("[Filter]", `text=foobar${i}`, `hitCount=${i+10}`, |
| 25 `lastHit=${i+1400000000000}`); |
| 26 } |
| 27 |
| 28 lines.push("[Subscription]", "url=http://foo.example.com/", |
| 29 "title=Test subscription"); |
| 30 lines.push("[Subscription filters]"); |
| 31 for (let i = 0; i < 40000; i++) |
| 32 lines.push(`foobar${i}`); |
| 33 return lines.join("\n") + "\n"; |
| 34 })(); |
| 35 |
| 36 function writeToFile(file, data) |
| 37 { |
| 38 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] |
| 39 .createInstance(Ci.nsIScriptableUnicodeConverter); |
| 40 converter.charset = "utf-8"; |
| 41 data = converter.ConvertFromUnicode(data); |
| 42 |
| 43 let stream = FileUtils.openFileOutputStream(file); |
| 44 stream.write(data, data.length); |
| 45 stream.close(); |
| 46 } |
| 47 |
| 48 function readFromFile(file) |
| 49 { |
| 50 return new Promise((resolve, reject) => |
| 51 { |
| 52 let stream = Cc["@mozilla.org/network/file-input-stream;1"] |
| 53 .createInstance(Ci.nsIFileInputStream); |
| 54 stream.init(file, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, |
| 55 Ci.nsIFileInputStream.DEFER_OPEN); |
| 56 |
| 57 NetUtil.asyncFetch(stream, (inputStream, nsresult) => |
| 58 { |
| 59 resolve(NetUtil.readInputStreamToString(inputStream, |
| 60 inputStream.available(), {charset: "utf-8"})); |
| 61 }); |
| 62 }); |
| 63 } |
| 64 |
| 65 function canonicalizeFiltersData(data) |
| 66 { |
| 67 let curSection = null; |
| 68 let sections = []; |
| 69 for (let line of (data + "\n[end]").split(/[\r\n]+/)) |
| 70 { |
| 71 if (/^\[.*\]$/.test(line)) |
| 72 { |
| 73 if (curSection) |
| 74 sections.push(curSection); |
| 75 |
| 76 curSection = {header: line, data: []}; |
| 77 } |
| 78 else if (curSection && /\S/.test(line)) |
| 79 curSection.data.push(line); |
| 80 } |
| 81 for (let section of sections) |
| 82 { |
| 83 section.key = section.header + " " + section.data[0]; |
| 84 section.data.sort(); |
| 85 } |
| 86 sections.sort((a, b) => |
| 87 { |
| 88 if (a.key < b.key) |
| 89 return -1; |
| 90 else if (a.key > b.key) |
| 91 return 1; |
| 92 else |
| 93 return 0; |
| 94 }); |
| 95 return sections.map(section => |
| 96 { |
| 97 return [section.header].concat(section.data).join("\n"); |
| 98 }).join("\n"); |
| 99 } |
| 100 |
| 101 function testReadWrite() |
| 102 { |
| 103 let tempFile = FileUtils.getFile("TmpD", ["temp_patterns.ini"]); |
| 104 tempFile.createUnique(tempFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); |
| 105 writeToFile(tempFile, filtersData); |
| 106 |
| 107 Promise.resolve().then(() => |
| 108 { |
| 109 FilterStorage.loadFromDisk(tempFile); |
| 110 return FilterNotifier.once("load"); |
| 111 }).then(() => |
| 112 { |
| 113 tempFile.remove(false); |
| 114 FilterStorage.saveToDisk(tempFile); |
| 115 return FilterNotifier.once("save"); |
| 116 }).then(() => |
| 117 { |
| 118 return readFromFile(tempFile); |
| 119 }).then(fileData => |
| 120 { |
| 121 tempFile.remove(false); |
| 122 |
| 123 equal(canonicalizeFiltersData(fileData), |
| 124 canonicalizeFiltersData(filtersData), |
| 125 "Read/write result"); |
| 126 start(); |
| 127 }).catch(error => |
| 128 { |
| 129 Cu.reportError(error); |
| 130 ok(false, "Caught error: " + error); |
| 131 start(); |
| 132 }); |
| 133 } |
| 134 |
| 135 asyncTest("Read and save to file", testReadWrite); |
| 136 asyncTest("Read and save with please_kill_startup_performance set", () => |
| 137 { |
| 138 Prefs.please_kill_startup_performance = true; |
| 139 testReadWrite(); |
| 140 }); |
| 141 })(); |
OLD | NEW |