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 "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 // animating page, the potential delay to quitting the RunLoop would be | 27 // animating page, the potential delay to quitting the RunLoop would be |
28 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as | 28 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as |
29 // 200ms/frame. | 29 // 200ms/frame. |
30 static const int kNumQuitDeferrals = 10; | 30 static const int kNumQuitDeferrals = 10; |
31 | 31 |
32 static void DeferredQuitRunLoop(const base::Closure& quit_task, | 32 static void DeferredQuitRunLoop(const base::Closure& quit_task, |
33 int num_quit_deferrals) { | 33 int num_quit_deferrals) { |
34 if (num_quit_deferrals <= 0) { | 34 if (num_quit_deferrals <= 0) { |
35 quit_task.Run(); | 35 quit_task.Run(); |
36 } else { | 36 } else { |
37 MessageLoop::current()->PostTask(FROM_HERE, | 37 base::MessageLoop::current()->PostTask( |
| 38 FROM_HERE, |
38 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); | 39 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); |
39 } | 40 } |
40 } | 41 } |
41 | 42 |
42 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, | 43 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, |
43 const base::Closure& quit_task) { | 44 const base::Closure& quit_task) { |
44 RunAllPendingInMessageLoop(); | 45 RunAllPendingInMessageLoop(); |
45 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); | 46 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); |
46 } | 47 } |
47 | 48 |
48 // Class used handle result callbacks for ExecuteScriptAndGetValue. | 49 // Class used handle result callbacks for ExecuteScriptAndGetValue. |
49 class ScriptCallback { | 50 class ScriptCallback { |
50 public: | 51 public: |
51 ScriptCallback() { } | 52 ScriptCallback() { } |
52 virtual ~ScriptCallback() { } | 53 virtual ~ScriptCallback() { } |
53 void ResultCallback(const base::Value* result); | 54 void ResultCallback(const base::Value* result); |
54 | 55 |
55 scoped_ptr<base::Value> result() { return result_.Pass(); } | 56 scoped_ptr<base::Value> result() { return result_.Pass(); } |
56 | 57 |
57 private: | 58 private: |
58 scoped_ptr<base::Value> result_; | 59 scoped_ptr<base::Value> result_; |
59 | 60 |
60 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); | 61 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); |
61 }; | 62 }; |
62 | 63 |
63 void ScriptCallback::ResultCallback(const base::Value* result) { | 64 void ScriptCallback::ResultCallback(const base::Value* result) { |
64 if (result) | 65 if (result) |
65 result_.reset(result->DeepCopy()); | 66 result_.reset(result->DeepCopy()); |
66 MessageLoop::current()->Quit(); | 67 base::MessageLoop::current()->Quit(); |
67 } | 68 } |
68 | 69 |
69 } // namespace | 70 } // namespace |
70 | 71 |
71 void RunMessageLoop() { | 72 void RunMessageLoop() { |
72 base::RunLoop run_loop; | 73 base::RunLoop run_loop; |
73 RunThisRunLoop(&run_loop); | 74 RunThisRunLoop(&run_loop); |
74 } | 75 } |
75 | 76 |
76 void RunThisRunLoop(base::RunLoop* run_loop) { | 77 void RunThisRunLoop(base::RunLoop* run_loop) { |
77 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 78 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 79 base::MessageLoop::current()); |
78 | 80 |
79 // If we're running inside a browser test, we might need to allow the test | 81 // If we're running inside a browser test, we might need to allow the test |
80 // launcher to do extra work before/after running a nested message loop. | 82 // launcher to do extra work before/after running a nested message loop. |
81 TestLauncherDelegate* delegate = NULL; | 83 TestLauncherDelegate* delegate = NULL; |
82 #if !defined(OS_IOS) | 84 #if !defined(OS_IOS) |
83 delegate = GetCurrentTestLauncherDelegate(); | 85 delegate = GetCurrentTestLauncherDelegate(); |
84 #endif | 86 #endif |
85 if (delegate) | 87 if (delegate) |
86 delegate->PreRunMessageLoop(run_loop); | 88 delegate->PreRunMessageLoop(run_loop); |
87 run_loop->Run(); | 89 run_loop->Run(); |
88 if (delegate) | 90 if (delegate) |
89 delegate->PostRunMessageLoop(); | 91 delegate->PostRunMessageLoop(); |
90 } | 92 } |
91 | 93 |
92 void RunAllPendingInMessageLoop() { | 94 void RunAllPendingInMessageLoop() { |
93 MessageLoop::current()->PostTask(FROM_HERE, | 95 base::MessageLoop::current()->PostTask( |
94 MessageLoop::QuitWhenIdleClosure()); | 96 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
95 RunMessageLoop(); | 97 RunMessageLoop(); |
96 } | 98 } |
97 | 99 |
98 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { | 100 void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) { |
99 if (BrowserThread::CurrentlyOn(thread_id)) { | 101 if (BrowserThread::CurrentlyOn(thread_id)) { |
100 RunAllPendingInMessageLoop(); | 102 RunAllPendingInMessageLoop(); |
101 return; | 103 return; |
102 } | 104 } |
103 BrowserThread::ID current_thread_id; | 105 BrowserThread::ID current_thread_id; |
104 if (!BrowserThread::GetCurrentThreadIdentifier(¤t_thread_id)) { | 106 if (!BrowserThread::GetCurrentThreadIdentifier(¤t_thread_id)) { |
(...skipping 15 matching lines...) Expand all Loading... |
120 | 122 |
121 scoped_ptr<base::Value> ExecuteScriptAndGetValue( | 123 scoped_ptr<base::Value> ExecuteScriptAndGetValue( |
122 RenderViewHost* render_view_host, | 124 RenderViewHost* render_view_host, |
123 const std::string& script) { | 125 const std::string& script) { |
124 ScriptCallback observer; | 126 ScriptCallback observer; |
125 | 127 |
126 render_view_host->ExecuteJavascriptInWebFrameCallbackResult( | 128 render_view_host->ExecuteJavascriptInWebFrameCallbackResult( |
127 string16(), // frame_xpath, | 129 string16(), // frame_xpath, |
128 UTF8ToUTF16(script), | 130 UTF8ToUTF16(script), |
129 base::Bind(&ScriptCallback::ResultCallback, base::Unretained(&observer))); | 131 base::Bind(&ScriptCallback::ResultCallback, base::Unretained(&observer))); |
130 MessageLoop* loop = MessageLoop::current(); | 132 base::MessageLoop* loop = base::MessageLoop::current(); |
131 loop->Run(); | 133 loop->Run(); |
132 return observer.result().Pass(); | 134 return observer.result().Pass(); |
133 } | 135 } |
134 | 136 |
135 MessageLoopRunner::MessageLoopRunner() | 137 MessageLoopRunner::MessageLoopRunner() |
136 : loop_running_(false), | 138 : loop_running_(false), |
137 quit_closure_called_(false) { | 139 quit_closure_called_(false) { |
138 } | 140 } |
139 | 141 |
140 MessageLoopRunner::~MessageLoopRunner() { | 142 MessageLoopRunner::~MessageLoopRunner() { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 details_ = details; | 196 details_ = details; |
195 seen_ = true; | 197 seen_ = true; |
196 if (!running_) | 198 if (!running_) |
197 return; | 199 return; |
198 | 200 |
199 message_loop_runner_->Quit(); | 201 message_loop_runner_->Quit(); |
200 running_ = false; | 202 running_ = false; |
201 } | 203 } |
202 | 204 |
203 } // namespace content | 205 } // namespace content |
OLD | NEW |