| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| 11 #include "content/public/test/test_launcher.h" | 11 #include "content/public/test/test_launcher.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace content { |
| 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 // Number of times to repost a Quit task so that the MessageLoop finishes up | 18 // Number of times to repost a Quit task so that the MessageLoop finishes up |
| 17 // pending tasks and tasks posted by those pending tasks without risking the | 19 // pending tasks and tasks posted by those pending tasks without risking the |
| 18 // potential hang behavior of MessageLoop::QuitWhenIdle. | 20 // potential hang behavior of MessageLoop::QuitWhenIdle. |
| 19 // The criteria for choosing this number: it should be high enough to make the | 21 // The criteria for choosing this number: it should be high enough to make the |
| 20 // quit act like QuitWhenIdle, while taking into account that any page which is | 22 // quit act like QuitWhenIdle, while taking into account that any page which is |
| 21 // animating may be rendering another frame for each quit deferral. For an | 23 // animating may be rendering another frame for each quit deferral. For an |
| 22 // animating page, the potential delay to quitting the RunLoop would be | 24 // animating page, the potential delay to quitting the RunLoop would be |
| 23 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as | 25 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as |
| 24 // 200ms/frame. | 26 // 200ms/frame. |
| 25 static const int kNumQuitDeferrals = 10; | 27 static const int kNumQuitDeferrals = 10; |
| 26 | 28 |
| 27 static void DeferredQuitRunLoop(const base::Closure& quit_task, | 29 static void DeferredQuitRunLoop(const base::Closure& quit_task, |
| 28 int num_quit_deferrals) { | 30 int num_quit_deferrals) { |
| 29 if (num_quit_deferrals <= 0) { | 31 if (num_quit_deferrals <= 0) { |
| 30 quit_task.Run(); | 32 quit_task.Run(); |
| 31 } else { | 33 } else { |
| 32 MessageLoop::current()->PostTask(FROM_HERE, | 34 MessageLoop::current()->PostTask(FROM_HERE, |
| 33 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | 35 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); |
| 34 } | 36 } |
| 35 } | 37 } |
| 36 | 38 |
| 39 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, |
| 40 const base::Closure& quit_task) { |
| 41 RunAllPendingInMessageLoop(); |
| 42 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); |
| 37 } | 43 } |
| 38 | 44 |
| 39 namespace content { | 45 } // namespace |
| 40 | 46 |
| 41 void RunMessageLoop() { | 47 void RunMessageLoop() { |
| 42 base::RunLoop run_loop; | 48 base::RunLoop run_loop; |
| 43 RunThisRunLoop(&run_loop); | 49 RunThisRunLoop(&run_loop); |
| 44 } | 50 } |
| 45 | 51 |
| 46 void RunThisRunLoop(base::RunLoop* run_loop) { | 52 void RunThisRunLoop(base::RunLoop* run_loop) { |
| 47 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 53 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 48 | 54 |
| 49 // If we're running inside a browser test, we might need to allow the test | 55 // If we're running inside a browser test, we might need to allow the test |
| 50 // launcher to do extra work before/after running a nested message loop. | 56 // launcher to do extra work before/after running a nested message loop. |
| 51 test_launcher::TestLauncherDelegate* delegate = | 57 test_launcher::TestLauncherDelegate* delegate = |
| 52 test_launcher::GetCurrentTestLauncherDelegate(); | 58 test_launcher::GetCurrentTestLauncherDelegate(); |
| 53 if (delegate) | 59 if (delegate) |
| 54 delegate->PreRunMessageLoop(run_loop); | 60 delegate->PreRunMessageLoop(run_loop); |
| 55 run_loop->Run(); | 61 run_loop->Run(); |
| 56 if (delegate) | 62 if (delegate) |
| 57 delegate->PostRunMessageLoop(); | 63 delegate->PostRunMessageLoop(); |
| 58 } | 64 } |
| 59 | 65 |
| 66 void RunAllPendingInMessageLoop() { |
| 67 MessageLoop::current()->PostTask(FROM_HERE, |
| 68 MessageLoop::QuitWhenIdleClosure()); |
| 69 RunMessageLoop(); |
| 70 } |
| 71 |
| 72 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { |
| 73 if (BrowserThread::CurrentlyOn(thread_id)) { |
| 74 RunAllPendingInMessageLoop(); |
| 75 return; |
| 76 } |
| 77 BrowserThread::ID current_thread_id; |
| 78 if (!BrowserThread::GetCurrentThreadIdentifier(¤t_thread_id)) { |
| 79 NOTREACHED(); |
| 80 return; |
| 81 } |
| 82 |
| 83 base::RunLoop run_loop; |
| 84 BrowserThread::PostTask(thread_id, FROM_HERE, |
| 85 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id, |
| 86 run_loop.QuitClosure())); |
| 87 RunThisRunLoop(&run_loop); |
| 88 } |
| 89 |
| 60 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { | 90 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { |
| 61 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), | 91 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), |
| 62 kNumQuitDeferrals); | 92 kNumQuitDeferrals); |
| 63 } | 93 } |
| 64 | 94 |
| 65 MessageLoopRunner::MessageLoopRunner() { | 95 MessageLoopRunner::MessageLoopRunner() { |
| 66 } | 96 } |
| 67 | 97 |
| 68 MessageLoopRunner::~MessageLoopRunner() { | 98 MessageLoopRunner::~MessageLoopRunner() { |
| 69 } | 99 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 details_ = details; | 139 details_ = details; |
| 110 seen_ = true; | 140 seen_ = true; |
| 111 if (!running_) | 141 if (!running_) |
| 112 return; | 142 return; |
| 113 | 143 |
| 114 message_loop_runner_->Quit(); | 144 message_loop_runner_->Quit(); |
| 115 running_ = false; | 145 running_ = false; |
| 116 } | 146 } |
| 117 | 147 |
| 118 } // namespace content | 148 } // namespace content |
| OLD | NEW |