Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: chrome/content/tests/filterStorage_readwrite.js

Issue 6250418359238656: Issue 153 - Added tests for io.js and updated filter storage tests (Closed)
Patch Set: Created March 21, 2014, 1:15 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/content/common.js ('k') | chrome/content/tests/io.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/content/tests/filterStorage_readwrite.js
===================================================================
--- a/chrome/content/tests/filterStorage_readwrite.js
+++ b/chrome/content/tests/filterStorage_readwrite.js
@@ -28,16 +28,27 @@
callback();
}
};
FilterNotifier.addListener(listener);
FilterStorage.loadFromDisk(file);
}
+ 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 saveFilters(file, callback)
{
let listener = function(action)
{
if (action == "save")
{
FilterNotifier.removeListener(listener);
callback();
@@ -45,19 +56,20 @@
};
FilterNotifier.addListener(listener);
FilterStorage.saveToDisk(file);
}
function testReadWrite(withExternal)
{
- let tempFile = FileUtils.getFile("TmpD", ["temp_patterns.ini"]);
+ let tempFile = FileUtils.getFile("TmpD", ["temp_patterns1.ini"]);
+ let tempFile2 = FileUtils.getFile("TmpD", ["temp_patterns2.ini"]);
tempFile.createUnique(tempFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
- readFile();
+ createTempFile();
function canonize(data)
{
let curSection = null;
let sections = [];
for each (let line in (data + "\n[end]").split(/[\r\n]+/))
{
if (/^\[.*\]$/.test(line))
@@ -84,20 +96,27 @@
else
return 0;
});
return sections.map(function(section) {
return [section.header].concat(section.data).join("\n");
}).join("\n");
}
- function readFile()
+ function createTempFile()
{
- let source = Services.io.newURI("data/patterns.ini", null, Services.io.newURI(window.location.href, null, null));
- loadFilters(source, saveFile);
+ let request = new XMLHttpRequest();
+ request.open("GET", "data/patterns.ini");
+ request.overrideMimeType("text/plain; charset=utf-8");
+ request.addEventListener("load", function()
+ {
+ writeToFile(tempFile, request.responseText);
+ loadFilters(tempFile, saveFile);
+ }, false);
+ request.send(null);
}
function saveFile()
{
equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion, "File format version");
if (withExternal)
{
@@ -109,71 +128,78 @@
if (externalSubscriptions.length == 1)
{
equal(externalSubscriptions[0].url, "~external~external subscription ID", "ID of external subscription");
equal(externalSubscriptions[0].filters.length, 2, "Number of filters in external subscription");
}
}
- saveFilters(tempFile, compareFile);
+ saveFilters(tempFile2, compareFile);
}
function compareFile()
{
let stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
- stream.init(tempFile, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, Ci.nsIFileInputStream.DEFER_OPEN);
+ stream.init(tempFile2, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, Ci.nsIFileInputStream.DEFER_OPEN);
NetUtil.asyncFetch(stream, function(inputStream, nsresult)
{
let result = NetUtil.readInputStreamToString(inputStream, inputStream.available(), {charset: "utf-8"});
let request = new XMLHttpRequest();
request.open("GET", "data/patterns.ini");
request.overrideMimeType("text/plain");
request.addEventListener("load", function()
{
let expected = request.responseText;
equal(canonize(result), canonize(expected), "Read/write result");
tempFile.remove(false);
+ tempFile2.remove(false);
start();
}, false);
request.send(null);
});
}
}
- asyncTest("Read from URL, write to file", testReadWrite.bind(false));
- asyncTest("Read from URL, add external subscription, write to file", testReadWrite.bind(true));
+ asyncTest("Read and save to file", testReadWrite.bind(false));
+ asyncTest("Read, add external subscription and save to file", testReadWrite.bind(true));
let groupTests = [
["~wl~", "whitelist"],
["~fl~", "blocking"],
["~eh~", "elemhide"]
];
for (let i = 0; i < groupTests.length; i++)
{
let [url, defaults] = groupTests[i];
asyncTest("Read empty legacy user-defined group (" + url + ")", function()
{
let data = "[Subscription]\nurl=" + url;
- let source = "data:text/plain;charset=utf-8," + encodeURIComponent(data);
- loadFilters(Services.io.newURI(source, null, null), function()
+ let tempFile = FileUtils.getFile("TmpD", ["temp_patterns1.ini"]);
+ writeToFile(tempFile, data);
+
+ loadFilters(tempFile, function()
{
+ tempFile.remove(false);
equal(FilterStorage.subscriptions.length, 0, "Number of filter subscriptions");
start();
});
});
asyncTest("Read non-empty legacy user-defined group (" + url + ")", function()
{
let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo";
- let source = "data:text/plain;charset=utf-8," + encodeURIComponent(data);
- loadFilters(Services.io.newURI(source, null, null), function()
+ let tempFile = FileUtils.getFile("TmpD", ["temp_patterns1.ini"]);
+ writeToFile(tempFile, data);
+
+ loadFilters(tempFile, function()
{
+ tempFile.remove(false);
equal(FilterStorage.subscriptions.length, 1, "Number of filter subscriptions");
if (FilterStorage.subscriptions.length == 1)
{
let subscription = FilterStorage.subscriptions[0];
equal(subscription.url, url, "Subscription ID");
equal(subscription.title, Utils.getString(defaults + "Group_title"), "Subscription title");
deepEqual(subscription.defaults, [defaults], "Default types");
equal(subscription.filters.length, 1, "Number of subscription filters");
@@ -183,19 +209,22 @@
start();
});
});
}
asyncTest("Read legacy user-defined filters", function()
{
let data = "[Subscription]\nurl=~user~1234\ntitle=Foo\n[Subscription filters]\n[User patterns]\nfoo\n\\[bar]\nfoo#bar";
- let source = "data:text/plain;charset=utf-8," + encodeURIComponent(data);
- loadFilters(Services.io.newURI(source, null, null), function()
+ let tempFile = FileUtils.getFile("TmpD", ["temp_patterns1.ini"]);
+ writeToFile(tempFile, data);
+
+ loadFilters(tempFile, function()
{
+ tempFile.remove(false);
equal(FilterStorage.subscriptions.length, 1, "Number of filter subscriptions");
if (FilterStorage.subscriptions.length == 1)
{
let subscription = FilterStorage.subscriptions[0];
equal(subscription.filters.length, 3, "Number of subscription filters");
if (subscription.filters.length == 3)
{
equal(subscription.filters[0].text, "foo", "First filter");
« no previous file with comments | « chrome/content/common.js ('k') | chrome/content/tests/io.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld