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