Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: extensions/renderer/event_unittest.cc

Issue 1417513003: [Extensions] Don't allow built-in extensions code to be overridden (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Jochen's Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/api_test_base.cc ('k') | extensions/renderer/module_system.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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("utils", IDR_UTILS_JS); 21 env()->RegisterModule("utils", IDR_UTILS_JS);
22 22
23 // 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
24 // any invariants maintained by the real event_bindings are broken. 24 // any invariants maintained by the real event_bindings are broken.
25 env()->OverrideNativeHandler( 25 env()->OverrideNativeHandler(
26 "event_natives", 26 "event_natives",
27 "var assert = requireNative('assert');" 27 "var assert = requireNative('assert');"
28 "var attachedListeners = exports.attachedListeners = {};" 28 "exports.$set('attachedListeners', {});"
29 "var attachedFilteredListeners = " 29 "var attachedListeners = exports.attachedListeners;"
30 " exports.attachedFilteredListeners = {};" 30 "exports.$set('attachedFilteredListeners', {});"
31 "var attachedFilteredListeners = exports.attachedFilteredListeners;"
31 "var nextId = 0;" 32 "var nextId = 0;"
32 "var idToName = {};" 33 "var idToName = {};"
33 "exports.AttachEvent = function(eventName) {" 34 "exports.$set('AttachEvent', function(eventName) {"
34 " assert.AssertFalse(!!attachedListeners[eventName]);" 35 " assert.AssertFalse(!!attachedListeners[eventName]);"
35 " attachedListeners[eventName] = 1;" 36 " attachedListeners[eventName] = 1;"
36 "};" 37 "});"
37 "exports.DetachEvent = function(eventName) {" 38 "exports.$set('DetachEvent', function(eventName) {"
38 " assert.AssertTrue(!!attachedListeners[eventName]);" 39 " assert.AssertTrue(!!attachedListeners[eventName]);"
39 " delete attachedListeners[eventName];" 40 " delete attachedListeners[eventName];"
40 "};" 41 "});"
41 "exports.IsEventAttached = function(eventName) {" 42 "exports.$set('IsEventAttached', function(eventName) {"
42 " return !!attachedListeners[eventName];" 43 " return !!attachedListeners[eventName];"
43 "};" 44 "});"
44 "exports.AttachFilteredEvent = function(name, filters) {" 45 "exports.$set('AttachFilteredEvent', function(name, filters) {"
45 " var id = nextId++;" 46 " var id = nextId++;"
46 " idToName[id] = name;" 47 " idToName[id] = name;"
47 " attachedFilteredListeners[name] =" 48 " attachedFilteredListeners[name] ="
48 " attachedFilteredListeners[name] || [];" 49 " attachedFilteredListeners[name] || [];"
49 " attachedFilteredListeners[name][id] = filters;" 50 " attachedFilteredListeners[name][id] = filters;"
50 " return id;" 51 " return id;"
51 "};" 52 "});"
52 "exports.DetachFilteredEvent = function(id, manual) {" 53 "exports.$set('DetachFilteredEvent', function(id, manual) {"
53 " var i = attachedFilteredListeners[idToName[id]].indexOf(id);" 54 " var i = attachedFilteredListeners[idToName[id]].indexOf(id);"
54 " attachedFilteredListeners[idToName[id]].splice(i, 1);" 55 " attachedFilteredListeners[idToName[id]].splice(i, 1);"
55 "};" 56 "});"
56 "exports.HasFilteredListener = function(name) {" 57 "exports.$set('HasFilteredListener', function(name) {"
57 " return attachedFilteredListeners[name].length;" 58 " return attachedFilteredListeners[name].length;"
58 "};"); 59 "});");
59 env()->OverrideNativeHandler("sendRequest", 60 env()->OverrideNativeHandler("sendRequest",
60 "exports.sendRequest = function() {};"); 61 "exports.$set('sendRequest', function() {});");
61 env()->OverrideNativeHandler( 62 env()->OverrideNativeHandler(
62 "apiDefinitions", 63 "apiDefinitions",
63 "exports.GetExtensionAPIDefinitionsForTest = function() {};"); 64 "exports.$set('GetExtensionAPIDefinitionsForTest', function() {});");
64 env()->OverrideNativeHandler("logging", "exports.DCHECK = function() {};"); 65 env()->OverrideNativeHandler("logging",
66 "exports.$set('DCHECK', function() {});");
65 env()->OverrideNativeHandler("schema_registry", 67 env()->OverrideNativeHandler("schema_registry",
66 "exports.GetSchema = function() {};"); 68 "exports.$set('GetSchema', function() {});");
67 } 69 }
68 }; 70 };
69 71
70 TEST_F(EventUnittest, TestNothing) { 72 TEST_F(EventUnittest, TestNothing) {
71 ExpectNoAssertionsMade(); 73 ExpectNoAssertionsMade();
72 } 74 }
73 75
74 TEST_F(EventUnittest, AddRemoveTwoListeners) { 76 TEST_F(EventUnittest, AddRemoveTwoListeners) {
75 ModuleSystem::NativesEnabledScope natives_enabled_scope( 77 ModuleSystem::NativesEnabledScope natives_enabled_scope(
76 env()->module_system()); 78 env()->module_system());
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 " e.addListener(cb);" 250 " e.addListener(cb);"
249 "} catch (e) {" 251 "} catch (e) {"
250 " caught = true;" 252 " caught = true;"
251 "}" 253 "}"
252 "assert.AssertTrue(caught);"); 254 "assert.AssertTrue(caught);");
253 env()->module_system()->Require("test"); 255 env()->module_system()->Require("test");
254 } 256 }
255 257
256 } // namespace 258 } // namespace
257 } // namespace extensions 259 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_test_base.cc ('k') | extensions/renderer/module_system.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698