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