Home | History | Annotate | Download | only in base
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/callback_list.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/bind.h"
      9 #include "base/bind_helpers.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace base {
     14 namespace {
     15 
     16 class Listener {
     17  public:
     18   Listener() : total_(0), scaler_(1) {}
     19   explicit Listener(int scaler) : total_(0), scaler_(scaler) {}
     20   void IncrementTotal() { total_++; }
     21   void IncrementByMultipleOfScaler(int x) { total_ += x * scaler_; }
     22 
     23   int total() const { return total_; }
     24 
     25  private:
     26   int total_;
     27   int scaler_;
     28   DISALLOW_COPY_AND_ASSIGN(Listener);
     29 };
     30 
     31 class Remover {
     32  public:
     33   Remover() : total_(0) {}
     34   void IncrementTotalAndRemove() {
     35     total_++;
     36     removal_subscription_.reset();
     37   }
     38   void SetSubscriptionToRemove(
     39       scoped_ptr<CallbackList<void(void)>::Subscription> sub) {
     40     removal_subscription_ = sub.Pass();
     41   }
     42 
     43   int total() const { return total_; }
     44 
     45  private:
     46   int total_;
     47   scoped_ptr<CallbackList<void(void)>::Subscription> removal_subscription_;
     48   DISALLOW_COPY_AND_ASSIGN(Remover);
     49 };
     50 
     51 class Adder {
     52  public:
     53   explicit Adder(CallbackList<void(void)>* cb_reg)
     54       : added_(false),
     55         total_(0),
     56         cb_reg_(cb_reg) {
     57   }
     58   void AddCallback() {
     59     if (!added_) {
     60       added_ = true;
     61       subscription_ =
     62           cb_reg_->Add(Bind(&Adder::IncrementTotal, Unretained(this)));
     63     }
     64   }
     65   void IncrementTotal() { total_++; }
     66 
     67   bool added() const { return added_; }
     68 
     69   int total() const { return total_; }
     70 
     71  private:
     72   bool added_;
     73   int total_;
     74   CallbackList<void(void)>* cb_reg_;
     75   scoped_ptr<CallbackList<void(void)>::Subscription> subscription_;
     76   DISALLOW_COPY_AND_ASSIGN(Adder);
     77 };
     78 
     79 class Summer {
     80  public:
     81   Summer() : value_(0) {}
     82 
     83   void AddOneParam(int a) { value_ = a; }
     84   void AddTwoParam(int a, int b) { value_ = a + b; }
     85   void AddThreeParam(int a, int b, int c) { value_ = a + b + c; }
     86   void AddFourParam(int a, int b, int c, int d) { value_ = a + b + c + d; }
     87   void AddFiveParam(int a, int b, int c, int d, int e) {
     88     value_ = a + b + c + d + e;
     89   }
     90   void AddSixParam(int a, int b, int c, int d, int e , int f) {
     91     value_ = a + b + c + d + e + f;
     92   }
     93 
     94   int value() const { return value_; }
     95 
     96  private:
     97   int value_;
     98   DISALLOW_COPY_AND_ASSIGN(Summer);
     99 };
    100 
    101 // Sanity check that we can instantiate a CallbackList for each arity.
    102 TEST(CallbackListTest, ArityTest) {
    103   Summer s;
    104 
    105   CallbackList<void(int)> c1;
    106   scoped_ptr<CallbackList<void(int)>::Subscription> subscription1 =
    107       c1.Add(Bind(&Summer::AddOneParam, Unretained(&s)));
    108 
    109   c1.Notify(1);
    110   EXPECT_EQ(1, s.value());
    111 
    112   CallbackList<void(int, int)> c2;
    113   scoped_ptr<CallbackList<void(int, int)>::Subscription> subscription2 =
    114       c2.Add(Bind(&Summer::AddTwoParam, Unretained(&s)));
    115 
    116   c2.Notify(1, 2);
    117   EXPECT_EQ(3, s.value());
    118 
    119   CallbackList<void(int, int, int)> c3;
    120   scoped_ptr<CallbackList<void(int, int, int)>::Subscription>
    121       subscription3 = c3.Add(Bind(&Summer::AddThreeParam, Unretained(&s)));
    122 
    123   c3.Notify(1, 2, 3);
    124   EXPECT_EQ(6, s.value());
    125 
    126   CallbackList<void(int, int, int, int)> c4;
    127   scoped_ptr<CallbackList<void(int, int, int, int)>::Subscription>
    128       subscription4 = c4.Add(Bind(&Summer::AddFourParam, Unretained(&s)));
    129 
    130   c4.Notify(1, 2, 3, 4);
    131   EXPECT_EQ(10, s.value());
    132 
    133   CallbackList<void(int, int, int, int, int)> c5;
    134   scoped_ptr<CallbackList<void(int, int, int, int, int)>::Subscription>
    135       subscription5 = c5.Add(Bind(&Summer::AddFiveParam, Unretained(&s)));
    136 
    137   c5.Notify(1, 2, 3, 4, 5);
    138   EXPECT_EQ(15, s.value());
    139 
    140   CallbackList<void(int, int, int, int, int, int)> c6;
    141   scoped_ptr<CallbackList<void(int, int, int, int, int, int)>::Subscription>
    142       subscription6 = c6.Add(Bind(&Summer::AddSixParam, Unretained(&s)));
    143 
    144   c6.Notify(1, 2, 3, 4, 5, 6);
    145   EXPECT_EQ(21, s.value());
    146 }
    147 
    148 // Sanity check that closures added to the list will be run, and those removed
    149 // from the list will not be run.
    150 TEST(CallbackListTest, BasicTest) {
    151   CallbackList<void(void)> cb_reg;
    152   Listener a, b, c;
    153 
    154   scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
    155       cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
    156   scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
    157       cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
    158 
    159   EXPECT_TRUE(a_subscription.get());
    160   EXPECT_TRUE(b_subscription.get());
    161 
    162   cb_reg.Notify();
    163 
    164   EXPECT_EQ(1, a.total());
    165   EXPECT_EQ(1, b.total());
    166 
    167   b_subscription.reset();
    168 
    169   scoped_ptr<CallbackList<void(void)>::Subscription> c_subscription =
    170       cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&c)));
    171 
    172   cb_reg.Notify();
    173 
    174   EXPECT_EQ(2, a.total());
    175   EXPECT_EQ(1, b.total());
    176   EXPECT_EQ(1, c.total());
    177 
    178   a_subscription.reset();
    179   b_subscription.reset();
    180   c_subscription.reset();
    181 }
    182 
    183 // Sanity check that callbacks with details added to the list will be run, with
    184 // the correct details, and those removed from the list will not be run.
    185 TEST(CallbackListTest, BasicTestWithParams) {
    186   CallbackList<void(int)> cb_reg;
    187   Listener a(1), b(-1), c(1);
    188 
    189   scoped_ptr<CallbackList<void(int)>::Subscription> a_subscription =
    190       cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&a)));
    191   scoped_ptr<CallbackList<void(int)>::Subscription> b_subscription =
    192       cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&b)));
    193 
    194   EXPECT_TRUE(a_subscription.get());
    195   EXPECT_TRUE(b_subscription.get());
    196 
    197   cb_reg.Notify(10);
    198 
    199   EXPECT_EQ(10, a.total());
    200   EXPECT_EQ(-10, b.total());
    201 
    202   b_subscription.reset();
    203 
    204   scoped_ptr<CallbackList<void(int)>::Subscription> c_subscription =
    205       cb_reg.Add(Bind(&Listener::IncrementByMultipleOfScaler, Unretained(&c)));
    206 
    207   cb_reg.Notify(10);
    208 
    209   EXPECT_EQ(20, a.total());
    210   EXPECT_EQ(-10, b.total());
    211   EXPECT_EQ(10, c.total());
    212 
    213   a_subscription.reset();
    214   b_subscription.reset();
    215   c_subscription.reset();
    216 }
    217 
    218 // Test the a callback can remove itself or a different callback from the list
    219 // during iteration without invalidating the iterator.
    220 TEST(CallbackListTest, RemoveCallbacksDuringIteration) {
    221   CallbackList<void(void)> cb_reg;
    222   Listener a, b;
    223   Remover remover_1, remover_2;
    224 
    225   scoped_ptr<CallbackList<void(void)>::Subscription> remover_1_sub =
    226       cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
    227           Unretained(&remover_1)));
    228   scoped_ptr<CallbackList<void(void)>::Subscription> remover_2_sub =
    229       cb_reg.Add(Bind(&Remover::IncrementTotalAndRemove,
    230           Unretained(&remover_2)));
    231   scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
    232       cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&a)));
    233   scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
    234       cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
    235 
    236   // |remover_1| will remove itself.
    237   remover_1.SetSubscriptionToRemove(remover_1_sub.Pass());
    238   // |remover_2| will remove a.
    239   remover_2.SetSubscriptionToRemove(a_subscription.Pass());
    240 
    241   cb_reg.Notify();
    242 
    243   // |remover_1| runs once (and removes itself), |remover_2| runs once (and
    244   // removes a), |a| never runs, and |b| runs once.
    245   EXPECT_EQ(1, remover_1.total());
    246   EXPECT_EQ(1, remover_2.total());
    247   EXPECT_EQ(0, a.total());
    248   EXPECT_EQ(1, b.total());
    249 
    250   cb_reg.Notify();
    251 
    252   // Only |remover_2| and |b| run this time.
    253   EXPECT_EQ(1, remover_1.total());
    254   EXPECT_EQ(2, remover_2.total());
    255   EXPECT_EQ(0, a.total());
    256   EXPECT_EQ(2, b.total());
    257 }
    258 
    259 // Test that a callback can add another callback to the list durning iteration
    260 // without invalidating the iterator. The newly added callback should be run on
    261 // the current iteration as will all other callbacks in the list.
    262 TEST(CallbackListTest, AddCallbacksDuringIteration) {
    263   CallbackList<void(void)> cb_reg;
    264   Adder a(&cb_reg);
    265   Listener b;
    266   scoped_ptr<CallbackList<void(void)>::Subscription> a_subscription =
    267       cb_reg.Add(Bind(&Adder::AddCallback, Unretained(&a)));
    268   scoped_ptr<CallbackList<void(void)>::Subscription> b_subscription =
    269       cb_reg.Add(Bind(&Listener::IncrementTotal, Unretained(&b)));
    270 
    271   cb_reg.Notify();
    272 
    273   EXPECT_EQ(1, a.total());
    274   EXPECT_EQ(1, b.total());
    275   EXPECT_TRUE(a.added());
    276 
    277   cb_reg.Notify();
    278 
    279   EXPECT_EQ(2, a.total());
    280   EXPECT_EQ(2, b.total());
    281 }
    282 
    283 // Sanity check: notifying an empty list is a no-op.
    284 TEST(CallbackListTest, EmptyList) {
    285   CallbackList<void(void)> cb_reg;
    286 
    287   cb_reg.Notify();
    288 }
    289 
    290 }  // namespace
    291 }  // namespace base
    292