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