OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "base/threading/post_task_and_reply_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/threading/sequenced_task_runner_handle.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 using ::testing::_; |
| 17 |
| 18 namespace base { |
| 19 namespace internal { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class PostTaskAndReplyTaskRunner : public internal::PostTaskAndReplyImpl { |
| 24 public: |
| 25 explicit PostTaskAndReplyTaskRunner(TaskRunner* destination) |
| 26 : destination_(destination) {} |
| 27 |
| 28 private: |
| 29 bool PostTask(const tracked_objects::Location& from_here, |
| 30 const Closure& task) override { |
| 31 return destination_->PostTask(from_here, task); |
| 32 } |
| 33 |
| 34 // Non-owning. |
| 35 TaskRunner* const destination_; |
| 36 }; |
| 37 |
| 38 class ObjectToDelete : public RefCounted<ObjectToDelete> { |
| 39 public: |
| 40 // |delete_flag| is set to true when this object is deleted |
| 41 ObjectToDelete(bool* delete_flag) : delete_flag_(delete_flag) { |
| 42 EXPECT_FALSE(*delete_flag_); |
| 43 } |
| 44 |
| 45 private: |
| 46 friend class RefCounted<ObjectToDelete>; |
| 47 ~ObjectToDelete() { *delete_flag_ = true; } |
| 48 |
| 49 bool* const delete_flag_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ObjectToDelete); |
| 52 }; |
| 53 |
| 54 class MockObject { |
| 55 public: |
| 56 MockObject() = default; |
| 57 |
| 58 MOCK_METHOD1(Task, void(scoped_refptr<ObjectToDelete>)); |
| 59 |
| 60 void Reply(bool* delete_flag) { |
| 61 // Expect the task's deletion flag to be set before the reply runs. |
| 62 EXPECT_TRUE(*delete_flag); |
| 63 ReplyMock(); |
| 64 } |
| 65 |
| 66 MOCK_METHOD0(ReplyMock, void()); |
| 67 |
| 68 private: |
| 69 DISALLOW_COPY_AND_ASSIGN(MockObject); |
| 70 }; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 TEST(PostTaskAndReplyImplTest, PostTaskAndReply) { |
| 75 scoped_refptr<TestSimpleTaskRunner> post_runner(new TestSimpleTaskRunner); |
| 76 scoped_refptr<TestSimpleTaskRunner> reply_runner(new TestSimpleTaskRunner); |
| 77 SequencedTaskRunnerHandle sequenced_task_runner_handle(reply_runner); |
| 78 |
| 79 testing::StrictMock<MockObject> mock_object; |
| 80 bool delete_flag = false; |
| 81 |
| 82 EXPECT_TRUE( |
| 83 PostTaskAndReplyTaskRunner(post_runner.get()) |
| 84 .PostTaskAndReply( |
| 85 FROM_HERE, |
| 86 Bind(&MockObject::Task, Unretained(&mock_object), |
| 87 make_scoped_refptr(new ObjectToDelete(&delete_flag))), |
| 88 Bind(&MockObject::Reply, Unretained(&mock_object), |
| 89 Unretained(&delete_flag)))); |
| 90 |
| 91 // Expect no reply in |reply_runner|. |
| 92 EXPECT_FALSE(reply_runner->HasPendingTask()); |
| 93 |
| 94 // Expect the task to be posted to |post_runner|. |
| 95 EXPECT_TRUE(post_runner->HasPendingTask()); |
| 96 EXPECT_CALL(mock_object, Task(_)); |
| 97 post_runner->RunUntilIdle(); |
| 98 testing::Mock::VerifyAndClear(&mock_object); |
| 99 |
| 100 // Expect the task's argument not to have been deleted yet. |
| 101 EXPECT_FALSE(delete_flag); |
| 102 |
| 103 // Expect the reply to be posted to |reply_runner|. |
| 104 EXPECT_FALSE(post_runner->HasPendingTask()); |
| 105 EXPECT_TRUE(reply_runner->HasPendingTask()); |
| 106 EXPECT_CALL(mock_object, ReplyMock()); |
| 107 reply_runner->RunUntilIdle(); |
| 108 testing::Mock::VerifyAndClear(&mock_object); |
| 109 EXPECT_TRUE(delete_flag); |
| 110 |
| 111 // Expect no pending task in |post_runner| and |reply_runner|. |
| 112 EXPECT_FALSE(post_runner->HasPendingTask()); |
| 113 EXPECT_FALSE(reply_runner->HasPendingTask()); |
| 114 } |
| 115 |
| 116 } // namespace internal |
| 117 } // namespace base |
OLD | NEW |