Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(557)

Side by Side Diff: components/test_runner/mock_web_speech_recognizer.cc

Issue 1852603002: Replacing most of web_task.h with base::Closure + base::WeakPtrFactory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move-mocks-to-test-runner
Patch Set: Rebasing... Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/test_runner/mock_web_speech_recognizer.h" 5 #include "components/test_runner/mock_web_speech_recognizer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "components/test_runner/web_task.h"
11 #include "components/test_runner/web_test_delegate.h" 14 #include "components/test_runner/web_test_delegate.h"
12 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h" 15 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h"
13 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h" 16 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h"
14 17
15 namespace test_runner { 18 namespace test_runner {
16 19
17 namespace { 20 namespace {
18 21
19 // Task class for calling a client function that does not take any parameters. 22 // Task class for calling a client function that does not take any parameters.
20 typedef void (blink::WebSpeechRecognizerClient::*ClientFunctionPointer)( 23 typedef void (blink::WebSpeechRecognizerClient::*ClientFunctionPointer)(
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 recognizer_->Client()->didEnd(handle); 125 recognizer_->Client()->didEnd(handle);
123 } 126 }
124 127
125 private: 128 private:
126 DISALLOW_COPY_AND_ASSIGN(EndedTask); 129 DISALLOW_COPY_AND_ASSIGN(EndedTask);
127 }; 130 };
128 131
129 } // namespace 132 } // namespace
130 133
131 MockWebSpeechRecognizer::MockWebSpeechRecognizer() 134 MockWebSpeechRecognizer::MockWebSpeechRecognizer()
132 : was_aborted_(false), task_queue_running_(false), delegate_(0) { 135 : was_aborted_(false),
133 } 136 task_queue_running_(false),
137 delegate_(0),
138 weak_factory_(this) {}
134 139
135 MockWebSpeechRecognizer::~MockWebSpeechRecognizer() { 140 MockWebSpeechRecognizer::~MockWebSpeechRecognizer() {
136 ClearTaskQueue(); 141 ClearTaskQueue();
137 } 142 }
138 143
139 void MockWebSpeechRecognizer::SetDelegate(WebTestDelegate* delegate) { 144 void MockWebSpeechRecognizer::SetDelegate(WebTestDelegate* delegate) {
140 delegate_ = delegate; 145 delegate_ = delegate;
141 } 146 }
142 147
143 void MockWebSpeechRecognizer::start( 148 void MockWebSpeechRecognizer::start(
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 ClearTaskQueue(); 237 ClearTaskQueue();
233 task_queue_.push_back(new ErrorTask(this, code, message)); 238 task_queue_.push_back(new ErrorTask(this, code, message));
234 task_queue_.push_back(new EndedTask(this)); 239 task_queue_.push_back(new EndedTask(this));
235 240
236 StartTaskQueue(); 241 StartTaskQueue();
237 } 242 }
238 243
239 void MockWebSpeechRecognizer::StartTaskQueue() { 244 void MockWebSpeechRecognizer::StartTaskQueue() {
240 if (task_queue_running_) 245 if (task_queue_running_)
241 return; 246 return;
242 delegate_->PostTask(new StepTask(this)); 247 PostRunTaskFromQueue();
243 task_queue_running_ = true;
244 } 248 }
245 249
246 void MockWebSpeechRecognizer::ClearTaskQueue() { 250 void MockWebSpeechRecognizer::ClearTaskQueue() {
247 while (!task_queue_.empty()) { 251 while (!task_queue_.empty()) {
248 delete task_queue_.front(); 252 delete task_queue_.front();
249 task_queue_.pop_front(); 253 task_queue_.pop_front();
250 } 254 }
251 task_queue_running_ = false; 255 task_queue_running_ = false;
252 } 256 }
253 257
254 void MockWebSpeechRecognizer::StepTask::RunIfValid() { 258 void MockWebSpeechRecognizer::PostRunTaskFromQueue() {
255 if (object_->task_queue_.empty()) { 259 task_queue_running_ = true;
256 object_->task_queue_running_ = false; 260 delegate_->PostTask(new WebCallbackTask(base::Bind(
261 &MockWebSpeechRecognizer::RunTaskFromQueue, weak_factory_.GetWeakPtr())));
262 }
263
264 void MockWebSpeechRecognizer::RunTaskFromQueue() {
265 if (task_queue_.empty()) {
266 task_queue_running_ = false;
257 return; 267 return;
258 } 268 }
259 269
260 MockWebSpeechRecognizer::Task* task = object_->task_queue_.front(); 270 MockWebSpeechRecognizer::Task* task = task_queue_.front();
261 object_->task_queue_.pop_front(); 271 task_queue_.pop_front();
262 task->run(); 272 task->run();
263 delete task; 273 delete task;
264 274
265 if (object_->task_queue_.empty()) { 275 if (task_queue_.empty()) {
266 object_->task_queue_running_ = false; 276 task_queue_running_ = false;
267 return; 277 return;
268 } 278 }
269 279
270 object_->delegate_->PostTask(new StepTask(object_)); 280 PostRunTaskFromQueue();
271 } 281 }
272 282
273 } // namespace test_runner 283 } // namespace test_runner
OLDNEW
« no previous file with comments | « components/test_runner/mock_web_speech_recognizer.h ('k') | components/test_runner/mock_web_user_media_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698