OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/event_router.h" | 5 #include "extensions/browser/event_router.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // Normal event names are passed through unchanged. | 101 // Normal event names are passed through unchanged. |
102 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 102 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
103 | 103 |
104 // Sub-events are converted to the part before the slash. | 104 // Sub-events are converted to the part before the slash. |
105 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 105 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); |
106 } | 106 } |
107 | 107 |
108 // Tests adding and removing observers from EventRouter. | 108 // Tests adding and removing observers from EventRouter. |
109 void EventRouterTest::RunEventRouterObserverTest( | 109 void EventRouterTest::RunEventRouterObserverTest( |
110 const EventListenerConstructor& constructor) { | 110 const EventListenerConstructor& constructor) { |
111 EventRouter router(NULL, NULL); | 111 EventRouter router(nullptr, nullptr); |
112 scoped_ptr<EventListener> listener = | 112 scoped_ptr<EventListener> listener = |
113 constructor.Run("event_name", NULL, new base::DictionaryValue()); | 113 constructor.Run("event_name", nullptr, new base::DictionaryValue()); |
114 | 114 |
115 // Add/remove works without any observers. | 115 // Add/remove works without any observers. |
116 router.OnListenerAdded(listener.get()); | 116 router.OnListenerAdded(listener.get()); |
117 router.OnListenerRemoved(listener.get()); | 117 router.OnListenerRemoved(listener.get()); |
118 | 118 |
119 // Register observers that both match and don't match the event above. | 119 // Register observers that both match and don't match the event above. |
120 MockEventRouterObserver matching_observer; | 120 MockEventRouterObserver matching_observer; |
121 router.RegisterObserver(&matching_observer, "event_name"); | 121 router.RegisterObserver(&matching_observer, "event_name"); |
122 MockEventRouterObserver non_matching_observer; | 122 MockEventRouterObserver non_matching_observer; |
123 router.RegisterObserver(&non_matching_observer, "other"); | 123 router.RegisterObserver(&non_matching_observer, "other"); |
(...skipping 15 matching lines...) Expand all Loading... |
139 | 139 |
140 // Removing the listener again notifies again. | 140 // Removing the listener again notifies again. |
141 router.OnListenerRemoved(listener.get()); | 141 router.OnListenerRemoved(listener.get()); |
142 EXPECT_EQ(2, matching_observer.listener_removed_count()); | 142 EXPECT_EQ(2, matching_observer.listener_removed_count()); |
143 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); | 143 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); |
144 | 144 |
145 // Adding a listener with a sub-event notifies the main observer with | 145 // Adding a listener with a sub-event notifies the main observer with |
146 // proper details. | 146 // proper details. |
147 matching_observer.Reset(); | 147 matching_observer.Reset(); |
148 scoped_ptr<EventListener> sub_event_listener = | 148 scoped_ptr<EventListener> sub_event_listener = |
149 constructor.Run("event_name/1", NULL, new base::DictionaryValue()); | 149 constructor.Run("event_name/1", nullptr, new base::DictionaryValue()); |
150 router.OnListenerAdded(sub_event_listener.get()); | 150 router.OnListenerAdded(sub_event_listener.get()); |
151 EXPECT_EQ(1, matching_observer.listener_added_count()); | 151 EXPECT_EQ(1, matching_observer.listener_added_count()); |
152 EXPECT_EQ(0, matching_observer.listener_removed_count()); | 152 EXPECT_EQ(0, matching_observer.listener_removed_count()); |
153 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 153 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
154 | 154 |
155 // Ditto for removing the listener. | 155 // Ditto for removing the listener. |
156 matching_observer.Reset(); | 156 matching_observer.Reset(); |
157 router.OnListenerRemoved(sub_event_listener.get()); | 157 router.OnListenerRemoved(sub_event_listener.get()); |
158 EXPECT_EQ(0, matching_observer.listener_added_count()); | 158 EXPECT_EQ(0, matching_observer.listener_added_count()); |
159 EXPECT_EQ(1, matching_observer.listener_removed_count()); | 159 EXPECT_EQ(1, matching_observer.listener_removed_count()); |
160 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 160 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
161 } | 161 } |
162 | 162 |
163 TEST_F(EventRouterTest, EventRouterObserverForExtensions) { | 163 TEST_F(EventRouterTest, EventRouterObserverForExtensions) { |
164 RunEventRouterObserverTest( | 164 RunEventRouterObserverTest( |
165 base::Bind(&CreateEventListenerForExtension, "extension_id")); | 165 base::Bind(&CreateEventListenerForExtension, "extension_id")); |
166 } | 166 } |
167 | 167 |
168 TEST_F(EventRouterTest, EventRouterObserverForURLs) { | 168 TEST_F(EventRouterTest, EventRouterObserverForURLs) { |
169 RunEventRouterObserverTest( | 169 RunEventRouterObserverTest( |
170 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); | 170 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); |
171 } | 171 } |
172 | 172 |
173 } // namespace extensions | 173 } // namespace extensions |
OLD | NEW |