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. 4 5 #include "extensions/common/one_shot_event.h" 6 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" 11 12 namespace extensions { 13 14 namespace { 15 16 void Increment(int* i) { ++*i; } 17 18 TEST(OneShotEventTest, RecordsSignal) { 19 OneShotEvent event; 20 EXPECT_FALSE(event.is_signaled()); 21 event.Signal(); 22 EXPECT_TRUE(event.is_signaled()); 23 } 24 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); 41 } 42 43 TEST(OneShotEventTest, CallsAfterSignalDontRunInline) { 44 OneShotEvent event; 45 scoped_refptr<base::TestSimpleTaskRunner> runner( 46 new base::TestSimpleTaskRunner); 47 int i = 0; 48 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); 55 } 56 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; 64 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); 75 } 76 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); 83 } 84 85 TEST(OneShotEventTest, IsSignaledAndPostsFromCallbackWork) { 86 OneShotEvent event; 87 scoped_refptr<base::TestSimpleTaskRunner> runner( 88 new base::TestSimpleTaskRunner); 89 int i = 0; 90 91 event.Post(FROM_HERE, 92 base::Bind(&CheckSignaledAndPostIncrement, &event, runner, &i), 93 runner); 94 EXPECT_EQ(0, i); 95 event.Signal(); 96 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); 108 } 109 110 } // namespace 111 } // namespace extensions 112