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

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

Issue 2768093002: [Reland][Extensions Bindings] Add support for filtered events (Closed)
Patch Set: Fix Created 3 years, 9 months 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/renderer/native_extension_bindings_system.h" 5 #include "extensions/renderer/native_extension_bindings_system.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "components/crx_file/id_util.h" 10 #include "components/crx_file/id_util.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 61
62 return ExtensionBuilder() 62 return ExtensionBuilder()
63 .SetManifest(manifest.Build()) 63 .SetManifest(manifest.Build())
64 .SetLocation(Manifest::INTERNAL) 64 .SetLocation(Manifest::INTERNAL)
65 .SetID(crx_file::id_util::GenerateId(name)) 65 .SetID(crx_file::id_util::GenerateId(name))
66 .Build(); 66 .Build();
67 } 67 }
68 68
69 class EventChangeHandler { 69 class EventChangeHandler {
70 public: 70 public:
71 MOCK_METHOD3(OnChange, 71 MOCK_METHOD4(OnChange,
72 void(binding::EventListenersChanged, 72 void(binding::EventListenersChanged,
73 ScriptContext*, 73 ScriptContext*,
74 const std::string& event_name)); 74 const std::string& event_name,
75 const base::DictionaryValue* filter));
75 }; 76 };
76 77
77 } // namespace 78 } // namespace
78 79
79 class NativeExtensionBindingsSystemUnittest : public APIBindingTest { 80 class NativeExtensionBindingsSystemUnittest : public APIBindingTest {
80 public: 81 public:
81 NativeExtensionBindingsSystemUnittest() {} 82 NativeExtensionBindingsSystemUnittest() {}
82 ~NativeExtensionBindingsSystemUnittest() override {} 83 ~NativeExtensionBindingsSystemUnittest() override {}
83 84
84 protected: 85 protected:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 last_params_.source_url = params.source_url; 124 last_params_.source_url = params.source_url;
124 last_params_.request_id = params.request_id; 125 last_params_.request_id = params.request_id;
125 last_params_.has_callback = params.has_callback; 126 last_params_.has_callback = params.has_callback;
126 last_params_.user_gesture = params.user_gesture; 127 last_params_.user_gesture = params.user_gesture;
127 last_params_.worker_thread_id = params.worker_thread_id; 128 last_params_.worker_thread_id = params.worker_thread_id;
128 last_params_.service_worker_version_id = params.service_worker_version_id; 129 last_params_.service_worker_version_id = params.service_worker_version_id;
129 } 130 }
130 131
131 void MockSendListenerIPC(binding::EventListenersChanged changed, 132 void MockSendListenerIPC(binding::EventListenersChanged changed,
132 ScriptContext* context, 133 ScriptContext* context,
133 const std::string& event_name) { 134 const std::string& event_name,
135 const base::DictionaryValue* filter) {
134 if (event_change_handler_) 136 if (event_change_handler_)
135 event_change_handler_->OnChange(changed, context, event_name); 137 event_change_handler_->OnChange(changed, context, event_name, filter);
136 } 138 }
137 139
138 ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context, 140 ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context,
139 Extension* extension, 141 Extension* extension,
140 Feature::Context context_type) { 142 Feature::Context context_type) {
141 auto script_context = base::MakeUnique<ScriptContext>( 143 auto script_context = base::MakeUnique<ScriptContext>(
142 v8_context, nullptr, extension, context_type, extension, context_type); 144 v8_context, nullptr, extension, context_type, extension, context_type);
143 script_context->set_module_system( 145 script_context->set_module_system(
144 base::MakeUnique<ModuleSystem>(script_context.get(), source_map())); 146 base::MakeUnique<ModuleSystem>(script_context.get(), source_map()));
145 ScriptContext* raw_script_context = script_context.get(); 147 ScriptContext* raw_script_context = script_context.get();
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 // Add a new event listener. We should be notified of the change. 562 // Add a new event listener. We should be notified of the change.
561 const char kEventName[] = "idle.onStateChanged"; 563 const char kEventName[] = "idle.onStateChanged";
562 v8::Local<v8::Function> listener = 564 v8::Local<v8::Function> listener =
563 FunctionFromString(context, "(function() {})"); 565 FunctionFromString(context, "(function() {})");
564 const char kAddListener[] = 566 const char kAddListener[] =
565 "(function(listener) {\n" 567 "(function(listener) {\n"
566 " chrome.idle.onStateChanged.addListener(listener);\n" 568 " chrome.idle.onStateChanged.addListener(listener);\n"
567 "});"; 569 "});";
568 v8::Local<v8::Function> add_listener = 570 v8::Local<v8::Function> add_listener =
569 FunctionFromString(context, kAddListener); 571 FunctionFromString(context, kAddListener);
570 EXPECT_CALL( 572 EXPECT_CALL(*event_change_handler(),
571 *event_change_handler(), 573 OnChange(binding::EventListenersChanged::HAS_LISTENERS,
572 OnChange(binding::EventListenersChanged::HAS_LISTENERS, 574 script_context, kEventName, nullptr))
573 script_context, 575 .Times(1);
574 kEventName)).Times(1);
575 v8::Local<v8::Value> argv[] = {listener}; 576 v8::Local<v8::Value> argv[] = {listener};
576 RunFunction(add_listener, context, arraysize(argv), argv); 577 RunFunction(add_listener, context, arraysize(argv), argv);
577 ::testing::Mock::VerifyAndClearExpectations(event_change_handler()); 578 ::testing::Mock::VerifyAndClearExpectations(event_change_handler());
578 579
579 // Remove the event listener. We should be notified again. 580 // Remove the event listener. We should be notified again.
580 const char kRemoveListener[] = 581 const char kRemoveListener[] =
581 "(function(listener) {\n" 582 "(function(listener) {\n"
582 " chrome.idle.onStateChanged.removeListener(listener);\n" 583 " chrome.idle.onStateChanged.removeListener(listener);\n"
583 "});"; 584 "});";
584 EXPECT_CALL( 585 EXPECT_CALL(*event_change_handler(),
585 *event_change_handler(), 586 OnChange(binding::EventListenersChanged::NO_LISTENERS,
586 OnChange(binding::EventListenersChanged::NO_LISTENERS, 587 script_context, kEventName, nullptr))
587 script_context, 588 .Times(1);
588 kEventName)).Times(1);
589 v8::Local<v8::Function> remove_listener = 589 v8::Local<v8::Function> remove_listener =
590 FunctionFromString(context, kRemoveListener); 590 FunctionFromString(context, kRemoveListener);
591 RunFunction(remove_listener, context, arraysize(argv), argv); 591 RunFunction(remove_listener, context, arraysize(argv), argv);
592 ::testing::Mock::VerifyAndClearExpectations(event_change_handler()); 592 ::testing::Mock::VerifyAndClearExpectations(event_change_handler());
593 } 593 }
594 594
595 TEST_F(NativeExtensionBindingsSystemUnittest, 595 TEST_F(NativeExtensionBindingsSystemUnittest,
596 TestPrefixedApiEventsAndAppBinding) { 596 TestPrefixedApiEventsAndAppBinding) {
597 InitEventChangeHandler(); 597 InitEventChangeHandler();
598 scoped_refptr<Extension> app = CreateExtension("foo", ItemType::PLATFORM_APP, 598 scoped_refptr<Extension> app = CreateExtension("foo", ItemType::PLATFORM_APP,
(...skipping 21 matching lines...) Expand all
620 gin::V8ToString(app_binding_keys)); 620 gin::V8ToString(app_binding_keys));
621 621
622 const char kUseAppRuntime[] = 622 const char kUseAppRuntime[] =
623 "(function() {\n" 623 "(function() {\n"
624 " chrome.app.runtime.onLaunched.addListener(function() {});\n" 624 " chrome.app.runtime.onLaunched.addListener(function() {});\n"
625 "});"; 625 "});";
626 v8::Local<v8::Function> use_app_runtime = 626 v8::Local<v8::Function> use_app_runtime =
627 FunctionFromString(context, kUseAppRuntime); 627 FunctionFromString(context, kUseAppRuntime);
628 EXPECT_CALL(*event_change_handler(), 628 EXPECT_CALL(*event_change_handler(),
629 OnChange(binding::EventListenersChanged::HAS_LISTENERS, 629 OnChange(binding::EventListenersChanged::HAS_LISTENERS,
630 script_context, "app.runtime.onLaunched")) 630 script_context, "app.runtime.onLaunched", nullptr))
631 .Times(1); 631 .Times(1);
632 RunFunctionOnGlobal(use_app_runtime, context, 0, nullptr); 632 RunFunctionOnGlobal(use_app_runtime, context, 0, nullptr);
633 ::testing::Mock::VerifyAndClearExpectations(event_change_handler()); 633 ::testing::Mock::VerifyAndClearExpectations(event_change_handler());
634 } 634 }
635 635
636 TEST_F(NativeExtensionBindingsSystemUnittest, 636 TEST_F(NativeExtensionBindingsSystemUnittest,
637 TestPrefixedApiMethodsAndSystemBinding) { 637 TestPrefixedApiMethodsAndSystemBinding) {
638 scoped_refptr<Extension> extension = 638 scoped_refptr<Extension> extension =
639 CreateExtension("foo", ItemType::EXTENSION, {"system.cpu"}); 639 CreateExtension("foo", ItemType::EXTENSION, {"system.cpu"});
640 RegisterExtension(extension->id()); 640 RegisterExtension(extension->id());
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 EXPECT_FALSE(value_b.IsEmpty()) << property; 781 EXPECT_FALSE(value_b.IsEmpty()) << property;
782 EXPECT_NE(value_a, value_b) << property; 782 EXPECT_NE(value_a, value_b) << property;
783 }; 783 };
784 784
785 check_properties_inequal(context_a, context_b, "chrome"); 785 check_properties_inequal(context_a, context_b, "chrome");
786 check_properties_inequal(context_a, context_b, "chrome.idle"); 786 check_properties_inequal(context_a, context_b, "chrome.idle");
787 check_properties_inequal(context_a, context_b, "chrome.idle.onStateChanged"); 787 check_properties_inequal(context_a, context_b, "chrome.idle.onStateChanged");
788 } 788 }
789 789
790 } // namespace extensions 790 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/native_extension_bindings_system.cc ('k') | extensions/renderer/resources/guest_view/guest_view_events.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698