[BuildBreak] Linux: Fix g++ 4.8 compilation for error: multi-line comment [-Werror...
[chromium-blink-merge.git] / extensions / common / one_shot_event_unittest.cc
blob4009a0106a2d7d31ded99a22357f7a8c3448fc93
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"
7 #include "base/bind.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 {
14 namespace {
16 void Increment(int* i) { ++*i; }
18 TEST(OneShotEventTest, RecordsSignal) {
19 OneShotEvent event;
20 EXPECT_FALSE(event.is_signaled());
21 event.Signal();
22 EXPECT_TRUE(event.is_signaled());
25 TEST(OneShotEventTest, CallsQueue) {
26 OneShotEvent event;
27 scoped_refptr<base::TestSimpleTaskRunner> runner(
28 new base::TestSimpleTaskRunner);
29 int i = 0;
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());
33 event.Signal();
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.";
38 EXPECT_EQ(0, i);
39 runner->RunPendingTasks();
40 EXPECT_EQ(2, i);
43 TEST(OneShotEventTest, CallsAfterSignalDontRunInline) {
44 OneShotEvent event;
45 scoped_refptr<base::TestSimpleTaskRunner> runner(
46 new base::TestSimpleTaskRunner);
47 int i = 0;
49 event.Signal();
50 event.Post(FROM_HERE, base::Bind(&Increment, &i), runner);
51 EXPECT_EQ(1U, runner->GetPendingTasks().size());
52 EXPECT_EQ(0, i);
53 runner->RunPendingTasks();
54 EXPECT_EQ(1, i);
57 TEST(OneShotEventTest, PostDefaultsToCurrentMessageLoop) {
58 OneShotEvent event;
59 scoped_refptr<base::TestSimpleTaskRunner> runner(
60 new base::TestSimpleTaskRunner);
61 base::MessageLoop loop;
62 int runner_i = 0;
63 int loop_i = 0;
65 event.Post(FROM_HERE, base::Bind(&Increment, &runner_i), runner);
66 event.Post(FROM_HERE, base::Bind(&Increment, &loop_i));
67 event.Signal();
68 EXPECT_EQ(1U, runner->GetPendingTasks().size());
69 EXPECT_EQ(0, runner_i);
70 runner->RunPendingTasks();
71 EXPECT_EQ(1, runner_i);
72 EXPECT_EQ(0, loop_i);
73 base::RunLoop().RunUntilIdle();
74 EXPECT_EQ(1, loop_i);
77 void CheckSignaledAndPostIncrement(
78 OneShotEvent* event,
79 const scoped_refptr<base::TaskRunner>& runner,
80 int* i) {
81 EXPECT_TRUE(event->is_signaled());
82 event->Post(FROM_HERE, base::Bind(&Increment, i), runner);
85 TEST(OneShotEventTest, IsSignaledAndPostsFromCallbackWork) {
86 OneShotEvent event;
87 scoped_refptr<base::TestSimpleTaskRunner> runner(
88 new base::TestSimpleTaskRunner);
89 int i = 0;
91 event.Post(FROM_HERE,
92 base::Bind(&CheckSignaledAndPostIncrement, &event, runner, &i),
93 runner);
94 EXPECT_EQ(0, i);
95 event.Signal();
97 // CheckSignaledAndPostIncrement is queued on |runner|.
98 EXPECT_EQ(1U, runner->GetPendingTasks().size());
99 EXPECT_EQ(0, i);
100 runner->RunPendingTasks();
101 // Increment is queued on |runner|.
102 EXPECT_EQ(1U, runner->GetPendingTasks().size());
103 EXPECT_EQ(0, i);
104 runner->RunPendingTasks();
105 // Increment has run.
106 EXPECT_EQ(0U, runner->GetPendingTasks().size());
107 EXPECT_EQ(1, i);
110 } // namespace
111 } // namespace extensions