blob: 4ae5ac333eb1191bff64f9cd4a3c2062a6429929 [file] [log] [blame]
initial.commitd7cae122008-07-26 21:49:381// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "base/observer_list.h"
31#include "testing/gtest/include/gtest/gtest.h"
32
33namespace {
34
35class ObserverListTest : public testing::Test {
36};
37
38class Foo {
39 public:
40 virtual void Observe(int x) = 0;
[email protected]bf92cfbe2008-08-08 16:37:4341 virtual ~Foo() {}
initial.commitd7cae122008-07-26 21:49:3842};
43
44class Adder : public Foo {
45 public:
[email protected]bf92cfbe2008-08-08 16:37:4346 Adder(int scaler) : total(0), scaler_(scaler) {}
initial.commitd7cae122008-07-26 21:49:3847 virtual void Observe(int x) {
48 total += x * scaler_;
49 }
[email protected]bf92cfbe2008-08-08 16:37:4350 virtual ~Adder() { }
initial.commitd7cae122008-07-26 21:49:3851 int total;
52 private:
53 int scaler_;
54};
55
56class Disrupter : public Foo {
57 public:
58 Disrupter(ObserverList<Foo>& list, Foo* doomed) : list_(list), doomed_(doomed) {
59 }
[email protected]bf92cfbe2008-08-08 16:37:4360 virtual ~Disrupter() { }
initial.commitd7cae122008-07-26 21:49:3861 virtual void Observe(int x) {
62 list_.RemoveObserver(doomed_);
63 }
64 private:
65 ObserverList<Foo>& list_;
66 Foo* doomed_;
67};
68
69} // namespace
70
71TEST(ObserverListTest, BasicTest) {
72 ObserverList<Foo> observer_list;
73 Adder a(1), b(-1), c(1), d(-1);
74 Disrupter evil(observer_list, &c);
75
76 observer_list.AddObserver(&a);
77 observer_list.AddObserver(&b);
78
79 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
80
81 observer_list.AddObserver(&evil);
82 observer_list.AddObserver(&c);
83 observer_list.AddObserver(&d);
84
85 FOR_EACH_OBSERVER(Foo, observer_list, Observe(10));
86
87 EXPECT_EQ(a.total, 20);
88 EXPECT_EQ(b.total, -20);
89 EXPECT_EQ(c.total, 0);
90 EXPECT_EQ(d.total, -10);
91}