| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 // Hop to the db thread and call PostAfterStartupTask. | 98 // Hop to the db thread and call PostAfterStartupTask. |
| 99 void PostAfterStartupTaskFromDBThread( | 99 void PostAfterStartupTaskFromDBThread( |
| 100 const tracked_objects::Location& from_here, | 100 const tracked_objects::Location& from_here, |
| 101 const scoped_refptr<base::TaskRunner>& task_runner, | 101 const scoped_refptr<base::TaskRunner>& task_runner, |
| 102 base::OnceClosure task) { | 102 base::OnceClosure task) { |
| 103 RunLoop run_loop; | 103 RunLoop run_loop; |
| 104 db_thread_->real_runner()->PostTaskAndReply( | 104 db_thread_->real_runner()->PostTaskAndReply( |
| 105 FROM_HERE, | 105 FROM_HERE, |
| 106 base::BindOnce(&AfterStartupTaskUtils::PostTask, from_here, task_runner, | 106 base::BindOnce(&AfterStartupTaskUtils::PostTask, from_here, task_runner, |
| 107 std::move(task)), | 107 std::move(task)), |
| 108 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); | 108 base::BindOnce(&RunLoop::Quit, base::Unretained(&run_loop))); |
| 109 run_loop.Run(); | 109 run_loop.Run(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 // Make sure all tasks posted to the DB thread get run. | 112 // Make sure all tasks posted to the DB thread get run. |
| 113 void FlushDBThread() { | 113 void FlushDBThread() { |
| 114 RunLoop run_loop; | 114 RunLoop run_loop; |
| 115 db_thread_->real_runner()->PostTaskAndReply( | 115 db_thread_->real_runner()->PostTaskAndReply( |
| 116 FROM_HERE, base::Bind(&base::DoNothing), | 116 FROM_HERE, base::BindOnce(&base::DoNothing), |
| 117 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); | 117 base::BindOnce(&RunLoop::Quit, base::Unretained(&run_loop))); |
| 118 run_loop.Run(); | 118 run_loop.Run(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 static void VerifyExpectedThread(BrowserThread::ID id) { | 121 static void VerifyExpectedThread(BrowserThread::ID id) { |
| 122 EXPECT_TRUE(BrowserThread::CurrentlyOn(id)); | 122 EXPECT_TRUE(BrowserThread::CurrentlyOn(id)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 protected: | 125 protected: |
| 126 scoped_refptr<WrappedTaskRunner> ui_thread_; | 126 scoped_refptr<WrappedTaskRunner> ui_thread_; |
| 127 scoped_refptr<WrappedTaskRunner> db_thread_; | 127 scoped_refptr<WrappedTaskRunner> db_thread_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 147 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); | 147 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); |
| 148 EXPECT_TRUE(AfterStartupTaskUtils::IsBrowserStartupComplete()); | 148 EXPECT_TRUE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 149 EXPECT_TRUE(GetIsBrowserStartupCompleteFromDBThread()); | 149 EXPECT_TRUE(GetIsBrowserStartupCompleteFromDBThread()); |
| 150 } | 150 } |
| 151 | 151 |
| 152 TEST_F(AfterStartupTaskTest, PostTask) { | 152 TEST_F(AfterStartupTaskTest, PostTask) { |
| 153 // Nothing should be posted prior to startup completion. | 153 // Nothing should be posted prior to startup completion. |
| 154 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); | 154 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 155 AfterStartupTaskUtils::PostTask( | 155 AfterStartupTaskUtils::PostTask( |
| 156 FROM_HERE, ui_thread_, | 156 FROM_HERE, ui_thread_, |
| 157 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, | 157 base::BindOnce(&AfterStartupTaskTest::VerifyExpectedThread, |
| 158 BrowserThread::UI)); | 158 BrowserThread::UI)); |
| 159 AfterStartupTaskUtils::PostTask( | 159 AfterStartupTaskUtils::PostTask( |
| 160 FROM_HERE, db_thread_, | 160 FROM_HERE, db_thread_, |
| 161 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, | 161 base::BindOnce(&AfterStartupTaskTest::VerifyExpectedThread, |
| 162 BrowserThread::DB)); | 162 BrowserThread::DB)); |
| 163 PostAfterStartupTaskFromDBThread( | 163 PostAfterStartupTaskFromDBThread( |
| 164 FROM_HERE, ui_thread_, | 164 FROM_HERE, ui_thread_, |
| 165 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, | 165 base::BindOnce(&AfterStartupTaskTest::VerifyExpectedThread, |
| 166 BrowserThread::UI)); | 166 BrowserThread::UI)); |
| 167 PostAfterStartupTaskFromDBThread( | 167 PostAfterStartupTaskFromDBThread( |
| 168 FROM_HERE, db_thread_, | 168 FROM_HERE, db_thread_, |
| 169 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, | 169 base::BindOnce(&AfterStartupTaskTest::VerifyExpectedThread, |
| 170 BrowserThread::DB)); | 170 BrowserThread::DB)); |
| 171 RunLoop().RunUntilIdle(); | 171 RunLoop().RunUntilIdle(); |
| 172 EXPECT_EQ(0, db_thread_->total_task_count() + ui_thread_->total_task_count()); | 172 EXPECT_EQ(0, db_thread_->total_task_count() + ui_thread_->total_task_count()); |
| 173 | 173 |
| 174 // Queued tasks should be posted upon setting the flag. | 174 // Queued tasks should be posted upon setting the flag. |
| 175 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); | 175 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); |
| 176 EXPECT_EQ(2, db_thread_->posted_task_count()); | 176 EXPECT_EQ(2, db_thread_->posted_task_count()); |
| 177 EXPECT_EQ(2, ui_thread_->posted_task_count()); | 177 EXPECT_EQ(2, ui_thread_->posted_task_count()); |
| 178 FlushDBThread(); | 178 FlushDBThread(); |
| 179 RunLoop().RunUntilIdle(); | 179 RunLoop().RunUntilIdle(); |
| 180 EXPECT_EQ(2, db_thread_->ran_task_count()); | 180 EXPECT_EQ(2, db_thread_->ran_task_count()); |
| 181 EXPECT_EQ(2, ui_thread_->ran_task_count()); | 181 EXPECT_EQ(2, ui_thread_->ran_task_count()); |
| 182 | 182 |
| 183 db_thread_->reset_task_counts(); | 183 db_thread_->reset_task_counts(); |
| 184 ui_thread_->reset_task_counts(); | 184 ui_thread_->reset_task_counts(); |
| 185 EXPECT_EQ(0, db_thread_->total_task_count() + ui_thread_->total_task_count()); | 185 EXPECT_EQ(0, db_thread_->total_task_count() + ui_thread_->total_task_count()); |
| 186 | 186 |
| 187 // Tasks posted after startup should get posted immediately. | 187 // Tasks posted after startup should get posted immediately. |
| 188 AfterStartupTaskUtils::PostTask(FROM_HERE, ui_thread_, | 188 AfterStartupTaskUtils::PostTask(FROM_HERE, ui_thread_, |
| 189 base::Bind(&base::DoNothing)); | 189 base::BindOnce(&base::DoNothing)); |
| 190 AfterStartupTaskUtils::PostTask(FROM_HERE, db_thread_, | 190 AfterStartupTaskUtils::PostTask(FROM_HERE, db_thread_, |
| 191 base::Bind(&base::DoNothing)); | 191 base::BindOnce(&base::DoNothing)); |
| 192 EXPECT_EQ(1, db_thread_->posted_task_count()); | 192 EXPECT_EQ(1, db_thread_->posted_task_count()); |
| 193 EXPECT_EQ(1, ui_thread_->posted_task_count()); | 193 EXPECT_EQ(1, ui_thread_->posted_task_count()); |
| 194 PostAfterStartupTaskFromDBThread(FROM_HERE, ui_thread_, | 194 PostAfterStartupTaskFromDBThread(FROM_HERE, ui_thread_, |
| 195 base::Bind(&base::DoNothing)); | 195 base::BindOnce(&base::DoNothing)); |
| 196 PostAfterStartupTaskFromDBThread(FROM_HERE, db_thread_, | 196 PostAfterStartupTaskFromDBThread(FROM_HERE, db_thread_, |
| 197 base::Bind(&base::DoNothing)); | 197 base::BindOnce(&base::DoNothing)); |
| 198 EXPECT_EQ(2, db_thread_->posted_task_count()); | 198 EXPECT_EQ(2, db_thread_->posted_task_count()); |
| 199 EXPECT_EQ(2, ui_thread_->posted_task_count()); | 199 EXPECT_EQ(2, ui_thread_->posted_task_count()); |
| 200 FlushDBThread(); | 200 FlushDBThread(); |
| 201 RunLoop().RunUntilIdle(); | 201 RunLoop().RunUntilIdle(); |
| 202 EXPECT_EQ(2, db_thread_->ran_task_count()); | 202 EXPECT_EQ(2, db_thread_->ran_task_count()); |
| 203 EXPECT_EQ(2, ui_thread_->ran_task_count()); | 203 EXPECT_EQ(2, ui_thread_->ran_task_count()); |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Verify that posting to an AfterStartupTaskUtils::Runner bound to |db_thread_| | 206 // Verify that posting to an AfterStartupTaskUtils::Runner bound to |db_thread_| |
| 207 // results in the same behavior as posting via | 207 // results in the same behavior as posting via |
| 208 // AfterStartupTaskUtils::PostTask(..., db_thread_, ...). | 208 // AfterStartupTaskUtils::PostTask(..., db_thread_, ...). |
| 209 TEST_F(AfterStartupTaskTest, AfterStartupTaskUtilsRunner) { | 209 TEST_F(AfterStartupTaskTest, AfterStartupTaskUtilsRunner) { |
| 210 scoped_refptr<base::TaskRunner> after_startup_runner = | 210 scoped_refptr<base::TaskRunner> after_startup_runner = |
| 211 make_scoped_refptr(new AfterStartupTaskUtils::Runner(db_thread_)); | 211 make_scoped_refptr(new AfterStartupTaskUtils::Runner(db_thread_)); |
| 212 | 212 |
| 213 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); | 213 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 214 after_startup_runner->PostTask( | 214 after_startup_runner->PostTask( |
| 215 FROM_HERE, base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, | 215 FROM_HERE, base::BindOnce(&AfterStartupTaskTest::VerifyExpectedThread, |
| 216 BrowserThread::DB)); | 216 BrowserThread::DB)); |
| 217 | 217 |
| 218 RunLoop().RunUntilIdle(); | 218 RunLoop().RunUntilIdle(); |
| 219 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); | 219 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 220 EXPECT_EQ(0, db_thread_->total_task_count()); | 220 EXPECT_EQ(0, db_thread_->total_task_count()); |
| 221 | 221 |
| 222 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); | 222 AfterStartupTaskUtils::SetBrowserStartupIsCompleteForTesting(); |
| 223 EXPECT_EQ(1, db_thread_->posted_task_count()); | 223 EXPECT_EQ(1, db_thread_->posted_task_count()); |
| 224 | 224 |
| 225 FlushDBThread(); | 225 FlushDBThread(); |
| 226 RunLoop().RunUntilIdle(); | 226 RunLoop().RunUntilIdle(); |
| 227 EXPECT_EQ(1, db_thread_->ran_task_count()); | 227 EXPECT_EQ(1, db_thread_->ran_task_count()); |
| 228 | 228 |
| 229 EXPECT_EQ(0, ui_thread_->total_task_count()); | 229 EXPECT_EQ(0, ui_thread_->total_task_count()); |
| 230 } | 230 } |
| OLD | NEW |