OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/observer_list.h" | 6 #include "base/observer_list.h" |
7 #include "base/observer_list_threadsafe.h" | 7 #include "base/observer_list_threadsafe.h" |
8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 : list_(list), doomed_(doomed) { } | 53 : list_(list), doomed_(doomed) { } |
54 virtual ~ThreadSafeDisrupter() { } | 54 virtual ~ThreadSafeDisrupter() { } |
55 virtual void Observe(int x) { | 55 virtual void Observe(int x) { |
56 list_->RemoveObserver(doomed_); | 56 list_->RemoveObserver(doomed_); |
57 } | 57 } |
58 private: | 58 private: |
59 ObserverListThreadSafe<Foo>* list_; | 59 ObserverListThreadSafe<Foo>* list_; |
60 Foo* doomed_; | 60 Foo* doomed_; |
61 }; | 61 }; |
62 | 62 |
| 63 class AddInObserve : public Foo { |
| 64 public: |
| 65 AddInObserve(ObserverList<Foo>* observer_list) |
| 66 : added(false), |
| 67 observer_list(observer_list), |
| 68 adder(1) { |
| 69 } |
| 70 virtual void Observe(int x) { |
| 71 if (!added) { |
| 72 added = true; |
| 73 observer_list->AddObserver(&adder); |
| 74 } |
| 75 } |
| 76 |
| 77 bool added; |
| 78 ObserverList<Foo>* observer_list; |
| 79 Adder adder; |
| 80 }; |
| 81 |
| 82 |
63 class ObserverListThreadSafeTest : public testing::Test { | 83 class ObserverListThreadSafeTest : public testing::Test { |
64 }; | 84 }; |
65 | 85 |
66 static const int kThreadRunTime = 10000; // ms to run the multi-threaded test. | 86 static const int kThreadRunTime = 10000; // ms to run the multi-threaded test. |
67 | 87 |
68 // A thread for use in the ThreadSafeObserver test | 88 // A thread for use in the ThreadSafeObserver test |
69 // which will add and remove itself from the notification | 89 // which will add and remove itself from the notification |
70 // list repeatedly. | 90 // list repeatedly. |
71 class AddRemoveThread : public PlatformThread::Delegate, | 91 class AddRemoveThread : public PlatformThread::Delegate, |
72 public Foo { | 92 public Foo { |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 // Use 7 observer threads. Notifications only come from | 275 // Use 7 observer threads. Notifications only come from |
256 // the main thread. | 276 // the main thread. |
257 ThreadSafeObserverHarness(7, false); | 277 ThreadSafeObserverHarness(7, false); |
258 } | 278 } |
259 | 279 |
260 TEST(ObserverListThreadSafeTest, CrossThreadNotifications) { | 280 TEST(ObserverListThreadSafeTest, CrossThreadNotifications) { |
261 // Use 3 observer threads. Notifications will fire from | 281 // Use 3 observer threads. Notifications will fire from |
262 // the main thread and all 3 observer threads. | 282 // the main thread and all 3 observer threads. |
263 ThreadSafeObserverHarness(3, true); | 283 ThreadSafeObserverHarness(3, true); |
264 } | 284 } |
| 285 |
| 286 TEST(ObserverListTest, Existing) { |
| 287 ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
| 288 Adder a(1); |
| 289 AddInObserve b(&observer_list); |
| 290 |
| 291 observer_list.AddObserver(&a); |
| 292 observer_list.AddObserver(&b); |
| 293 |
| 294 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 295 |
| 296 EXPECT_TRUE(b.added); |
| 297 // B's adder should not have been notified because it was added during |
| 298 // notificaiton. |
| 299 EXPECT_EQ(0, b.adder.total); |
| 300 |
| 301 // Notify again to make sure b's adder is notified. |
| 302 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 303 EXPECT_EQ(1, b.adder.total); |
| 304 } |
OLD | NEW |