diff options
author | xhwang@chromium.org <xhwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-13 05:49:48 +0000 |
---|---|---|
committer | xhwang@chromium.org <xhwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-13 05:49:48 +0000 |
commit | 8b10f2225bcce231a75e96755f8812f026271c08 (patch) | |
tree | 62ba81d8582d52b6dd8a973cd9227c0ee87d0d71 /media/base/gmock_callback_support.h | |
parent | 025f9a7e737e5448d17a882cf37a8ca3fcf3666f (diff) | |
download | chromium_src-8b10f2225bcce231a75e96755f8812f026271c08.zip chromium_src-8b10f2225bcce231a75e96755f8812f026271c08.tar.gz chromium_src-8b10f2225bcce231a75e96755f8812f026271c08.tar.bz2 |
Add RunCallback to invoke a callback parameter in unittests.
In gmock, ::testing::InvokeArgument doesn't support chromium base::Callback. Adding customized templated action RunCallback to support this.
Also added new matchers for base::callback.
BUG=none
TEST=Added unittests to test the new matchers and actions. Updated media_unittests to use the new matchers and actions.
Review URL: https://chromiumcodereview.appspot.com/11359100
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167309 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/base/gmock_callback_support.h')
-rw-r--r-- | media/base/gmock_callback_support.h | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/media/base/gmock_callback_support.h b/media/base/gmock_callback_support.h new file mode 100644 index 0000000..22f4c10 --- /dev/null +++ b/media/base/gmock_callback_support.h @@ -0,0 +1,107 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MEDIA_BASE_GMOCK_CALLBACK_SUPPORT_H_ +#define MEDIA_BASE_GMOCK_CALLBACK_SUPPORT_H_ + +#include "testing/gmock/include/gmock/gmock.h" + +namespace media { + +// Matchers for base::Callback and base::Closure. + +MATCHER(IsNullCallback, "a null callback") { + return (arg.is_null()); +} + +MATCHER(IsNotNullCallback, "a non-null callback") { + return (!arg.is_null()); +} + +// The RunClosure<N>() action invokes Run() method on the N-th (0-based) +// argument of the mock function. + +ACTION_TEMPLATE(RunClosure, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_0_VALUE_PARAMS()) { + ::std::tr1::get<k>(args).Run(); +} + +// Various overloads for RunCallback<N>(). +// +// The RunCallback<N>(p1, p2, ..., p_k) action invokes Run() method on the N-th +// (0-based) argument of the mock function, with arguments p1, p2, ..., p_k. +// +// Notes: +// +// 1. The arguments are passed by value by default. If you need to +// pass an argument by reference, wrap it inside ByRef(). For example, +// +// RunCallback<1>(5, string("Hello"), ByRef(foo)) +// +// passes 5 and string("Hello") by value, and passes foo by reference. +// +// 2. If the callback takes an argument by reference but ByRef() is +// not used, it will receive the reference to a copy of the value, +// instead of the original value. For example, when the 0-th +// argument of the callback takes a const string&, the action +// +// RunCallback<0>(string("Hello")) +// +// makes a copy of the temporary string("Hello") object and passes a +// reference of the copy, instead of the original temporary object, +// to the callback. This makes it easy for a user to define an +// RunCallback action from temporary values and have it performed later. + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_0_VALUE_PARAMS()) { + return ::std::tr1::get<k>(args).Run(); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_1_VALUE_PARAMS(p0)) { + return ::std::tr1::get<k>(args).Run(p0); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_2_VALUE_PARAMS(p0, p1)) { + return ::std::tr1::get<k>(args).Run(p0, p1); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_3_VALUE_PARAMS(p0, p1, p2)) { + return ::std::tr1::get<k>(args).Run(p0, p1, p2); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_4_VALUE_PARAMS(p0, p1, p2, p3)) { + return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4)) { + return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3, p4); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)) { + return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3, p4, p5); +} + +ACTION_TEMPLATE(RunCallback, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_7_VALUE_PARAMS(p0, p1, p2, p3, p4, p5, p6)) { + return ::std::tr1::get<k>(args).Run(p0, p1, p2, p3, p4, p5, p6); +} + +} // namespace media + +#endif // MEDIA_BASE_GMOCK_CALLBACK_SUPPORT_H_ |