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 namespace base { | |
17 namespace internal { | |
18 | |
19 namespace { | |
20 | |
21 class ObjectToDelete : public RefCounted<ObjectToDelete> { | |
22 public: | |
23 // |delete_flag| is set to true when this object is deleted | |
24 ObjectToDelete(bool* delete_flag) : delete_flag_(delete_flag) { | |
25 EXPECT_FALSE(*delete_flag_); | |
26 } | |
27 ~ObjectToDelete() { *delete_flag_ = true; } | |
gab
2016/07/25 17:57:44
Destructor private for RefCounted objects
fdoray
2016/07/25 20:40:04
Done.
| |
28 | |
29 private: | |
30 friend class RefCounted<ObjectToDelete>; | |
31 bool* const delete_flag_; | |
32 | |
33 DISALLOW_COPY_AND_ASSIGN(ObjectToDelete); | |
34 }; | |
35 | |
36 class MockObject { | |
37 public: | |
38 MockObject() = default; | |
39 | |
40 MOCK_METHOD1(Task, void(scoped_refptr<ObjectToDelete>)); | |
41 | |
42 void Reply(bool* delete_flag) { | |
43 // Expect the task's argument to be deleted before the reply runs. | |
gab
2016/07/25 17:57:44
s/task's argument to be deleted/task's deletion fl
fdoray
2016/07/25 20:40:04
Done.
| |
44 EXPECT_TRUE(*delete_flag); | |
45 ReplyMock(); | |
46 } | |
47 | |
48 MOCK_METHOD0(ReplyMock, void()); | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(MockObject); | |
52 }; | |
53 | |
54 } // namespace | |
55 | |
56 TEST(PostTaskAndReplyImplTest, PostTaskAndReply) { | |
57 scoped_refptr<TestSimpleTaskRunner> task_runner(new TestSimpleTaskRunner); | |
gab
2016/07/25 17:57:44
Should TestSimpleTaskRunner grab SequenceTokens on
fdoray
2016/07/25 20:40:04
Created bug to remember to do this crbug.com/63118
| |
58 scoped_refptr<TestSimpleTaskRunner> reply_runner(new TestSimpleTaskRunner); | |
gab
2016/07/25 17:57:44
s/reply_runner/current_runner/ (i.e. it's not a ne
fdoray
2016/07/25 20:40:04
Done.
| |
59 SequencedTaskRunnerHandle sequenced_task_runner_handle(reply_runner); | |
60 | |
61 testing::StrictMock<MockObject> mock_object; | |
62 bool delete_flag = false; | |
63 | |
64 EXPECT_TRUE(internal::PostTaskAndReply( | |
65 FROM_HERE, Bind(&MockObject::Task, Unretained(&mock_object), | |
66 make_scoped_refptr(new ObjectToDelete(&delete_flag))), | |
67 Bind(&MockObject::Reply, Unretained(&mock_object), | |
68 Unretained(&delete_flag)), | |
69 Bind(&TestSimpleTaskRunner::PostTask, task_runner.get()))); | |
70 | |
71 // Expect no reply in |reply_runner|. | |
72 EXPECT_FALSE(reply_runner->HasPendingTask()); | |
73 | |
74 // Expect the task to be posted to |task_runner|. | |
75 EXPECT_TRUE(task_runner->HasPendingTask()); | |
76 EXPECT_CALL(mock_object, Task(::testing::_)); | |
gab
2016/07/25 17:57:44
using testing::_;
after includes (same for other
fdoray
2016/07/25 20:40:04
Done.
| |
77 task_runner->RunUntilIdle(); | |
78 testing::Mock::VerifyAndClear(&mock_object); | |
79 | |
80 // Expect the task's argument not to have been deleted yet. | |
81 EXPECT_FALSE(delete_flag); | |
82 | |
83 // Expect the reply to be posted to |reply_runner|. | |
gab
2016/07/25 17:57:44
EXPECT_FALSE(task_runner->HasPendingTask());
EXPEC
fdoray
2016/07/25 20:40:04
Done.
| |
84 EXPECT_CALL(mock_object, ReplyMock()); | |
85 reply_runner->RunUntilIdle(); | |
86 testing::Mock::VerifyAndClear(&mock_object); | |
87 | |
88 // Expect no pending task in |task_runner| and |reply_runner|. | |
89 EXPECT_FALSE(task_runner->HasPendingTask()); | |
90 EXPECT_FALSE(reply_runner->HasPendingTask()); | |
91 } | |
92 | |
93 } // namespace internal | |
94 } // namespace base | |
OLD | NEW |