Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / event_unittest.cc
blob9d3b68f0014980dffae42b48e934db200af85446
1 // Copyright 2014 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 "extensions/common/extension_urls.h"
6 #include "extensions/renderer/module_system_test.h"
7 #include "grit/extensions_renderer_resources.h"
9 namespace extensions {
10 namespace {
12 class EventUnittest : public ModuleSystemTest {
13 void SetUp() override {
14 ModuleSystemTest::SetUp();
16 env()->RegisterModule(kEventBindings, IDR_EVENT_BINDINGS_JS);
17 env()->RegisterModule("json_schema", IDR_JSON_SCHEMA_JS);
18 env()->RegisterModule(kSchemaUtils, IDR_SCHEMA_UTILS_JS);
19 env()->RegisterModule("uncaught_exception_handler",
20 IDR_UNCAUGHT_EXCEPTION_HANDLER_JS);
21 env()->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 env()->OverrideNativeHandler(
26 "event_natives",
27 "var assert = requireNative('assert');"
28 "var attachedListeners = exports.attachedListeners = {};"
29 "var attachedFilteredListeners = "
30 " exports.attachedFilteredListeners = {};"
31 "var nextId = 0;"
32 "var idToName = {};"
33 "exports.AttachEvent = function(eventName) {"
34 " assert.AssertFalse(!!attachedListeners[eventName]);"
35 " attachedListeners[eventName] = 1;"
36 "};"
37 "exports.DetachEvent = function(eventName) {"
38 " assert.AssertTrue(!!attachedListeners[eventName]);"
39 " delete attachedListeners[eventName];"
40 "};"
41 "exports.IsEventAttached = function(eventName) {"
42 " return !!attachedListeners[eventName];"
43 "};"
44 "exports.AttachFilteredEvent = function(name, filters) {"
45 " var id = nextId++;"
46 " idToName[id] = name;"
47 " attachedFilteredListeners[name] ="
48 " attachedFilteredListeners[name] || [];"
49 " attachedFilteredListeners[name][id] = filters;"
50 " return id;"
51 "};"
52 "exports.DetachFilteredEvent = function(id, manual) {"
53 " var i = attachedFilteredListeners[idToName[id]].indexOf(id);"
54 " attachedFilteredListeners[idToName[id]].splice(i, 1);"
55 "};"
56 "exports.HasFilteredListener = function(name) {"
57 " return attachedFilteredListeners[name].length;"
58 "};");
59 env()->OverrideNativeHandler("sendRequest",
60 "exports.sendRequest = function() {};");
61 env()->OverrideNativeHandler(
62 "apiDefinitions",
63 "exports.GetExtensionAPIDefinitionsForTest = function() {};");
64 env()->OverrideNativeHandler("logging", "exports.DCHECK = function() {};");
65 env()->OverrideNativeHandler("schema_registry",
66 "exports.GetSchema = function() {};");
70 TEST_F(EventUnittest, TestNothing) {
71 ExpectNoAssertionsMade();
74 TEST_F(EventUnittest, AddRemoveTwoListeners) {
75 ModuleSystem::NativesEnabledScope natives_enabled_scope(
76 env()->module_system());
77 env()->RegisterModule(
78 "test",
79 "var assert = requireNative('assert');"
80 "var Event = require('event_bindings').Event;"
81 "var eventNatives = requireNative('event_natives');"
82 "var myEvent = new Event('named-event');"
83 "var cb1 = function() {};"
84 "var cb2 = function() {};"
85 "myEvent.addListener(cb1);"
86 "myEvent.addListener(cb2);"
87 "myEvent.removeListener(cb1);"
88 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);"
89 "myEvent.removeListener(cb2);"
90 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
91 env()->module_system()->Require("test");
94 TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) {
95 ModuleSystem::NativesEnabledScope natives_enabled_scope(
96 env()->module_system());
97 env()->RegisterModule(
98 "test",
99 "var Event = require('event_bindings').Event;"
100 "var eventOpts = {supportsRules: true};"
101 "var assert = requireNative('assert');"
102 "var caught = false;"
103 "try {"
104 " var myEvent = new Event(undefined, undefined, eventOpts);"
105 "} catch (e) {"
106 " caught = true;"
108 "assert.AssertTrue(caught);");
109 env()->module_system()->Require("test");
112 TEST_F(EventUnittest, NamedEventDispatch) {
113 ModuleSystem::NativesEnabledScope natives_enabled_scope(
114 env()->module_system());
115 env()->RegisterModule(
116 "test",
117 "var Event = require('event_bindings').Event;"
118 "var dispatchEvent = require('event_bindings').dispatchEvent;"
119 "var assert = requireNative('assert');"
120 "var e = new Event('myevent');"
121 "var called = false;"
122 "e.addListener(function() { called = true; });"
123 "dispatchEvent('myevent', []);"
124 "assert.AssertTrue(called);");
125 env()->module_system()->Require("test");
128 TEST_F(EventUnittest, AddListenerWithFiltersThrowsErrorByDefault) {
129 ModuleSystem::NativesEnabledScope natives_enabled_scope(
130 env()->module_system());
131 env()->RegisterModule("test",
132 "var Event = require('event_bindings').Event;"
133 "var assert = requireNative('assert');"
134 "var e = new Event('myevent');"
135 "var filter = [{"
136 " url: {hostSuffix: 'google.com'},"
137 "}];"
138 "var caught = false;"
139 "try {"
140 " e.addListener(function() {}, filter);"
141 "} catch (e) {"
142 " caught = true;"
144 "assert.AssertTrue(caught);");
145 env()->module_system()->Require("test");
148 TEST_F(EventUnittest, FilteredEventsAttachment) {
149 ModuleSystem::NativesEnabledScope natives_enabled_scope(
150 env()->module_system());
151 env()->RegisterModule(
152 "test",
153 "var Event = require('event_bindings').Event;"
154 "var assert = requireNative('assert');"
155 "var bindings = requireNative('event_natives');"
156 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
157 "var e = new Event('myevent', undefined, eventOpts);"
158 "var cb = function() {};"
159 "var filters = {url: [{hostSuffix: 'google.com'}]};"
160 "e.addListener(cb, filters);"
161 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
162 "e.removeListener(cb);"
163 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
164 env()->module_system()->Require("test");
167 TEST_F(EventUnittest, DetachFilteredEvent) {
168 ModuleSystem::NativesEnabledScope natives_enabled_scope(
169 env()->module_system());
170 env()->RegisterModule(
171 "test",
172 "var Event = require('event_bindings').Event;"
173 "var assert = requireNative('assert');"
174 "var bindings = requireNative('event_natives');"
175 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
176 "var e = new Event('myevent', undefined, eventOpts);"
177 "var cb1 = function() {};"
178 "var cb2 = function() {};"
179 "var filters = {url: [{hostSuffix: 'google.com'}]};"
180 "e.addListener(cb1, filters);"
181 "e.addListener(cb2, filters);"
182 "privates(e).impl.detach_();"
183 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
184 env()->module_system()->Require("test");
187 TEST_F(EventUnittest, AttachAndRemoveSameFilteredEventListener) {
188 ModuleSystem::NativesEnabledScope natives_enabled_scope(
189 env()->module_system());
190 env()->RegisterModule(
191 "test",
192 "var Event = require('event_bindings').Event;"
193 "var assert = requireNative('assert');"
194 "var bindings = requireNative('event_natives');"
195 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
196 "var e = new Event('myevent', undefined, eventOpts);"
197 "var cb = function() {};"
198 "var filters = {url: [{hostSuffix: 'google.com'}]};"
199 "e.addListener(cb, filters);"
200 "e.addListener(cb, filters);"
201 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
202 "e.removeListener(cb);"
203 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
204 "e.removeListener(cb);"
205 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
206 env()->module_system()->Require("test");
209 TEST_F(EventUnittest, AddingFilterWithUrlFieldNotAListThrowsException) {
210 ModuleSystem::NativesEnabledScope natives_enabled_scope(
211 env()->module_system());
212 env()->RegisterModule(
213 "test",
214 "var Event = require('event_bindings').Event;"
215 "var assert = requireNative('assert');"
216 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
217 "var e = new Event('myevent', undefined, eventOpts);"
218 "var cb = function() {};"
219 "var filters = {url: {hostSuffix: 'google.com'}};"
220 "var caught = false;"
221 "try {"
222 " e.addListener(cb, filters);"
223 "} catch (e) {"
224 " caught = true;"
226 "assert.AssertTrue(caught);");
227 env()->module_system()->Require("test");
230 TEST_F(EventUnittest, MaxListeners) {
231 ModuleSystem::NativesEnabledScope natives_enabled_scope(
232 env()->module_system());
233 env()->RegisterModule(
234 "test",
235 "var Event = require('event_bindings').Event;"
236 "var assert = requireNative('assert');"
237 "var eventOpts = {supportsListeners: true, maxListeners: 1};"
238 "var e = new Event('myevent', undefined, eventOpts);"
239 "var cb = function() {};"
240 "var caught = false;"
241 "try {"
242 " e.addListener(cb);"
243 "} catch (e) {"
244 " caught = true;"
246 "assert.AssertTrue(!caught);"
247 "try {"
248 " e.addListener(cb);"
249 "} catch (e) {"
250 " caught = true;"
252 "assert.AssertTrue(caught);");
253 env()->module_system()->Require("test");
256 } // namespace
257 } // namespace extensions