Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2014 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_BASE_SIGSLOTTESTER_H_
     29 #define TALK_BASE_SIGSLOTTESTER_H_
     30 
     31 // To generate sigslottester.h from sigslottester.h.pump, execute:
     32 // /home/build/google3/third_party/gtest/scripts/pump.py sigslottester.h.pump
     33 
     34 
     35 // SigslotTester(s) are utility classes to check if signals owned by an
     36 // object are being invoked at the right time and with the right arguments.
     37 // They are meant to be used in tests. Tests must provide "capture" pointers
     38 // (i.e. address of variables) where the arguments from the signal callback
     39 // can be stored.
     40 //
     41 // Example:
     42 //   /* Some signal */
     43 //   sigslot::signal1<const std::string&> foo;
     44 //
     45 //   /* We want to monitor foo in some test. Note how signal argument is
     46 //      const std::string&, but capture-type is std::string. Capture type
     47 //      must be type that can be assigned to. */
     48 //   std::string capture;
     49 //   SigslotTester1<const std::string&, std::string> slot(&foo, &capture);
     50 //   foo.emit("hello");
     51 //   EXPECT_EQ(1, slot.callback_count());
     52 //   EXPECT_EQ("hello", capture);
     53 //   /* See unit-tests for more examples */
     54 
     55 #include "talk/base/constructormagic.h"
     56 #include "talk/base/sigslot.h"
     57 
     58 namespace talk_base {
     59 
     60 // For all the templates below:
     61 // - A1-A5 is the type of the argument i in the callback. Signals may and often
     62 //   do use const-references here for efficiency.
     63 // - C1-C5 is the type of the variable to capture argument i. These should be
     64 //   non-const value types suitable for use as lvalues.
     65 
     66 $var n = 5
     67 $range i 1..n
     68 $for i [[
     69 $range j 1..i
     70 
     71 template <$for j , [[class A$j]], $for j , [[class C$j]]>
     72 class SigslotTester$i : public sigslot::has_slots<> {
     73  public:
     74   SigslotTester$i(sigslot::signal$i<$for j , [[A$j]]>* signal,
     75                 $for j , [[C$j* capture$j]])
     76       : callback_count_(0),
     77       $for j , [[capture$j[[]]_(capture$j)]] {
     78     signal->connect(this, &SigslotTester$i::OnSignalCallback);
     79   }
     80 
     81   int callback_count() const { return callback_count_; }
     82 
     83  private:
     84   void OnSignalCallback($for j , [[A$j arg$j]]) {
     85     callback_count_++;$for j [[
     86 
     87     *capture$j[[]]_ = arg$j;]]
     88 
     89   }
     90 
     91   int callback_count_;$for j [[
     92 
     93   C$j* capture$j[[]]_;]]
     94 
     95 
     96   DISALLOW_COPY_AND_ASSIGN(SigslotTester$i);
     97 };
     98 
     99 ]]
    100 }  // namespace talk_base
    101 
    102 #endif  // TALK_BASE_SIGSLOTTESTER_H_
    103