| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/scoped_vector.h" | 5 #include "base/memory/scoped_vector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // The LifeCycleObject notifies its Observer upon construction & destruction. | 14 // The LifeCycleObject notifies its Observer upon construction & destruction. |
| 15 class LifeCycleObject { | 15 class LifeCycleObject { |
| 16 public: | 16 public: |
| 17 class Observer { | 17 class Observer { |
| 18 public: | 18 public: |
| 19 virtual void OnLifeCycleConstruct(LifeCycleObject* o) = 0; | 19 virtual void OnLifeCycleConstruct(LifeCycleObject* o) = 0; |
| 20 virtual void OnLifeCycleDestroy(LifeCycleObject* o) = 0; | 20 virtual void OnLifeCycleDestroy(LifeCycleObject* o) = 0; |
| 21 | 21 |
| 22 protected: | 22 protected: |
| 23 virtual ~Observer() {} | 23 virtual ~Observer() {} |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 ~LifeCycleObject() { |
| 27 observer_->OnLifeCycleDestroy(this); |
| 28 } |
| 29 |
| 30 private: |
| 31 friend class LifeCycleWatcher; |
| 32 |
| 26 explicit LifeCycleObject(Observer* observer) | 33 explicit LifeCycleObject(Observer* observer) |
| 27 : observer_(observer) { | 34 : observer_(observer) { |
| 28 observer_->OnLifeCycleConstruct(this); | 35 observer_->OnLifeCycleConstruct(this); |
| 29 } | 36 } |
| 30 | 37 |
| 31 ~LifeCycleObject() { | |
| 32 observer_->OnLifeCycleDestroy(this); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 Observer* observer_; | 38 Observer* observer_; |
| 37 | 39 |
| 38 DISALLOW_COPY_AND_ASSIGN(LifeCycleObject); | 40 DISALLOW_COPY_AND_ASSIGN(LifeCycleObject); |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 // The life cycle states we care about for the purposes of testing ScopedVector | 43 // The life cycle states we care about for the purposes of testing ScopedVector |
| 42 // against objects. | 44 // against objects. |
| 43 enum LifeCycleState { | 45 enum LifeCycleState { |
| 44 LC_INITIAL, | 46 LC_INITIAL, |
| 45 LC_CONSTRUCTED, | 47 LC_CONSTRUCTED, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } | 110 } |
| 109 | 111 |
| 110 TEST(ScopedVectorTest, Clear) { | 112 TEST(ScopedVectorTest, Clear) { |
| 111 LifeCycleWatcher watcher; | 113 LifeCycleWatcher watcher; |
| 112 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 114 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
| 113 ScopedVector<LifeCycleObject> scoped_vector; | 115 ScopedVector<LifeCycleObject> scoped_vector; |
| 114 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 116 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
| 115 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 117 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 116 scoped_vector.clear(); | 118 scoped_vector.clear(); |
| 117 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); | 119 EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); |
| 118 EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size()); | 120 EXPECT_TRUE(scoped_vector.empty()); |
| 119 } | 121 } |
| 120 | 122 |
| 121 TEST(ScopedVectorTest, WeakClear) { | 123 TEST(ScopedVectorTest, WeakClear) { |
| 122 LifeCycleWatcher watcher; | 124 LifeCycleWatcher watcher; |
| 123 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 125 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
| 124 ScopedVector<LifeCycleObject> scoped_vector; | 126 ScopedVector<LifeCycleObject> scoped_vector; |
| 125 scoped_ptr<LifeCycleObject> object(watcher.NewLifeCycleObject()); | 127 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
| 126 scoped_vector.push_back(object.get()); | |
| 127 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 128 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 128 scoped_vector.weak_clear(); | 129 scoped_vector.weak_clear(); |
| 129 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 130 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 130 EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size()); | 131 EXPECT_TRUE(scoped_vector.empty()); |
| 131 } | 132 } |
| 132 | 133 |
| 133 TEST(ScopedVectorTest, Scope) { | 134 TEST(ScopedVectorTest, Scope) { |
| 134 LifeCycleWatcher watcher; | 135 LifeCycleWatcher watcher; |
| 135 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); | 136 EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); |
| 136 { | 137 { |
| 137 ScopedVector<LifeCycleObject> scoped_vector; | 138 ScopedVector<LifeCycleObject> scoped_vector; |
| 138 scoped_vector.push_back(watcher.NewLifeCycleObject()); | 139 scoped_vector.push_back(watcher.NewLifeCycleObject()); |
| 139 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); | 140 EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); |
| 140 } | 141 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) | 235 for(LifeCycleWatcher* it = watchers; it != watchers + 1; ++it) |
| 235 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); | 236 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); |
| 236 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) | 237 for(LifeCycleWatcher* it = watchers + 1; it != watchers + 3; ++it) |
| 237 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); | 238 EXPECT_EQ(LC_DESTROYED, it->life_cycle_state()); |
| 238 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); | 239 for(LifeCycleWatcher* it = watchers + 3; it != watchers + arraysize(watchers); |
| 239 ++it) | 240 ++it) |
| 240 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); | 241 EXPECT_EQ(LC_CONSTRUCTED, it->life_cycle_state()); |
| 241 } | 242 } |
| 242 | 243 |
| 243 } // namespace | 244 } // namespace |
| OLD | NEW |