summaryrefslogtreecommitdiff
path: root/chromium/base/ios/callback_counter_unittest.mm
blob: 6d194aaf6480a53209f077d6fa8b82762dd14091 (plain)
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
// Copyright 2015 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.

#include "base/ios/callback_counter.h"

#include "base/bind.h"
#include "base/mac/bind_objc_block.h"
#include "base/memory/ref_counted.h"
#import "base/test/ios/wait_util.h"
#include "testing/platform_test.h"

using CallbackCounterTest = PlatformTest;

// Tests that CallbackCounter works with adding callbacks one by one.
TEST_F(CallbackCounterTest, BasicIncrementByOne) {
  __block BOOL block_was_called = NO;
  scoped_refptr<CallbackCounter> callback_counter =
      new CallbackCounter(base::BindBlock(^{
        block_was_called = YES;
      }));

  // Enqueue the first callback.
  callback_counter->IncrementCount();
  dispatch_async(dispatch_get_main_queue(), ^{
    callback_counter->DecrementCount();
  });

  // Enqueue the second callback.
  callback_counter->IncrementCount();
  dispatch_async(dispatch_get_main_queue(), ^{
    callback_counter->DecrementCount();
  });

  base::test::ios::WaitUntilCondition(^bool() {
    return block_was_called;
  });
}

// Tests that CallbackCounter works with adding all callbacks at once.
TEST_F(CallbackCounterTest, BasicIncrementByMoreThanOne) {
  __block BOOL block_was_called = NO;
  scoped_refptr<CallbackCounter> callback_counter =
      new CallbackCounter(base::BindBlock(^{
        block_was_called = YES;
      }));

  // Enqueue the 5 callbacks.
  callback_counter->IncrementCount(5);
  for (int i = 0; i < 5; i++) {
    dispatch_async(dispatch_get_main_queue(), ^{
      callback_counter->DecrementCount();
    });
  }
  base::test::ios::WaitUntilCondition(^bool() {
    return block_was_called;
  });
}