| 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/browser_test_utils.h" | 5 #include "content/public/test/browser_test_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "content/public/test/test_launcher.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Number of times to repost a Quit task so that the MessageLoop finishes up | |
| 15 // pending tasks and tasks posted by those pending tasks without risking the | |
| 16 // potential hang behavior of MessageLoop::QuitWhenIdle. | |
| 17 // The criteria for choosing this number: it should be high enough to make the | |
| 18 // quit act like QuitWhenIdle, while taking into account that any page which is | |
| 19 // animating may be rendering another frame for each quit deferral. For an | |
| 20 // animating page, the potential delay to quitting the RunLoop would be | |
| 21 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as | |
| 22 // 200ms/frame. | |
| 23 static const int kNumQuitDeferrals = 10; | |
| 24 | |
| 25 static void DeferredQuitRunLoop(const base::Closure& quit_task, | |
| 26 int num_quit_deferrals) { | |
| 27 if (num_quit_deferrals <= 0) { | |
| 28 quit_task.Run(); | |
| 29 } else { | |
| 30 MessageLoop::current()->PostTask(FROM_HERE, | |
| 31 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 } | |
| 36 | |
| 37 namespace content { | 7 namespace content { |
| 38 | 8 |
| 39 void RunThisRunLoop(base::RunLoop* run_loop) { | |
| 40 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 41 | |
| 42 test_launcher::TestLauncherDelegate* delegate = | |
| 43 test_launcher::GetCurrentTestLauncherDelegate(); | |
| 44 // TODO(jam): the null check shouldn't be needed, since this code should only | |
| 45 // be called from browser_tests. Unfortunately some unittests are calling this | |
| 46 // code. | |
| 47 if (delegate) | |
| 48 delegate->PreRunMessageLoop(run_loop); | |
| 49 run_loop->Run(); | |
| 50 if (delegate) | |
| 51 delegate->PostRunMessageLoop(); | |
| 52 } | |
| 53 | |
| 54 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { | |
| 55 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), | |
| 56 kNumQuitDeferrals); | |
| 57 } | |
| 58 | |
| 59 } // namespace content | 9 } // namespace content |
| OLD | NEW |