Left: | ||
Right: |
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++) | |
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}`); | |
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 | |
25 lines.push("[Subscription]", "url=http://foo.example.com/", "title=Test subs cription"); | |
26 lines.push("[Subscription filters]"); | |
27 for (let i = 0; i < 40000; i++) | |
28 lines.push(`foobar${i}`); | |
29 return lines.join("\n") + "\n"; | |
30 })(); | |
31 | |
32 function writeToFile(file, data) | |
33 { | |
34 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createIns tance(Ci.nsIScriptableUnicodeConverter); | |
35 converter.charset = "utf-8"; | |
36 data = converter.ConvertFromUnicode(data); | |
37 | |
38 let stream = FileUtils.openFileOutputStream(file); | |
39 stream.write(data, data.length); | |
40 stream.close(); | |
41 } | |
42 | |
43 function readFromFile(file) | |
44 { | |
45 return new Promise((resolve, reject) => | |
46 { | |
47 let stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance (Ci.nsIFileInputStream); | |
48 stream.init(file, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, Ci.nsIFileI nputStream.DEFER_OPEN); | |
49 | |
50 NetUtil.asyncFetch(stream, function(inputStream, nsresult) | |
51 { | |
52 resolve(NetUtil.readInputStreamToString(inputStream, inputStream.availab le(), {charset: "utf-8"})); | |
53 }); | |
54 }); | |
55 } | |
56 | |
57 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 :
| |
58 { | |
59 let curSection = null; | |
60 let sections = []; | |
61 for (let line of (data + "\n[end]").split(/[\r\n]+/)) | |
62 { | |
63 if (/^\[.*\]$/.test(line)) | |
64 { | |
65 if (curSection) | |
66 sections.push(curSection); | |
67 | |
68 curSection = {header: line, data: []}; | |
69 } | |
70 else if (curSection && /\S/.test(line)) | |
71 curSection.data.push(line); | |
72 } | |
73 for (let section of sections) | |
74 { | |
75 section.key = section.header + " " + section.data[0]; | |
76 section.data.sort(); | |
77 } | |
78 sections.sort(function(a, b) | |
79 { | |
80 if (a.key < b.key) | |
81 return -1; | |
82 else if (a.key > b.key) | |
83 return 1; | |
84 else | |
85 return 0; | |
86 }); | |
87 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.
| |
88 return [section.header].concat(section.data).join("\n"); | |
89 }).join("\n"); | |
90 } | |
91 | |
92 function testReadWrite() | |
93 { | |
94 let tempFile = FileUtils.getFile("TmpD", ["temp_patterns.ini"]); | |
95 tempFile.createUnique(tempFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); | |
96 writeToFile(tempFile, filtersData); | |
97 | |
98 Promise.resolve().then(() => | |
99 { | |
100 FilterStorage.loadFromDisk(tempFile); | |
101 return FilterNotifier.once("load"); | |
102 }).then(() => | |
103 { | |
104 tempFile.remove(false); | |
105 FilterStorage.saveToDisk(tempFile); | |
106 return FilterNotifier.once("save"); | |
107 }).then(() => | |
108 { | |
109 return readFromFile(tempFile); | |
110 }).then(fileData => | |
111 { | |
112 tempFile.remove(false); | |
113 | |
114 equal(canonizeFiltersData(fileData), canonizeFiltersData(filtersData), "Re ad/write result"); | |
115 start(); | |
116 }).catch(error => | |
117 { | |
118 Cu.reportError(error); | |
119 ok(false, "Caught error: " + error); | |
120 start(); | |
121 }); | |
122 } | |
123 | |
124 asyncTest("Read and save to file", testReadWrite); | |
125 asyncTest("Read and save to file, with please_kill_startup_performance set", ( ) => | |
126 { | |
127 Prefs.please_kill_startup_performance = true; | |
128 testReadWrite(); | |
129 }); | |
130 })(); | |
OLD | NEW |