| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "base/mac/bind_objc_block.h" | 5 #import "base/mac/bind_objc_block.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "build/build_config.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/gtest_mac.h" |
| 13 | 15 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
| 16 #endif | 18 #endif |
| 17 | 19 |
| 18 // This free-function is there to ensure that the object file in not discarded | |
| 19 // at link time when building with gyp (it is required because targets is built | |
| 20 // as a static library with gyp and not source set which cause the object file | |
| 21 // to be discarded if no symbol is used). Remove once gyp support is dropped. | |
| 22 void BindObjcBlockUnittestArcLinkerWorkaround() {} | |
| 23 | |
| 24 namespace { | 20 namespace { |
| 25 | 21 |
| 26 TEST(BindObjcBlockTestARC, TestScopedClosureRunnerExitScope) { | 22 TEST(BindObjcBlockTestARC, TestScopedClosureRunnerExitScope) { |
| 27 int run_count = 0; | 23 int run_count = 0; |
| 28 int* ptr = &run_count; | 24 int* ptr = &run_count; |
| 29 { | 25 { |
| 30 base::ScopedClosureRunner runner(base::BindBlock(^{ | 26 base::ScopedClosureRunner runner(base::BindBlockArc(^{ |
| 31 (*ptr)++; | 27 (*ptr)++; |
| 32 })); | 28 })); |
| 33 EXPECT_EQ(0, run_count); | 29 EXPECT_EQ(0, run_count); |
| 34 } | 30 } |
| 35 EXPECT_EQ(1, run_count); | 31 EXPECT_EQ(1, run_count); |
| 36 } | 32 } |
| 37 | 33 |
| 38 TEST(BindObjcBlockTestARC, TestScopedClosureRunnerRelease) { | 34 TEST(BindObjcBlockTestARC, TestScopedClosureRunnerRelease) { |
| 39 int run_count = 0; | 35 int run_count = 0; |
| 40 int* ptr = &run_count; | 36 int* ptr = &run_count; |
| 41 base::Closure c; | 37 base::Closure c; |
| 42 { | 38 { |
| 43 base::ScopedClosureRunner runner(base::BindBlock(^{ | 39 base::ScopedClosureRunner runner(base::BindBlockArc(^{ |
| 44 (*ptr)++; | 40 (*ptr)++; |
| 45 })); | 41 })); |
| 46 c = runner.Release(); | 42 c = runner.Release(); |
| 47 EXPECT_EQ(0, run_count); | 43 EXPECT_EQ(0, run_count); |
| 48 } | 44 } |
| 49 EXPECT_EQ(0, run_count); | 45 EXPECT_EQ(0, run_count); |
| 50 c.Run(); | 46 c.Run(); |
| 51 EXPECT_EQ(1, run_count); | 47 EXPECT_EQ(1, run_count); |
| 52 } | 48 } |
| 53 | 49 |
| 54 TEST(BindObjcBlockTestARC, TestReturnValue) { | 50 TEST(BindObjcBlockTestARC, TestReturnValue) { |
| 55 const int kReturnValue = 42; | 51 const int kReturnValue = 42; |
| 56 base::Callback<int(void)> c = base::BindBlock(^{return kReturnValue;}); | 52 base::Callback<int(void)> c = base::BindBlockArc(^{ |
| 53 return kReturnValue; |
| 54 }); |
| 57 EXPECT_EQ(kReturnValue, c.Run()); | 55 EXPECT_EQ(kReturnValue, c.Run()); |
| 58 } | 56 } |
| 59 | 57 |
| 60 TEST(BindObjcBlockTestARC, TestArgument) { | 58 TEST(BindObjcBlockTestARC, TestArgument) { |
| 61 const int kArgument = 42; | 59 const int kArgument = 42; |
| 62 base::Callback<int(int)> c = base::BindBlock(^(int a){return a + 1;}); | 60 base::Callback<int(int)> c = base::BindBlockArc(^(int a) { |
| 61 return a + 1; |
| 62 }); |
| 63 EXPECT_EQ(kArgument + 1, c.Run(kArgument)); | 63 EXPECT_EQ(kArgument + 1, c.Run(kArgument)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 TEST(BindObjcBlockTestARC, TestTwoArguments) { | 66 TEST(BindObjcBlockTestARC, TestTwoArguments) { |
| 67 std::string result; | 67 std::string result; |
| 68 std::string* ptr = &result; | 68 std::string* ptr = &result; |
| 69 base::Callback<void(const std::string&, const std::string&)> c = | 69 base::Callback<void(const std::string&, const std::string&)> c = |
| 70 base::BindBlock(^(const std::string& a, const std::string& b) { | 70 base::BindBlockArc(^(const std::string& a, const std::string& b) { |
| 71 *ptr = a + b; | 71 *ptr = a + b; |
| 72 }); | 72 }); |
| 73 c.Run("forty", "two"); | 73 c.Run("forty", "two"); |
| 74 EXPECT_EQ(result, "fortytwo"); | 74 EXPECT_EQ(result, "fortytwo"); |
| 75 } | 75 } |
| 76 | 76 |
| 77 TEST(BindObjcBlockTestARC, TestThreeArguments) { | 77 TEST(BindObjcBlockTestARC, TestThreeArguments) { |
| 78 std::string result; | 78 std::string result; |
| 79 std::string* ptr = &result; | 79 std::string* ptr = &result; |
| 80 base::Callback<void(const std::string&, | 80 base::Callback<void(const std::string&, const std::string&, |
| 81 const std::string&, | 81 const std::string&)> |
| 82 const std::string&)> c = | 82 c = base::BindBlockArc( |
| 83 base::BindBlock(^(const std::string& a, | 83 ^(const std::string& a, const std::string& b, const std::string& c) { |
| 84 const std::string& b, | 84 *ptr = a + b + c; |
| 85 const std::string& c) { | 85 }); |
| 86 *ptr = a + b + c; | |
| 87 }); | |
| 88 c.Run("six", "times", "nine"); | 86 c.Run("six", "times", "nine"); |
| 89 EXPECT_EQ(result, "sixtimesnine"); | 87 EXPECT_EQ(result, "sixtimesnine"); |
| 90 } | 88 } |
| 91 | 89 |
| 92 TEST(BindObjcBlockTestARC, TestSixArguments) { | 90 TEST(BindObjcBlockTestARC, TestSixArguments) { |
| 93 std::string result1; | 91 std::string result1; |
| 94 std::string* ptr = &result1; | 92 std::string* ptr = &result1; |
| 95 int result2; | 93 int result2; |
| 96 int* ptr2 = &result2; | 94 int* ptr2 = &result2; |
| 97 base::Callback<void(int, int, const std::string&, const std::string&, | 95 base::Callback<void(int, int, const std::string&, const std::string&, int, |
| 98 int, const std::string&)> c = | 96 const std::string&)> |
| 99 base::BindBlock(^(int a, int b, const std::string& c, | 97 c = base::BindBlockArc(^(int a, int b, const std::string& c, |
| 100 const std::string& d, int e, const std::string& f) { | 98 const std::string& d, int e, |
| 101 *ptr = c + d + f; | 99 const std::string& f) { |
| 102 *ptr2 = a + b + e; | 100 *ptr = c + d + f; |
| 101 *ptr2 = a + b + e; |
| 103 }); | 102 }); |
| 104 c.Run(1, 2, "infinite", "improbability", 3, "drive"); | 103 c.Run(1, 2, "infinite", "improbability", 3, "drive"); |
| 105 EXPECT_EQ(result1, "infiniteimprobabilitydrive"); | 104 EXPECT_EQ(result1, "infiniteimprobabilitydrive"); |
| 106 EXPECT_EQ(result2, 6); | 105 EXPECT_EQ(result2, 6); |
| 107 } | 106 } |
| 108 | 107 |
| 108 #if defined(OS_IOS) |
| 109 |
| 110 TEST(BindObjcBlockTestARC, TestBlockReleased) { |
| 111 __weak NSObject* weak_nsobject; |
| 112 @autoreleasepool { |
| 113 NSObject* nsobject = [[NSObject alloc] init]; |
| 114 weak_nsobject = nsobject; |
| 115 |
| 116 auto callback = base::BindBlockArc(^{ |
| 117 [nsobject description]; |
| 118 }); |
| 119 } |
| 120 EXPECT_NSEQ(nil, weak_nsobject); |
| 121 } |
| 122 |
| 123 #endif |
| 124 |
| 109 } // namespace | 125 } // namespace |
| OLD | NEW |