OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/extension_urls.h" | 5 #include "extensions/common/extension_urls.h" |
6 #include "extensions/renderer/module_system_test.h" | 6 #include "extensions/renderer/module_system_test.h" |
7 #include "grit/extensions_renderer_resources.h" | 7 #include "grit/extensions_renderer_resources.h" |
8 | 8 |
9 namespace extensions { | 9 namespace extensions { |
10 namespace { | 10 namespace { |
11 | 11 |
12 class EventUnittest : public ModuleSystemTest { | 12 class EventUnittest : public ModuleSystemTest { |
13 void SetUp() override { | 13 void SetUp() override { |
14 ModuleSystemTest::SetUp(); | 14 ModuleSystemTest::SetUp(); |
15 | 15 |
16 env()->RegisterModule(kEventBindings, IDR_EVENT_BINDINGS_JS); | 16 env()->RegisterModule(kEventBindings, IDR_EVENT_BINDINGS_JS); |
17 env()->RegisterModule("json_schema", IDR_JSON_SCHEMA_JS); | 17 env()->RegisterModule("json_schema", IDR_JSON_SCHEMA_JS); |
18 env()->RegisterModule(kSchemaUtils, IDR_SCHEMA_UTILS_JS); | 18 env()->RegisterModule(kSchemaUtils, IDR_SCHEMA_UTILS_JS); |
19 env()->RegisterModule("uncaught_exception_handler", | 19 env()->RegisterModule("uncaught_exception_handler", |
20 IDR_UNCAUGHT_EXCEPTION_HANDLER_JS); | 20 IDR_UNCAUGHT_EXCEPTION_HANDLER_JS); |
21 env()->RegisterModule("unload_event", IDR_UNLOAD_EVENT_JS); | |
22 env()->RegisterModule("utils", IDR_UTILS_JS); | 21 env()->RegisterModule("utils", IDR_UTILS_JS); |
23 | 22 |
24 // Mock out the native handler for event_bindings. These mocks will fail if | 23 // Mock out the native handler for event_bindings. These mocks will fail if |
25 // any invariants maintained by the real event_bindings are broken. | 24 // any invariants maintained by the real event_bindings are broken. |
26 env()->OverrideNativeHandler( | 25 env()->OverrideNativeHandler( |
27 "event_natives", | 26 "event_natives", |
28 "var assert = requireNative('assert');" | 27 "var assert = requireNative('assert');" |
29 "var attachedListeners = exports.attachedListeners = {};" | 28 "var attachedListeners = exports.attachedListeners = {};" |
30 "var attachedFilteredListeners = " | 29 "var attachedFilteredListeners = " |
31 " exports.attachedFilteredListeners = {};" | 30 " exports.attachedFilteredListeners = {};" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 "var cb2 = function() {};" | 84 "var cb2 = function() {};" |
86 "myEvent.addListener(cb1);" | 85 "myEvent.addListener(cb1);" |
87 "myEvent.addListener(cb2);" | 86 "myEvent.addListener(cb2);" |
88 "myEvent.removeListener(cb1);" | 87 "myEvent.removeListener(cb1);" |
89 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);" | 88 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);" |
90 "myEvent.removeListener(cb2);" | 89 "myEvent.removeListener(cb2);" |
91 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);"); | 90 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);"); |
92 env()->module_system()->Require("test"); | 91 env()->module_system()->Require("test"); |
93 } | 92 } |
94 | 93 |
95 TEST_F(EventUnittest, OnUnloadDetachesAllListeners) { | |
96 ModuleSystem::NativesEnabledScope natives_enabled_scope( | |
97 env()->module_system()); | |
98 env()->RegisterModule( | |
99 "test", | |
100 "var assert = requireNative('assert');" | |
101 "var Event = require('event_bindings').Event;" | |
102 "var eventNatives = requireNative('event_natives');" | |
103 "var myEvent = new Event('named-event');" | |
104 "var cb1 = function() {};" | |
105 "var cb2 = function() {};" | |
106 "myEvent.addListener(cb1);" | |
107 "myEvent.addListener(cb2);" | |
108 "require('unload_event').dispatch();" | |
109 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);"); | |
110 env()->module_system()->Require("test"); | |
111 } | |
112 | |
113 TEST_F(EventUnittest, OnUnloadDetachesAllListenersEvenDupes) { | |
114 ModuleSystem::NativesEnabledScope natives_enabled_scope( | |
115 env()->module_system()); | |
116 env()->RegisterModule( | |
117 "test", | |
118 "var assert = requireNative('assert');" | |
119 "var Event = require('event_bindings').Event;" | |
120 "var eventNatives = requireNative('event_natives');" | |
121 "var myEvent = new Event('named-event');" | |
122 "var cb1 = function() {};" | |
123 "myEvent.addListener(cb1);" | |
124 "myEvent.addListener(cb1);" | |
125 "require('unload_event').dispatch();" | |
126 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);"); | |
127 env()->module_system()->Require("test"); | |
128 } | |
129 | |
130 TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) { | 94 TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) { |
131 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 95 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
132 env()->module_system()); | 96 env()->module_system()); |
133 env()->RegisterModule( | 97 env()->RegisterModule( |
134 "test", | 98 "test", |
135 "var Event = require('event_bindings').Event;" | 99 "var Event = require('event_bindings').Event;" |
136 "var eventOpts = {supportsRules: true};" | 100 "var eventOpts = {supportsRules: true};" |
137 "var assert = requireNative('assert');" | 101 "var assert = requireNative('assert');" |
138 "var caught = false;" | 102 "var caught = false;" |
139 "try {" | 103 "try {" |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 " e.addListener(cb);" | 248 " e.addListener(cb);" |
285 "} catch (e) {" | 249 "} catch (e) {" |
286 " caught = true;" | 250 " caught = true;" |
287 "}" | 251 "}" |
288 "assert.AssertTrue(caught);"); | 252 "assert.AssertTrue(caught);"); |
289 env()->module_system()->Require("test"); | 253 env()->module_system()->Require("test"); |
290 } | 254 } |
291 | 255 |
292 } // namespace | 256 } // namespace |
293 } // namespace extensions | 257 } // namespace extensions |
OLD | NEW |