diff options
author | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-30 18:28:39 +0000 |
---|---|---|
committer | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-30 18:28:39 +0000 |
commit | c1abb3237596e83cc12639d5b9e7607b67ae77f2 (patch) | |
tree | 4696ac8ad2d52243324f89fd2f238669785541e2 /extensions/browser/event_router_unittest.cc | |
parent | 056fb8baa877888f0996eb5283721f1dbf2b5fb4 (diff) | |
download | chromium_src-c1abb3237596e83cc12639d5b9e7607b67ae77f2.zip chromium_src-c1abb3237596e83cc12639d5b9e7607b67ae77f2.tar.gz chromium_src-c1abb3237596e83cc12639d5b9e7607b67ae77f2.tar.bz2 |
Allow restricting WebUI-enabled extension APIs to URL patterns.
BUG=391944
R=rockot@chromium.org, jschuh@chromium.org
Review URL: https://codereview.chromium.org/422433005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@286564 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/browser/event_router_unittest.cc')
-rw-r--r-- | extensions/browser/event_router_unittest.cc | 70 |
1 files changed, 54 insertions, 16 deletions
diff --git a/extensions/browser/event_router_unittest.cc b/extensions/browser/event_router_unittest.cc index c47b7f2..aa068ce 100644 --- a/extensions/browser/event_router_unittest.cc +++ b/extensions/browser/event_router_unittest.cc @@ -6,6 +6,7 @@ #include <string> +#include "base/bind.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "base/values.h" @@ -53,9 +54,37 @@ class MockEventRouterObserver : public EventRouter::Observer { DISALLOW_COPY_AND_ASSIGN(MockEventRouterObserver); }; +typedef base::Callback<scoped_ptr<EventListener>( + const std::string&, // event_name + content::RenderProcessHost*, // process + base::DictionaryValue* // filter (takes ownership) + )> EventListenerConstructor; + +scoped_ptr<EventListener> CreateEventListenerForExtension( + const std::string& extension_id, + const std::string& event_name, + content::RenderProcessHost* process, + base::DictionaryValue* filter) { + return EventListener::ForExtension( + event_name, extension_id, process, make_scoped_ptr(filter)); +} + +scoped_ptr<EventListener> CreateEventListenerForURL( + const GURL& listener_url, + const std::string& event_name, + content::RenderProcessHost* process, + base::DictionaryValue* filter) { + return EventListener::ForURL( + event_name, listener_url, process, make_scoped_ptr(filter)); +} + } // namespace -typedef testing::Test EventRouterTest; +class EventRouterTest : public testing::Test { + protected: + // Tests adding and removing observers from EventRouter. + void RunEventRouterObserverTest(const EventListenerConstructor& constructor); +}; TEST_F(EventRouterTest, GetBaseEventName) { // Normal event names are passed through unchanged. @@ -66,14 +95,15 @@ TEST_F(EventRouterTest, GetBaseEventName) { } // Tests adding and removing observers from EventRouter. -TEST_F(EventRouterTest, EventRouterObserver) { +void EventRouterTest::RunEventRouterObserverTest( + const EventListenerConstructor& constructor) { EventRouter router(NULL, NULL); - EventListener listener( - "event_name", "extension_id", NULL, scoped_ptr<base::DictionaryValue>()); + scoped_ptr<EventListener> listener = + constructor.Run("event_name", NULL, new base::DictionaryValue()); // Add/remove works without any observers. - router.OnListenerAdded(&listener); - router.OnListenerRemoved(&listener); + router.OnListenerAdded(listener.get()); + router.OnListenerRemoved(listener.get()); // Register observers that both match and don't match the event above. MockEventRouterObserver matching_observer; @@ -82,43 +112,51 @@ TEST_F(EventRouterTest, EventRouterObserver) { router.RegisterObserver(&non_matching_observer, "other"); // Adding a listener notifies the appropriate observers. - router.OnListenerAdded(&listener); + router.OnListenerAdded(listener.get()); EXPECT_EQ(1, matching_observer.listener_added_count()); EXPECT_EQ(0, non_matching_observer.listener_added_count()); // Removing a listener notifies the appropriate observers. - router.OnListenerRemoved(&listener); + router.OnListenerRemoved(listener.get()); EXPECT_EQ(1, matching_observer.listener_removed_count()); EXPECT_EQ(0, non_matching_observer.listener_removed_count()); // Adding the listener again notifies again. - router.OnListenerAdded(&listener); + router.OnListenerAdded(listener.get()); EXPECT_EQ(2, matching_observer.listener_added_count()); EXPECT_EQ(0, non_matching_observer.listener_added_count()); // Removing the listener again notifies again. - router.OnListenerRemoved(&listener); + router.OnListenerRemoved(listener.get()); EXPECT_EQ(2, matching_observer.listener_removed_count()); EXPECT_EQ(0, non_matching_observer.listener_removed_count()); // Adding a listener with a sub-event notifies the main observer with // proper details. matching_observer.Reset(); - EventListener sub_event_listener("event_name/1", - "extension_id", - NULL, - scoped_ptr<base::DictionaryValue>()); - router.OnListenerAdded(&sub_event_listener); + scoped_ptr<EventListener> sub_event_listener = + constructor.Run("event_name/1", NULL, new base::DictionaryValue()); + router.OnListenerAdded(sub_event_listener.get()); EXPECT_EQ(1, matching_observer.listener_added_count()); EXPECT_EQ(0, matching_observer.listener_removed_count()); EXPECT_EQ("event_name/1", matching_observer.last_event_name()); // Ditto for removing the listener. matching_observer.Reset(); - router.OnListenerRemoved(&sub_event_listener); + router.OnListenerRemoved(sub_event_listener.get()); EXPECT_EQ(0, matching_observer.listener_added_count()); EXPECT_EQ(1, matching_observer.listener_removed_count()); EXPECT_EQ("event_name/1", matching_observer.last_event_name()); } +TEST_F(EventRouterTest, EventRouterObserverForExtensions) { + RunEventRouterObserverTest( + base::Bind(&CreateEventListenerForExtension, "extension_id")); +} + +TEST_F(EventRouterTest, EventRouterObserverForURLs) { + RunEventRouterObserverTest( + base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); +} + } // namespace extensions |