| 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 "base/observer_list.h" | 5 #include "base/observer_list.h" |
| 6 #include "base/observer_list_threadsafe.h" | 6 #include "base/observer_list_threadsafe.h" |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 168 |
| 169 int count_observes_; // Number of times we observed. | 169 int count_observes_; // Number of times we observed. |
| 170 int count_addtask_; // Number of times thread AddTask was called | 170 int count_addtask_; // Number of times thread AddTask was called |
| 171 bool do_notifies_; // Whether these threads should do notifications. | 171 bool do_notifies_; // Whether these threads should do notifications. |
| 172 | 172 |
| 173 ScopedRunnableMethodFactory<AddRemoveThread>* factory_; | 173 ScopedRunnableMethodFactory<AddRemoveThread>* factory_; |
| 174 }; | 174 }; |
| 175 | 175 |
| 176 TEST(ObserverListTest, BasicTest) { | 176 TEST(ObserverListTest, BasicTest) { |
| 177 ObserverList<Foo> observer_list; | 177 ObserverList<Foo> observer_list; |
| 178 Adder a(1), b(-1), c(1), d(-1); | 178 Adder a(1), b(-1), c(1), d(-1), e(-1); |
| 179 Disrupter evil(&observer_list, &c); | 179 Disrupter evil(&observer_list, &c); |
| 180 | 180 |
| 181 observer_list.AddObserver(&a); | 181 observer_list.AddObserver(&a); |
| 182 observer_list.AddObserver(&b); | 182 observer_list.AddObserver(&b); |
| 183 | 183 |
| 184 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); | 184 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 185 | 185 |
| 186 observer_list.AddObserver(&evil); | 186 observer_list.AddObserver(&evil); |
| 187 observer_list.AddObserver(&c); | 187 observer_list.AddObserver(&c); |
| 188 observer_list.AddObserver(&d); | 188 observer_list.AddObserver(&d); |
| 189 | 189 |
| 190 // Removing an observer not in the list should do nothing. |
| 191 observer_list.RemoveObserver(&e); |
| 192 |
| 190 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); | 193 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 191 | 194 |
| 192 EXPECT_EQ(a.total, 20); | 195 EXPECT_EQ(a.total, 20); |
| 193 EXPECT_EQ(b.total, -20); | 196 EXPECT_EQ(b.total, -20); |
| 194 EXPECT_EQ(c.total, 0); | 197 EXPECT_EQ(c.total, 0); |
| 195 EXPECT_EQ(d.total, -10); | 198 EXPECT_EQ(d.total, -10); |
| 199 EXPECT_EQ(e.total, 0); |
| 196 } | 200 } |
| 197 | 201 |
| 198 TEST(ObserverListThreadSafeTest, BasicTest) { | 202 TEST(ObserverListThreadSafeTest, BasicTest) { |
| 199 MessageLoop loop; | 203 MessageLoop loop; |
| 200 | 204 |
| 201 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 205 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 202 new ObserverListThreadSafe<Foo>); | 206 new ObserverListThreadSafe<Foo>); |
| 203 Adder a(1); | 207 Adder a(1); |
| 204 Adder b(-1); | 208 Adder b(-1); |
| 205 Adder c(1); | 209 Adder c(1); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 | 222 |
| 219 observer_list->Notify(&Foo::Observe, 10); | 223 observer_list->Notify(&Foo::Observe, 10); |
| 220 loop.RunAllPending(); | 224 loop.RunAllPending(); |
| 221 | 225 |
| 222 EXPECT_EQ(a.total, 20); | 226 EXPECT_EQ(a.total, 20); |
| 223 EXPECT_EQ(b.total, -20); | 227 EXPECT_EQ(b.total, -20); |
| 224 EXPECT_EQ(c.total, 0); | 228 EXPECT_EQ(c.total, 0); |
| 225 EXPECT_EQ(d.total, -10); | 229 EXPECT_EQ(d.total, -10); |
| 226 } | 230 } |
| 227 | 231 |
| 232 TEST(ObserverListThreadSafeTest, RemoveObserver) { |
| 233 MessageLoop loop; |
| 234 |
| 235 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 236 new ObserverListThreadSafe<Foo>); |
| 237 Adder a(1), b(1); |
| 238 |
| 239 // Should do nothing. |
| 240 observer_list->RemoveObserver(&a); |
| 241 observer_list->RemoveObserver(&b); |
| 242 |
| 243 observer_list->Notify(&Foo::Observe, 10); |
| 244 loop.RunAllPending(); |
| 245 |
| 246 EXPECT_EQ(a.total, 0); |
| 247 EXPECT_EQ(b.total, 0); |
| 248 |
| 249 observer_list->AddObserver(&a); |
| 250 |
| 251 // Should also do nothing. |
| 252 observer_list->RemoveObserver(&b); |
| 253 |
| 254 observer_list->Notify(&Foo::Observe, 10); |
| 255 loop.RunAllPending(); |
| 256 |
| 257 EXPECT_EQ(a.total, 10); |
| 258 EXPECT_EQ(b.total, 0); |
| 259 } |
| 260 |
| 228 class FooRemover : public Foo { | 261 class FooRemover : public Foo { |
| 229 public: | 262 public: |
| 230 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} | 263 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} |
| 231 virtual ~FooRemover() {} | 264 virtual ~FooRemover() {} |
| 232 | 265 |
| 233 void AddFooToRemove(Foo* foo) { | 266 void AddFooToRemove(Foo* foo) { |
| 234 foos_.push_back(foo); | 267 foos_.push_back(foo); |
| 235 } | 268 } |
| 236 | 269 |
| 237 virtual void Observe(int x) { | 270 virtual void Observe(int x) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 | 416 |
| 384 observer_list.AddObserver(&a); | 417 observer_list.AddObserver(&a); |
| 385 | 418 |
| 386 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); | 419 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 387 EXPECT_TRUE(a.added()); | 420 EXPECT_TRUE(a.added()); |
| 388 EXPECT_EQ(0, a.adder().total) | 421 EXPECT_EQ(0, a.adder().total) |
| 389 << "Adder should not observe, so sum should still be 0."; | 422 << "Adder should not observe, so sum should still be 0."; |
| 390 } | 423 } |
| 391 | 424 |
| 392 } // namespace | 425 } // namespace |
| OLD | NEW |