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/test/test_simple_task_runner.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions
{
16 void Increment(int* i
) { ++*i
; }
18 TEST(OneShotEventTest
, RecordsSignal
) {
20 EXPECT_FALSE(event
.is_signaled());
22 EXPECT_TRUE(event
.is_signaled());
25 TEST(OneShotEventTest
, CallsQueue
) {
27 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
28 new base::TestSimpleTaskRunner
);
30 event
.Post(FROM_HERE
, base::Bind(&Increment
, &i
), runner
);
31 event
.Post(FROM_HERE
, base::Bind(&Increment
, &i
), runner
);
32 EXPECT_EQ(0U, runner
->GetPendingTasks().size());
34 ASSERT_EQ(2U, runner
->GetPendingTasks().size());
35 EXPECT_NE(runner
->GetPendingTasks()[0].location
.line_number(),
36 runner
->GetPendingTasks()[1].location
.line_number())
37 << "Make sure FROM_HERE is propagated.";
39 runner
->RunPendingTasks();
43 TEST(OneShotEventTest
, CallsAfterSignalDontRunInline
) {
45 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
46 new base::TestSimpleTaskRunner
);
50 event
.Post(FROM_HERE
, base::Bind(&Increment
, &i
), runner
);
51 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
53 runner
->RunPendingTasks();
57 TEST(OneShotEventTest
, PostDefaultsToCurrentMessageLoop
) {
59 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
60 new base::TestSimpleTaskRunner
);
61 base::MessageLoop loop
;
65 event
.Post(FROM_HERE
, base::Bind(&Increment
, &runner_i
), runner
);
66 event
.Post(FROM_HERE
, base::Bind(&Increment
, &loop_i
));
68 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
69 EXPECT_EQ(0, runner_i
);
70 runner
->RunPendingTasks();
71 EXPECT_EQ(1, runner_i
);
73 base::RunLoop().RunUntilIdle();
77 void CheckSignaledAndPostIncrement(
79 const scoped_refptr
<base::TaskRunner
>& runner
,
81 EXPECT_TRUE(event
->is_signaled());
82 event
->Post(FROM_HERE
, base::Bind(&Increment
, i
), runner
);
85 TEST(OneShotEventTest
, IsSignaledAndPostsFromCallbackWork
) {
87 scoped_refptr
<base::TestSimpleTaskRunner
> runner(
88 new base::TestSimpleTaskRunner
);
92 base::Bind(&CheckSignaledAndPostIncrement
, &event
, runner
, &i
),
97 // CheckSignaledAndPostIncrement is queued on |runner|.
98 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
100 runner
->RunPendingTasks();
101 // Increment is queued on |runner|.
102 EXPECT_EQ(1U, runner
->GetPendingTasks().size());
104 runner
->RunPendingTasks();
105 // Increment has run.
106 EXPECT_EQ(0U, runner
->GetPendingTasks().size());
111 } // namespace extensions