| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/platform_thread.h" | |
| 12 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using base::PlatformThread; |
| 15 using base::Time; | 16 using base::Time; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 class ObserverListTest : public testing::Test { | |
| 20 }; | |
| 21 | |
| 22 class Foo { | 20 class Foo { |
| 23 public: | 21 public: |
| 24 virtual void Observe(int x) = 0; | 22 virtual void Observe(int x) = 0; |
| 25 virtual ~Foo() {} | 23 virtual ~Foo() {} |
| 26 }; | 24 }; |
| 27 | 25 |
| 28 class Adder : public Foo { | 26 class Adder : public Foo { |
| 29 public: | 27 public: |
| 30 explicit Adder(int scaler) : total(0), scaler_(scaler) {} | 28 explicit Adder(int scaler) : total(0), scaler_(scaler) {} |
| 31 virtual void Observe(int x) { | 29 virtual void Observe(int x) { |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 new ObserverListThreadSafe<Foo>); | 282 new ObserverListThreadSafe<Foo>); |
| 285 Adder a(1); | 283 Adder a(1); |
| 286 Adder b(-1); | 284 Adder b(-1); |
| 287 Adder c(1); | 285 Adder c(1); |
| 288 Adder d(-1); | 286 Adder d(-1); |
| 289 | 287 |
| 290 observer_list->AddObserver(&a); | 288 observer_list->AddObserver(&a); |
| 291 observer_list->AddObserver(&b); | 289 observer_list->AddObserver(&b); |
| 292 | 290 |
| 293 AddRemoveThread* threaded_observer[kMaxThreads]; | 291 AddRemoveThread* threaded_observer[kMaxThreads]; |
| 294 PlatformThreadHandle threads[kMaxThreads]; | 292 base::PlatformThreadHandle threads[kMaxThreads]; |
| 295 for (int index = 0; index < num_threads; index++) { | 293 for (int index = 0; index < num_threads; index++) { |
| 296 threaded_observer[index] = new AddRemoveThread(observer_list.get(), false); | 294 threaded_observer[index] = new AddRemoveThread(observer_list.get(), false); |
| 297 EXPECT_TRUE(PlatformThread::Create(0, | 295 EXPECT_TRUE(PlatformThread::Create(0, |
| 298 threaded_observer[index], &threads[index])); | 296 threaded_observer[index], &threads[index])); |
| 299 } | 297 } |
| 300 | 298 |
| 301 Time start = Time::Now(); | 299 Time start = Time::Now(); |
| 302 while (true) { | 300 while (true) { |
| 303 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime) | 301 if ((Time::Now() - start).InMilliseconds() > kThreadRunTime) |
| 304 break; | 302 break; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 383 |
| 386 observer_list.AddObserver(&a); | 384 observer_list.AddObserver(&a); |
| 387 | 385 |
| 388 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); | 386 FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 389 EXPECT_TRUE(a.added()); | 387 EXPECT_TRUE(a.added()); |
| 390 EXPECT_EQ(0, a.adder().total) | 388 EXPECT_EQ(0, a.adder().total) |
| 391 << "Adder should not observe, so sum should still be 0."; | 389 << "Adder should not observe, so sum should still be 0."; |
| 392 } | 390 } |
| 393 | 391 |
| 394 } // namespace | 392 } // namespace |
| OLD | NEW |