Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / renderer / extensions / event_unittest.cc
blob6614c54eb941c49ab4588b5136bcfc5215a35e97
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 "extensions/common/extension_urls.h"
8 #include "grit/renderer_resources.h"
10 namespace extensions {
11 namespace {
13 class EventUnittest : public ModuleSystemTest {
14 virtual void SetUp() OVERRIDE {
15 ModuleSystemTest::SetUp();
17 RegisterModule(kEventBindings, IDR_EVENT_BINDINGS_JS);
18 RegisterModule("json_schema", IDR_JSON_SCHEMA_JS);
19 RegisterModule(kSchemaUtils, IDR_SCHEMA_UTILS_JS);
20 RegisterModule("unload_event", IDR_UNLOAD_EVENT_JS);
21 RegisterModule("utils", IDR_UTILS_JS);
23 // Mock out the native handler for event_bindings. These mocks will fail if
24 // any invariants maintained by the real event_bindings are broken.
25 OverrideNativeHandler("event_natives",
26 "var assert = requireNative('assert');"
27 "var attachedListeners = exports.attachedListeners = {};"
28 "var attachedFilteredListeners = "
29 " exports.attachedFilteredListeners = {};"
30 "var nextId = 0;"
31 "var idToName = {};"
33 "exports.AttachEvent = function(eventName) {"
34 " assert.AssertFalse(!!attachedListeners[eventName]);"
35 " attachedListeners[eventName] = 1;"
36 "};"
38 "exports.DetachEvent = function(eventName) {"
39 " assert.AssertTrue(!!attachedListeners[eventName]);"
40 " delete attachedListeners[eventName];"
41 "};"
43 "exports.IsEventAttached = function(eventName) {"
44 " return !!attachedListeners[eventName];"
45 "};"
47 "exports.AttachFilteredEvent = function(name, filters) {"
48 " var id = nextId++;"
49 " idToName[id] = name;"
50 " attachedFilteredListeners[name] ="
51 " attachedFilteredListeners[name] || [];"
52 " attachedFilteredListeners[name][id] = filters;"
53 " return id;"
54 "};"
56 "exports.DetachFilteredEvent = function(id, manual) {"
57 " var i = attachedFilteredListeners[idToName[id]].indexOf(id);"
58 " attachedFilteredListeners[idToName[id]].splice(i, 1);"
59 "};"
61 "exports.HasFilteredListener = function(name) {"
62 " return attachedFilteredListeners[name].length;"
63 "};");
64 OverrideNativeHandler("sendRequest",
65 "exports.sendRequest = function() {};");
66 OverrideNativeHandler("apiDefinitions",
67 "exports.GetExtensionAPIDefinitionsForTest = function() {};");
68 OverrideNativeHandler("logging",
69 "exports.DCHECK = function() {};");
70 OverrideNativeHandler("schema_registry",
71 "exports.GetSchema = function() {};");
75 TEST_F(EventUnittest, TestNothing) {
76 ExpectNoAssertionsMade();
79 TEST_F(EventUnittest, AddRemoveTwoListeners) {
80 ModuleSystem::NativesEnabledScope natives_enabled_scope(
81 context_->module_system());
82 RegisterModule("test",
83 "var assert = requireNative('assert');"
84 "var Event = require('event_bindings').Event;"
85 "var eventNatives = requireNative('event_natives');"
86 "var myEvent = new Event('named-event');"
87 "var cb1 = function() {};"
88 "var cb2 = function() {};"
89 "myEvent.addListener(cb1);"
90 "myEvent.addListener(cb2);"
91 "myEvent.removeListener(cb1);"
92 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);"
93 "myEvent.removeListener(cb2);"
94 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
95 context_->module_system()->Require("test");
98 TEST_F(EventUnittest, OnUnloadDetachesAllListeners) {
99 ModuleSystem::NativesEnabledScope natives_enabled_scope(
100 context_->module_system());
101 RegisterModule("test",
102 "var assert = requireNative('assert');"
103 "var Event = require('event_bindings').Event;"
104 "var eventNatives = requireNative('event_natives');"
105 "var myEvent = new Event('named-event');"
106 "var cb1 = function() {};"
107 "var cb2 = function() {};"
108 "myEvent.addListener(cb1);"
109 "myEvent.addListener(cb2);"
110 "require('unload_event').dispatch();"
111 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
112 context_->module_system()->Require("test");
115 TEST_F(EventUnittest, OnUnloadDetachesAllListenersEvenDupes) {
116 ModuleSystem::NativesEnabledScope natives_enabled_scope(
117 context_->module_system());
118 RegisterModule("test",
119 "var assert = requireNative('assert');"
120 "var Event = require('event_bindings').Event;"
121 "var eventNatives = requireNative('event_natives');"
122 "var myEvent = new Event('named-event');"
123 "var cb1 = function() {};"
124 "myEvent.addListener(cb1);"
125 "myEvent.addListener(cb1);"
126 "require('unload_event').dispatch();"
127 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
128 context_->module_system()->Require("test");
131 TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) {
132 ModuleSystem::NativesEnabledScope natives_enabled_scope(
133 context_->module_system());
134 RegisterModule("test",
135 "var Event = require('event_bindings').Event;"
136 "var eventOpts = {supportsRules: true};"
137 "var assert = requireNative('assert');"
138 "var caught = false;"
139 "try {"
140 " var myEvent = new Event(undefined, undefined, eventOpts);"
141 "} catch (e) {"
142 " caught = true;"
144 "assert.AssertTrue(caught);");
145 context_->module_system()->Require("test");
148 TEST_F(EventUnittest, NamedEventDispatch) {
149 ModuleSystem::NativesEnabledScope natives_enabled_scope(
150 context_->module_system());
151 RegisterModule("test",
152 "var Event = require('event_bindings').Event;"
153 "var dispatchEvent = require('event_bindings').dispatchEvent;"
154 "var assert = requireNative('assert');"
155 "var e = new Event('myevent');"
156 "var called = false;"
157 "e.addListener(function() { called = true; });"
158 "dispatchEvent('myevent', []);"
159 "assert.AssertTrue(called);");
160 context_->module_system()->Require("test");
163 TEST_F(EventUnittest, AddListenerWithFiltersThrowsErrorByDefault) {
164 ModuleSystem::NativesEnabledScope natives_enabled_scope(
165 context_->module_system());
166 RegisterModule("test",
167 "var Event = require('event_bindings').Event;"
168 "var assert = requireNative('assert');"
169 "var e = new Event('myevent');"
170 "var filter = [{"
171 " url: {hostSuffix: 'google.com'},"
172 "}];"
173 "var caught = false;"
174 "try {"
175 " e.addListener(function() {}, filter);"
176 "} catch (e) {"
177 " caught = true;"
179 "assert.AssertTrue(caught);");
180 context_->module_system()->Require("test");
183 TEST_F(EventUnittest, FilteredEventsAttachment) {
184 ModuleSystem::NativesEnabledScope natives_enabled_scope(
185 context_->module_system());
186 RegisterModule("test",
187 "var Event = require('event_bindings').Event;"
188 "var assert = requireNative('assert');"
189 "var bindings = requireNative('event_natives');"
190 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
191 "var e = new Event('myevent', undefined, eventOpts);"
192 "var cb = function() {};"
193 "var filters = {url: [{hostSuffix: 'google.com'}]};"
194 "e.addListener(cb, filters);"
195 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
196 "e.removeListener(cb);"
197 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
198 context_->module_system()->Require("test");
201 TEST_F(EventUnittest, DetachFilteredEvent) {
202 ModuleSystem::NativesEnabledScope natives_enabled_scope(
203 context_->module_system());
204 RegisterModule("test",
205 "var Event = require('event_bindings').Event;"
206 "var assert = requireNative('assert');"
207 "var bindings = requireNative('event_natives');"
208 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
209 "var e = new Event('myevent', undefined, eventOpts);"
210 "var cb1 = function() {};"
211 "var cb2 = function() {};"
212 "var filters = {url: [{hostSuffix: 'google.com'}]};"
213 "e.addListener(cb1, filters);"
214 "e.addListener(cb2, filters);"
215 "privates(e).impl.detach_();"
216 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
217 context_->module_system()->Require("test");
220 TEST_F(EventUnittest, AttachAndRemoveSameFilteredEventListener) {
221 ModuleSystem::NativesEnabledScope natives_enabled_scope(
222 context_->module_system());
223 RegisterModule("test",
224 "var Event = require('event_bindings').Event;"
225 "var assert = requireNative('assert');"
226 "var bindings = requireNative('event_natives');"
227 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
228 "var e = new Event('myevent', undefined, eventOpts);"
229 "var cb = function() {};"
230 "var filters = {url: [{hostSuffix: 'google.com'}]};"
231 "e.addListener(cb, filters);"
232 "e.addListener(cb, filters);"
233 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
234 "e.removeListener(cb);"
235 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
236 "e.removeListener(cb);"
237 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
238 context_->module_system()->Require("test");
241 TEST_F(EventUnittest, AddingFilterWithUrlFieldNotAListThrowsException) {
242 ModuleSystem::NativesEnabledScope natives_enabled_scope(
243 context_->module_system());
244 RegisterModule("test",
245 "var Event = require('event_bindings').Event;"
246 "var assert = requireNative('assert');"
247 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
248 "var e = new Event('myevent', undefined, eventOpts);"
249 "var cb = function() {};"
250 "var filters = {url: {hostSuffix: 'google.com'}};"
251 "var caught = false;"
252 "try {"
253 " e.addListener(cb, filters);"
254 "} catch (e) {"
255 " caught = true;"
257 "assert.AssertTrue(caught);");
258 context_->module_system()->Require("test");
261 TEST_F(EventUnittest, MaxListeners) {
262 ModuleSystem::NativesEnabledScope natives_enabled_scope(
263 context_->module_system());
264 RegisterModule("test",
265 "var Event = require('event_bindings').Event;"
266 "var assert = requireNative('assert');"
267 "var eventOpts = {supportsListeners: true, maxListeners: 1};"
268 "var e = new Event('myevent', undefined, eventOpts);"
269 "var cb = function() {};"
270 "var caught = false;"
271 "try {"
272 " e.addListener(cb);"
273 "} catch (e) {"
274 " caught = true;"
276 "assert.AssertTrue(!caught);"
277 "try {"
278 " e.addListener(cb);"
279 "} catch (e) {"
280 " caught = true;"
282 "assert.AssertTrue(caught);");
283 context_->module_system()->Require("test");
286 } // namespace
287 } // namespace extensions