Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 "content/browser/notification_service_impl.h"
      6 
      7 #include "content/public/browser/notification_observer.h"
      8 #include "content/public/browser/notification_registrar.h"
      9 #include "content/public/browser/notification_types.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace content {
     13 
     14 namespace {
     15 
     16 // Bogus class to act as a NotificationSource for the messages.
     17 class TestSource {};
     18 
     19 class TestObserver : public NotificationObserver {
     20  public:
     21   TestObserver() : notification_count_(0) {}
     22 
     23   int notification_count() const { return notification_count_; }
     24 
     25   virtual void Observe(int type,
     26                        const NotificationSource& source,
     27                        const NotificationDetails& details) OVERRIDE {
     28     ++notification_count_;
     29   }
     30 
     31  private:
     32   int notification_count_;
     33 };
     34 
     35 const int kNotification1 = 1;
     36 const int kNotification2 = 2;
     37 
     38 }  // namespace
     39 
     40 
     41 class NotificationServiceImplTest : public testing::Test {
     42  protected:
     43   NotificationRegistrar registrar_;
     44 };
     45 
     46 TEST_F(NotificationServiceImplTest, Basic) {
     47   TestSource test_source;
     48   TestSource other_source;
     49 
     50   // Check the equality operators defined for NotificationSource
     51   EXPECT_TRUE(Source<TestSource>(&test_source) ==
     52               Source<TestSource>(&test_source));
     53   EXPECT_TRUE(Source<TestSource>(&test_source) !=
     54               Source<TestSource>(&other_source));
     55 
     56   TestObserver all_types_all_sources;
     57   TestObserver idle_all_sources;
     58   TestObserver all_types_test_source;
     59   TestObserver idle_test_source;
     60 
     61   // Make sure it doesn't freak out when there are no observers.
     62   NotificationService* service = NotificationService::current();
     63   service->Notify(kNotification1,
     64                   Source<TestSource>(&test_source),
     65                   NotificationService::NoDetails());
     66 
     67   registrar_.Add(&all_types_all_sources, NOTIFICATION_ALL,
     68                  NotificationService::AllSources());
     69   registrar_.Add(&idle_all_sources, kNotification1,
     70                  NotificationService::AllSources());
     71   registrar_.Add(&all_types_test_source, NOTIFICATION_ALL,
     72                  Source<TestSource>(&test_source));
     73   registrar_.Add(&idle_test_source, kNotification1,
     74                  Source<TestSource>(&test_source));
     75 
     76   EXPECT_EQ(0, all_types_all_sources.notification_count());
     77   EXPECT_EQ(0, idle_all_sources.notification_count());
     78   EXPECT_EQ(0, all_types_test_source.notification_count());
     79   EXPECT_EQ(0, idle_test_source.notification_count());
     80 
     81   service->Notify(kNotification1,
     82                   Source<TestSource>(&test_source),
     83                   NotificationService::NoDetails());
     84 
     85   EXPECT_EQ(1, all_types_all_sources.notification_count());
     86   EXPECT_EQ(1, idle_all_sources.notification_count());
     87   EXPECT_EQ(1, all_types_test_source.notification_count());
     88   EXPECT_EQ(1, idle_test_source.notification_count());
     89 
     90   service->Notify(kNotification2,
     91                   Source<TestSource>(&test_source),
     92                   NotificationService::NoDetails());
     93 
     94   EXPECT_EQ(2, all_types_all_sources.notification_count());
     95   EXPECT_EQ(1, idle_all_sources.notification_count());
     96   EXPECT_EQ(2, all_types_test_source.notification_count());
     97   EXPECT_EQ(1, idle_test_source.notification_count());
     98 
     99   service->Notify(kNotification1,
    100                   Source<TestSource>(&other_source),
    101                   NotificationService::NoDetails());
    102 
    103   EXPECT_EQ(3, all_types_all_sources.notification_count());
    104   EXPECT_EQ(2, idle_all_sources.notification_count());
    105   EXPECT_EQ(2, all_types_test_source.notification_count());
    106   EXPECT_EQ(1, idle_test_source.notification_count());
    107 
    108   service->Notify(kNotification2,
    109                   Source<TestSource>(&other_source),
    110                   NotificationService::NoDetails());
    111 
    112   EXPECT_EQ(4, all_types_all_sources.notification_count());
    113   EXPECT_EQ(2, idle_all_sources.notification_count());
    114   EXPECT_EQ(2, all_types_test_source.notification_count());
    115   EXPECT_EQ(1, idle_test_source.notification_count());
    116 
    117   // Try send with NULL source.
    118   service->Notify(kNotification1,
    119                   NotificationService::AllSources(),
    120                   NotificationService::NoDetails());
    121 
    122   EXPECT_EQ(5, all_types_all_sources.notification_count());
    123   EXPECT_EQ(3, idle_all_sources.notification_count());
    124   EXPECT_EQ(2, all_types_test_source.notification_count());
    125   EXPECT_EQ(1, idle_test_source.notification_count());
    126 
    127   registrar_.RemoveAll();
    128 
    129   service->Notify(kNotification1,
    130                   Source<TestSource>(&test_source),
    131                   NotificationService::NoDetails());
    132 
    133   EXPECT_EQ(5, all_types_all_sources.notification_count());
    134   EXPECT_EQ(3, idle_all_sources.notification_count());
    135   EXPECT_EQ(2, all_types_test_source.notification_count());
    136   EXPECT_EQ(1, idle_test_source.notification_count());
    137 }
    138 
    139 TEST_F(NotificationServiceImplTest, MultipleRegistration) {
    140   TestSource test_source;
    141 
    142   TestObserver idle_test_source;
    143 
    144   NotificationService* service = NotificationService::current();
    145 
    146   registrar_.Add(&idle_test_source, kNotification1,
    147                  Source<TestSource>(&test_source));
    148   registrar_.Add(&idle_test_source, NOTIFICATION_ALL,
    149                  Source<TestSource>(&test_source));
    150 
    151   service->Notify(kNotification1,
    152                   Source<TestSource>(&test_source),
    153                   NotificationService::NoDetails());
    154   EXPECT_EQ(2, idle_test_source.notification_count());
    155 
    156   registrar_.Remove(&idle_test_source, kNotification1,
    157                     Source<TestSource>(&test_source));
    158 
    159   service->Notify(kNotification1,
    160                  Source<TestSource>(&test_source),
    161                  NotificationService::NoDetails());
    162   EXPECT_EQ(3, idle_test_source.notification_count());
    163 
    164   registrar_.Remove(&idle_test_source, NOTIFICATION_ALL,
    165                     Source<TestSource>(&test_source));
    166 
    167   service->Notify(kNotification1,
    168                   Source<TestSource>(&test_source),
    169                   NotificationService::NoDetails());
    170   EXPECT_EQ(3, idle_test_source.notification_count());
    171 }
    172 
    173 }  // namespace content
    174