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