initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1 | // 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 | |
| 33 | namespace { |
| 34 | |
| 35 | class ObserverListTest : public testing::Test { |
| 36 | }; |
| 37 | |
| 38 | class Foo { |
| 39 | public: |
| 40 | virtual void Observe(int x) = 0; |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 41 | virtual ~Foo() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class Adder : public Foo { |
| 45 | public: |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 46 | Adder(int scaler) : total(0), scaler_(scaler) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 47 | virtual void Observe(int x) { |
| 48 | total += x * scaler_; |
| 49 | } |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 50 | virtual ~Adder() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | int total; |
| 52 | private: |
| 53 | int scaler_; |
| 54 | }; |
| 55 | |
| 56 | class Disrupter : public Foo { |
| 57 | public: |
| 58 | Disrupter(ObserverList<Foo>& list, Foo* doomed) : list_(list), doomed_(doomed) { |
| 59 | } |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 60 | virtual ~Disrupter() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | virtual void Observe(int x) { |
| 62 | list_.RemoveObserver(doomed_); |
| 63 | } |
| 64 | private: |
| 65 | ObserverList<Foo>& list_; |
| 66 | Foo* doomed_; |
| 67 | }; |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | TEST(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 | } |