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
3 // found in the LICENSE file.
5 #include "extensions/browser/event_router.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "extensions/browser/event_listener_map.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
19 // A simple mock to keep track of listener additions and removals.
20 class MockEventRouterObserver
: public EventRouter::Observer
{
22 MockEventRouterObserver()
23 : listener_added_count_(0),
24 listener_removed_count_(0) {}
25 virtual ~MockEventRouterObserver() {}
27 int listener_added_count() const { return listener_added_count_
; }
28 int listener_removed_count() const { return listener_removed_count_
; }
29 const std::string
& last_event_name() const { return last_event_name_
; }
32 listener_added_count_
= 0;
33 listener_removed_count_
= 0;
34 last_event_name_
.clear();
37 // EventRouter::Observer overrides:
38 virtual void OnListenerAdded(const EventListenerInfo
& details
) OVERRIDE
{
39 listener_added_count_
++;
40 last_event_name_
= details
.event_name
;
43 virtual void OnListenerRemoved(const EventListenerInfo
& details
) OVERRIDE
{
44 listener_removed_count_
++;
45 last_event_name_
= details
.event_name
;
49 int listener_added_count_
;
50 int listener_removed_count_
;
51 std::string last_event_name_
;
53 DISALLOW_COPY_AND_ASSIGN(MockEventRouterObserver
);
58 typedef testing::Test EventRouterTest
;
60 TEST_F(EventRouterTest
, GetBaseEventName
) {
61 // Normal event names are passed through unchanged.
62 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar"));
64 // Sub-events are converted to the part before the slash.
65 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123"));
68 // Tests adding and removing observers from EventRouter.
69 TEST_F(EventRouterTest
, EventRouterObserver
) {
70 EventRouter
router(NULL
, NULL
);
71 EventListener
listener(
72 "event_name", "extension_id", NULL
, scoped_ptr
<base::DictionaryValue
>());
74 // Add/remove works without any observers.
75 router
.OnListenerAdded(&listener
);
76 router
.OnListenerRemoved(&listener
);
78 // Register observers that both match and don't match the event above.
79 MockEventRouterObserver matching_observer
;
80 router
.RegisterObserver(&matching_observer
, "event_name");
81 MockEventRouterObserver non_matching_observer
;
82 router
.RegisterObserver(&non_matching_observer
, "other");
84 // Adding a listener notifies the appropriate observers.
85 router
.OnListenerAdded(&listener
);
86 EXPECT_EQ(1, matching_observer
.listener_added_count());
87 EXPECT_EQ(0, non_matching_observer
.listener_added_count());
89 // Removing a listener notifies the appropriate observers.
90 router
.OnListenerRemoved(&listener
);
91 EXPECT_EQ(1, matching_observer
.listener_removed_count());
92 EXPECT_EQ(0, non_matching_observer
.listener_removed_count());
94 // Adding the listener again notifies again.
95 router
.OnListenerAdded(&listener
);
96 EXPECT_EQ(2, matching_observer
.listener_added_count());
97 EXPECT_EQ(0, non_matching_observer
.listener_added_count());
99 // Removing the listener again notifies again.
100 router
.OnListenerRemoved(&listener
);
101 EXPECT_EQ(2, matching_observer
.listener_removed_count());
102 EXPECT_EQ(0, non_matching_observer
.listener_removed_count());
104 // Adding a listener with a sub-event notifies the main observer with
106 matching_observer
.Reset();
107 EventListener
sub_event_listener("event_name/1",
110 scoped_ptr
<base::DictionaryValue
>());
111 router
.OnListenerAdded(&sub_event_listener
);
112 EXPECT_EQ(1, matching_observer
.listener_added_count());
113 EXPECT_EQ(0, matching_observer
.listener_removed_count());
114 EXPECT_EQ("event_name/1", matching_observer
.last_event_name());
116 // Ditto for removing the listener.
117 matching_observer
.Reset();
118 router
.OnListenerRemoved(&sub_event_listener
);
119 EXPECT_EQ(0, matching_observer
.listener_added_count());
120 EXPECT_EQ(1, matching_observer
.listener_removed_count());
121 EXPECT_EQ("event_name/1", matching_observer
.last_event_name());
124 } // namespace extensions