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