Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / one_shot_event_unittest.cc
blobc19b036609e8f27b2df72977b84f5f49ce0ee960
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/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 {
15 namespace {
17 void Increment(int* i) { ++*i; }
19 TEST(OneShotEventTest, RecordsSignal) {
20 OneShotEvent event;
21 EXPECT_FALSE(event.is_signaled());
22 event.Signal();
23 EXPECT_TRUE(event.is_signaled());
26 TEST(OneShotEventTest, CallsQueue) {
27 OneShotEvent event;
28 scoped_refptr<base::TestSimpleTaskRunner> runner(
29 new base::TestSimpleTaskRunner);
30 int i = 0;
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());
34 event.Signal();
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.";
39 EXPECT_EQ(0, i);
40 runner->RunPendingTasks();
41 EXPECT_EQ(2, i);
44 TEST(OneShotEventTest, CallsAfterSignalDontRunInline) {
45 OneShotEvent event;
46 scoped_refptr<base::TestSimpleTaskRunner> runner(
47 new base::TestSimpleTaskRunner);
48 int i = 0;
50 event.Signal();
51 event.Post(FROM_HERE, base::Bind(&Increment, &i), runner);
52 EXPECT_EQ(1U, runner->GetPendingTasks().size());
53 EXPECT_EQ(0, i);
54 runner->RunPendingTasks();
55 EXPECT_EQ(1, i);
58 TEST(OneShotEventTest, PostDefaultsToCurrentMessageLoop) {
59 OneShotEvent event;
60 scoped_refptr<base::TestSimpleTaskRunner> runner(
61 new base::TestSimpleTaskRunner);
62 base::MessageLoop loop;
63 int runner_i = 0;
64 int loop_i = 0;
66 event.Post(FROM_HERE, base::Bind(&Increment, &runner_i), runner);
67 event.Post(FROM_HERE, base::Bind(&Increment, &loop_i));
68 event.Signal();
69 EXPECT_EQ(1U, runner->GetPendingTasks().size());
70 EXPECT_EQ(0, runner_i);
71 runner->RunPendingTasks();
72 EXPECT_EQ(1, runner_i);
73 EXPECT_EQ(0, loop_i);
74 base::RunLoop().RunUntilIdle();
75 EXPECT_EQ(1, loop_i);
78 void CheckSignaledAndPostIncrement(
79 OneShotEvent* event,
80 const scoped_refptr<base::SingleThreadTaskRunner>& runner,
81 int* i) {
82 EXPECT_TRUE(event->is_signaled());
83 event->Post(FROM_HERE, base::Bind(&Increment, i), runner);
86 TEST(OneShotEventTest, IsSignaledAndPostsFromCallbackWork) {
87 OneShotEvent event;
88 scoped_refptr<base::TestSimpleTaskRunner> runner(
89 new base::TestSimpleTaskRunner);
90 int i = 0;
92 event.Post(FROM_HERE,
93 base::Bind(&CheckSignaledAndPostIncrement, &event, runner, &i),
94 runner);
95 EXPECT_EQ(0, i);
96 event.Signal();
98 // CheckSignaledAndPostIncrement is queued on |runner|.
99 EXPECT_EQ(1U, runner->GetPendingTasks().size());
100 EXPECT_EQ(0, i);
101 runner->RunPendingTasks();
102 // Increment is queued on |runner|.
103 EXPECT_EQ(1U, runner->GetPendingTasks().size());
104 EXPECT_EQ(0, i);
105 runner->RunPendingTasks();
106 // Increment has run.
107 EXPECT_EQ(0U, runner->GetPendingTasks().size());
108 EXPECT_EQ(1, i);
111 } // namespace
112 } // namespace extensions