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 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 AddInClearObserve a(&observer_list); | 415 AddInClearObserve a(&observer_list); |
416 | 416 |
417 observer_list.AddObserver(&a); | 417 observer_list.AddObserver(&a); |
418 | 418 |
419 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); | 419 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
420 EXPECT_TRUE(a.added()); | 420 EXPECT_TRUE(a.added()); |
421 EXPECT_EQ(0, a.adder().total) | 421 EXPECT_EQ(0, a.adder().total) |
422 << "Adder should not observe, so sum should still be 0."; | 422 << "Adder should not observe, so sum should still be 0."; |
423 } | 423 } |
424 | 424 |
425 class ListDestructor : public Foo { | |
426 public: | |
427 explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} | |
428 virtual void Observe(int x) { | |
429 delete list_; | |
430 } | |
431 virtual ~ListDestructor() { } | |
432 int total; | |
433 private: | |
434 ObserverList<Foo>* list_; | |
435 }; | |
436 | |
437 | |
438 TEST(ObserverListTest, IteratorOutlivesList) { | |
439 ObserverList<Foo>* observer_list = new ObserverList<Foo>; | |
440 ListDestructor a(observer_list); | |
441 observer_list->AddObserver(&a); | |
442 | |
443 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); | |
444 // If this test fails, there'll be Valgrind errors when this function goes out | |
445 // of scope. | |
446 } | |
447 | |
448 } // namespace | 425 } // namespace |
OLD | NEW |