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

Side by Side Diff: ppapi/tests/test_utils.cc

Issue 9937001: PPAPI: Refactor ppapi test callbacks to ease testing blocking callbacks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 8 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 | Annotate | Revision Log
« no previous file with comments | « ppapi/tests/test_utils.h ('k') | ppapi/tests/test_websocket.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "ppapi/tests/test_utils.h" 5 #include "ppapi/tests/test_utils.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #if defined(_MSC_VER) 9 #if defined(_MSC_VER)
10 #include <windows.h> 10 #include <windows.h>
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 return false; 64 return false;
65 65
66 int i = atoi(url.substr(components.port.begin, components.port.len).c_str()); 66 int i = atoi(url.substr(components.port.begin, components.port.len).c_str());
67 if (i < 0 || i > 65535) 67 if (i < 0 || i > 65535)
68 return false; 68 return false;
69 *port = static_cast<uint16_t>(i); 69 *port = static_cast<uint16_t>(i);
70 70
71 return true; 71 return true;
72 } 72 }
73 73
74 void NestedEvent::Wait() {
75 // Don't allow nesting more than once; it doesn't work with the code as-is,
76 // and probably is a bad idea most of the time anyway.
77 PP_DCHECK(!waiting_);
78 if (signalled_)
79 return;
80 waiting_ = true;
81 while (!signalled_)
82 GetTestingInterface()->RunMessageLoop(instance_);
83 waiting_ = false;
84 }
85
86 void NestedEvent::Signal() {
87 signalled_ = true;
88 if (waiting_)
89 GetTestingInterface()->QuitMessageLoop(instance_);
90 }
91
74 TestCompletionCallback::TestCompletionCallback(PP_Instance instance) 92 TestCompletionCallback::TestCompletionCallback(PP_Instance instance)
75 : have_result_(false), 93 : wait_for_result_called_(false),
94 have_result_(false),
76 result_(PP_OK_COMPLETIONPENDING), 95 result_(PP_OK_COMPLETIONPENDING),
77 force_async_(false), 96 // TODO(dmichael): The default should probably be PP_REQUIRED, but this is
97 // what the tests currently expect.
98 callback_type_(PP_OPTIONAL),
78 post_quit_task_(false), 99 post_quit_task_(false),
79 run_count_(0),
80 instance_(instance) { 100 instance_(instance) {
81 } 101 }
82 102
83 TestCompletionCallback::TestCompletionCallback(PP_Instance instance, 103 TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
84 bool force_async) 104 bool force_async)
85 : have_result_(false), 105 : wait_for_result_called_(false),
106 have_result_(false),
86 result_(PP_OK_COMPLETIONPENDING), 107 result_(PP_OK_COMPLETIONPENDING),
87 force_async_(force_async), 108 callback_type_(force_async ? PP_REQUIRED : PP_OPTIONAL),
88 post_quit_task_(false), 109 post_quit_task_(false),
89 run_count_(0), 110 instance_(instance) {
111 }
112
113 TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
114 CallbackType callback_type)
115 : wait_for_result_called_(false),
116 have_result_(false),
117 result_(PP_OK_COMPLETIONPENDING),
118 callback_type_(callback_type),
119 post_quit_task_(false),
90 instance_(instance) { 120 instance_(instance) {
91 } 121 }
92 122
93 int32_t TestCompletionCallback::WaitForResult() { 123 int32_t TestCompletionCallback::WaitForResult() {
124 PP_DCHECK(!wait_for_result_called_);
125 wait_for_result_called_ = true;
126 errors_.clear();
94 if (!have_result_) { 127 if (!have_result_) {
95 result_ = PP_OK_COMPLETIONPENDING; // Reset
96 post_quit_task_ = true; 128 post_quit_task_ = true;
97 GetTestingInterface()->RunMessageLoop(instance_); 129 GetTestingInterface()->RunMessageLoop(instance_);
98 } 130 }
99 have_result_ = false;
100 return result_; 131 return result_;
101 } 132 }
102 133
103 TestCompletionCallback::operator pp::CompletionCallback() const { 134 void TestCompletionCallback::WaitForResult(int32_t result) {
104 int32_t flags = (force_async_ ? 0 : PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); 135 PP_DCHECK(!wait_for_result_called_);
136 wait_for_result_called_ = true;
137 errors_.clear();
138 if (result == PP_OK_COMPLETIONPENDING) {
139 if (!have_result_) {
140 post_quit_task_ = true;
141 GetTestingInterface()->RunMessageLoop(instance_);
142 }
143 if (callback_type_ == PP_BLOCKING) {
144 errors_.assign(
145 ReportError("TestCompletionCallback: Call did not run synchronously "
146 "when passed a blocking completion callback!",
147 result_));
148 return;
149 }
150 } else {
151 result_ = result;
152 have_result_ = true;
153 if (callback_type_ == PP_REQUIRED) {
154 errors_.assign(
155 ReportError("TestCompletionCallback: Call ran synchronously when "
156 "passed a required completion callback!",
157 result_));
158 return;
159 }
160 }
161 PP_DCHECK(have_result_ == true);
162 }
163
164 void TestCompletionCallback::WaitForAbortResult(int32_t result) {
165 WaitForResult(result);
166 int32_t final_result = result_;
167 if (result == PP_OK_COMPLETIONPENDING) {
168 if (final_result != PP_ERROR_ABORTED) {
169 errors_.assign(
170 ReportError("TestCompletionCallback: Expected PP_ERROR_ABORTED or "
171 "PP_OK. Ran asynchronously.",
172 final_result));
173 return;
174 }
175 } else if (result != PP_OK) {
176 errors_.assign(
177 ReportError("TestCompletionCallback: Expected PP_ERROR_ABORTED or"
178 "PP_OK. Ran synchronously.",
179 result));
180 return;
181 }
182 }
183
184 pp::CompletionCallback TestCompletionCallback::GetCallback() {
185 Reset();
186 int32_t flags = 0;
187 if (callback_type_ == PP_BLOCKING)
188 return pp::CompletionCallback();
189 else if (callback_type_ == PP_OPTIONAL)
190 flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL;
105 return pp::CompletionCallback(&TestCompletionCallback::Handler, 191 return pp::CompletionCallback(&TestCompletionCallback::Handler,
106 const_cast<TestCompletionCallback*>(this), 192 const_cast<TestCompletionCallback*>(this),
107 flags); 193 flags);
108 } 194 }
109 195
196 void TestCompletionCallback::Reset() {
197 wait_for_result_called_ = false;
198 result_ = PP_OK_COMPLETIONPENDING;
199 have_result_ = false;
200 post_quit_task_ = false;
201 errors_.clear();
202 }
203
110 // static 204 // static
111 void TestCompletionCallback::Handler(void* user_data, int32_t result) { 205 void TestCompletionCallback::Handler(void* user_data, int32_t result) {
112 TestCompletionCallback* callback = 206 TestCompletionCallback* callback =
113 static_cast<TestCompletionCallback*>(user_data); 207 static_cast<TestCompletionCallback*>(user_data);
208 PP_DCHECK(!callback->have_result_);
114 callback->result_ = result; 209 callback->result_ = result;
115 callback->have_result_ = true; 210 callback->have_result_ = true;
116 callback->run_count_++;
117 if (callback->post_quit_task_) { 211 if (callback->post_quit_task_) {
118 callback->post_quit_task_ = false; 212 callback->post_quit_task_ = false;
119 GetTestingInterface()->QuitMessageLoop(callback->instance_); 213 GetTestingInterface()->QuitMessageLoop(callback->instance_);
120 } 214 }
121 } 215 }
216
OLDNEW
« no previous file with comments | « ppapi/tests/test_utils.h ('k') | ppapi/tests/test_websocket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698