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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // Normal event names are passed through unchanged. | 61 // Normal event names are passed through unchanged. |
62 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 62 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
63 | 63 |
64 // Sub-events are converted to the part before the slash. | 64 // Sub-events are converted to the part before the slash. |
65 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 65 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); |
66 } | 66 } |
67 | 67 |
68 // Tests adding and removing observers from EventRouter. | 68 // Tests adding and removing observers from EventRouter. |
69 TEST_F(EventRouterTest, EventRouterObserver) { | 69 TEST_F(EventRouterTest, EventRouterObserver) { |
70 EventRouter router(NULL, NULL); | 70 EventRouter router(NULL, NULL); |
71 EventListener listener( | 71 EventListener listener("event_name", |
72 "event_name", "extension_id", NULL, scoped_ptr<base::DictionaryValue>()); | 72 "extension_id", |
| 73 GURL(), |
| 74 NULL, |
| 75 scoped_ptr<base::DictionaryValue>()); |
73 | 76 |
74 // Add/remove works without any observers. | 77 // Add/remove works without any observers. |
75 router.OnListenerAdded(&listener); | 78 router.OnListenerAdded(&listener); |
76 router.OnListenerRemoved(&listener); | 79 router.OnListenerRemoved(&listener); |
77 | 80 |
78 // Register observers that both match and don't match the event above. | 81 // Register observers that both match and don't match the event above. |
79 MockEventRouterObserver matching_observer; | 82 MockEventRouterObserver matching_observer; |
80 router.RegisterObserver(&matching_observer, "event_name"); | 83 router.RegisterObserver(&matching_observer, "event_name"); |
81 MockEventRouterObserver non_matching_observer; | 84 MockEventRouterObserver non_matching_observer; |
82 router.RegisterObserver(&non_matching_observer, "other"); | 85 router.RegisterObserver(&non_matching_observer, "other"); |
(...skipping 16 matching lines...) Expand all Loading... |
99 // Removing the listener again notifies again. | 102 // Removing the listener again notifies again. |
100 router.OnListenerRemoved(&listener); | 103 router.OnListenerRemoved(&listener); |
101 EXPECT_EQ(2, matching_observer.listener_removed_count()); | 104 EXPECT_EQ(2, matching_observer.listener_removed_count()); |
102 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); | 105 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); |
103 | 106 |
104 // Adding a listener with a sub-event notifies the main observer with | 107 // Adding a listener with a sub-event notifies the main observer with |
105 // proper details. | 108 // proper details. |
106 matching_observer.Reset(); | 109 matching_observer.Reset(); |
107 EventListener sub_event_listener("event_name/1", | 110 EventListener sub_event_listener("event_name/1", |
108 "extension_id", | 111 "extension_id", |
| 112 GURL(), |
109 NULL, | 113 NULL, |
110 scoped_ptr<base::DictionaryValue>()); | 114 scoped_ptr<base::DictionaryValue>()); |
111 router.OnListenerAdded(&sub_event_listener); | 115 router.OnListenerAdded(&sub_event_listener); |
112 EXPECT_EQ(1, matching_observer.listener_added_count()); | 116 EXPECT_EQ(1, matching_observer.listener_added_count()); |
113 EXPECT_EQ(0, matching_observer.listener_removed_count()); | 117 EXPECT_EQ(0, matching_observer.listener_removed_count()); |
114 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 118 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
115 | 119 |
116 // Ditto for removing the listener. | 120 // Ditto for removing the listener. |
117 matching_observer.Reset(); | 121 matching_observer.Reset(); |
118 router.OnListenerRemoved(&sub_event_listener); | 122 router.OnListenerRemoved(&sub_event_listener); |
119 EXPECT_EQ(0, matching_observer.listener_added_count()); | 123 EXPECT_EQ(0, matching_observer.listener_added_count()); |
120 EXPECT_EQ(1, matching_observer.listener_removed_count()); | 124 EXPECT_EQ(1, matching_observer.listener_removed_count()); |
121 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 125 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
122 } | 126 } |
123 | 127 |
124 } // namespace extensions | 128 } // namespace extensions |
OLD | NEW |