remove in_module mechanism
[conkeror/arlinius.git] / tests / simple / modules.js
bloba88dc116635e047cd80606a2de1b312396dd2775
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/extensions/foo",
61              "chrome://conkeror/content/extensions/foo.js",
62              "chrome://conkeror/content/page-modes/foo",
63              "chrome://conkeror/content/page-modes/foo.js"],
64             this.ob);
65     },
66     test_load_search_2__with_extension: function () {
67         load_paths = ["chrome://conkeror/content/",
68                       "chrome://conkeror/content/extensions",
69                       "chrome://conkeror/content/page-modes"];
70         try {
71             load("foo.js");
72         } catch (e) {
73         }
74         assert_objects_equal(
75             ["chrome://conkeror/content/foo.js",
76              "chrome://conkeror/content/extensions/foo.js",
77              "chrome://conkeror/content/page-modes/foo.js"],
78             this.ob);
79     },
80     test_load_search_3__load_path_dups: function () {
81         load_paths = ["chrome://conkeror/content/",
82                       "chrome://conkeror/content/extensions",
83                       "chrome://conkeror/content/extensions/",
84                       "chrome://conkeror/content/page-modes"];
85         try {
86             load("foo.js");
87         } catch (e) {
88         }
89         assert_objects_equal(
90             ["chrome://conkeror/content/foo.js",
91              "chrome://conkeror/content/extensions/foo.js",
92              "chrome://conkeror/content/page-modes/foo.js"],
93             this.ob);
94     },
95     test_load_search_4__require_skips_cur_dir: function () {
96         loading_paths = ["file:///foo/bar/baz/"];
97         load_paths = ["chrome://conkeror/content/"];
98         try {
99             require("foo.js");
100         } catch (e) {
101         }
102         assert_objects_equal(
103             ["chrome://conkeror/content/foo.js"],
104             this.ob);
105     },
106     test_load_search_5__relative_path: function () {
107         load_paths = ["chrome://conkeror/content/",
108                       "chrome://conkeror/content/extensions",
109                       "chrome://conkeror/content/page-modes"];
110         try {
111             load("page-modes/foo");
112         } catch (e) {
113         }
114         assert_objects_equal(
115             this.ob,
116             ["chrome://conkeror/content/page-modes/foo",
117              "chrome://conkeror/content/page-modes/foo.js",
118              "chrome://conkeror/content/extensions/page-modes/foo",
119              "chrome://conkeror/content/extensions/page-modes/foo.js",
120              "chrome://conkeror/content/page-modes/page-modes/foo",
121              "chrome://conkeror/content/page-modes/page-modes/foo.js"]);
122     }
126 walnut_run({
127     suite_setup: function () {
128         this._load_paths = load_paths;
129         this._loading_paths = loading_paths;
130         this._loading_urls = loading_urls;
131         this._loading_features = loading_features;
132         this._pending_loads = pending_loads;
133         this._features = features;
134         this._after_load_functions = after_load_functions;
135         this._load_url = load_url;
136     },
137     teardown: function () {
138         load_paths = this._load_paths;
139         load_url = this._load_url;
140         loading_paths = this._loading_paths;
141         loading_urls = this._loading_urls;
142         loading_features = this._loading_features;
143         pending_loads = this._pending_loads;
144         features = this._features;
145         after_load_functions = this._after_load_functions;
146     },
147     test_load__circular_load_is_error: function () {
148         load_url = function () {
149             load(make_uri("chrome://conkeror/content/foo.js"));
150         };
151         assert_error(function () {
152             load(make_uri("chrome://conkeror/content/foo.js"));
153         });
154     },
155     test_load__not_found_is_error: function () {
156         load_paths = [];
157         loading_paths = [];
158         loading_urls = [];
159         loading_features = [];
160         pending_loads = [];
161         assert_error(function () load("foo"));
162     },
163     test_require_later_1: function () {
164         loading_urls = [];
165         pending_loads = [];
166         var ob = "";
167         function mock_foo () {
168             ob += "a";
169             require_later("bar");
170             ob += "b";
171             load_url = mock_bar;
172         }
173         function mock_bar () {
174             ob += "c";
175         }
176         load_url = mock_foo;
177         load("foo");
178         assert_equals(ob, "abc");
179     },
180     test_require_later_2: function () {
181         loading_paths = [];
182         loading_urls = [];
183         loading_features = [];
184         pending_loads = [];
185         after_load_functions = [];
186         features = {};
187         var ob = [];
188         var mock_modules = {
189             foo: function () {
190                 ob.push("foo");
191                 require_later("baz");
192                 require("bar");
193                 provide("foo");
194             },
195             bar: function () {
196                 ob.push("bar");
197                 provide("bar");
198             },
199             baz: function () {
200                 ob.push("baz");
201                 require_later("quux");
202                 provide("baz");
203             },
204             quux: function () {
205                 ob.push("quux");
206                 provide("quux");
207             }
208         };
209         load_url = function (url) {
210             var module = url.substr(url.lastIndexOf('/')+1);
211             mock_modules[module]();
212         };
213         load("foo");
214         assert_objects_equal(ob, ["foo", "bar", "baz", "quux"]);
215     },
216     test_provide__load_order: function () {
217         // want to make sure that after_load_functions only get called
218         // after the completion of the load which provided the feature.
219         loading_paths = [];
220         loading_urls = [];
221         loading_features = [];
222         pending_loads = [];
223         after_load_functions = [];
224         features = {};
226         var called = false;
227         var mock_modules = {
228             foo: function () {
229                 called = true;
230                 provide("foo");
231                 assert_not(featurep("foo"));
232             }
233         };
234         load_url = function (url) {
235             var module = url.substr(url.lastIndexOf('/')+1);
236             mock_modules[module]();
237         };
238         load("foo");
239         assert(called);
240         assert(featurep("foo"));
241     },
242     test_provide: function () {
243         loading_paths = [];
244         loading_urls = [];
245         loading_features = [];
246         pending_loads = [];
247         after_load_functions = [];
248         features = {};
249         provide("foo");
250         assert(featurep("foo"));
251     },
252     test_load_relative_of_nsIURI: function () {
253         var tried = null;
254         load_url = function (url) {
255             if (url == "chrome://conkeror/content/foo.js") {
256                 load("bar.js");
257             } else {
258                 tried = url;
259             }
260         };
261         load(make_uri("chrome://conkeror/content/foo.js"));
262         assert_equals(tried, "chrome://conkeror/content/bar.js");
263     }