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