Home | History | Annotate | Download | only in base
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef MEDIA_BASE_GMOCK_CALLBACK_SUPPORT_H_
      6 #define MEDIA_BASE_GMOCK_CALLBACK_SUPPORT_H_
      7 
      8 #include "testing/gmock/include/gmock/gmock.h"
      9 
     10 namespace media {
     11 
     12 // Matchers for base::Callback and base::Closure.
     13 
     14 MATCHER(IsNullCallback, "a null callback") {
     15   return (arg.is_null());
     16 }
     17 
     18 MATCHER(IsNotNullCallback, "a non-null callback") {
     19   return (!arg.is_null());
     20 }
     21 
     22 // The RunClosure<N>() action invokes Run() method on the N-th (0-based)
     23 // argument of the mock function.
     24 
     25 ACTION_TEMPLATE(RunClosure,
     26                 HAS_1_TEMPLATE_PARAMS(int, k),
     27                 AND_0_VALUE_PARAMS()) {
     28   ::std::tr1::get<k>(args).Run();
     29 }
     30 
     31 // Various overloads for RunCallback<N>().
     32 //
     33 // The RunCallback<N>(p1, p2, ..., p_k) action invokes Run() method on the N-th
     34 // (0-based) argument of the mock function, with arguments p1, p2, ..., p_k.
     35 //
     36 // Notes:
     37 //
     38 //   1. The arguments are passed by value by default.  If you need to
     39 //   pass an argument by reference, wrap it inside ByRef().  For example,
     40 //
     41 //     RunCallback<1>(5, string("Hello"), ByRef(foo))
     42 //
     43 //   passes 5 and string("Hello") by value, and passes foo by reference.
     44 //
     45 //   2. If the callback takes an argument by reference but ByRef() is
     46 //   not used, it will receive the reference to a copy of the value,
     47 //   instead of the original value.  For example, when the 0-th
     48 //   argument of the callback takes a const string&, the action
     49 //
     50 //     RunCallback<0>(string("Hello"))
     51 //
     52 //   makes a copy of the temporary string("Hello") object and passes a
     53 //   reference of the copy, instead of the original temporary object,
     54 //   to the callback.  This makes it easy for a user to define an
     55 //   RunCallback action from temporary values and have it performed later.
     56 
     57 ACTION_TEMPLATE(RunCallback,
     58                 HAS_1_TEMPLATE_PARAMS(int, k),
     59                 AND_0_VALUE_PARAMS()) {
     60   return ::std::tr1::get<k>(args).Run();
     61 }
     62 
     63 ACTION_TEMPLATE(RunCallback,
     64                 HAS_1_TEMPLATE_PARAMS(int, k),
     65                 AND_1_VALUE_PARAMS(p0)) {
     66   return ::std::tr1::get<k>(args).Run(p0);
     67 }
     68 
     69 ACTION_TEMPLATE(RunCallback,
     70                 HAS_1_TEMPLATE_PARAMS(int, k),
     71                 AND_2_VALUE_PARAMS(p0, p1)) {
     72   return ::std::tr1::get<k>(args).Run(p0, p1);
     73 }
     74 
     75 ACTION_TEMPLATE(RunCallback,
     76                 HAS_1_TEMPLATE_PARAMS(int, k),
     77                 AND_3_VALUE_PARAMS(p0, p1, p2)) {
     78   return ::std::tr1::get<k>(args).Run(p0, p1, p2);
     79 }
     80 
     81 ACTION_TEMPLATE(RunCallback,
     82                 HAS_1_TEMPLATE_PARAMS(int, k),
     83                 AND_4_VALUE_PARAMS(p0, p1, p2, p3)) {
     84   return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3);
     85 }
     86 
     87 ACTION_TEMPLATE(RunCallback,
     88                 HAS_1_TEMPLATE_PARAMS(int, k),
     89                 AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4)) {
     90   return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3, p4);
     91 }
     92 
     93 ACTION_TEMPLATE(RunCallback,
     94                 HAS_1_TEMPLATE_PARAMS(int, k),
     95                 AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)) {
     96   return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3, p4, p5);
     97 }
     98 
     99 ACTION_TEMPLATE(RunCallback,
    100                 HAS_1_TEMPLATE_PARAMS(int, k),
    101                 AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6)) {
    102   return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3, p4, p5, p6);
    103 }
    104 
    105 }  // namespace media
    106 
    107 #endif  // MEDIA_BASE_GMOCK_CALLBACK_SUPPORT_H_
    108