1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/test/base/module_system_test.h"
7 #include "grit/renderer_resources.h"
12 class EventUnittest
: public ModuleSystemTest
{
13 virtual void SetUp() OVERRIDE
{
14 ModuleSystemTest::SetUp();
15 module_system_
->RunString("chrome = {};", "setup-chrome");
17 RegisterModule("event", IDR_EVENT_BINDINGS_JS
);
18 RegisterModule("schemaUtils", IDR_SCHEMA_UTILS_JS
);
19 RegisterModule("utils", IDR_UTILS_JS
);
20 RegisterModule("json_schema", IDR_JSON_SCHEMA_JS
);
22 // Mock out the native handler for event_bindings. These mocks will fail if
23 // any invariants maintained by the real event_bindings are broken.
24 OverrideNativeHandler("event_bindings",
25 "var assert = requireNative('assert');"
26 "var attachedListeners = exports.attachedListeners = {};"
27 "var attachedFilteredListeners = "
28 " exports.attachedFilteredListeners = {};"
32 "exports.AttachEvent = function(eventName) {"
33 " assert.AssertFalse(!!attachedListeners[eventName]);"
34 " attachedListeners[eventName] = 1;"
37 "exports.DetachEvent = function(eventName) {"
38 " assert.AssertTrue(!!attachedListeners[eventName]);"
39 " delete attachedListeners[eventName];"
42 "exports.IsEventAttached = function(eventName) {"
43 " return !!attachedListeners[eventName];"
46 "exports.AttachFilteredEvent = function(name, filters) {"
48 " idToName[id] = name;"
49 " attachedFilteredListeners[name] ="
50 " attachedFilteredListeners[name] || [];"
51 " attachedFilteredListeners[name][id] = filters;"
55 "exports.DetachFilteredEvent = function(id, manual) {"
56 " var i = attachedFilteredListeners[idToName[id]].indexOf(id);"
57 " attachedFilteredListeners[idToName[id]].splice(i, 1);"
60 "exports.HasFilteredListener = function(name) {"
61 " return attachedFilteredListeners[name].length;"
63 OverrideNativeHandler("chrome_hidden",
64 "var chromeHidden = {};"
65 "exports.GetChromeHidden = function() { return chromeHidden; };");
66 OverrideNativeHandler("chrome",
68 "exports.GetChrome = function() { return chrome; };");
69 OverrideNativeHandler("sendRequest",
70 "exports.sendRequest = function() {};");
71 OverrideNativeHandler("apiDefinitions",
72 "exports.GetExtensionAPIDefinition = function() {};");
73 OverrideNativeHandler("logging",
74 "exports.DCHECK = function() {};");
75 OverrideNativeHandler("schema_registry",
76 "exports.GetSchema = function() {};");
80 TEST_F(EventUnittest
, TestNothing
) {
81 ExpectNoAssertionsMade();
84 TEST_F(EventUnittest
, AddRemoveTwoListeners
) {
85 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
86 RegisterModule("test",
87 "var assert = requireNative('assert');"
89 "var Event = requireNative('chrome').GetChrome().Event;"
90 "var eventBindings = requireNative('event_bindings');"
91 "var myEvent = new Event('named-event');"
92 "var cb1 = function() {};"
93 "var cb2 = function() {};"
94 "myEvent.addListener(cb1);"
95 "myEvent.addListener(cb2);"
96 "myEvent.removeListener(cb1);"
97 "assert.AssertTrue(!!eventBindings.attachedListeners['named-event']);"
98 "myEvent.removeListener(cb2);"
99 "assert.AssertFalse(!!eventBindings.attachedListeners['named-event']);");
100 module_system_
->Require("test");
103 TEST_F(EventUnittest
, OnUnloadDetachesAllListeners
) {
104 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
105 RegisterModule("test",
106 "var assert = requireNative('assert');"
108 "var Event = requireNative('chrome').GetChrome().Event;"
109 "var eventBindings = requireNative('event_bindings');"
110 "var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();"
111 "var myEvent = new Event('named-event');"
112 "var cb1 = function() {};"
113 "var cb2 = function() {};"
114 "myEvent.addListener(cb1);"
115 "myEvent.addListener(cb2);"
116 "chromeHidden.dispatchOnUnload();"
117 "assert.AssertFalse(!!eventBindings.attachedListeners['named-event']);");
118 module_system_
->Require("test");
121 TEST_F(EventUnittest
, OnUnloadDetachesAllListenersEvenDupes
) {
122 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
123 RegisterModule("test",
124 "var assert = requireNative('assert');"
126 "var Event = requireNative('chrome').GetChrome().Event;"
127 "var eventBindings = requireNative('event_bindings');"
128 "var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();"
129 "var myEvent = new Event('named-event');"
130 "var cb1 = function() {};"
131 "myEvent.addListener(cb1);"
132 "myEvent.addListener(cb1);"
133 "chromeHidden.dispatchOnUnload();"
134 "assert.AssertFalse(!!eventBindings.attachedListeners['named-event']);");
135 module_system_
->Require("test");
138 TEST_F(EventUnittest
, EventsThatSupportRulesMustHaveAName
) {
139 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
140 RegisterModule("test",
142 "var Event = requireNative('chrome').GetChrome().Event;"
143 "var eventOpts = {supportsRules: true};"
144 "var assert = requireNative('assert');"
145 "var caught = false;"
147 " var myEvent = new Event(undefined, undefined, eventOpts);"
151 "assert.AssertTrue(caught);");
152 module_system_
->Require("test");
155 TEST_F(EventUnittest
, NamedEventDispatch
) {
156 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
157 RegisterModule("test",
159 "var Event = requireNative('chrome').GetChrome().Event;"
160 "var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();"
161 "var assert = requireNative('assert');"
162 "var e = new Event('myevent');"
163 "var called = false;"
164 "e.addListener(function() { called = true; });"
165 "chromeHidden.Event.dispatchEvent('myevent', []);"
166 "assert.AssertTrue(called);");
167 module_system_
->Require("test");
170 TEST_F(EventUnittest
, AddListenerWithFiltersThrowsErrorByDefault
) {
171 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
172 RegisterModule("test",
174 "var Event = requireNative('chrome').GetChrome().Event;"
175 "var assert = requireNative('assert');"
176 "var e = new Event('myevent');"
178 " url: {hostSuffix: 'google.com'},"
180 "var caught = false;"
182 " e.addListener(function() {}, filter);"
186 "assert.AssertTrue(caught);");
187 module_system_
->Require("test");
190 TEST_F(EventUnittest
, FilteredEventsAttachment
) {
191 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
192 RegisterModule("test",
194 "var Event = requireNative('chrome').GetChrome().Event;"
195 "var assert = requireNative('assert');"
196 "var bindings = requireNative('event_bindings');"
197 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
198 "var e = new Event('myevent', undefined, eventOpts);"
199 "var cb = function() {};"
200 "var filters = {url: [{hostSuffix: 'google.com'}]};"
201 "e.addListener(cb, filters);"
202 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
203 "e.removeListener(cb);"
204 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
205 module_system_
->Require("test");
208 TEST_F(EventUnittest
, DetachFilteredEvent
) {
209 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
210 RegisterModule("test",
212 "var Event = requireNative('chrome').GetChrome().Event;"
213 "var assert = requireNative('assert');"
214 "var bindings = requireNative('event_bindings');"
215 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
216 "var e = new Event('myevent', undefined, eventOpts);"
217 "var cb1 = function() {};"
218 "var cb2 = function() {};"
219 "var filters = {url: [{hostSuffix: 'google.com'}]};"
220 "e.addListener(cb1, filters);"
221 "e.addListener(cb2, filters);"
223 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
224 module_system_
->Require("test");
227 TEST_F(EventUnittest
, AttachAndRemoveSameFilteredEventListener
) {
228 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
229 RegisterModule("test",
231 "var Event = requireNative('chrome').GetChrome().Event;"
232 "var assert = requireNative('assert');"
233 "var bindings = requireNative('event_bindings');"
234 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
235 "var e = new Event('myevent', undefined, eventOpts);"
236 "var cb = function() {};"
237 "var filters = {url: [{hostSuffix: 'google.com'}]};"
238 "e.addListener(cb, filters);"
239 "e.addListener(cb, filters);"
240 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
241 "e.removeListener(cb);"
242 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
243 "e.removeListener(cb);"
244 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
245 module_system_
->Require("test");
248 TEST_F(EventUnittest
, AddingFilterWithUrlFieldNotAListThrowsException
) {
249 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
250 RegisterModule("test",
252 "var Event = requireNative('chrome').GetChrome().Event;"
253 "var assert = requireNative('assert');"
254 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
255 "var e = new Event('myevent', undefined, eventOpts);"
256 "var cb = function() {};"
257 "var filters = {url: {hostSuffix: 'google.com'}};"
258 "var caught = false;"
260 " e.addListener(cb, filters);"
264 "assert.AssertTrue(caught);");
265 module_system_
->Require("test");
268 TEST_F(EventUnittest
, MaxListeners
) {
269 ModuleSystem::NativesEnabledScope
natives_enabled_scope(module_system_
.get());
270 RegisterModule("test",
272 "var Event = requireNative('chrome').GetChrome().Event;"
273 "var assert = requireNative('assert');"
274 "var eventOpts = {supportsListeners: true, maxListeners: 1};"
275 "var e = new Event('myevent', undefined, eventOpts);"
276 "var cb = function() {};"
277 "var caught = false;"
279 " e.addListener(cb);"
283 "assert.AssertTrue(!caught);"
285 " e.addListener(cb);"
289 "assert.AssertTrue(caught);");
290 module_system_
->Require("test");
294 } // namespace extensions