Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1093)

Side by Side Diff: chrome/browser/after_startup_task_utils_unittest.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: s/base::ResetAndReturn/std::move/ Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/after_startup_task_utils.h" 5 #include "chrome/browser/after_startup_task_utils.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/callback_helpers.h"
11 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 15 #include "base/run_loop.h"
14 #include "base/task_runner_util.h" 16 #include "base/task_runner_util.h"
15 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
16 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
17 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
20 22
21 using base::RunLoop; 23 using base::RunLoop;
22 using content::BrowserThread; 24 using content::BrowserThread;
23 using content::TestBrowserThreadBundle; 25 using content::TestBrowserThreadBundle;
24 26
25 namespace { 27 namespace {
26 28
27 class WrappedTaskRunner : public base::TaskRunner { 29 class WrappedTaskRunner : public base::TaskRunner {
28 public: 30 public:
29 explicit WrappedTaskRunner(const scoped_refptr<TaskRunner>& real_runner) 31 explicit WrappedTaskRunner(const scoped_refptr<TaskRunner>& real_runner)
30 : real_task_runner_(real_runner) {} 32 : real_task_runner_(real_runner) {}
31 33
32 bool PostDelayedTask(const tracked_objects::Location& from_here, 34 bool PostDelayedTask(const tracked_objects::Location& from_here,
33 const base::Closure& task, 35 base::Closure task,
34 base::TimeDelta delay) override { 36 base::TimeDelta delay) override {
35 ++posted_task_count_; 37 ++posted_task_count_;
36 return real_task_runner_->PostDelayedTask( 38 return real_task_runner_->PostDelayedTask(
37 from_here, base::Bind(&WrappedTaskRunner::RunWrappedTask, this, task), 39 from_here,
40 base::Bind(&WrappedTaskRunner::RunWrappedTask, this, std::move(task)),
38 base::TimeDelta()); // Squash all delays so our tests complete asap. 41 base::TimeDelta()); // Squash all delays so our tests complete asap.
39 } 42 }
40 43
41 bool RunsTasksOnCurrentThread() const override { 44 bool RunsTasksOnCurrentThread() const override {
42 return real_task_runner_->RunsTasksOnCurrentThread(); 45 return real_task_runner_->RunsTasksOnCurrentThread();
43 } 46 }
44 47
45 base::TaskRunner* real_runner() const { return real_task_runner_.get(); } 48 base::TaskRunner* real_runner() const { return real_task_runner_.get(); }
46 49
47 int total_task_count() const { return posted_task_count_ + ran_task_count_; } 50 int total_task_count() const { return posted_task_count_ + ran_task_count_; }
48 int posted_task_count() const { return posted_task_count_; } 51 int posted_task_count() const { return posted_task_count_; }
49 int ran_task_count() const { return ran_task_count_; } 52 int ran_task_count() const { return ran_task_count_; }
50 53
51 void reset_task_counts() { 54 void reset_task_counts() {
52 posted_task_count_ = 0; 55 posted_task_count_ = 0;
53 ran_task_count_ = 0; 56 ran_task_count_ = 0;
54 } 57 }
55 58
56 private: 59 private:
57 ~WrappedTaskRunner() override {} 60 ~WrappedTaskRunner() override {}
58 61
59 void RunWrappedTask(const base::Closure& task) { 62 void RunWrappedTask(base::Closure task) {
60 ++ran_task_count_; 63 ++ran_task_count_;
61 task.Run(); 64 std::move(task).Run();
sky 2017/03/22 17:34:11 ?
62 } 65 }
63 66
64 scoped_refptr<TaskRunner> real_task_runner_; 67 scoped_refptr<TaskRunner> real_task_runner_;
65 int posted_task_count_ = 0; 68 int posted_task_count_ = 0;
66 int ran_task_count_ = 0; 69 int ran_task_count_ = 0;
67 }; 70 };
68 71
69 } // namespace 72 } // namespace
70 73
71 class AfterStartupTaskTest : public testing::Test { 74 class AfterStartupTaskTest : public testing::Test {
(...skipping 17 matching lines...) Expand all
89 base::Bind(&AfterStartupTaskTest::GotIsOnBrowserStartupComplete, 92 base::Bind(&AfterStartupTaskTest::GotIsOnBrowserStartupComplete,
90 &run_loop, &is_complete)); 93 &run_loop, &is_complete));
91 run_loop.Run(); 94 run_loop.Run();
92 return is_complete; 95 return is_complete;
93 } 96 }
94 97
95 // Hop to the db thread and call PostAfterStartupTask. 98 // Hop to the db thread and call PostAfterStartupTask.
96 void PostAfterStartupTaskFromDBThread( 99 void PostAfterStartupTaskFromDBThread(
97 const tracked_objects::Location& from_here, 100 const tracked_objects::Location& from_here,
98 const scoped_refptr<base::TaskRunner>& task_runner, 101 const scoped_refptr<base::TaskRunner>& task_runner,
99 const base::Closure& task) { 102 base::Closure task) {
100 RunLoop run_loop; 103 RunLoop run_loop;
101 db_thread_->real_runner()->PostTaskAndReply( 104 db_thread_->real_runner()->PostTaskAndReply(
102 FROM_HERE, base::Bind(&AfterStartupTaskUtils::PostTask, from_here, 105 FROM_HERE,
103 task_runner, task), 106 base::Bind(&AfterStartupTaskUtils::PostTask, from_here, task_runner,
107 std::move(task)),
104 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); 108 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop)));
105 run_loop.Run(); 109 run_loop.Run();
106 } 110 }
107 111
108 // Make sure all tasks posted to the DB thread get run. 112 // Make sure all tasks posted to the DB thread get run.
109 void FlushDBThread() { 113 void FlushDBThread() {
110 RunLoop run_loop; 114 RunLoop run_loop;
111 db_thread_->real_runner()->PostTaskAndReply( 115 db_thread_->real_runner()->PostTaskAndReply(
112 FROM_HERE, base::Bind(&base::DoNothing), 116 FROM_HERE, base::Bind(&base::DoNothing),
113 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); 117 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop)));
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 221
218 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); 222 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting();
219 EXPECT_EQ(1, db_thread_->posted_task_count()); 223 EXPECT_EQ(1, db_thread_->posted_task_count());
220 224
221 FlushDBThread(); 225 FlushDBThread();
222 RunLoop().RunUntilIdle(); 226 RunLoop().RunUntilIdle();
223 EXPECT_EQ(1, db_thread_->ran_task_count()); 227 EXPECT_EQ(1, db_thread_->ran_task_count());
224 228
225 EXPECT_EQ(0, ui_thread_->total_task_count()); 229 EXPECT_EQ(0, ui_thread_->total_task_count());
226 } 230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698