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