| 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/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // Should also do nothing. | 251 // Should also do nothing. |
| 252 observer_list->RemoveObserver(&b); | 252 observer_list->RemoveObserver(&b); |
| 253 | 253 |
| 254 observer_list->Notify(&Foo::Observe, 10); | 254 observer_list->Notify(&Foo::Observe, 10); |
| 255 loop.RunAllPending(); | 255 loop.RunAllPending(); |
| 256 | 256 |
| 257 EXPECT_EQ(a.total, 10); | 257 EXPECT_EQ(a.total, 10); |
| 258 EXPECT_EQ(b.total, 0); | 258 EXPECT_EQ(b.total, 0); |
| 259 } | 259 } |
| 260 | 260 |
| 261 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) { |
| 262 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 263 new ObserverListThreadSafe<Foo>); |
| 264 |
| 265 Adder a(1), b(1), c(1); |
| 266 |
| 267 // No MessageLoop, so these should not be added. |
| 268 observer_list->AddObserver(&a); |
| 269 observer_list->AddObserver(&b); |
| 270 |
| 271 { |
| 272 // Add c when there's a loop. |
| 273 MessageLoop loop; |
| 274 observer_list->AddObserver(&c); |
| 275 |
| 276 observer_list->Notify(&Foo::Observe, 10); |
| 277 loop.RunAllPending(); |
| 278 |
| 279 EXPECT_EQ(0, a.total); |
| 280 EXPECT_EQ(0, b.total); |
| 281 EXPECT_EQ(10, c.total); |
| 282 |
| 283 // Now add a when there's a loop. |
| 284 observer_list->AddObserver(&a); |
| 285 |
| 286 // Remove c when there's a loop. |
| 287 observer_list->RemoveObserver(&c); |
| 288 |
| 289 // Notify again. |
| 290 observer_list->Notify(&Foo::Observe, 20); |
| 291 loop.RunAllPending(); |
| 292 |
| 293 EXPECT_EQ(20, a.total); |
| 294 EXPECT_EQ(0, b.total); |
| 295 EXPECT_EQ(10, c.total); |
| 296 } |
| 297 |
| 298 // Removing should always succeed with or without a loop. |
| 299 observer_list->RemoveObserver(&a); |
| 300 |
| 301 // Notifying should not fail but should also be a no-op. |
| 302 MessageLoop loop; |
| 303 observer_list->AddObserver(&b); |
| 304 observer_list->Notify(&Foo::Observe, 30); |
| 305 loop.RunAllPending(); |
| 306 |
| 307 EXPECT_EQ(20, a.total); |
| 308 EXPECT_EQ(30, b.total); |
| 309 EXPECT_EQ(10, c.total); |
| 310 } |
| 311 |
| 261 class FooRemover : public Foo { | 312 class FooRemover : public Foo { |
| 262 public: | 313 public: |
| 263 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} | 314 explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} |
| 264 virtual ~FooRemover() {} | 315 virtual ~FooRemover() {} |
| 265 | 316 |
| 266 void AddFooToRemove(Foo* foo) { | 317 void AddFooToRemove(Foo* foo) { |
| 267 foos_.push_back(foo); | 318 foos_.push_back(foo); |
| 268 } | 319 } |
| 269 | 320 |
| 270 virtual void Observe(int x) { | 321 virtual void Observe(int x) { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 ObserverList<Foo>* observer_list = new ObserverList<Foo>; | 527 ObserverList<Foo>* observer_list = new ObserverList<Foo>; |
| 477 ListDestructor a(observer_list); | 528 ListDestructor a(observer_list); |
| 478 observer_list->AddObserver(&a); | 529 observer_list->AddObserver(&a); |
| 479 | 530 |
| 480 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); | 531 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
| 481 // If this test fails, there'll be Valgrind errors when this function goes out | 532 // If this test fails, there'll be Valgrind errors when this function goes out |
| 482 // of scope. | 533 // of scope. |
| 483 } | 534 } |
| 484 | 535 |
| 485 } // namespace | 536 } // namespace |
| OLD | NEW |