tests/simple/modules.js: clean up load_url modification
[conkeror.git] / tests / simple / modules.js
blob26cf4a1673e2262b68c99b424f903ff4c9e249e8
2 require('walnut.js');
4 walnut_run({
5     test_featurep_1: function () {
6         assert(! featurep("non-existent module"));
7     },
8     test_featurep_2: function () {
9         assert(featurep("conkeror"));
10     },
11     test_call_after_load_1: function () {
12         let a = 0;
13         call_after_load("conkeror", function () { a = 1; });
14         assert_equals(a, 1);
15     }
16 });
19 walnut_run({
20     suite_setup: function () {
21         this._load_paths = load_paths;
22         this._loading_paths = loading_paths;
23         this._loading_urls = loading_urls;
24         this._loading_modules = loading_modules;
25         this._loading_features = loading_features;
26         this._pending_loads = pending_loads;
27         this._features = features;
28         this._after_load_functions = after_load_functions;
29         this._load_url = load_url;
30         var suite = this;
31         load_url = function (url) {
32             suite.ob.push(url); //initialized for each test in suite.setup
33             throw "Error opening input stream (invalid filename?)";
34         };
35     },
36     suite_teardown: function () {
37         load_paths = this._load_paths;
38         load_url = this._load_url;
39         loading_paths = this._loading_paths;
40         loading_urls = this._loading_urls;
41         loading_modules = this._loading_modules;
42         loading_features = this._loading_features;
43         pending_loads = this._pending_loads;
44         features = this._features;
45         after_load_functions = this._after_load_functions;
46     },
47     setup: function () {
48         loading_paths = [];
49         this.ob = [];
50     },
51     test_load_search_1__sans_extension: function () {
52         load_paths = ["chrome://conkeror/content/",
53                       "chrome://conkeror/content/extensions",
54                       "chrome://conkeror/content/page-modes"];
55         try {
56             load("foo");
57         } catch (e) {
58         }
59         assert_objects_equal(
60             ["chrome://conkeror/content/foo",
61              "chrome://conkeror/content/foo.js",
62              "chrome://conkeror/content/extensions/foo",
63              "chrome://conkeror/content/extensions/foo.js",
64              "chrome://conkeror/content/page-modes/foo",
65              "chrome://conkeror/content/page-modes/foo.js"],
66             this.ob);
67     },
68     test_load_search_2__with_extension: function () {
69         load_paths = ["chrome://conkeror/content/",
70                       "chrome://conkeror/content/extensions",
71                       "chrome://conkeror/content/page-modes"];
72         try {
73             load("foo.js");
74         } catch (e) {
75         }
76         assert_objects_equal(
77             ["chrome://conkeror/content/foo.js",
78              "chrome://conkeror/content/extensions/foo.js",
79              "chrome://conkeror/content/page-modes/foo.js"],
80             this.ob);
81     },
82     test_load_search_3__load_path_dups: function () {
83         load_paths = ["chrome://conkeror/content/",
84                       "chrome://conkeror/content/extensions",
85                       "chrome://conkeror/content/extensions/",
86                       "chrome://conkeror/content/page-modes"];
87         try {
88             load("foo.js");
89         } catch (e) {
90         }
91         assert_objects_equal(
92             ["chrome://conkeror/content/foo.js",
93              "chrome://conkeror/content/extensions/foo.js",
94              "chrome://conkeror/content/page-modes/foo.js"],
95             this.ob);
96     },
97     test_load_search_4__require_skips_cur_dir: function () {
98         loading_paths = ["file:///foo/bar/baz/"];
99         load_paths = ["chrome://conkeror/content/"];
100         try {
101             require("foo.js");
102         } catch (e) {
103         }
104         assert_objects_equal(
105             ["chrome://conkeror/content/foo.js"],
106             this.ob);
107     },
108     test_load_search_5__relative_path: function () {
109         load_paths = ["chrome://conkeror/content/",
110                       "chrome://conkeror/content/extensions",
111                       "chrome://conkeror/content/page-modes"];
112         try {
113             load("page-modes/foo");
114         } catch (e) {
115         }
116         assert_objects_equal(
117             this.ob,
118             ["chrome://conkeror/content/page-modes/foo",
119              "chrome://conkeror/content/page-modes/foo.js",
120              "chrome://conkeror/content/extensions/page-modes/foo",
121              "chrome://conkeror/content/extensions/page-modes/foo.js",
122              "chrome://conkeror/content/page-modes/page-modes/foo",
123              "chrome://conkeror/content/page-modes/page-modes/foo.js"]);
124     }
128 walnut_run({
129     suite_setup: function () {
130         this._load_paths = load_paths;
131         this._loading_paths = loading_paths;
132         this._loading_urls = loading_urls;
133         this._loading_modules = loading_modules;
134         this._loading_features = loading_features;
135         this._pending_loads = pending_loads;
136         this._features = features;
137         this._after_load_functions = after_load_functions;
138         this._load_url = load_url;
139     },
140     teardown: function () {
141         load_paths = this._load_paths;
142         load_url = this._load_url;
143         loading_paths = this._loading_paths;
144         loading_urls = this._loading_urls;
145         loading_modules = this._loading_modules;
146         loading_features = this._loading_features;
147         pending_loads = this._pending_loads;
148         features = this._features;
149         after_load_functions = this._after_load_functions;
150     },
151     test_load__in_module_conflict_1: function () {
152         load_url = function () {
153             in_module(null);
154             in_module("bar");
155         };
156         var err;
157         try {
158             load("foo");
159         } catch (e) {
160             err = e;
161         }
162         assert(err instanceof module_assert_conflict_error);
163     },
164     test_load__in_module_conflict_2: function () {
165         load_url = function () {
166             in_module(null);
167             in_module("bar");
168         };
169         var err;
170         try {
171             load(make_uri("chrome://conkeror/content/foo.js"));
172         } catch (e) {
173             err = e;
174         }
175         assert(err instanceof module_assert_conflict_error);
176     },
177     test_load__circular_load_is_error: function () {
178         load_url = function () {
179             load(make_uri("chrome://conkeror/content/foo.js"));
180         };
181         assert_error(function () {
182             load(make_uri("chrome://conkeror/content/foo.js"));
183         });
184     },
185     test_load__not_found_is_error: function () {
186         load_paths = [];
187         loading_paths = [];
188         loading_urls = [];
189         loading_modules = [];
190         loading_features = [];
191         pending_loads = [];
192         assert_error(function () load("foo"));
193     },
194     test_require_later_1: function () {
195         loading_urls = [];
196         pending_loads = [];
197         var ob = "";
198         function mock_foo () {
199             in_module(null);
200             ob += "a";
201             require_later("bar");
202             ob += "b";
203             load_url = mock_bar;
204         }
205         function mock_bar () {
206             in_module(null);
207             ob += "c";
208         }
209         load_url = mock_foo;
210         load("foo");
211         assert_equals(ob, "abc");
212     },
213     test_require_later_2: function () {
214         loading_paths = [];
215         loading_urls = [];
216         loading_modules = [];
217         loading_features = [];
218         pending_loads = [];
219         after_load_functions = [];
220         features = {};
221         var ob = [];
222         var mock_modules = {
223             foo: function () {
224                 in_module(null);
225                 ob.push("foo");
226                 require_later("baz");
227                 require("bar");
228                 provide("foo");
229             },
230             bar: function () {
231                 in_module(null);
232                 ob.push("bar");
233                 provide("bar");
234             },
235             baz: function () {
236                 in_module(null);
237                 ob.push("baz");
238                 require_later("quux");
239                 provide("baz");
240             },
241             quux: function () {
242                 in_module(null);
243                 ob.push("quux");
244                 provide("quux");
245             }
246         };
247         load_url = function (url) {
248             var module = url.substr(url.lastIndexOf('/')+1);
249             mock_modules[module]();
250         };
251         load("foo");
252         assert_objects_equal(ob, ["foo", "bar", "baz", "quux"]);
253     },
254     test_provide__load_order: function () {
255         // want to make sure that after_load_functions only get called
256         // after the completion of the load which provided the feature.
257         loading_paths = [];
258         loading_urls = [];
259         loading_modules = [];
260         loading_features = [];
261         pending_loads = [];
262         after_load_functions = [];
263         features = {};
265         var oldfoo = conkeror.foo;
267         var called = false;
268         var mock_modules = {
269             foo: function () {
270                 in_module("foo");
271                 called = true;
272                 provide("foo");
273                 assert_not(featurep("foo"));
274             }
275         };
276         load_url = function (url) {
277             var module = url.substr(url.lastIndexOf('/')+1);
278             mock_modules[module]();
279         };
280         load("foo");
281         assert(called);
282         assert(featurep("foo"));
284         conkeror.foo = oldfoo;
285     },
286     test_call_after_load: function () {
287         loading_paths = [];
288         loading_urls = [];
289         loading_modules = [];
290         loading_features = [];
291         pending_loads = [];
292         after_load_functions = [];
293         features = {};
295         var oldfoo = conkeror.foo;
297         var mock_modules = {
298             foo: function () {
299                 in_module("foo");
300                 provide("foo");
301             }
302         };
303         load_url = function (url) {
304             var module = url.substr(url.lastIndexOf('/')+1);
305             mock_modules[module]();
306         };
307         call_after_load("foo", function () {
308             foo.called = true;
309         });
310         load("foo");
311         assert(foo.called);
313         conkeror.foo = oldfoo;
314     },
315     test_provide: function () {
316         loading_paths = [];
317         loading_urls = [];
318         loading_modules = [];
319         loading_features = [];
320         pending_loads = [];
321         after_load_functions = [];
322         features = {};
323         provide("foo");
324         assert(featurep("foo"));
325     },
326     test_load_relative_of_nsIURI: function () {
327         var tried = null;
328         load_url = function (url) {
329             if (url == "chrome://conkeror/content/foo.js") {
330                 load("bar.js");
331             } else {
332                 tried = url;
333             }
334         };
335         load(make_uri("chrome://conkeror/content/foo.js"));
336         assert_equals(tried, "chrome://conkeror/content/bar.js");
337     }