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); |
21 env()->RegisterModule("utils", IDR_UTILS_JS); | 22 env()->RegisterModule("utils", IDR_UTILS_JS); |
22 | 23 |
23 // Mock out the native handler for event_bindings. These mocks will fail if | 24 // 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 // any invariants maintained by the real event_bindings are broken. |
25 env()->OverrideNativeHandler( | 26 env()->OverrideNativeHandler( |
26 "event_natives", | 27 "event_natives", |
27 "var assert = requireNative('assert');" | 28 "var assert = requireNative('assert');" |
28 "var attachedListeners = exports.attachedListeners = {};" | 29 "var attachedListeners = exports.attachedListeners = {};" |
29 "var attachedFilteredListeners = " | 30 "var attachedFilteredListeners = " |
30 " exports.attachedFilteredListeners = {};" | 31 " exports.attachedFilteredListeners = {};" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 "var cb1 = function() {};" | 84 "var cb1 = function() {};" |
84 "var cb2 = function() {};" | 85 "var cb2 = function() {};" |
85 "myEvent.addListener(cb1);" | 86 "myEvent.addListener(cb1);" |
86 "myEvent.addListener(cb2);" | 87 "myEvent.addListener(cb2);" |
87 "myEvent.removeListener(cb1);" | 88 "myEvent.removeListener(cb1);" |
88 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);" | 89 "assert.AssertTrue(!!eventNatives.attachedListeners['named-event']);" |
89 "myEvent.removeListener(cb2);" | 90 "myEvent.removeListener(cb2);" |
90 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);"); | 91 "assert.AssertFalse(!!eventNatives.attachedListeners['named-event']);"); |
91 env()->module_system()->Require("test"); | 92 env()->module_system()->Require("test"); |
92 } | 93 } |
| 94 |
| 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 } |
93 | 129 |
94 TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) { | 130 TEST_F(EventUnittest, EventsThatSupportRulesMustHaveAName) { |
95 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 131 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
96 env()->module_system()); | 132 env()->module_system()); |
97 env()->RegisterModule( | 133 env()->RegisterModule( |
98 "test", | 134 "test", |
99 "var Event = require('event_bindings').Event;" | 135 "var Event = require('event_bindings').Event;" |
100 "var eventOpts = {supportsRules: true};" | 136 "var eventOpts = {supportsRules: true};" |
101 "var assert = requireNative('assert');" | 137 "var assert = requireNative('assert');" |
102 "var caught = false;" | 138 "var caught = false;" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 " e.addListener(cb);" | 284 " e.addListener(cb);" |
249 "} catch (e) {" | 285 "} catch (e) {" |
250 " caught = true;" | 286 " caught = true;" |
251 "}" | 287 "}" |
252 "assert.AssertTrue(caught);"); | 288 "assert.AssertTrue(caught);"); |
253 env()->module_system()->Require("test"); | 289 env()->module_system()->Require("test"); |
254 } | 290 } |
255 | 291 |
256 } // namespace | 292 } // namespace |
257 } // namespace extensions | 293 } // namespace extensions |
OLD | NEW |