summaryrefslogtreecommitdiff
path: root/chromium/base/run_loop_unittest.cc
blob: a87ced09885f041a148f04c639620fec4e58aea3 (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
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
// Copyright 2016 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/run_loop.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

void QuitWhenIdleTask(RunLoop* run_loop, int* counter) {
  run_loop->QuitWhenIdle();
  ++(*counter);
}

void ShouldRunTask(int* counter) {
  ++(*counter);
}

void ShouldNotRunTask() {
  ADD_FAILURE() << "Ran a task that shouldn't run.";
}

void RunNestedLoopTask(int* counter) {
  RunLoop nested_run_loop;

  // This task should quit |nested_run_loop| but not the main RunLoop.
  ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, Bind(&QuitWhenIdleTask, Unretained(&nested_run_loop),
                      Unretained(counter)));

  ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1));

  MessageLoop::ScopedNestableTaskAllower allower(MessageLoop::current());
  nested_run_loop.Run();

  ++(*counter);
}

class RunLoopTest : public testing::Test {
 protected:
  RunLoopTest() = default;

  MessageLoop message_loop_;
  RunLoop run_loop_;
  int counter_ = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(RunLoopTest);
};

}  // namespace

TEST_F(RunLoopTest, QuitWhenIdle) {
  message_loop_.task_runner()->PostTask(
      FROM_HERE,
      Bind(&QuitWhenIdleTask, Unretained(&run_loop_), Unretained(&counter_)));
  message_loop_.task_runner()->PostTask(
      FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_)));
  message_loop_.task_runner()->PostDelayedTask(
      FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1));

  run_loop_.Run();
  EXPECT_EQ(2, counter_);
}

TEST_F(RunLoopTest, QuitWhenIdleNestedLoop) {
  message_loop_.task_runner()->PostTask(
      FROM_HERE, Bind(&RunNestedLoopTask, Unretained(&counter_)));
  message_loop_.task_runner()->PostTask(
      FROM_HERE,
      Bind(&QuitWhenIdleTask, Unretained(&run_loop_), Unretained(&counter_)));
  message_loop_.task_runner()->PostTask(
      FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_)));
  message_loop_.task_runner()->PostDelayedTask(
      FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1));

  run_loop_.Run();
  EXPECT_EQ(4, counter_);
}

TEST_F(RunLoopTest, QuitWhenIdleClosure) {
  message_loop_.task_runner()->PostTask(FROM_HERE,
                                        run_loop_.QuitWhenIdleClosure());
  message_loop_.task_runner()->PostTask(
      FROM_HERE, Bind(&ShouldRunTask, Unretained(&counter_)));
  message_loop_.task_runner()->PostDelayedTask(
      FROM_HERE, Bind(&ShouldNotRunTask), TimeDelta::FromDays(1));

  run_loop_.Run();
  EXPECT_EQ(1, counter_);
}

// Verify that the QuitWhenIdleClosure() can run after the RunLoop has been
// deleted. It should have no effect.
TEST_F(RunLoopTest, QuitWhenIdleClosureAfterRunLoopScope) {
  Closure quit_when_idle_closure;
  {
    RunLoop run_loop;
    quit_when_idle_closure = run_loop.QuitWhenIdleClosure();
    run_loop.RunUntilIdle();
  }
  quit_when_idle_closure.Run();
}

}  // namespace base