OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/public/test/test_utils.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "content/public/browser/notification_service.h" | |
11 #include "content/public/test/test_launcher.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace content { | |
15 | |
16 namespace { | |
17 | |
18 // Number of times to repost a Quit task so that the MessageLoop finishes up | |
19 // pending tasks and tasks posted by those pending tasks without risking the | |
20 // potential hang behavior of MessageLoop::QuitWhenIdle. | |
21 // The criteria for choosing this number: it should be high enough to make the | |
22 // quit act like QuitWhenIdle, while taking into account that any page which is | |
23 // animating may be rendering another frame for each quit deferral. For an | |
24 // animating page, the potential delay to quitting the RunLoop would be | |
25 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as | |
26 // 200ms/frame. | |
27 static const int kNumQuitDeferrals = 10; | |
28 | |
29 static void DeferredQuitRunLoop(const base::Closure& quit_task, | |
30 int num_quit_deferrals) { | |
31 if (num_quit_deferrals <= 0) { | |
32 quit_task.Run(); | |
33 } else { | |
34 MessageLoop::current()->PostTask(FROM_HERE, | |
35 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | |
36 } | |
37 } | |
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); | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 void RunMessageLoop() { | |
48 base::RunLoop run_loop; | |
49 RunThisRunLoop(&run_loop); | |
50 } | |
51 | |
52 void RunThisRunLoop(base::RunLoop* run_loop) { | |
53 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
54 | |
55 // If we're running inside a browser test, we might need to allow the test | |
56 // launcher to do extra work before/after running a nested message loop. | |
57 test_launcher::TestLauncherDelegate* delegate = | |
58 test_launcher::GetCurrentTestLauncherDelegate(); | |
59 if (delegate) | |
60 delegate->PreRunMessageLoop(run_loop); | |
61 run_loop->Run(); | |
62 if (delegate) | |
63 delegate->PostRunMessageLoop(); | |
64 } | |
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 | |
90 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { | |
91 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), | |
92 kNumQuitDeferrals); | |
93 } | |
94 | |
95 MessageLoopRunner::MessageLoopRunner() { | |
96 } | |
97 | |
98 MessageLoopRunner::~MessageLoopRunner() { | |
99 } | |
100 | |
101 void MessageLoopRunner::Run() { | |
102 RunThisRunLoop(&run_loop_); | |
103 } | |
104 | |
105 base::Closure MessageLoopRunner::QuitClosure() { | |
106 return base::Bind(&MessageLoopRunner::Quit, this); | |
107 } | |
108 | |
109 void MessageLoopRunner::Quit() { | |
110 GetQuitTaskForRunLoop(&run_loop_).Run(); | |
111 } | |
112 | |
113 WindowedNotificationObserver::WindowedNotificationObserver( | |
114 int notification_type, | |
115 const NotificationSource& source) | |
116 : seen_(false), | |
117 running_(false), | |
118 source_(NotificationService::AllSources()) { | |
119 registrar_.Add(this, notification_type, source); | |
120 } | |
121 | |
122 WindowedNotificationObserver::~WindowedNotificationObserver() {} | |
123 | |
124 void WindowedNotificationObserver::Wait() { | |
125 if (seen_) | |
126 return; | |
127 | |
128 running_ = true; | |
129 message_loop_runner_ = new MessageLoopRunner; | |
130 message_loop_runner_->Run(); | |
131 EXPECT_TRUE(seen_); | |
132 } | |
133 | |
134 void WindowedNotificationObserver::Observe( | |
135 int type, | |
136 const NotificationSource& source, | |
137 const NotificationDetails& details) { | |
138 source_ = source; | |
139 details_ = details; | |
140 seen_ = true; | |
141 if (!running_) | |
142 return; | |
143 | |
144 message_loop_runner_->Quit(); | |
145 running_ = false; | |
146 } | |
147 | |
148 } // namespace content | |
OLD | NEW |