Chromium Code Reviews| 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/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | 42 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, | 46 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, |
| 47 const base::Closure& quit_task) { | 47 const base::Closure& quit_task) { |
| 48 RunAllPendingInMessageLoop(); | 48 RunAllPendingInMessageLoop(); |
| 49 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); | 49 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Class used handle result callbacks for ExecuteScriptAndGetValue. | 52 // Class used to handle result callbacks for ExecuteScriptAndGetValue. |
| 53 class ScriptCallback { | 53 class ScriptCallback { |
| 54 public: | 54 public: |
| 55 ScriptCallback() { } | 55 ScriptCallback() { } |
| 56 virtual ~ScriptCallback() { } | 56 virtual ~ScriptCallback() { } |
| 57 void ResultCallback(const base::Value* result); | 57 void ResultCallback(const base::Value* result); |
| 58 | 58 |
| 59 scoped_ptr<base::Value> result() { return result_.Pass(); } | 59 scoped_ptr<base::Value> result() { return result_.Pass(); } |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 scoped_ptr<base::Value> result_; | 62 scoped_ptr<base::Value> result_; |
| 63 | 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); | 64 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 void ScriptCallback::ResultCallback(const base::Value* result) { | 67 void ScriptCallback::ResultCallback(const base::Value* result) { |
| 68 if (result) | 68 if (result) |
| 69 result_.reset(result->DeepCopy()); | 69 result_.reset(result->DeepCopy()); |
| 70 base::MessageLoop::current()->Quit(); | 70 base::MessageLoop::current()->Quit(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Monitors if any task is posted to a message loop. | |
| 74 class TaskObserver : public base::MessageLoop::TaskObserver { | |
| 75 public: | |
| 76 TaskObserver() : posted_(false) {} | |
| 77 virtual ~TaskObserver() {} | |
| 78 | |
| 79 // MessageLoop::TaskObserver overrides. | |
| 80 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE { | |
| 81 } | |
| 82 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE { | |
| 83 posted_ = true; | |
| 84 } | |
| 85 | |
| 86 // Returns true if any task was posted. | |
| 87 bool posted() const { return posted_; } | |
| 88 | |
| 89 private: | |
| 90 bool posted_; | |
| 91 DISALLOW_COPY_AND_ASSIGN(TaskObserver); | |
| 92 }; | |
| 93 | |
| 73 // Adapter that makes a WindowedNotificationObserver::ConditionTestCallback from | 94 // Adapter that makes a WindowedNotificationObserver::ConditionTestCallback from |
| 74 // a WindowedNotificationObserver::ConditionTestCallbackWithoutSourceAndDetails | 95 // a WindowedNotificationObserver::ConditionTestCallbackWithoutSourceAndDetails |
| 75 // by ignoring the notification source and details. | 96 // by ignoring the notification source and details. |
| 76 bool IgnoreSourceAndDetails( | 97 bool IgnoreSourceAndDetails( |
| 77 const WindowedNotificationObserver:: | 98 const WindowedNotificationObserver:: |
| 78 ConditionTestCallbackWithoutSourceAndDetails& callback, | 99 ConditionTestCallbackWithoutSourceAndDetails& callback, |
| 79 const NotificationSource& source, | 100 const NotificationSource& source, |
| 80 const NotificationDetails& details) { | 101 const NotificationDetails& details) { |
| 81 return callback.Run(); | 102 return callback.Run(); |
| 82 } | 103 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 return; | 143 return; |
| 123 } | 144 } |
| 124 | 145 |
| 125 base::RunLoop run_loop; | 146 base::RunLoop run_loop; |
| 126 BrowserThread::PostTask(thread_id, FROM_HERE, | 147 BrowserThread::PostTask(thread_id, FROM_HERE, |
| 127 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id, | 148 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id, |
| 128 run_loop.QuitClosure())); | 149 run_loop.QuitClosure())); |
| 129 RunThisRunLoop(&run_loop); | 150 RunThisRunLoop(&run_loop); |
| 130 } | 151 } |
| 131 | 152 |
| 153 void RunAllBlockingPoolTasksUntilIdle() { | |
| 154 while (true) { | |
| 155 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 156 | |
| 157 TaskObserver task_observer; | |
|
sky
2014/07/11 16:37:54
It seems like you want to know if any tasks are ad
mtomasz
2014/07/14 07:33:57
Done.
hashimoto
2014/07/15 05:17:41
No need to start observing the message loop while
mtomasz
2014/07/15 05:45:29
Done.
| |
| 158 base::MessageLoop::current()->AddTaskObserver(&task_observer); | |
| 159 base::RunLoop().RunUntilIdle(); | |
| 160 base::MessageLoop::current()->RemoveTaskObserver(&task_observer); | |
| 161 | |
| 162 if (!task_observer.posted()) | |
| 163 break; | |
| 164 } | |
| 165 } | |
| 166 | |
| 132 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { | 167 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { |
| 133 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), | 168 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), |
| 134 kNumQuitDeferrals); | 169 kNumQuitDeferrals); |
| 135 } | 170 } |
| 136 | 171 |
| 137 scoped_ptr<base::Value> ExecuteScriptAndGetValue( | 172 scoped_ptr<base::Value> ExecuteScriptAndGetValue( |
| 138 RenderFrameHost* render_frame_host, const std::string& script) { | 173 RenderFrameHost* render_frame_host, const std::string& script) { |
| 139 ScriptCallback observer; | 174 ScriptCallback observer; |
| 140 | 175 |
| 141 render_frame_host->ExecuteJavaScript( | 176 render_frame_host->ExecuteJavaScript( |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 void InProcessUtilityThreadHelper::BrowserChildProcessHostDisconnected( | 303 void InProcessUtilityThreadHelper::BrowserChildProcessHostDisconnected( |
| 269 const ChildProcessData& data) { | 304 const ChildProcessData& data) { |
| 270 if (--child_thread_count_) | 305 if (--child_thread_count_) |
| 271 return; | 306 return; |
| 272 | 307 |
| 273 if (runner_.get()) | 308 if (runner_.get()) |
| 274 runner_->Quit(); | 309 runner_->Quit(); |
| 275 } | 310 } |
| 276 | 311 |
| 277 } // namespace content | 312 } // namespace content |
| OLD | NEW |