| 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/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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 194 |
| 195 observer_list.AddObserver(&evil); | 195 observer_list.AddObserver(&evil); |
| 196 observer_list.AddObserver(&c); | 196 observer_list.AddObserver(&c); |
| 197 observer_list.AddObserver(&d); | 197 observer_list.AddObserver(&d); |
| 198 | 198 |
| 199 // Removing an observer not in the list should do nothing. | 199 // Removing an observer not in the list should do nothing. |
| 200 observer_list.RemoveObserver(&e); | 200 observer_list.RemoveObserver(&e); |
| 201 | 201 |
| 202 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); | 202 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 203 | 203 |
| 204 EXPECT_EQ(a.total, 20); | 204 EXPECT_EQ(20, a.total); |
| 205 EXPECT_EQ(b.total, -20); | 205 EXPECT_EQ(-20, b.total); |
| 206 EXPECT_EQ(c.total, 0); | 206 EXPECT_EQ(0, c.total); |
| 207 EXPECT_EQ(d.total, -10); | 207 EXPECT_EQ(-10, d.total); |
| 208 EXPECT_EQ(e.total, 0); | 208 EXPECT_EQ(0, e.total); |
| 209 } | 209 } |
| 210 | 210 |
| 211 TEST(ObserverListThreadSafeTest, BasicTest) { | 211 TEST(ObserverListThreadSafeTest, BasicTest) { |
| 212 MessageLoop loop; | 212 MessageLoop loop; |
| 213 | 213 |
| 214 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 214 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 215 new ObserverListThreadSafe<Foo>); | 215 new ObserverListThreadSafe<Foo>); |
| 216 Adder a(1); | 216 Adder a(1); |
| 217 Adder b(-1); | 217 Adder b(-1); |
| 218 Adder c(1); | 218 Adder c(1); |
| 219 Adder d(-1); | 219 Adder d(-1); |
| 220 ThreadSafeDisrupter evil(observer_list.get(), &c); | 220 ThreadSafeDisrupter evil(observer_list.get(), &c); |
| 221 | 221 |
| 222 observer_list->AddObserver(&a); | 222 observer_list->AddObserver(&a); |
| 223 observer_list->AddObserver(&b); | 223 observer_list->AddObserver(&b); |
| 224 | 224 |
| 225 observer_list->Notify(&Foo::Observe, 10); | 225 observer_list->Notify(&Foo::Observe, 10); |
| 226 loop.RunAllPending(); | 226 loop.RunAllPending(); |
| 227 | 227 |
| 228 observer_list->AddObserver(&evil); | 228 observer_list->AddObserver(&evil); |
| 229 observer_list->AddObserver(&c); | 229 observer_list->AddObserver(&c); |
| 230 observer_list->AddObserver(&d); | 230 observer_list->AddObserver(&d); |
| 231 | 231 |
| 232 observer_list->Notify(&Foo::Observe, 10); | 232 observer_list->Notify(&Foo::Observe, 10); |
| 233 loop.RunAllPending(); | 233 loop.RunAllPending(); |
| 234 | 234 |
| 235 EXPECT_EQ(a.total, 20); | 235 EXPECT_EQ(20, a.total); |
| 236 EXPECT_EQ(b.total, -20); | 236 EXPECT_EQ(-20, b.total); |
| 237 EXPECT_EQ(c.total, 0); | 237 EXPECT_EQ(0, c.total); |
| 238 EXPECT_EQ(d.total, -10); | 238 EXPECT_EQ(-10, d.total); |
| 239 } | 239 } |
| 240 | 240 |
| 241 TEST(ObserverListThreadSafeTest, RemoveObserver) { | 241 TEST(ObserverListThreadSafeTest, RemoveObserver) { |
| 242 MessageLoop loop; | 242 MessageLoop loop; |
| 243 | 243 |
| 244 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 244 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 245 new ObserverListThreadSafe<Foo>); | 245 new ObserverListThreadSafe<Foo>); |
| 246 Adder a(1), b(1); | 246 Adder a(1), b(1); |
| 247 | 247 |
| 248 // A workaround for the compiler bug. See http://crbug.com/121960. | 248 // A workaround for the compiler bug. See http://crbug.com/121960. |
| 249 EXPECT_NE(&a, &b); | 249 EXPECT_NE(&a, &b); |
| 250 | 250 |
| 251 // Should do nothing. | 251 // Should do nothing. |
| 252 observer_list->RemoveObserver(&a); | 252 observer_list->RemoveObserver(&a); |
| 253 observer_list->RemoveObserver(&b); | 253 observer_list->RemoveObserver(&b); |
| 254 | 254 |
| 255 observer_list->Notify(&Foo::Observe, 10); | 255 observer_list->Notify(&Foo::Observe, 10); |
| 256 loop.RunAllPending(); | 256 loop.RunAllPending(); |
| 257 | 257 |
| 258 EXPECT_EQ(a.total, 0); | 258 EXPECT_EQ(0, a.total); |
| 259 EXPECT_EQ(b.total, 0); | 259 EXPECT_EQ(0, b.total); |
| 260 | 260 |
| 261 observer_list->AddObserver(&a); | 261 observer_list->AddObserver(&a); |
| 262 | 262 |
| 263 // Should also do nothing. | 263 // Should also do nothing. |
| 264 observer_list->RemoveObserver(&b); | 264 observer_list->RemoveObserver(&b); |
| 265 | 265 |
| 266 observer_list->Notify(&Foo::Observe, 10); | 266 observer_list->Notify(&Foo::Observe, 10); |
| 267 loop.RunAllPending(); | 267 loop.RunAllPending(); |
| 268 | 268 |
| 269 EXPECT_EQ(a.total, 10); | 269 EXPECT_EQ(10, a.total); |
| 270 EXPECT_EQ(b.total, 0); | 270 EXPECT_EQ(0, b.total); |
| 271 } | 271 } |
| 272 | 272 |
| 273 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) { | 273 TEST(ObserverListThreadSafeTest, WithoutMessageLoop) { |
| 274 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( | 274 scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 275 new ObserverListThreadSafe<Foo>); | 275 new ObserverListThreadSafe<Foo>); |
| 276 | 276 |
| 277 Adder a(1), b(1), c(1); | 277 Adder a(1), b(1), c(1); |
| 278 | 278 |
| 279 // No MessageLoop, so these should not be added. | 279 // No MessageLoop, so these should not be added. |
| 280 observer_list->AddObserver(&a); | 280 observer_list->AddObserver(&a); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 ObserverList<Foo>* observer_list = new ObserverList<Foo>; | 540 ObserverList<Foo>* observer_list = new ObserverList<Foo>; |
| 541 ListDestructor a(observer_list); | 541 ListDestructor a(observer_list); |
| 542 observer_list->AddObserver(&a); | 542 observer_list->AddObserver(&a); |
| 543 | 543 |
| 544 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); | 544 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
| 545 // If this test fails, there'll be Valgrind errors when this function goes out | 545 // If this test fails, there'll be Valgrind errors when this function goes out |
| 546 // of scope. | 546 // of scope. |
| 547 } | 547 } |
| 548 | 548 |
| 549 } // namespace | 549 } // namespace |
| OLD | NEW |