Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "extensions/browser/event_router.h"
      6 
      7 #include <string>
      8 
      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"
     14 
     15 namespace extensions {
     16 
     17 namespace {
     18 
     19 // A simple mock to keep track of listener additions and removals.
     20 class MockEventRouterObserver : public EventRouter::Observer {
     21  public:
     22   MockEventRouterObserver()
     23       : listener_added_count_(0),
     24         listener_removed_count_(0) {}
     25   virtual ~MockEventRouterObserver() {}
     26 
     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_; }
     30 
     31   void Reset() {
     32     listener_added_count_ = 0;
     33     listener_removed_count_ = 0;
     34     last_event_name_.clear();
     35   }
     36 
     37   // EventRouter::Observer overrides:
     38   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE {
     39     listener_added_count_++;
     40     last_event_name_ = details.event_name;
     41   }
     42 
     43   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE {
     44     listener_removed_count_++;
     45     last_event_name_ = details.event_name;
     46   }
     47 
     48  private:
     49   int listener_added_count_;
     50   int listener_removed_count_;
     51   std::string last_event_name_;
     52 
     53   DISALLOW_COPY_AND_ASSIGN(MockEventRouterObserver);
     54 };
     55 
     56 }  // namespace
     57 
     58 typedef testing::Test EventRouterTest;
     59 
     60 TEST_F(EventRouterTest, GetBaseEventName) {
     61   // Normal event names are passed through unchanged.
     62   EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar"));
     63 
     64   // Sub-events are converted to the part before the slash.
     65   EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123"));
     66 }
     67 
     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<DictionaryValue>());
     73 
     74   // Add/remove works without any observers.
     75   router.OnListenerAdded(&listener);
     76   router.OnListenerRemoved(&listener);
     77 
     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");
     83 
     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());
     88 
     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());
     93 
     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());
     98 
     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());
    103 
    104   // Adding a listener with a sub-event notifies the main observer with
    105   // proper details.
    106   matching_observer.Reset();
    107   EventListener sub_event_listener(
    108       "event_name/1", "extension_id", NULL, scoped_ptr<DictionaryValue>());
    109   router.OnListenerAdded(&sub_event_listener);
    110   EXPECT_EQ(1, matching_observer.listener_added_count());
    111   EXPECT_EQ(0, matching_observer.listener_removed_count());
    112   EXPECT_EQ("event_name/1", matching_observer.last_event_name());
    113 
    114   // Ditto for removing the listener.
    115   matching_observer.Reset();
    116   router.OnListenerRemoved(&sub_event_listener);
    117   EXPECT_EQ(0, matching_observer.listener_added_count());
    118   EXPECT_EQ(1, matching_observer.listener_removed_count());
    119   EXPECT_EQ("event_name/1", matching_observer.last_event_name());
    120 }
    121 
    122 }  // namespace extensions
    123