| 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 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 // Bogus class to act as a NotificationSource for the messages. | 15 // Bogus class to act as a NotificationSource for the messages. |
| 14 class TestSource {}; | 16 class TestSource {}; |
| 15 | 17 |
| 16 class TestObserver : public content::NotificationObserver { | 18 class TestObserver : public NotificationObserver { |
| 17 public: | 19 public: |
| 18 TestObserver() : notification_count_(0) {} | 20 TestObserver() : notification_count_(0) {} |
| 19 | 21 |
| 20 int notification_count() { return notification_count_; } | 22 int notification_count() { return notification_count_; } |
| 21 | 23 |
| 22 void Observe(int type, | 24 virtual void Observe(int type, |
| 23 const content::NotificationSource& source, | 25 const NotificationSource& source, |
| 24 const content::NotificationDetails& details) { | 26 const NotificationDetails& details) OVERRIDE { |
| 25 ++notification_count_; | 27 ++notification_count_; |
| 26 } | 28 } |
| 27 | 29 |
| 28 private: | 30 private: |
| 29 int notification_count_; | 31 int notification_count_; |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 } // namespace | 34 } // namespace |
| 33 | 35 |
| 34 | |
| 35 class NotificationServiceImplTest : public testing::Test { | 36 class NotificationServiceImplTest : public testing::Test { |
| 36 protected: | 37 protected: |
| 37 content::NotificationRegistrar registrar_; | 38 NotificationRegistrar registrar_; |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 TEST_F(NotificationServiceImplTest, Basic) { | 41 TEST_F(NotificationServiceImplTest, Basic) { |
| 41 TestSource test_source; | 42 TestSource test_source; |
| 42 TestSource other_source; | 43 TestSource other_source; |
| 43 | 44 |
| 44 // Check the equality operators defined for NotificationSource | 45 // Check the equality operators defined for NotificationSource |
| 45 EXPECT_TRUE(content::Source<TestSource>(&test_source) == | 46 EXPECT_TRUE(Source<TestSource>(&test_source) == |
| 46 content::Source<TestSource>(&test_source)); | 47 Source<TestSource>(&test_source)); |
| 47 EXPECT_TRUE(content::Source<TestSource>(&test_source) != | 48 EXPECT_TRUE(Source<TestSource>(&test_source) != |
| 48 content::Source<TestSource>(&other_source)); | 49 Source<TestSource>(&other_source)); |
| 49 | 50 |
| 50 TestObserver all_types_all_sources; | 51 TestObserver all_types_all_sources; |
| 51 TestObserver idle_all_sources; | 52 TestObserver idle_all_sources; |
| 52 TestObserver all_types_test_source; | 53 TestObserver all_types_test_source; |
| 53 TestObserver idle_test_source; | 54 TestObserver idle_test_source; |
| 54 | 55 |
| 55 // Make sure it doesn't freak out when there are no observers. | 56 // Make sure it doesn't freak out when there are no observers. |
| 56 content::NotificationService* service = | 57 NotificationService* service = NotificationService::current(); |
| 57 content::NotificationService::current(); | 58 service->Notify(NOTIFICATION_IDLE, |
| 58 service->Notify(content::NOTIFICATION_IDLE, | 59 Source<TestSource>(&test_source), |
| 59 content::Source<TestSource>(&test_source), | 60 NotificationService::NoDetails()); |
| 60 content::NotificationService::NoDetails()); | |
| 61 | 61 |
| 62 registrar_.Add(&all_types_all_sources, content::NOTIFICATION_ALL, | 62 registrar_.Add(&all_types_all_sources, NOTIFICATION_ALL, |
| 63 content::NotificationService::AllSources()); | 63 NotificationService::AllSources()); |
| 64 registrar_.Add(&idle_all_sources, content::NOTIFICATION_IDLE, | 64 registrar_.Add(&idle_all_sources, NOTIFICATION_IDLE, |
| 65 content::NotificationService::AllSources()); | 65 NotificationService::AllSources()); |
| 66 registrar_.Add(&all_types_test_source, content::NOTIFICATION_ALL, | 66 registrar_.Add(&all_types_test_source, NOTIFICATION_ALL, |
| 67 content::Source<TestSource>(&test_source)); | 67 Source<TestSource>(&test_source)); |
| 68 registrar_.Add(&idle_test_source, content::NOTIFICATION_IDLE, | 68 registrar_.Add(&idle_test_source, NOTIFICATION_IDLE, |
| 69 content::Source<TestSource>(&test_source)); | 69 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(content::NOTIFICATION_IDLE, | 76 service->Notify(NOTIFICATION_IDLE, |
| 77 content::Source<TestSource>(&test_source), | 77 Source<TestSource>(&test_source), |
| 78 content::NotificationService::NoDetails()); | 78 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(content::NOTIFICATION_BUSY, | 85 service->Notify(NOTIFICATION_BUSY, |
| 86 content::Source<TestSource>(&test_source), | 86 Source<TestSource>(&test_source), |
| 87 content::NotificationService::NoDetails()); | 87 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(content::NOTIFICATION_IDLE, | 94 service->Notify(NOTIFICATION_IDLE, |
| 95 content::Source<TestSource>(&other_source), | 95 Source<TestSource>(&other_source), |
| 96 content::NotificationService::NoDetails()); | 96 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(content::NOTIFICATION_BUSY, | 103 service->Notify(NOTIFICATION_BUSY, |
| 104 content::Source<TestSource>(&other_source), | 104 Source<TestSource>(&other_source), |
| 105 content::NotificationService::NoDetails()); | 105 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(content::NOTIFICATION_IDLE, | 113 service->Notify(NOTIFICATION_IDLE, |
| 114 content::NotificationService::AllSources(), | 114 NotificationService::AllSources(), |
| 115 content::NotificationService::NoDetails()); | 115 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(content::NOTIFICATION_IDLE, | 124 service->Notify(NOTIFICATION_IDLE, |
| 125 content::Source<TestSource>(&test_source), | 125 Source<TestSource>(&test_source), |
| 126 content::NotificationService::NoDetails()); | 126 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 content::NotificationService* service = | 139 NotificationService* service = NotificationService::current(); |
| 140 content::NotificationService::current(); | |
| 141 | 140 |
| 142 registrar_.Add(&idle_test_source, content::NOTIFICATION_IDLE, | 141 registrar_.Add(&idle_test_source, NOTIFICATION_IDLE, |
| 143 content::Source<TestSource>(&test_source)); | 142 Source<TestSource>(&test_source)); |
| 144 registrar_.Add(&idle_test_source, content::NOTIFICATION_ALL, | 143 registrar_.Add(&idle_test_source, NOTIFICATION_ALL, |
| 145 content::Source<TestSource>(&test_source)); | 144 Source<TestSource>(&test_source)); |
| 146 | 145 |
| 147 service->Notify(content::NOTIFICATION_IDLE, | 146 service->Notify(NOTIFICATION_IDLE, |
| 148 content::Source<TestSource>(&test_source), | 147 Source<TestSource>(&test_source), |
| 149 content::NotificationService::NoDetails()); | 148 NotificationService::NoDetails()); |
| 150 EXPECT_EQ(2, idle_test_source.notification_count()); | 149 EXPECT_EQ(2, idle_test_source.notification_count()); |
| 151 | 150 |
| 152 registrar_.Remove(&idle_test_source, content::NOTIFICATION_IDLE, | 151 registrar_.Remove(&idle_test_source, NOTIFICATION_IDLE, |
| 153 content::Source<TestSource>(&test_source)); | 152 Source<TestSource>(&test_source)); |
| 154 | 153 |
| 155 service->Notify(content::NOTIFICATION_IDLE, | 154 service->Notify(NOTIFICATION_IDLE, |
| 156 content::Source<TestSource>(&test_source), | 155 Source<TestSource>(&test_source), |
| 157 content::NotificationService::NoDetails()); | 156 NotificationService::NoDetails()); |
| 158 EXPECT_EQ(3, idle_test_source.notification_count()); | 157 EXPECT_EQ(3, idle_test_source.notification_count()); |
| 159 | 158 |
| 160 registrar_.Remove(&idle_test_source, content::NOTIFICATION_ALL, | 159 registrar_.Remove(&idle_test_source, NOTIFICATION_ALL, |
| 161 content::Source<TestSource>(&test_source)); | 160 Source<TestSource>(&test_source)); |
| 162 | 161 |
| 163 service->Notify(content::NOTIFICATION_IDLE, | 162 service->Notify(NOTIFICATION_IDLE, |
| 164 content::Source<TestSource>(&test_source), | 163 Source<TestSource>(&test_source), |
| 165 content::NotificationService::NoDetails()); | 164 NotificationService::NoDetails()); |
| 166 EXPECT_EQ(3, idle_test_source.notification_count()); | 165 EXPECT_EQ(3, idle_test_source.notification_count()); |
| 167 } | 166 } |
| 167 |
| 168 } // namespace content |
| OLD | NEW |