blob: 689b76e175542149536bf5b9d442405f2bdd338c [file] [log] [blame]
[email protected]582f6e92014-07-16 23:39:151// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]11844fa2012-05-10 00:35:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a0ed2682013-09-06 08:41:075#include "extensions/common/extension_urls.h"
[email protected]582f6e92014-07-16 23:39:156#include "extensions/renderer/module_system_test.h"
[email protected]8f857ef82014-06-04 23:46:167#include "grit/extensions_renderer_resources.h"
[email protected]11844fa2012-05-10 00:35:598
[email protected]8fe74bf2012-08-07 21:08:429namespace extensions {
[email protected]11844fa2012-05-10 00:35:5910namespace {
11
12class EventUnittest : public ModuleSystemTest {
13 virtual void SetUp() OVERRIDE {
14 ModuleSystemTest::SetUp();
[email protected]11844fa2012-05-10 00:35:5915
[email protected]d9f51dad2014-07-09 05:39:3816 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("unload_event", IDR_UNLOAD_EVENT_JS);
22 env()->RegisterModule("utils", IDR_UTILS_JS);
[email protected]11844fa2012-05-10 00:35:5923
24 // Mock out the native handler for event_bindings. These mocks will fail if
25 // any invariants maintained by the real event_bindings are broken.
[email protected]d9f51dad2014-07-09 05:39:3826 env()->OverrideNativeHandler(
27 "event_natives",
[email protected]11844fa2012-05-10 00:35:5928 "var assert = requireNative('assert');"
29 "var attachedListeners = exports.attachedListeners = {};"
[email protected]d9e559d2012-07-05 01:04:5730 "var attachedFilteredListeners = "
31 " exports.attachedFilteredListeners = {};"
32 "var nextId = 0;"
33 "var idToName = {};"
[email protected]11844fa2012-05-10 00:35:5934 "exports.AttachEvent = function(eventName) {"
35 " assert.AssertFalse(!!attachedListeners[eventName]);"
36 " attachedListeners[eventName] = 1;"
37 "};"
[email protected]11844fa2012-05-10 00:35:5938 "exports.DetachEvent = function(eventName) {"
39 " assert.AssertTrue(!!attachedListeners[eventName]);"
40 " delete attachedListeners[eventName];"
[email protected]d9e559d2012-07-05 01:04:5741 "};"
[email protected]d9e559d2012-07-05 01:04:5742 "exports.IsEventAttached = function(eventName) {"
43 " return !!attachedListeners[eventName];"
44 "};"
[email protected]d9e559d2012-07-05 01:04:5745 "exports.AttachFilteredEvent = function(name, filters) {"
46 " var id = nextId++;"
47 " idToName[id] = name;"
48 " attachedFilteredListeners[name] ="
49 " attachedFilteredListeners[name] || [];"
50 " attachedFilteredListeners[name][id] = filters;"
51 " return id;"
52 "};"
[email protected]d9e559d2012-07-05 01:04:5753 "exports.DetachFilteredEvent = function(id, manual) {"
54 " var i = attachedFilteredListeners[idToName[id]].indexOf(id);"
55 " attachedFilteredListeners[idToName[id]].splice(i, 1);"
56 "};"
[email protected]d9e559d2012-07-05 01:04:5757 "exports.HasFilteredListener = function(name) {"
58 " return attachedFilteredListeners[name].length;"
[email protected]11844fa2012-05-10 00:35:5959 "};");
[email protected]d9f51dad2014-07-09 05:39:3860 env()->OverrideNativeHandler("sendRequest",
61 "exports.sendRequest = function() {};");
62 env()->OverrideNativeHandler(
63 "apiDefinitions",
[email protected]6876bb92013-06-14 06:03:5164 "exports.GetExtensionAPIDefinitionsForTest = function() {};");
[email protected]d9f51dad2014-07-09 05:39:3865 env()->OverrideNativeHandler("logging", "exports.DCHECK = function() {};");
66 env()->OverrideNativeHandler("schema_registry",
67 "exports.GetSchema = function() {};");
[email protected]11844fa2012-05-10 00:35:5968 }
69};
70
71TEST_F(EventUnittest, TestNothing) {
72 ExpectNoAssertionsMade();
73}
74
75TEST_F(EventUnittest, AddRemoveTwoListeners) {
[email protected]2a356872014-02-21 23:18:5276 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:3877 env()->module_system());
78 env()->RegisterModule(
79 "test",
[email protected]11844fa2012-05-10 00:35:5980 "var assert = requireNative('assert');"
[email protected]f8d87d32013-06-06 02:51:2981 "var Event = require('event_bindings').Event;"
82 "var eventNatives = requireNative('event_natives');"
[email protected]4f1633f2013-03-09 14:26:2483 "var myEvent = new Event('named-event');"
[email protected]11844fa2012-05-10 00:35:5984 "var cb1 = function() {};"
85 "var cb2 = function() {};"
86 "myEvent.addListener(cb1);"
87 "myEvent.addListener(cb2);"
88 "myEvent.removeListener(cb1);"
[email protected]f8d87d32013-06-06 02:51:2989 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);"
[email protected]11844fa2012-05-10 00:35:5990 "myEvent.removeListener(cb2);"
[email protected]f8d87d32013-06-06 02:51:2991 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
[email protected]d9f51dad2014-07-09 05:39:3892 env()->module_system()->Require("test");
[email protected]11844fa2012-05-10 00:35:5993}
94
[email protected]d9e559d2012-07-05 01:04:5795TEST_F(EventUnittest, OnUnloadDetachesAllListeners) {
[email protected]2a356872014-02-21 23:18:5296 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:3897 env()->module_system());
98 env()->RegisterModule(
99 "test",
[email protected]d9e559d2012-07-05 01:04:57100 "var assert = requireNative('assert');"
[email protected]f8d87d32013-06-06 02:51:29101 "var Event = require('event_bindings').Event;"
102 "var eventNatives = requireNative('event_natives');"
[email protected]4f1633f2013-03-09 14:26:24103 "var myEvent = new Event('named-event');"
[email protected]d9e559d2012-07-05 01:04:57104 "var cb1 = function() {};"
105 "var cb2 = function() {};"
106 "myEvent.addListener(cb1);"
107 "myEvent.addListener(cb2);"
[email protected]68e63ea12013-06-05 05:00:54108 "require('unload_event').dispatch();"
[email protected]f8d87d32013-06-06 02:51:29109 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
[email protected]d9f51dad2014-07-09 05:39:38110 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57111}
112
113TEST_F(EventUnittest, OnUnloadDetachesAllListenersEvenDupes) {
[email protected]2a356872014-02-21 23:18:52114 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38115 env()->module_system());
116 env()->RegisterModule(
117 "test",
[email protected]d9e559d2012-07-05 01:04:57118 "var assert = requireNative('assert');"
[email protected]f8d87d32013-06-06 02:51:29119 "var Event = require('event_bindings').Event;"
120 "var eventNatives = requireNative('event_natives');"
[email protected]4f1633f2013-03-09 14:26:24121 "var myEvent = new Event('named-event');"
[email protected]d9e559d2012-07-05 01:04:57122 "var cb1 = function() {};"
123 "myEvent.addListener(cb1);"
124 "myEvent.addListener(cb1);"
[email protected]68e63ea12013-06-05 05:00:54125 "require('unload_event').dispatch();"
[email protected]f8d87d32013-06-06 02:51:29126 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);");
[email protected]d9f51dad2014-07-09 05:39:38127 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57128}
129
[email protected]11844fa2012-05-10 00:35:59130TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) {
[email protected]2a356872014-02-21 23:18:52131 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38132 env()->module_system());
133 env()->RegisterModule(
134 "test",
[email protected]f8d87d32013-06-06 02:51:29135 "var Event = require('event_bindings').Event;"
[email protected]11844fa2012-05-10 00:35:59136 "var eventOpts = {supportsRules: true};"
137 "var assert = requireNative('assert');"
138 "var caught = false;"
139 "try {"
[email protected]4f1633f2013-03-09 14:26:24140 " var myEvent = new Event(undefined, undefined, eventOpts);"
[email protected]11844fa2012-05-10 00:35:59141 "} catch (e) {"
142 " caught = true;"
143 "}"
144 "assert.AssertTrue(caught);");
[email protected]d9f51dad2014-07-09 05:39:38145 env()->module_system()->Require("test");
[email protected]11844fa2012-05-10 00:35:59146}
147
148TEST_F(EventUnittest, NamedEventDispatch) {
[email protected]2a356872014-02-21 23:18:52149 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38150 env()->module_system());
151 env()->RegisterModule(
152 "test",
[email protected]f8d87d32013-06-06 02:51:29153 "var Event = require('event_bindings').Event;"
154 "var dispatchEvent = require('event_bindings').dispatchEvent;"
[email protected]11844fa2012-05-10 00:35:59155 "var assert = requireNative('assert');"
[email protected]4f1633f2013-03-09 14:26:24156 "var e = new Event('myevent');"
[email protected]11844fa2012-05-10 00:35:59157 "var called = false;"
158 "e.addListener(function() { called = true; });"
[email protected]f8d87d32013-06-06 02:51:29159 "dispatchEvent('myevent', []);"
[email protected]11844fa2012-05-10 00:35:59160 "assert.AssertTrue(called);");
[email protected]d9f51dad2014-07-09 05:39:38161 env()->module_system()->Require("test");
[email protected]11844fa2012-05-10 00:35:59162}
163
[email protected]d9e559d2012-07-05 01:04:57164TEST_F(EventUnittest, AddListenerWithFiltersThrowsErrorByDefault) {
[email protected]2a356872014-02-21 23:18:52165 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38166 env()->module_system());
167 env()->RegisterModule("test",
168 "var Event = require('event_bindings').Event;"
169 "var assert = requireNative('assert');"
170 "var e = new Event('myevent');"
171 "var filter = [{"
172 " url: {hostSuffix: 'google.com'},"
173 "}];"
174 "var caught = false;"
175 "try {"
176 " e.addListener(function() {}, filter);"
177 "} catch (e) {"
178 " caught = true;"
179 "}"
180 "assert.AssertTrue(caught);");
181 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57182}
183
184TEST_F(EventUnittest, FilteredEventsAttachment) {
[email protected]2a356872014-02-21 23:18:52185 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38186 env()->module_system());
187 env()->RegisterModule(
188 "test",
[email protected]f8d87d32013-06-06 02:51:29189 "var Event = require('event_bindings').Event;"
[email protected]d9e559d2012-07-05 01:04:57190 "var assert = requireNative('assert');"
[email protected]f8d87d32013-06-06 02:51:29191 "var bindings = requireNative('event_natives');"
[email protected]d9e559d2012-07-05 01:04:57192 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
[email protected]4f1633f2013-03-09 14:26:24193 "var e = new Event('myevent', undefined, eventOpts);"
[email protected]d9e559d2012-07-05 01:04:57194 "var cb = function() {};"
195 "var filters = {url: [{hostSuffix: 'google.com'}]};"
196 "e.addListener(cb, filters);"
197 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
198 "e.removeListener(cb);"
199 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
[email protected]d9f51dad2014-07-09 05:39:38200 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57201}
202
203TEST_F(EventUnittest, DetachFilteredEvent) {
[email protected]2a356872014-02-21 23:18:52204 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38205 env()->module_system());
206 env()->RegisterModule(
207 "test",
[email protected]f8d87d32013-06-06 02:51:29208 "var Event = require('event_bindings').Event;"
[email protected]d9e559d2012-07-05 01:04:57209 "var assert = requireNative('assert');"
[email protected]f8d87d32013-06-06 02:51:29210 "var bindings = requireNative('event_natives');"
[email protected]d9e559d2012-07-05 01:04:57211 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
[email protected]4f1633f2013-03-09 14:26:24212 "var e = new Event('myevent', undefined, eventOpts);"
[email protected]d9e559d2012-07-05 01:04:57213 "var cb1 = function() {};"
214 "var cb2 = function() {};"
215 "var filters = {url: [{hostSuffix: 'google.com'}]};"
216 "e.addListener(cb1, filters);"
217 "e.addListener(cb2, filters);"
[email protected]fe35d54c2014-01-29 01:54:54218 "privates(e).impl.detach_();"
[email protected]d9e559d2012-07-05 01:04:57219 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
[email protected]d9f51dad2014-07-09 05:39:38220 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57221}
222
223TEST_F(EventUnittest, AttachAndRemoveSameFilteredEventListener) {
[email protected]2a356872014-02-21 23:18:52224 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38225 env()->module_system());
226 env()->RegisterModule(
227 "test",
[email protected]f8d87d32013-06-06 02:51:29228 "var Event = require('event_bindings').Event;"
[email protected]d9e559d2012-07-05 01:04:57229 "var assert = requireNative('assert');"
[email protected]f8d87d32013-06-06 02:51:29230 "var bindings = requireNative('event_natives');"
[email protected]d9e559d2012-07-05 01:04:57231 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
[email protected]4f1633f2013-03-09 14:26:24232 "var e = new Event('myevent', undefined, eventOpts);"
[email protected]d9e559d2012-07-05 01:04:57233 "var cb = function() {};"
234 "var filters = {url: [{hostSuffix: 'google.com'}]};"
235 "e.addListener(cb, filters);"
236 "e.addListener(cb, filters);"
237 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
238 "e.removeListener(cb);"
239 "assert.AssertTrue(bindings.HasFilteredListener('myevent'));"
240 "e.removeListener(cb);"
241 "assert.AssertFalse(bindings.HasFilteredListener('myevent'));");
[email protected]d9f51dad2014-07-09 05:39:38242 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57243}
244
245TEST_F(EventUnittest, AddingFilterWithUrlFieldNotAListThrowsException) {
[email protected]2a356872014-02-21 23:18:52246 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38247 env()->module_system());
248 env()->RegisterModule(
249 "test",
[email protected]f8d87d32013-06-06 02:51:29250 "var Event = require('event_bindings').Event;"
[email protected]d9e559d2012-07-05 01:04:57251 "var assert = requireNative('assert');"
252 "var eventOpts = {supportsListeners: true, supportsFilters: true};"
[email protected]4f1633f2013-03-09 14:26:24253 "var e = new Event('myevent', undefined, eventOpts);"
[email protected]d9e559d2012-07-05 01:04:57254 "var cb = function() {};"
255 "var filters = {url: {hostSuffix: 'google.com'}};"
256 "var caught = false;"
257 "try {"
258 " e.addListener(cb, filters);"
259 "} catch (e) {"
260 " caught = true;"
261 "}"
262 "assert.AssertTrue(caught);");
[email protected]d9f51dad2014-07-09 05:39:38263 env()->module_system()->Require("test");
[email protected]d9e559d2012-07-05 01:04:57264}
265
[email protected]452b36f2012-07-12 05:27:54266TEST_F(EventUnittest, MaxListeners) {
[email protected]2a356872014-02-21 23:18:52267 ModuleSystem::NativesEnabledScope natives_enabled_scope(
[email protected]d9f51dad2014-07-09 05:39:38268 env()->module_system());
269 env()->RegisterModule(
270 "test",
[email protected]f8d87d32013-06-06 02:51:29271 "var Event = require('event_bindings').Event;"
[email protected]452b36f2012-07-12 05:27:54272 "var assert = requireNative('assert');"
273 "var eventOpts = {supportsListeners: true, maxListeners: 1};"
[email protected]4f1633f2013-03-09 14:26:24274 "var e = new Event('myevent', undefined, eventOpts);"
[email protected]452b36f2012-07-12 05:27:54275 "var cb = function() {};"
276 "var caught = false;"
277 "try {"
278 " e.addListener(cb);"
279 "} catch (e) {"
280 " caught = true;"
281 "}"
282 "assert.AssertTrue(!caught);"
283 "try {"
284 " e.addListener(cb);"
285 "} catch (e) {"
286 " caught = true;"
287 "}"
288 "assert.AssertTrue(caught);");
[email protected]d9f51dad2014-07-09 05:39:38289 env()->module_system()->Require("test");
[email protected]452b36f2012-07-12 05:27:54290}
291
[email protected]11844fa2012-05-10 00:35:59292} // namespace
[email protected]8fe74bf2012-08-07 21:08:42293} // namespace extensions