Update coroutines to support Promises, cancelation
[conkeror.git] / tests / simple / modules.js
blob9e39040e062342f1fb8e94846691de58ff12c719
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_features = loading_features;
25         this._pending_loads = pending_loads;
26         this._features = features;
27         this._after_load_functions = after_load_functions;
28         this._load_url = load_url;
29         var suite = this;
30         load_url = function (url) {
31             suite.ob.push(url); //initialized for each test in suite.setup
32             throw "Error opening input stream (invalid filename?)";
33         };
34     },
35     suite_teardown: function () {
36         load_paths = this._load_paths;
37         load_url = this._load_url;
38         loading_paths = this._loading_paths;
39         loading_urls = this._loading_urls;
40         loading_features = this._loading_features;
41         pending_loads = this._pending_loads;
42         features = this._features;
43         after_load_functions = this._after_load_functions;
44     },
45     setup: function () {
46         loading_paths = [];
47         this.ob = [];
48     },
49     test_load_search_1__sans_extension: function () {
50         load_paths = ["chrome://conkeror/content/",
51                       "chrome://conkeror/content/extensions",
52                       "chrome://conkeror/content/page-modes"];
53         try {
54             load("foo");
55         } catch (e) {
56         }
57         assert_objects_equal(
58             ["chrome://conkeror/content/foo",
59              "chrome://conkeror/content/foo.js",
60              "chrome://conkeror/content/foo.jsx",
61              "chrome://conkeror/content/extensions/foo",
62              "chrome://conkeror/content/extensions/foo.js",
63              "chrome://conkeror/content/extensions/foo.jsx",
64              "chrome://conkeror/content/page-modes/foo",
65              "chrome://conkeror/content/page-modes/foo.js",
66              "chrome://conkeror/content/page-modes/foo.jsx"],
67             this.ob);
68     },
69     test_load_search_2__with_extension: function () {
70         load_paths = ["chrome://conkeror/content/",
71                       "chrome://conkeror/content/extensions",
72                       "chrome://conkeror/content/page-modes"];
73         try {
74             load("foo.js");
75         } catch (e) {
76         }
77         assert_objects_equal(
78             ["chrome://conkeror/content/foo.js",
79              "chrome://conkeror/content/extensions/foo.js",
80              "chrome://conkeror/content/page-modes/foo.js"],
81             this.ob);
82     },
83     test_load_search_3__load_path_dups: function () {
84         load_paths = ["chrome://conkeror/content/",
85                       "chrome://conkeror/content/extensions",
86                       "chrome://conkeror/content/extensions/",
87                       "chrome://conkeror/content/page-modes"];
88         try {
89             load("foo.js");
90         } catch (e) {
91         }
92         assert_objects_equal(
93             ["chrome://conkeror/content/foo.js",
94              "chrome://conkeror/content/extensions/foo.js",
95              "chrome://conkeror/content/page-modes/foo.js"],
96             this.ob);
97     },
98     test_load_search_4__require_skips_cur_dir: function () {
99         loading_paths = ["file:///foo/bar/baz/"];
100         load_paths = ["chrome://conkeror/content/"];
101         try {
102             require("foo.js");
103         } catch (e) {
104         }
105         assert_objects_equal(
106             ["chrome://conkeror/content/foo.js"],
107             this.ob);
108     },
109     test_load_search_5__relative_path: function () {
110         load_paths = ["chrome://conkeror/content/",
111                       "chrome://conkeror/content/extensions",
112                       "chrome://conkeror/content/page-modes"];
113         try {
114             load("page-modes/foo");
115         } catch (e) {
116         }
117         assert_objects_equal(
118             this.ob,
119             ["chrome://conkeror/content/page-modes/foo",
120              "chrome://conkeror/content/page-modes/foo.js",
121              "chrome://conkeror/content/page-modes/foo.jsx",
122              "chrome://conkeror/content/extensions/page-modes/foo",
123              "chrome://conkeror/content/extensions/page-modes/foo.js",
124              "chrome://conkeror/content/extensions/page-modes/foo.jsx",
125              "chrome://conkeror/content/page-modes/page-modes/foo",
126              "chrome://conkeror/content/page-modes/page-modes/foo.js",
127              "chrome://conkeror/content/page-modes/page-modes/foo.jsx"]);
128     }
132 walnut_run({
133     suite_setup: function () {
134         this._load_paths = load_paths;
135         this._loading_paths = loading_paths;
136         this._loading_urls = loading_urls;
137         this._loading_features = loading_features;
138         this._pending_loads = pending_loads;
139         this._features = features;
140         this._after_load_functions = after_load_functions;
141         this._load_url = load_url;
142     },
143     teardown: function () {
144         load_paths = this._load_paths;
145         load_url = this._load_url;
146         loading_paths = this._loading_paths;
147         loading_urls = this._loading_urls;
148         loading_features = this._loading_features;
149         pending_loads = this._pending_loads;
150         features = this._features;
151         after_load_functions = this._after_load_functions;
152     },
153     test_load__circular_load_is_error: function () {
154         load_url = function () {
155             load(make_uri("chrome://conkeror/content/foo.js"));
156         };
157         assert_error(function () {
158             load(make_uri("chrome://conkeror/content/foo.js"));
159         });
160     },
161     test_load__not_found_is_error: function () {
162         load_paths = [];
163         loading_paths = [];
164         loading_urls = [];
165         loading_features = [];
166         pending_loads = [];
167         assert_error(function () load("foo"));
168     },
169     test_require_later_1: function () {
170         loading_urls = [];
171         pending_loads = [];
172         var ob = "";
173         function mock_foo () {
174             ob += "a";
175             require_later("bar");
176             ob += "b";
177             load_url = mock_bar;
178         }
179         function mock_bar () {
180             ob += "c";
181         }
182         load_url = mock_foo;
183         load("foo");
184         assert_equals(ob, "abc");
185     },
186     test_require_later_2: function () {
187         loading_paths = [];
188         loading_urls = [];
189         loading_features = [];
190         pending_loads = [];
191         after_load_functions = [];
192         features = {};
193         var ob = [];
194         var mock_modules = {
195             foo: function () {
196                 ob.push("foo");
197                 require_later("baz");
198                 require("bar");
199                 provide("foo");
200             },
201             bar: function () {
202                 ob.push("bar");
203                 provide("bar");
204             },
205             baz: function () {
206                 ob.push("baz");
207                 require_later("quux");
208                 provide("baz");
209             },
210             quux: function () {
211                 ob.push("quux");
212                 provide("quux");
213             }
214         };
215         load_url = function (url) {
216             var module = url.substr(url.lastIndexOf('/')+1);
217             mock_modules[module]();
218         };
219         load("foo");
220         assert_objects_equal(ob, ["foo", "bar", "baz", "quux"]);
221     },
222     test_provide__load_order: function () {
223         // want to make sure that after_load_functions only get called
224         // after the completion of the load which provided the feature.
225         loading_paths = [];
226         loading_urls = [];
227         loading_features = [];
228         pending_loads = [];
229         after_load_functions = [];
230         features = {};
232         var called = false;
233         var mock_modules = {
234             foo: function () {
235                 called = true;
236                 provide("foo");
237                 assert_not(featurep("foo"));
238             }
239         };
240         load_url = function (url) {
241             var module = url.substr(url.lastIndexOf('/')+1);
242             mock_modules[module]();
243         };
244         load("foo");
245         assert(called);
246         assert(featurep("foo"));
247     },
248     test_provide: function () {
249         loading_paths = [];
250         loading_urls = [];
251         loading_features = [];
252         pending_loads = [];
253         after_load_functions = [];
254         features = {};
255         provide("foo");
256         assert(featurep("foo"));
257     },
258     test_load_relative_of_nsIURI: function () {
259         var tried = null;
260         load_url = function (url) {
261             if (url == "chrome://conkeror/content/foo.js") {
262                 load("bar.js");
263             } else {
264                 tried = url;
265             }
266         };
267         load(make_uri("chrome://conkeror/content/foo.js"));
268         assert_equals(tried, "chrome://conkeror/content/bar.js");
269     },
270     test_load_relative_path_nested: function () {
271         loading_paths = [];
272         loading_urls = [];
273         loading_features = [];
274         pending_loads = [];
275         after_load_functions = [];
276         features = {};
277         var ob = [];
278         var mock_modules = {
279             foo: function () {
280                 load("deeper/bar");
281                 provide("foo");
282             },
283             bar: function () {
284                 assert(loading_paths[0].indexOf("some-path/deeper") > 0);
285                 provide("bar");
286             }
287         };
288         load_url = function (url) {
289             var module = url.substr(url.lastIndexOf('/')+1);
290             mock_modules[module]();
291         };
292         load("some-path/foo");
293         assert(featurep("foo"));
294         assert(featurep("bar"));
295     }