| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/test/test_utils.h" | 5 #include "content/public/test/test_utils.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 // Number of times to repost a Quit task so that the MessageLoop finishes up | 39 // Number of times to repost a Quit task so that the MessageLoop finishes up |
| 40 // pending tasks and tasks posted by those pending tasks without risking the | 40 // pending tasks and tasks posted by those pending tasks without risking the |
| 41 // potential hang behavior of MessageLoop::QuitWhenIdle. | 41 // potential hang behavior of MessageLoop::QuitWhenIdle. |
| 42 // The criteria for choosing this number: it should be high enough to make the | 42 // The criteria for choosing this number: it should be high enough to make the |
| 43 // quit act like QuitWhenIdle, while taking into account that any page which is | 43 // quit act like QuitWhenIdle, while taking into account that any page which is |
| 44 // animating may be rendering another frame for each quit deferral. For an | 44 // animating may be rendering another frame for each quit deferral. For an |
| 45 // animating page, the potential delay to quitting the RunLoop would be | 45 // animating page, the potential delay to quitting the RunLoop would be |
| 46 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as | 46 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as |
| 47 // 200ms/frame. | 47 // 200ms/frame. |
| 48 static const int kNumQuitDeferrals = 10; | 48 constexpr int kNumQuitDeferrals = 10; |
| 49 | 49 |
| 50 static void DeferredQuitRunLoop(const base::Closure& quit_task, | 50 void DeferredQuitRunLoop(const base::Closure& quit_task, |
| 51 int num_quit_deferrals) { | 51 int num_quit_deferrals) { |
| 52 if (num_quit_deferrals <= 0) { | 52 if (num_quit_deferrals <= 0) { |
| 53 quit_task.Run(); | 53 quit_task.Run(); |
| 54 } else { | 54 } else { |
| 55 base::ThreadTaskRunnerHandle::Get()->PostTask( | 55 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 56 FROM_HERE, | 56 FROM_HERE, |
| 57 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | 57 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, | |
| 62 const base::Closure& quit_task) { | |
| 63 RunAllPendingInMessageLoop(); | |
| 64 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); | |
| 65 } | |
| 66 | |
| 67 // Class used to handle result callbacks for ExecuteScriptAndGetValue. | 61 // Class used to handle result callbacks for ExecuteScriptAndGetValue. |
| 68 class ScriptCallback { | 62 class ScriptCallback { |
| 69 public: | 63 public: |
| 70 ScriptCallback() { } | 64 ScriptCallback() { } |
| 71 virtual ~ScriptCallback() { } | 65 virtual ~ScriptCallback() { } |
| 72 void ResultCallback(const base::Value* result); | 66 void ResultCallback(const base::Value* result); |
| 73 | 67 |
| 74 std::unique_ptr<base::Value> result() { return std::move(result_); } | 68 std::unique_ptr<base::Value> result() { return std::move(result_); } |
| 75 | 69 |
| 76 private: | 70 private: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 TestLauncherDelegate* delegate = NULL; | 126 TestLauncherDelegate* delegate = NULL; |
| 133 delegate = GetCurrentTestLauncherDelegate(); | 127 delegate = GetCurrentTestLauncherDelegate(); |
| 134 if (delegate) | 128 if (delegate) |
| 135 delegate->PreRunMessageLoop(run_loop); | 129 delegate->PreRunMessageLoop(run_loop); |
| 136 run_loop->Run(); | 130 run_loop->Run(); |
| 137 if (delegate) | 131 if (delegate) |
| 138 delegate->PostRunMessageLoop(); | 132 delegate->PostRunMessageLoop(); |
| 139 } | 133 } |
| 140 | 134 |
| 141 void RunAllPendingInMessageLoop() { | 135 void RunAllPendingInMessageLoop() { |
| 136 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 142 base::RunLoop run_loop; | 137 base::RunLoop run_loop; |
| 143 base::ThreadTaskRunnerHandle::Get()->PostTask( | 138 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 144 FROM_HERE, GetQuitTaskForRunLoop(&run_loop)); | 139 FROM_HERE, GetQuitTaskForRunLoop(&run_loop)); |
| 145 RunThisRunLoop(&run_loop); | 140 RunThisRunLoop(&run_loop); |
| 146 } | 141 } |
| 147 | 142 |
| 148 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { | 143 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { |
| 149 if (BrowserThread::CurrentlyOn(thread_id)) { | 144 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 145 if (thread_id == BrowserThread::UI) { |
| 150 RunAllPendingInMessageLoop(); | 146 RunAllPendingInMessageLoop(); |
| 151 return; | 147 return; |
| 152 } | 148 } |
| 153 BrowserThread::ID current_thread_id; | |
| 154 if (!BrowserThread::GetCurrentThreadIdentifier(¤t_thread_id)) { | |
| 155 NOTREACHED(); | |
| 156 return; | |
| 157 } | |
| 158 | 149 |
| 150 // Post a DeferredQuitRunLoop() task to |thread_id|. Then, run a RunLoop on |
| 151 // this thread. When a few generations of pending tasks have run on |
| 152 // |thread_id|, a task will be posted to this thread to exit the RunLoop. |
| 159 base::RunLoop run_loop; | 153 base::RunLoop run_loop; |
| 160 BrowserThread::PostTask(thread_id, FROM_HERE, | 154 const base::Closure post_quit_run_loop_to_ui_thread = base::Bind( |
| 161 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id, | 155 base::IgnoreResult(&base::SingleThreadTaskRunner::PostTask), |
| 162 run_loop.QuitClosure())); | 156 base::ThreadTaskRunnerHandle::Get(), FROM_HERE, run_loop.QuitClosure()); |
| 157 BrowserThread::PostTask( |
| 158 thread_id, FROM_HERE, |
| 159 base::Bind(&DeferredQuitRunLoop, post_quit_run_loop_to_ui_thread, |
| 160 kNumQuitDeferrals)); |
| 163 RunThisRunLoop(&run_loop); | 161 RunThisRunLoop(&run_loop); |
| 164 } | 162 } |
| 165 | 163 |
| 166 void RunAllBlockingPoolTasksUntilIdle() { | 164 void RunAllBlockingPoolTasksUntilIdle() { |
| 167 while (true) { | 165 while (true) { |
| 168 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 166 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 169 | 167 |
| 170 TaskObserver task_observer; | 168 TaskObserver task_observer; |
| 171 base::MessageLoop::current()->AddTaskObserver(&task_observer); | 169 base::MessageLoop::current()->AddTaskObserver(&task_observer); |
| 172 base::RunLoop().RunUntilIdle(); | 170 base::RunLoop().RunUntilIdle(); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 382 |
| 385 void WebContentsDestroyedWatcher::Wait() { | 383 void WebContentsDestroyedWatcher::Wait() { |
| 386 message_loop_runner_->Run(); | 384 message_loop_runner_->Run(); |
| 387 } | 385 } |
| 388 | 386 |
| 389 void WebContentsDestroyedWatcher::WebContentsDestroyed() { | 387 void WebContentsDestroyedWatcher::WebContentsDestroyed() { |
| 390 message_loop_runner_->Quit(); | 388 message_loop_runner_->Quit(); |
| 391 } | 389 } |
| 392 | 390 |
| 393 } // namespace content | 391 } // namespace content |
| OLD | NEW |