Index: chrome/content/tests/filterStorage_readwrite.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/chrome/content/tests/filterStorage_readwrite.js |
@@ -0,0 +1,130 @@ |
+(function() |
+{ |
+ module("Filter storage read/write", { |
+ setup: function() |
+ { |
+ prepareFilterComponents.call(this); |
+ preparePrefs.call(this); |
+ }, |
+ teardown: function() |
+ { |
+ restoreFilterComponents.call(this); |
+ restorePrefs.call(this); |
+ } |
+ }); |
+ |
+ let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null); |
+ let {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", null); |
+ |
+ let filtersData = (function() |
+ { |
+ let lines = ["# Adblock Plus preferences", "version=4"]; |
+ for (let i = 0; i <40000; i++) |
kzar
2016/10/28 10:23:56
Nit: Missing space before the 40000.
Wladimir Palant
2016/10/28 11:09:38
Done.
|
+ lines.push("[Filter]", `text=foobar${i}`, `hitCount=${i+10}`, `lastHit=${i+1400000000000}`); |
kzar
2016/10/28 10:23:56
Nit: Mind wrapping some of these long lines?
Wladimir Palant
2016/10/28 11:09:38
Done.
|
+ |
+ lines.push("[Subscription]", "url=http://foo.example.com/", "title=Test subscription"); |
+ lines.push("[Subscription filters]"); |
+ for (let i = 0; i < 40000; i++) |
+ lines.push(`foobar${i}`); |
+ return lines.join("\n") + "\n"; |
+ })(); |
+ |
+ function writeToFile(file, data) |
+ { |
+ let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter); |
+ converter.charset = "utf-8"; |
+ data = converter.ConvertFromUnicode(data); |
+ |
+ let stream = FileUtils.openFileOutputStream(file); |
+ stream.write(data, data.length); |
+ stream.close(); |
+ } |
+ |
+ function readFromFile(file) |
+ { |
+ return new Promise((resolve, reject) => |
+ { |
+ let stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); |
+ stream.init(file, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, Ci.nsIFileInputStream.DEFER_OPEN); |
+ |
+ NetUtil.asyncFetch(stream, function(inputStream, nsresult) |
+ { |
+ resolve(NetUtil.readInputStreamToString(inputStream, inputStream.available(), {charset: "utf-8"})); |
+ }); |
+ }); |
+ } |
+ |
+ function canonizeFiltersData(data) |
kzar
2016/10/28 10:23:56
Could be wrong here but shouldn't it be canonicali
Wladimir Palant
2016/10/28 11:09:38
Whatever, was the name used by the original test :
|
+ { |
+ let curSection = null; |
+ let sections = []; |
+ for (let line of (data + "\n[end]").split(/[\r\n]+/)) |
+ { |
+ if (/^\[.*\]$/.test(line)) |
+ { |
+ if (curSection) |
+ sections.push(curSection); |
+ |
+ curSection = {header: line, data: []}; |
+ } |
+ else if (curSection && /\S/.test(line)) |
+ curSection.data.push(line); |
+ } |
+ for (let section of sections) |
+ { |
+ section.key = section.header + " " + section.data[0]; |
+ section.data.sort(); |
+ } |
+ sections.sort(function(a, b) |
+ { |
+ if (a.key < b.key) |
+ return -1; |
+ else if (a.key > b.key) |
+ return 1; |
+ else |
+ return 0; |
+ }); |
+ return sections.map(function(section) { |
kzar
2016/10/28 10:23:56
Nit: Arrow function? (Same in the sort above.)
Wladimir Palant
2016/10/28 11:09:38
It's old code that I didn't touch but sure.
|
+ return [section.header].concat(section.data).join("\n"); |
+ }).join("\n"); |
+ } |
+ |
+ function testReadWrite() |
+ { |
+ let tempFile = FileUtils.getFile("TmpD", ["temp_patterns.ini"]); |
+ tempFile.createUnique(tempFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); |
+ writeToFile(tempFile, filtersData); |
+ |
+ Promise.resolve().then(() => |
+ { |
+ FilterStorage.loadFromDisk(tempFile); |
+ return FilterNotifier.once("load"); |
+ }).then(() => |
+ { |
+ tempFile.remove(false); |
+ FilterStorage.saveToDisk(tempFile); |
+ return FilterNotifier.once("save"); |
+ }).then(() => |
+ { |
+ return readFromFile(tempFile); |
+ }).then(fileData => |
+ { |
+ tempFile.remove(false); |
+ |
+ equal(canonizeFiltersData(fileData), canonizeFiltersData(filtersData), "Read/write result"); |
+ start(); |
+ }).catch(error => |
+ { |
+ Cu.reportError(error); |
+ ok(false, "Caught error: " + error); |
+ start(); |
+ }); |
+ } |
+ |
+ asyncTest("Read and save to file", testReadWrite); |
+ asyncTest("Read and save to file, with please_kill_startup_performance set", () => |
+ { |
+ Prefs.please_kill_startup_performance = true; |
+ testReadWrite(); |
+ }); |
+})(); |