blob: 1b1a50c2000094f3dc86bdd34c96dc218f54aef3 [file] [log] [blame]
[email protected]a5e2cd182012-07-25 17:47:581// 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#import "base/mac/bind_objc_block.h"
6
[email protected]d15483912014-01-28 21:11:157#include <string>
8
[email protected]a5e2cd182012-07-25 17:47:589#include "base/bind.h"
tapted574f09c2015-05-19 13:08:0810#include "base/callback.h"
[email protected]1c232c22013-08-30 02:04:0411#include "base/callback_helpers.h"
sdefresne3baab422016-11-23 16:41:2112#include "build/build_config.h"
[email protected]a5e2cd182012-07-25 17:47:5813#include "testing/gtest/include/gtest/gtest.h"
sdefresne3baab422016-11-23 16:41:2114#include "testing/gtest_mac.h"
[email protected]a5e2cd182012-07-25 17:47:5815
sdefresne3baab422016-11-23 16:41:2116#if defined(OS_IOS)
17#include "base/ios/weak_nsobject.h"
18#include "base/mac/scoped_nsautorelease_pool.h"
19#endif
sdefresnefd6251252016-06-16 11:51:0820
[email protected]a5e2cd182012-07-25 17:47:5821namespace {
22
23TEST(BindObjcBlockTest, TestScopedClosureRunnerExitScope) {
24 int run_count = 0;
25 int* ptr = &run_count;
26 {
27 base::ScopedClosureRunner runner(base::BindBlock(^{
28 (*ptr)++;
29 }));
30 EXPECT_EQ(0, run_count);
31 }
32 EXPECT_EQ(1, run_count);
33}
34
35TEST(BindObjcBlockTest, TestScopedClosureRunnerRelease) {
36 int run_count = 0;
37 int* ptr = &run_count;
38 base::Closure c;
39 {
40 base::ScopedClosureRunner runner(base::BindBlock(^{
41 (*ptr)++;
42 }));
43 c = runner.Release();
44 EXPECT_EQ(0, run_count);
45 }
46 EXPECT_EQ(0, run_count);
47 c.Run();
48 EXPECT_EQ(1, run_count);
49}
50
[email protected]49ac2072013-01-15 20:36:2951TEST(BindObjcBlockTest, TestReturnValue) {
52 const int kReturnValue = 42;
53 base::Callback<int(void)> c = base::BindBlock(^{return kReturnValue;});
54 EXPECT_EQ(kReturnValue, c.Run());
55}
56
[email protected]defe38f42013-04-24 08:56:1157TEST(BindObjcBlockTest, TestArgument) {
58 const int kArgument = 42;
59 base::Callback<int(int)> c = base::BindBlock(^(int a){return a + 1;});
60 EXPECT_EQ(kArgument + 1, c.Run(kArgument));
61}
62
[email protected]d15483912014-01-28 21:11:1563TEST(BindObjcBlockTest, TestTwoArguments) {
64 std::string result;
65 std::string* ptr = &result;
66 base::Callback<void(const std::string&, const std::string&)> c =
67 base::BindBlock(^(const std::string& a, const std::string& b) {
68 *ptr = a + b;
69 });
70 c.Run("forty", "two");
71 EXPECT_EQ(result, "fortytwo");
72}
73
marqb239b092014-11-07 21:20:1574TEST(BindObjcBlockTest, TestThreeArguments) {
75 std::string result;
76 std::string* ptr = &result;
77 base::Callback<void(const std::string&,
78 const std::string&,
79 const std::string&)> c =
80 base::BindBlock(^(const std::string& a,
81 const std::string& b,
82 const std::string& c) {
83 *ptr = a + b + c;
84 });
85 c.Run("six", "times", "nine");
86 EXPECT_EQ(result, "sixtimesnine");
87}
88
89TEST(BindObjcBlockTest, TestSixArguments) {
90 std::string result1;
91 std::string* ptr = &result1;
92 int result2;
93 int* ptr2 = &result2;
94 base::Callback<void(int, int, const std::string&, const std::string&,
95 int, const std::string&)> c =
96 base::BindBlock(^(int a, int b, const std::string& c,
97 const std::string& d, int e, const std::string& f) {
98 *ptr = c + d + f;
99 *ptr2 = a + b + e;
100 });
101 c.Run(1, 2, "infinite", "improbability", 3, "drive");
102 EXPECT_EQ(result1, "infiniteimprobabilitydrive");
103 EXPECT_EQ(result2, 6);
104}
105
sdefresne3baab422016-11-23 16:41:21106#if defined(OS_IOS)
107
108TEST(BindObjcBlockTest, TestBlockReleased) {
109 base::WeakNSObject<NSObject> weak_nsobject;
110 {
111 base::mac::ScopedNSAutoreleasePool autorelease_pool;
112 NSObject* nsobject = [[[NSObject alloc] init] autorelease];
113 weak_nsobject.reset(nsobject);
114
115 auto callback = base::BindBlock(^{
116 [nsobject description];
117 });
118 }
119 EXPECT_NSEQ(nil, weak_nsobject);
120}
121
122#endif
123
[email protected]a5e2cd182012-07-25 17:47:58124} // namespace