| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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( |
| 72 "event_name", "extension_id", NULL, scoped_ptr<DictionaryValue>()); | 72 "event_name", "extension_id", NULL, scoped_ptr<base::DictionaryValue>()); |
| 73 | 73 |
| 74 // Add/remove works without any observers. | 74 // Add/remove works without any observers. |
| 75 router.OnListenerAdded(&listener); | 75 router.OnListenerAdded(&listener); |
| 76 router.OnListenerRemoved(&listener); | 76 router.OnListenerRemoved(&listener); |
| 77 | 77 |
| 78 // Register observers that both match and don't match the event above. | 78 // Register observers that both match and don't match the event above. |
| 79 MockEventRouterObserver matching_observer; | 79 MockEventRouterObserver matching_observer; |
| 80 router.RegisterObserver(&matching_observer, "event_name"); | 80 router.RegisterObserver(&matching_observer, "event_name"); |
| 81 MockEventRouterObserver non_matching_observer; | 81 MockEventRouterObserver non_matching_observer; |
| 82 router.RegisterObserver(&non_matching_observer, "other"); | 82 router.RegisterObserver(&non_matching_observer, "other"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 97 EXPECT_EQ(0, non_matching_observer.listener_added_count()); | 97 EXPECT_EQ(0, non_matching_observer.listener_added_count()); |
| 98 | 98 |
| 99 // Removing the listener again notifies again. | 99 // Removing the listener again notifies again. |
| 100 router.OnListenerRemoved(&listener); | 100 router.OnListenerRemoved(&listener); |
| 101 EXPECT_EQ(2, matching_observer.listener_removed_count()); | 101 EXPECT_EQ(2, matching_observer.listener_removed_count()); |
| 102 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); | 102 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); |
| 103 | 103 |
| 104 // Adding a listener with a sub-event notifies the main observer with | 104 // Adding a listener with a sub-event notifies the main observer with |
| 105 // proper details. | 105 // proper details. |
| 106 matching_observer.Reset(); | 106 matching_observer.Reset(); |
| 107 EventListener sub_event_listener( | 107 EventListener sub_event_listener("event_name/1", |
| 108 "event_name/1", "extension_id", NULL, scoped_ptr<DictionaryValue>()); | 108 "extension_id", |
| 109 NULL, |
| 110 scoped_ptr<base::DictionaryValue>()); |
| 109 router.OnListenerAdded(&sub_event_listener); | 111 router.OnListenerAdded(&sub_event_listener); |
| 110 EXPECT_EQ(1, matching_observer.listener_added_count()); | 112 EXPECT_EQ(1, matching_observer.listener_added_count()); |
| 111 EXPECT_EQ(0, matching_observer.listener_removed_count()); | 113 EXPECT_EQ(0, matching_observer.listener_removed_count()); |
| 112 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 114 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
| 113 | 115 |
| 114 // Ditto for removing the listener. | 116 // Ditto for removing the listener. |
| 115 matching_observer.Reset(); | 117 matching_observer.Reset(); |
| 116 router.OnListenerRemoved(&sub_event_listener); | 118 router.OnListenerRemoved(&sub_event_listener); |
| 117 EXPECT_EQ(0, matching_observer.listener_added_count()); | 119 EXPECT_EQ(0, matching_observer.listener_added_count()); |
| 118 EXPECT_EQ(1, matching_observer.listener_removed_count()); | 120 EXPECT_EQ(1, matching_observer.listener_removed_count()); |
| 119 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 121 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
| 120 } | 122 } |
| 121 | 123 |
| 122 } // namespace extensions | 124 } // namespace extensions |
| OLD | NEW |