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