OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/after_startup_task_utils.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/task_runner_util.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using base::RunLoop; |
| 21 using content::BrowserThread; |
| 22 using content::TestBrowserThreadBundle; |
| 23 |
| 24 namespace { |
| 25 |
| 26 class WrappedTaskRunner : public base::TaskRunner { |
| 27 public: |
| 28 explicit WrappedTaskRunner(const scoped_refptr<TaskRunner>& real_runner) |
| 29 : real_task_runner_(real_runner) {} |
| 30 |
| 31 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 32 const base::Closure& task, |
| 33 base::TimeDelta delay) override { |
| 34 ++posted_task_count_; |
| 35 return real_task_runner_->PostDelayedTask( |
| 36 from_here, base::Bind(&WrappedTaskRunner::RunWrappedTask, this, task), |
| 37 base::TimeDelta()); // Squash all delays so our tests complete asap. |
| 38 } |
| 39 |
| 40 bool RunsTasksOnCurrentThread() const override { |
| 41 return real_task_runner_->RunsTasksOnCurrentThread(); |
| 42 } |
| 43 |
| 44 base::TaskRunner* real_runner() const { return real_task_runner_.get(); } |
| 45 |
| 46 int total_task_count() const { return posted_task_count_ + ran_task_count_; } |
| 47 int posted_task_count() const { return posted_task_count_; } |
| 48 int ran_task_count() const { return ran_task_count_; } |
| 49 |
| 50 void reset_task_counts() { |
| 51 posted_task_count_ = 0; |
| 52 ran_task_count_ = 0; |
| 53 } |
| 54 |
| 55 private: |
| 56 ~WrappedTaskRunner() override {} |
| 57 |
| 58 void RunWrappedTask(const base::Closure& task) { |
| 59 ++ran_task_count_; |
| 60 task.Run(); |
| 61 } |
| 62 |
| 63 scoped_refptr<TaskRunner> real_task_runner_; |
| 64 int posted_task_count_ = 0; |
| 65 int ran_task_count_ = 0; |
| 66 }; |
| 67 |
| 68 } // namespace |
| 69 |
| 70 class AfterStartupTaskTest : public testing::Test { |
| 71 public: |
| 72 AfterStartupTaskTest() |
| 73 : browser_thread_bundle_(TestBrowserThreadBundle::REAL_DB_THREAD) { |
| 74 ui_thread_ = new WrappedTaskRunner( |
| 75 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)); |
| 76 db_thread_ = new WrappedTaskRunner( |
| 77 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)); |
| 78 AfterStartupTaskUtils::UnsafeResetForTesting(); |
| 79 } |
| 80 |
| 81 // Hop to the db thread and call IsBrowserStartupComplete. |
| 82 bool GetIsBrowserStartupCompleteFromDBThread() { |
| 83 RunLoop run_loop; |
| 84 bool is_complete; |
| 85 base::PostTaskAndReplyWithResult( |
| 86 db_thread_->real_runner(), FROM_HERE, |
| 87 base::Bind(&AfterStartupTaskUtils::IsBrowserStartupComplete), |
| 88 base::Bind(&AfterStartupTaskTest::GotIsOnBrowserStartupComplete, |
| 89 &run_loop, &is_complete)); |
| 90 run_loop.Run(); |
| 91 return is_complete; |
| 92 } |
| 93 |
| 94 // Hop to the db thread and call PostAfterStartupTask. |
| 95 void PostAfterStartupTaskFromDBThread( |
| 96 const tracked_objects::Location& from_here, |
| 97 const scoped_refptr<base::TaskRunner>& task_runner, |
| 98 const base::Closure& task) { |
| 99 RunLoop run_loop; |
| 100 db_thread_->real_runner()->PostTaskAndReply( |
| 101 FROM_HERE, base::Bind(&AfterStartupTaskUtils::PostTask, from_here, |
| 102 task_runner, task), |
| 103 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); |
| 104 run_loop.Run(); |
| 105 } |
| 106 |
| 107 // Make sure all tasks posted to the DB thread get run. |
| 108 void FlushDBThread() { |
| 109 RunLoop run_loop; |
| 110 db_thread_->real_runner()->PostTaskAndReply( |
| 111 FROM_HERE, base::Bind(&base::DoNothing), |
| 112 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); |
| 113 run_loop.Run(); |
| 114 } |
| 115 |
| 116 static void VerifyExpectedThread(BrowserThread::ID id) { |
| 117 EXPECT_TRUE(BrowserThread::CurrentlyOn(id)); |
| 118 } |
| 119 |
| 120 protected: |
| 121 scoped_refptr<WrappedTaskRunner> ui_thread_; |
| 122 scoped_refptr<WrappedTaskRunner> db_thread_; |
| 123 |
| 124 private: |
| 125 static void GotIsOnBrowserStartupComplete(RunLoop* loop, |
| 126 bool* out, |
| 127 bool is_complete) { |
| 128 *out = is_complete; |
| 129 loop->Quit(); |
| 130 } |
| 131 |
| 132 TestBrowserThreadBundle browser_thread_bundle_; |
| 133 }; |
| 134 |
| 135 TEST_F(AfterStartupTaskTest, IsStartupComplete) { |
| 136 // Check IsBrowserStartupComplete on a background thread first to |
| 137 // verify that it does not allocate the underlying flag on that thread. |
| 138 // That allocation thread correctness part of this test relies on |
| 139 // the DCHECK in CancellationFlag::Set(). |
| 140 EXPECT_FALSE(GetIsBrowserStartupCompleteFromDBThread()); |
| 141 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 142 AfterStartupTaskUtils::SetBrowserStartupIsComplete(); |
| 143 EXPECT_TRUE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 144 EXPECT_TRUE(GetIsBrowserStartupCompleteFromDBThread()); |
| 145 } |
| 146 |
| 147 TEST_F(AfterStartupTaskTest, PostTask) { |
| 148 // Nothing should be posted prior to startup completion. |
| 149 EXPECT_FALSE(AfterStartupTaskUtils::IsBrowserStartupComplete()); |
| 150 AfterStartupTaskUtils::PostTask( |
| 151 FROM_HERE, ui_thread_, |
| 152 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, |
| 153 BrowserThread::UI)); |
| 154 AfterStartupTaskUtils::PostTask( |
| 155 FROM_HERE, db_thread_, |
| 156 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, |
| 157 BrowserThread::DB)); |
| 158 PostAfterStartupTaskFromDBThread( |
| 159 FROM_HERE, ui_thread_, |
| 160 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, |
| 161 BrowserThread::UI)); |
| 162 PostAfterStartupTaskFromDBThread( |
| 163 FROM_HERE, db_thread_, |
| 164 base::Bind(&AfterStartupTaskTest::VerifyExpectedThread, |
| 165 BrowserThread::DB)); |
| 166 RunLoop().RunUntilIdle(); |
| 167 EXPECT_EQ(0, db_thread_->total_task_count() + ui_thread_->total_task_count()); |
| 168 |
| 169 // Queued tasks should be posted upon setting the flag. |
| 170 AfterStartupTaskUtils::SetBrowserStartupIsComplete(); |
| 171 EXPECT_EQ(2, db_thread_->posted_task_count()); |
| 172 EXPECT_EQ(2, ui_thread_->posted_task_count()); |
| 173 FlushDBThread(); |
| 174 RunLoop().RunUntilIdle(); |
| 175 EXPECT_EQ(2, db_thread_->ran_task_count()); |
| 176 EXPECT_EQ(2, ui_thread_->ran_task_count()); |
| 177 |
| 178 db_thread_->reset_task_counts(); |
| 179 ui_thread_->reset_task_counts(); |
| 180 EXPECT_EQ(0, db_thread_->total_task_count() + ui_thread_->total_task_count()); |
| 181 |
| 182 // Tasks posted after startup should get posted immediately. |
| 183 AfterStartupTaskUtils::PostTask(FROM_HERE, ui_thread_, |
| 184 base::Bind(&base::DoNothing)); |
| 185 AfterStartupTaskUtils::PostTask(FROM_HERE, db_thread_, |
| 186 base::Bind(&base::DoNothing)); |
| 187 EXPECT_EQ(1, db_thread_->posted_task_count()); |
| 188 EXPECT_EQ(1, ui_thread_->posted_task_count()); |
| 189 PostAfterStartupTaskFromDBThread(FROM_HERE, ui_thread_, |
| 190 base::Bind(&base::DoNothing)); |
| 191 PostAfterStartupTaskFromDBThread(FROM_HERE, db_thread_, |
| 192 base::Bind(&base::DoNothing)); |
| 193 EXPECT_EQ(2, db_thread_->posted_task_count()); |
| 194 EXPECT_EQ(2, ui_thread_->posted_task_count()); |
| 195 FlushDBThread(); |
| 196 RunLoop().RunUntilIdle(); |
| 197 EXPECT_EQ(2, db_thread_->ran_task_count()); |
| 198 EXPECT_EQ(2, ui_thread_->ran_task_count()); |
| 199 } |
OLD | NEW |