| OLD | NEW |
| 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 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 using content::TestBrowserThreadBundle; | 24 using content::TestBrowserThreadBundle; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 class WrappedTaskRunner : public base::TaskRunner { | 28 class WrappedTaskRunner : public base::TaskRunner { |
| 29 public: | 29 public: |
| 30 explicit WrappedTaskRunner(const scoped_refptr<TaskRunner>& real_runner) | 30 explicit WrappedTaskRunner(const scoped_refptr<TaskRunner>& real_runner) |
| 31 : real_task_runner_(real_runner) {} | 31 : real_task_runner_(real_runner) {} |
| 32 | 32 |
| 33 bool PostDelayedTask(const tracked_objects::Location& from_here, | 33 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 34 base::Closure task, | 34 base::OnceClosure task, |
| 35 base::TimeDelta delay) override { | 35 base::TimeDelta delay) override { |
| 36 ++posted_task_count_; | 36 ++posted_task_count_; |
| 37 return real_task_runner_->PostDelayedTask( | 37 return real_task_runner_->PostDelayedTask( |
| 38 from_here, | 38 from_here, |
| 39 base::Bind(&WrappedTaskRunner::RunWrappedTask, this, std::move(task)), | 39 base::BindOnce(&WrappedTaskRunner::RunWrappedTask, this, |
| 40 std::move(task)), |
| 40 base::TimeDelta()); // Squash all delays so our tests complete asap. | 41 base::TimeDelta()); // Squash all delays so our tests complete asap. |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool RunsTasksOnCurrentThread() const override { | 44 bool RunsTasksOnCurrentThread() const override { |
| 44 return real_task_runner_->RunsTasksOnCurrentThread(); | 45 return real_task_runner_->RunsTasksOnCurrentThread(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 base::TaskRunner* real_runner() const { return real_task_runner_.get(); } | 48 base::TaskRunner* real_runner() const { return real_task_runner_.get(); } |
| 48 | 49 |
| 49 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_; } |
| 50 int posted_task_count() const { return posted_task_count_; } | 51 int posted_task_count() const { return posted_task_count_; } |
| 51 int ran_task_count() const { return ran_task_count_; } | 52 int ran_task_count() const { return ran_task_count_; } |
| 52 | 53 |
| 53 void reset_task_counts() { | 54 void reset_task_counts() { |
| 54 posted_task_count_ = 0; | 55 posted_task_count_ = 0; |
| 55 ran_task_count_ = 0; | 56 ran_task_count_ = 0; |
| 56 } | 57 } |
| 57 | 58 |
| 58 private: | 59 private: |
| 59 ~WrappedTaskRunner() override {} | 60 ~WrappedTaskRunner() override {} |
| 60 | 61 |
| 61 void RunWrappedTask(base::Closure task) { | 62 void RunWrappedTask(base::OnceClosure task) { |
| 62 ++ran_task_count_; | 63 ++ran_task_count_; |
| 63 std::move(task).Run(); | 64 std::move(task).Run(); |
| 64 } | 65 } |
| 65 | 66 |
| 66 scoped_refptr<TaskRunner> real_task_runner_; | 67 scoped_refptr<TaskRunner> real_task_runner_; |
| 67 int posted_task_count_ = 0; | 68 int posted_task_count_ = 0; |
| 68 int ran_task_count_ = 0; | 69 int ran_task_count_ = 0; |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 } // namespace | 72 } // namespace |
| (...skipping 19 matching lines...) Expand all Loading... |
| 91 base::Bind(&AfterStartupTaskTest::GotIsOnBrowserStartupComplete, | 92 base::Bind(&AfterStartupTaskTest::GotIsOnBrowserStartupComplete, |
| 92 &run_loop, &is_complete)); | 93 &run_loop, &is_complete)); |
| 93 run_loop.Run(); | 94 run_loop.Run(); |
| 94 return is_complete; | 95 return is_complete; |
| 95 } | 96 } |
| 96 | 97 |
| 97 // Hop to the db thread and call PostAfterStartupTask. | 98 // Hop to the db thread and call PostAfterStartupTask. |
| 98 void PostAfterStartupTaskFromDBThread( | 99 void PostAfterStartupTaskFromDBThread( |
| 99 const tracked_objects::Location& from_here, | 100 const tracked_objects::Location& from_here, |
| 100 const scoped_refptr<base::TaskRunner>& task_runner, | 101 const scoped_refptr<base::TaskRunner>& task_runner, |
| 101 base::Closure task) { | 102 base::OnceClosure task) { |
| 102 RunLoop run_loop; | 103 RunLoop run_loop; |
| 103 db_thread_->real_runner()->PostTaskAndReply( | 104 db_thread_->real_runner()->PostTaskAndReply( |
| 104 FROM_HERE, | 105 FROM_HERE, |
| 105 base::Bind(&AfterStartupTaskUtils::PostTask, from_here, task_runner, | 106 base::BindOnce(&AfterStartupTaskUtils::PostTask, from_here, task_runner, |
| 106 std::move(task)), | 107 std::move(task)), |
| 107 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); | 108 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); |
| 108 run_loop.Run(); | 109 run_loop.Run(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 // Make sure all tasks posted to the DB thread get run. | 112 // Make sure all tasks posted to the DB thread get run. |
| 112 void FlushDBThread() { | 113 void FlushDBThread() { |
| 113 RunLoop run_loop; | 114 RunLoop run_loop; |
| 114 db_thread_->real_runner()->PostTaskAndReply( | 115 db_thread_->real_runner()->PostTaskAndReply( |
| 115 FROM_HERE, base::Bind(&base::DoNothing), | 116 FROM_HERE, base::Bind(&base::DoNothing), |
| 116 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 Loading... |
| 220 | 221 |
| 221 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); | 222 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); |
| 222 EXPECT_EQ(1, db_thread_->posted_task_count()); | 223 EXPECT_EQ(1, db_thread_->posted_task_count()); |
| 223 | 224 |
| 224 FlushDBThread(); | 225 FlushDBThread(); |
| 225 RunLoop().RunUntilIdle(); | 226 RunLoop().RunUntilIdle(); |
| 226 EXPECT_EQ(1, db_thread_->ran_task_count()); | 227 EXPECT_EQ(1, db_thread_->ran_task_count()); |
| 227 | 228 |
| 228 EXPECT_EQ(0, ui_thread_->total_task_count()); | 229 EXPECT_EQ(0, ui_thread_->total_task_count()); |
| 229 } | 230 } |
| OLD | NEW |