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/lazy_instance.h" | |
9 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
10 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 11 #include "base/values.h" |
13 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
14 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
15 #include "content/public/test/test_launcher.h" | 14 #include "content/public/test/test_launcher.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 base::LazyInstance<std::vector<RunMessageLoopHook> >::Leaky | |
23 g_pre_run_message_loop_hooks = LAZY_INSTANCE_INITIALIZER; | |
24 | |
25 base::LazyInstance<std::vector<RunMessageLoopHook> >::Leaky | |
26 g_post_run_message_loop_hooks = LAZY_INSTANCE_INITIALIZER; | |
27 | |
28 // Number of times to repost a Quit task so that the MessageLoop finishes up | 21 // Number of times to repost a Quit task so that the MessageLoop finishes up |
29 // pending tasks and tasks posted by those pending tasks without risking the | 22 // pending tasks and tasks posted by those pending tasks without risking the |
30 // potential hang behavior of MessageLoop::QuitWhenIdle. | 23 // potential hang behavior of MessageLoop::QuitWhenIdle. |
31 // The criteria for choosing this number: it should be high enough to make the | 24 // The criteria for choosing this number: it should be high enough to make the |
32 // quit act like QuitWhenIdle, while taking into account that any page which is | 25 // quit act like QuitWhenIdle, while taking into account that any page which is |
33 // animating may be rendering another frame for each quit deferral. For an | 26 // animating may be rendering another frame for each quit deferral. For an |
34 // animating page, the potential delay to quitting the RunLoop would be | 27 // animating page, the potential delay to quitting the RunLoop would be |
35 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as | 28 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as |
36 // 200ms/frame. | 29 // 200ms/frame. |
37 static const int kNumQuitDeferrals = 10; | 30 static const int kNumQuitDeferrals = 10; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 71 |
79 void RunMessageLoop() { | 72 void RunMessageLoop() { |
80 base::RunLoop run_loop; | 73 base::RunLoop run_loop; |
81 RunThisRunLoop(&run_loop); | 74 RunThisRunLoop(&run_loop); |
82 } | 75 } |
83 | 76 |
84 void RunThisRunLoop(base::RunLoop* run_loop) { | 77 void RunThisRunLoop(base::RunLoop* run_loop) { |
85 base::MessageLoop::ScopedNestableTaskAllower allow( | 78 base::MessageLoop::ScopedNestableTaskAllower allow( |
86 base::MessageLoop::current()); | 79 base::MessageLoop::current()); |
87 | 80 |
88 for (size_t i = 0; i < g_pre_run_message_loop_hooks.Get().size(); i++) | 81 // If we're running inside a browser test, we might need to allow the test |
89 g_pre_run_message_loop_hooks.Get()[i].Run(run_loop); | 82 // launcher to do extra work before/after running a nested message loop. |
90 | 83 TestLauncherDelegate* delegate = NULL; |
| 84 #if !defined(OS_IOS) |
| 85 delegate = GetCurrentTestLauncherDelegate(); |
| 86 #endif |
| 87 if (delegate) |
| 88 delegate->PreRunMessageLoop(run_loop); |
91 run_loop->Run(); | 89 run_loop->Run(); |
92 | 90 if (delegate) |
93 for (size_t i = 0; i < g_pre_run_message_loop_hooks.Get().size(); i++) | 91 delegate->PostRunMessageLoop(); |
94 g_post_run_message_loop_hooks.Get()[i].Run(run_loop); | |
95 } | |
96 | |
97 void AddPreRunMessageLoopHook(const RunMessageLoopHook& hook) { | |
98 g_pre_run_message_loop_hooks.Get().push_back(hook); | |
99 } | |
100 | |
101 void AddPostRunMessageLoopHook(const RunMessageLoopHook& hook) { | |
102 g_post_run_message_loop_hooks.Get().push_back(hook); | |
103 } | 92 } |
104 | 93 |
105 void RunAllPendingInMessageLoop() { | 94 void RunAllPendingInMessageLoop() { |
106 base::MessageLoop::current()->PostTask( | 95 base::MessageLoop::current()->PostTask( |
107 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 96 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
108 RunMessageLoop(); | 97 RunMessageLoop(); |
109 } | 98 } |
110 | 99 |
111 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { | 100 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { |
112 if (BrowserThread::CurrentlyOn(thread_id)) { | 101 if (BrowserThread::CurrentlyOn(thread_id)) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 details_ = details; | 206 details_ = details; |
218 seen_ = true; | 207 seen_ = true; |
219 if (!running_ || (!callback_.is_null() && !callback_.Run())) | 208 if (!running_ || (!callback_.is_null() && !callback_.Run())) |
220 return; | 209 return; |
221 | 210 |
222 message_loop_runner_->Quit(); | 211 message_loop_runner_->Quit(); |
223 running_ = false; | 212 running_ = false; |
224 } | 213 } |
225 | 214 |
226 } // namespace content | 215 } // namespace content |
OLD | NEW |