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.
5 #include "content/browser/notification_service_impl.h"
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"
16 // Bogus class to act as a NotificationSource for the messages.
19 class TestObserver
: public NotificationObserver
{
21 TestObserver() : notification_count_(0) {}
23 int notification_count() const { return notification_count_
; }
25 virtual void Observe(int type
,
26 const NotificationSource
& source
,
27 const NotificationDetails
& details
) OVERRIDE
{
28 ++notification_count_
;
32 int notification_count_
;
35 const int kNotification1
= 1;
36 const int kNotification2
= 2;
41 class NotificationServiceImplTest
: public testing::Test
{
43 NotificationRegistrar registrar_
;
46 TEST_F(NotificationServiceImplTest
, Basic
) {
47 TestSource test_source
;
48 TestSource other_source
;
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
));
56 TestObserver all_types_all_sources
;
57 TestObserver idle_all_sources
;
58 TestObserver all_types_test_source
;
59 TestObserver idle_test_source
;
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());
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
));
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());
81 service
->Notify(kNotification1
,
82 Source
<TestSource
>(&test_source
),
83 NotificationService::NoDetails());
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());
90 service
->Notify(kNotification2
,
91 Source
<TestSource
>(&test_source
),
92 NotificationService::NoDetails());
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());
99 service
->Notify(kNotification1
,
100 Source
<TestSource
>(&other_source
),
101 NotificationService::NoDetails());
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());
108 service
->Notify(kNotification2
,
109 Source
<TestSource
>(&other_source
),
110 NotificationService::NoDetails());
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());
117 // Try send with NULL source.
118 service
->Notify(kNotification1
,
119 NotificationService::AllSources(),
120 NotificationService::NoDetails());
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());
127 registrar_
.RemoveAll();
129 service
->Notify(kNotification1
,
130 Source
<TestSource
>(&test_source
),
131 NotificationService::NoDetails());
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());
139 TEST_F(NotificationServiceImplTest
, MultipleRegistration
) {
140 TestSource test_source
;
142 TestObserver idle_test_source
;
144 NotificationService
* service
= NotificationService::current();
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
));
151 service
->Notify(kNotification1
,
152 Source
<TestSource
>(&test_source
),
153 NotificationService::NoDetails());
154 EXPECT_EQ(2, idle_test_source
.notification_count());
156 registrar_
.Remove(&idle_test_source
, kNotification1
,
157 Source
<TestSource
>(&test_source
));
159 service
->Notify(kNotification1
,
160 Source
<TestSource
>(&test_source
),
161 NotificationService::NoDetails());
162 EXPECT_EQ(3, idle_test_source
.notification_count());
164 registrar_
.Remove(&idle_test_source
, NOTIFICATION_ALL
,
165 Source
<TestSource
>(&test_source
));
167 service
->Notify(kNotification1
,
168 Source
<TestSource
>(&test_source
),
169 NotificationService::NoDetails());
170 EXPECT_EQ(3, idle_test_source
.notification_count());
173 } // namespace content