1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288

base / apple / bind_objc_block_unittest.mm [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"

@interface CounterForBindObjcBlockTest : NSObject
@property(nonatomic, assign) NSUInteger counter;
@end

@implementation CounterForBindObjcBlockTest
@synthesize counter = _counter;
@end

@interface HelperForBindObjcBlockTest : NSObject

- (instancetype)initWithCounter:(CounterForBindObjcBlockTest*)counter;
- (void)incrementCounter;

@end

@implementation HelperForBindObjcBlockTest {
  CounterForBindObjcBlockTest* _counter;
}

- (instancetype)initWithCounter:(CounterForBindObjcBlockTest*)counter {
  if ((self = [super init])) {
    _counter = counter;
    DCHECK(_counter);
  }
  return self;
}

- (void)incrementCounter {
  ++_counter.counter;
}

@end

namespace {

TEST(BindObjcBlockTest, TestScopedClosureRunnerExitScope) {
  int run_count = 0;
  int* ptr = &run_count;
  {
    base::ScopedClosureRunner runner(base::BindOnce(^{
      (*ptr)++;
    }));
    EXPECT_EQ(0, run_count);
  }
  EXPECT_EQ(1, run_count);
}

TEST(BindObjcBlockTest, TestScopedClosureRunnerRelease) {
  int run_count = 0;
  int* ptr = &run_count;
  base::OnceClosure c;
  {
    base::ScopedClosureRunner runner(base::BindOnce(^{
      (*ptr)++;
    }));
    c = runner.Release();
    EXPECT_EQ(0, run_count);
  }
  EXPECT_EQ(0, run_count);
  std::move(c).Run();
  EXPECT_EQ(1, run_count);
}

TEST(BindObjcBlockTest, TestReturnValue) {
  const int kReturnValue = 42;
  base::OnceCallback<int(void)> c = base::BindOnce(^{
    return kReturnValue;
  });
  EXPECT_EQ(kReturnValue, std::move(c).Run());
}

TEST(BindObjcBlockTest, TestArgument) {
  const int kArgument = 42;
  base::OnceCallback<int(int)> c = base::BindOnce(^(int a) {
    return a + 1;
  });
  EXPECT_EQ(kArgument + 1, std::move(c).Run(kArgument));
}

TEST(BindObjcBlockTest, TestTwoArguments) {
  std::string result;
  std::string* ptr = &result;
  base::OnceCallback<void(const std::string&, const std::string&)> c =
      base::BindOnce(^(const std::string& a, const std::string& b) {
        *ptr = a + b;
      });
  std::move(c).Run("forty", "two");
  EXPECT_EQ(result, "fortytwo");
}

TEST(BindObjcBlockTest, TestThreeArguments) {
  std::string result;
  std::string* ptr = &result;
  base::OnceCallback<void(const std::string&, const std::string&,
                          const std::string&)>
      cb = base::BindOnce(
          ^(const std::string& a, const std::string& b, const std::string& c) {
            *ptr = a + b + c;
          });
  std::move(cb).Run("six", "times", "nine");
  EXPECT_EQ(result, "sixtimesnine");
}

TEST(BindObjcBlockTest, TestSixArguments) {
  std::string result1;
  std::string* ptr = &result1;
  int result2;
  int* ptr2 = &result2;
  base::OnceCallback<void(int, int, const std::string&, const std::string&, int,
                          const std::string&)>
      cb = base::BindOnce(^(int a, int b, const std::string& c,
                            const std::string& d, int e, const std::string& f) {
        *ptr = c + d + f;
        *ptr2 = a + b + e;
      });
  std::move(cb).Run(1, 2, "infinite", "improbability", 3, "drive");
  EXPECT_EQ(result1, "infiniteimprobabilitydrive");
  EXPECT_EQ(result2, 6);
}

TEST(BindObjcBlockTest, TestBlockMoveable) {
  base::OnceClosure c;
  __block BOOL invoked_block = NO;
  @autoreleasepool {
    c = base::BindOnce(
        ^(std::unique_ptr<BOOL> v) {
          invoked_block = *v;
        },
        std::make_unique<BOOL>(YES));
  }
  std::move(c).Run();
  EXPECT_TRUE(invoked_block);
}

// Tests that the bound block is retained until the end of its execution, even
// if the callback itself is destroyed during the invocation. It was found that
// some code depends on this behaviour (see https://crbug.com/845687).
TEST(BindObjcBlockTest, TestBlockDeallocation) {
  base::RepeatingClosure closure;
  __block BOOL invoked_block = NO;
  closure = base::BindRepeating(
      ^(base::RepeatingClosure* this_closure) {
        *this_closure = base::RepeatingClosure();
        invoked_block = YES;
      },
      &closure);
  closure.Run();
  EXPECT_TRUE(invoked_block);
}

TEST(BindObjcBlockTest, TestBlockReleased) {
  __weak NSObject* weak_nsobject;
  @autoreleasepool {
    NSObject* nsobject = [[NSObject alloc] init];
    weak_nsobject = nsobject;

    auto callback = base::BindOnce(^{
      [nsobject description];
    });
  }
  EXPECT_NSEQ(nil, weak_nsobject);
}

// Tests that base::BindOnce(..., __strong NSObject*, ...) strongly captures
// the Objective-C object.
TEST(BindObjcBlockTest, TestBindOnceBoundStrongPointer) {
  CounterForBindObjcBlockTest* counter =
      [[CounterForBindObjcBlockTest alloc] init];
  ASSERT_EQ(counter.counter, 0u);

  base::OnceClosure closure;
  @autoreleasepool {
    HelperForBindObjcBlockTest* helper =
        [[HelperForBindObjcBlockTest alloc] initWithCounter:counter];

    // Creates a closure with a lambda taking the parameter as a __strong
    // pointer and bound with a __strong pointer. This should retain the
    // object.
    closure = base::BindOnce(
        [](HelperForBindObjcBlockTest* helper) { [helper incrementCounter]; },
        helper);
  }

  // Check that calling the closure increments the counter since the helper
  // object was captured strongly and thus is retained by the closure.
  std::move(closure).Run();
  EXPECT_EQ(counter.counter, 1u);
}

// Tests that base::BindOnce(..., __weak NSObject*, ...) weakly captures
// the Objective-C object.
TEST(BindObjcBlockTest, TestBindOnceBoundWeakPointer) {
  CounterForBindObjcBlockTest* counter =
      [[CounterForBindObjcBlockTest alloc] init];
  ASSERT_EQ(counter.counter, 0u);

  base::OnceClosure closure;
  @autoreleasepool {
    HelperForBindObjcBlockTest* helper =
        [[HelperForBindObjcBlockTest alloc] initWithCounter:counter];

    // Creates a closure with a lambda taking the parameter as a __strong
    // pointer and bound with a __weak pointer. This should not retain the
    // object.
    __weak HelperForBindObjcBlockTest* weak_helper = helper;
    closure = base::BindOnce(
        [](HelperForBindObjcBlockTest* helper) { [helper incrementCounter]; },
        weak_helper);
  }

  // Check that calling the closure does not increment the counter since
  // the helper object was captured weakly and thus is not retained by
  // the closure.
  std::move(closure).Run();
  EXPECT_EQ(counter.counter, 0u);
}

// Tests that base::BindRepeating(..., __strong NSObject*, ...) strongly
// captures the Objective-C object.
TEST(BindObjcBlockTest, TestBindRepeatingBoundStrongPointer) {
  CounterForBindObjcBlockTest* counter =
      [[CounterForBindObjcBlockTest alloc] init];
  ASSERT_EQ(counter.counter, 0u);

  base::RepeatingClosure closure;
  @autoreleasepool {
    HelperForBindObjcBlockTest* helper =
        [[HelperForBindObjcBlockTest alloc] initWithCounter:counter];

    // Creates a closure with a lambda taking the parameter as a __strong
    // pointer and bound with a __strong pointer. This should retain the
    // object.
    closure = base::BindRepeating(
        [](HelperForBindObjcBlockTest* helper) { [helper incrementCounter]; },
        helper);
  }

  // Check that calling the closure increments the counter since the helper
  // object was captured strongly and thus is retained by the closure.
  closure.Run();
  closure.Run();
  closure.Run();
  EXPECT_EQ(counter.counter, 3u);
}

// Tests that base::BindRepeating(..., __weak NSObject*, ...) weakly captures
// the Objective-C object.
TEST(BindObjcBlockTest, TestBindRepeatingBoundWeakPointer) {
  CounterForBindObjcBlockTest* counter =
      [[CounterForBindObjcBlockTest alloc] init];
  ASSERT_EQ(counter.counter, 0u);

  base::RepeatingClosure closure;
  @autoreleasepool {
    HelperForBindObjcBlockTest* helper =
        [[HelperForBindObjcBlockTest alloc] initWithCounter:counter];

    // Creates a closure with a lambda taking the parameter as a __strong
    // pointer and bound with a __weak pointer. This should not retain the
    // object.
    __weak HelperForBindObjcBlockTest* weak_helper = helper;
    closure = base::BindRepeating(
        [](HelperForBindObjcBlockTest* helper) { [helper incrementCounter]; },
        weak_helper);
  }

  // Check that calling the closure does not increment the counter since
  // the helper object was captured weakly and thus is not retained by
  // the closure.
  closure.Run();
  closure.Run();
  closure.Run();
  EXPECT_EQ(counter.counter, 0u);
}

}  // namespace