fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / base / barrier_closure_unittest.cc
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/barrier_closure.h"
6
7 #include "base/functional/bind.h"
8 #include "base/functional/callback.h"
9 #include "base/functional/callback_helpers.h"
10 #include "base/memory/raw_ptr.h"
11 #include "base/test/bind.h"
12 #include "base/test/gtest_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 TEST(BarrierClosureTest, RunImmediatelyForZeroClosures) {
18   int count = 0;
19   base::RepeatingClosure barrier_closure = base::BarrierClosure(
20       0, base::BindLambdaForTesting([&count]() { ++count; }));
21   EXPECT_EQ(1, count);
22 }
23
24 TEST(BarrierClosureTest, ChecksIfCalledForZeroClosures) {
25   base::RepeatingClosure barrier_closure =
26       base::BarrierClosure(0, base::DoNothing());
27   EXPECT_FALSE(barrier_closure.is_null());
28
29   EXPECT_CHECK_DEATH(barrier_closure.Run());
30 }
31
32 TEST(BarrierClosureTest, RunAfterNumClosures) {
33   int count = 0;
34   base::RepeatingClosure barrier_closure = base::BarrierClosure(
35       2, base::BindLambdaForTesting([&count]() { ++count; }));
36   EXPECT_EQ(0, count);
37
38   barrier_closure.Run();
39   EXPECT_EQ(0, count);
40
41   barrier_closure.Run();
42   EXPECT_EQ(1, count);
43 }
44
45 class DestructionIndicator {
46  public:
47   // Sets |*destructed| to true in destructor.
48   DestructionIndicator(bool* destructed) : destructed_(destructed) {
49     *destructed_ = false;
50   }
51
52   ~DestructionIndicator() { *destructed_ = true; }
53
54   void DoNothing() {}
55
56  private:
57   raw_ptr<bool> destructed_;
58 };
59
60 TEST(BarrierClosureTest, ReleasesDoneClosureWhenDone) {
61   bool done_destructed = false;
62   base::RepeatingClosure barrier_closure = base::BarrierClosure(
63       1,
64       base::BindOnce(&DestructionIndicator::DoNothing,
65                      base::Owned(new DestructionIndicator(&done_destructed))));
66   EXPECT_FALSE(done_destructed);
67   barrier_closure.Run();
68   EXPECT_TRUE(done_destructed);
69 }
70
71 // Tests a case when |done_closure| resets a |barrier_closure|.
72 // |barrier_closure| is a RepeatingClosure holding the |done_closure|.
73 // |done_closure| holds a pointer back to the |barrier_closure|. When
74 // |barrier_closure| is Run() it calls ResetBarrierClosure() which erases the
75 // |barrier_closure| while still inside of its Run(). The Run() implementation
76 // (in base::BarrierClosure) must not try use itself after executing
77 // ResetBarrierClosure() or this test would crash inside Run().
78 TEST(BarrierClosureTest, KeepingClosureAliveUntilDone) {
79   base::RepeatingClosure barrier_closure;
80   barrier_closure =
81       base::BarrierClosure(1, base::BindLambdaForTesting([&barrier_closure]() {
82                              barrier_closure = base::RepeatingClosure();
83                            }));
84   barrier_closure.Run();
85   EXPECT_TRUE(barrier_closure.is_null());
86 }
87
88 }  // namespace