1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/common/one_shot_event.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/test/test_simple_task_runner.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions
{
17 void Increment(int* i
) { ++*i
; }
19 TEST(OneShotEventTest
, RecordsSignal
) {
21 EXPECT_FALSE(event
.is_signaled());
23 EXPECT_TRUE(event
.is_signaled());
26 TEST(OneShotEventTest
, CallsQueue
) {
28 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
29 new base::TestSimpleTaskRunner
);
31 event
.Post(FROM_HERE
, base::Bind(&Increment
, &i
), runner
);
32 event
.Post(FROM_HERE
, base::Bind(&Increment
, &i
), runner
);
33 EXPECT_EQ(0U, runner
->GetPendingTasks().size());
35 ASSERT_EQ(2U, runner
->GetPendingTasks().size());
36 EXPECT_NE(runner
->GetPendingTasks()[0].location
.line_number(),
37 runner
->GetPendingTasks()[1].location
.line_number())
38 << "Make sure FROM_HERE is propagated.";
40 runner
->RunPendingTasks();
44 TEST(OneShotEventTest
, CallsAfterSignalDontRunInline
) {
46 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
47 new base::TestSimpleTaskRunner
);
51 event
.Post(FROM_HERE
, base::Bind(&Increment
, &i
), runner
);
52 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
54 runner
->RunPendingTasks();
58 TEST(OneShotEventTest
, PostDefaultsToCurrentMessageLoop
) {
60 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
61 new base::TestSimpleTaskRunner
);
62 base::MessageLoop loop
;
66 event
.Post(FROM_HERE
, base::Bind(&Increment
, &runner_i
), runner
);
67 event
.Post(FROM_HERE
, base::Bind(&Increment
, &loop_i
));
69 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
70 EXPECT_EQ(0, runner_i
);
71 runner
->RunPendingTasks();
72 EXPECT_EQ(1, runner_i
);
74 base::RunLoop().RunUntilIdle();
78 void CheckSignaledAndPostIncrement(
80 const scoped_refptr
<base::SingleThreadTaskRunner
>& runner
,
82 EXPECT_TRUE(event
->is_signaled());
83 event
->Post(FROM_HERE
, base::Bind(&Increment
, i
), runner
);
86 TEST(OneShotEventTest
, IsSignaledAndPostsFromCallbackWork
) {
88 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
89 new base::TestSimpleTaskRunner
);
93 base::Bind(&CheckSignaledAndPostIncrement
, &event
, runner
, &i
),
98 // CheckSignaledAndPostIncrement is queued on |runner|.
99 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
101 runner
->RunPendingTasks();
102 // Increment is queued on |runner|.
103 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
105 runner
->RunPendingTasks();
106 // Increment has run.
107 EXPECT_EQ(0U, runner
->GetPendingTasks().size());
112 } // namespace extensions