This source file includes following definitions.
- listener_removed_count_
- listener_added_count
- listener_removed_count
- last_event_name
- Reset
- OnListenerAdded
- OnListenerRemoved
- TEST_F
- TEST_F
#include "extensions/browser/event_router.h"
#include <string>
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "extensions/browser/event_listener_map.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
class MockEventRouterObserver : public EventRouter::Observer {
public:
MockEventRouterObserver()
: listener_added_count_(0),
listener_removed_count_(0) {}
virtual ~MockEventRouterObserver() {}
int listener_added_count() const { return listener_added_count_; }
int listener_removed_count() const { return listener_removed_count_; }
const std::string& last_event_name() const { return last_event_name_; }
void Reset() {
listener_added_count_ = 0;
listener_removed_count_ = 0;
last_event_name_.clear();
}
virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE {
listener_added_count_++;
last_event_name_ = details.event_name;
}
virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE {
listener_removed_count_++;
last_event_name_ = details.event_name;
}
private:
int listener_added_count_;
int listener_removed_count_;
std::string last_event_name_;
DISALLOW_COPY_AND_ASSIGN(MockEventRouterObserver);
};
}
typedef testing::Test EventRouterTest;
TEST_F(EventRouterTest, GetBaseEventName) {
EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar"));
EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123"));
}
TEST_F(EventRouterTest, EventRouterObserver) {
EventRouter router(NULL, NULL);
EventListener listener(
"event_name", "extension_id", NULL, scoped_ptr<base::DictionaryValue>());
router.OnListenerAdded(&listener);
router.OnListenerRemoved(&listener);
MockEventRouterObserver matching_observer;
router.RegisterObserver(&matching_observer, "event_name");
MockEventRouterObserver non_matching_observer;
router.RegisterObserver(&non_matching_observer, "other");
router.OnListenerAdded(&listener);
EXPECT_EQ(1, matching_observer.listener_added_count());
EXPECT_EQ(0, non_matching_observer.listener_added_count());
router.OnListenerRemoved(&listener);
EXPECT_EQ(1, matching_observer.listener_removed_count());
EXPECT_EQ(0, non_matching_observer.listener_removed_count());
router.OnListenerAdded(&listener);
EXPECT_EQ(2, matching_observer.listener_added_count());
EXPECT_EQ(0, non_matching_observer.listener_added_count());
router.OnListenerRemoved(&listener);
EXPECT_EQ(2, matching_observer.listener_removed_count());
EXPECT_EQ(0, non_matching_observer.listener_removed_count());
matching_observer.Reset();
EventListener sub_event_listener("event_name/1",
"extension_id",
NULL,
scoped_ptr<base::DictionaryValue>());
router.OnListenerAdded(&sub_event_listener);
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());
matching_observer.Reset();
router.OnListenerRemoved(&sub_event_listener);
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());
}
}