[email protected] | a5e2cd18 | 2012-07-25 17:47:58 | [diff] [blame] | 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 | #import "base/mac/bind_objc_block.h" |
| 6 | |
[email protected] | d1548391 | 2014-01-28 21:11:15 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | a5e2cd18 | 2012-07-25 17:47:58 | [diff] [blame] | 9 | #include "base/bind.h" |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
sdefresne | 3baab42 | 2016-11-23 16:41:21 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | a5e2cd18 | 2012-07-25 17:47:58 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
sdefresne | 3baab42 | 2016-11-23 16:41:21 | [diff] [blame] | 14 | #include "testing/gtest_mac.h" |
[email protected] | a5e2cd18 | 2012-07-25 17:47:58 | [diff] [blame] | 15 | |
sdefresne | 3baab42 | 2016-11-23 16:41:21 | [diff] [blame] | 16 | #if defined(OS_IOS) |
| 17 | #include "base/ios/weak_nsobject.h" |
| 18 | #include "base/mac/scoped_nsautorelease_pool.h" |
| 19 | #endif |
sdefresne | fd625125 | 2016-06-16 11:51:08 | [diff] [blame] | 20 | |
[email protected] | a5e2cd18 | 2012-07-25 17:47:58 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | TEST(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 | |
| 35 | TEST(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] | 49ac207 | 2013-01-15 20:36:29 | [diff] [blame] | 51 | TEST(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] | defe38f4 | 2013-04-24 08:56:11 | [diff] [blame] | 57 | TEST(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] | d1548391 | 2014-01-28 21:11:15 | [diff] [blame] | 63 | TEST(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 | |
marq | b239b09 | 2014-11-07 21:20:15 | [diff] [blame] | 74 | TEST(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 | |
| 89 | TEST(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 | |
sdefresne | 3baab42 | 2016-11-23 16:41:21 | [diff] [blame] | 106 | #if defined(OS_IOS) |
| 107 | |
| 108 | TEST(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] | a5e2cd18 | 2012-07-25 17:47:58 | [diff] [blame] | 124 | } // namespace |