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

Side by Side Diff: chrome/browser/extensions/extension_function_test_utils.cc

Issue 8588067: Refactor to allow same code to test both sync and async functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial Created 9 years, 1 month 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
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 "chrome/browser/extensions/extension_function_test_utils.h" 5 #include "chrome/browser/extensions/extension_function_test_utils.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 112 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
113 const std::string& args, 113 const std::string& args,
114 Browser* browser) { 114 Browser* browser) {
115 return RunFunctionAndReturnError(function, args, browser, NONE); 115 return RunFunctionAndReturnError(function, args, browser, NONE);
116 } 116 }
117 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 117 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
118 const std::string& args, 118 const std::string& args,
119 Browser* browser, 119 Browser* browser,
120 RunFunctionFlags flags) { 120 RunFunctionFlags flags) {
121 scoped_refptr<ExtensionFunction> function_owner(function); 121 scoped_refptr<ExtensionFunction> function_owner(function);
122 RunFunction(function, args, browser, flags); 122 RunAsyncFunction(function, args, browser, flags);
123 EXPECT_FALSE(function->GetResultValue()) << "Unexpected function result " << 123 EXPECT_FALSE(function->GetResultValue()) << "Unexpected function result " <<
124 function->GetResult(); 124 function->GetResult();
125 return function->GetError(); 125 return function->GetError();
126 } 126 }
127 127
128 base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, 128 base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
129 const std::string& args, 129 const std::string& args,
130 Browser* browser) { 130 Browser* browser) {
131 return RunFunctionAndReturnResult(function, args, browser, NONE); 131 return RunFunctionAndReturnResult(function, args, browser, NONE);
132 } 132 }
133 base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, 133 base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
134 const std::string& args, 134 const std::string& args,
135 Browser* browser, 135 Browser* browser,
136 RunFunctionFlags flags) { 136 RunFunctionFlags flags) {
137 scoped_refptr<ExtensionFunction> function_owner(function); 137 scoped_refptr<ExtensionFunction> function_owner(function);
138 RunFunction(function, args, browser, flags); 138 RunAsyncFunction(function, args, browser, flags);
139 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " 139 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
140 << function->GetError(); 140 << function->GetError();
141 EXPECT_TRUE(function->GetResultValue()) << "No result value found"; 141 EXPECT_TRUE(function->GetResultValue()) << "No result value found";
142 return function->GetResultValue()->DeepCopy(); 142 return function->GetResultValue()->DeepCopy();
143 } 143 }
144 144
145 void RunFunction(UIThreadExtensionFunction* function, 145 void RunFunction(UIThreadExtensionFunction* function,
146 const std::string& args, 146 const std::string& args,
147 Browser* browser, 147 Browser* browser,
148 RunFunctionFlags flags) { 148 RunFunctionFlags flags) {
149 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); 149 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
150 ASSERT_TRUE(parsed_args.get()) << 150 ASSERT_TRUE(parsed_args.get()) <<
151 "Could not parse extension function arguments: " << args; 151 "Could not parse extension function arguments: " << args;
152 function->SetArgs(parsed_args.get()); 152 function->SetArgs(parsed_args.get());
153 153
154 TestFunctionDispatcherDelegate dispatcher_delegate(browser); 154 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
155 ExtensionFunctionDispatcher dispatcher( 155 ExtensionFunctionDispatcher dispatcher(
156 browser->profile(), &dispatcher_delegate); 156 browser->profile(), &dispatcher_delegate);
157 function->set_dispatcher(dispatcher.AsWeakPtr()); 157 function->set_dispatcher(dispatcher.AsWeakPtr());
158 158
159 function->set_profile(browser->profile()); 159 function->set_profile(browser->profile());
160 function->set_include_incognito(flags & INCLUDE_INCOGNITO); 160 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
161 function->Run(); 161 function->Run();
162 } 162 }
163 163
164 // This helps us be able to wait until an AsyncExtensionFunction calls 164 // This helps us be able to wait until an AsyncExtensionFunction calls
165 // SendResponse. 165 // SendResponse.
166 class SendResponseDelegate : public AsyncExtensionFunction::DelegateForTests { 166 class SendResponseDelegate
167 : public UIThreadExtensionFunction::DelegateForTests {
167 public: 168 public:
168 SendResponseDelegate() : should_post_quit_(false) {} 169 SendResponseDelegate() : should_post_quit_(false) {}
169 170
170 virtual ~SendResponseDelegate() {} 171 virtual ~SendResponseDelegate() {}
171 172
172 void set_should_post_quit(bool should_quit) { 173 void set_should_post_quit(bool should_quit) {
173 should_post_quit_ = should_quit; 174 should_post_quit_ = should_quit;
174 } 175 }
175 176
176 bool HasResponse() { 177 bool HasResponse() {
177 return response_.get() != NULL; 178 return response_.get() != NULL;
178 } 179 }
179 180
180 bool GetResponse() { 181 bool GetResponse() {
181 EXPECT_TRUE(HasResponse()); 182 EXPECT_TRUE(HasResponse());
182 return *response_.get(); 183 return *response_.get();
183 } 184 }
184 185
185 virtual void OnSendResponse(AsyncExtensionFunction* function, bool success) { 186 virtual void OnSendResponse(UIThreadExtensionFunction* function,
187 bool success) {
186 ASSERT_FALSE(HasResponse()); 188 ASSERT_FALSE(HasResponse());
187 response_.reset(new bool); 189 response_.reset(new bool);
188 *response_ = success; 190 *response_ = success;
189 if (should_post_quit_) { 191 if (should_post_quit_) {
190 MessageLoopForUI::current()->Quit(); 192 MessageLoopForUI::current()->Quit();
191 } 193 }
192 } 194 }
193 195
194 private: 196 private:
195 scoped_ptr<bool> response_; 197 scoped_ptr<bool> response_;
196 bool should_post_quit_; 198 bool should_post_quit_;
197 }; 199 };
198 200
199 bool RunAsyncFunction(AsyncExtensionFunction* function, 201 bool RunAsyncFunction(UIThreadExtensionFunction* function,
200 const std::string& args, 202 const std::string& args,
201 Browser* browser, 203 Browser* browser,
202 RunFunctionFlags flags) { 204 RunFunctionFlags flags) {
203 SendResponseDelegate response_delegate; 205 SendResponseDelegate response_delegate;
204 function->set_test_delegate(&response_delegate); 206 function->set_test_delegate(&response_delegate);
205 RunFunction(function, args, browser, flags); 207 RunFunction(function, args, browser, flags);
206 208
207 // If the RunImpl of |function| didn't already call SendResponse, run the 209 // If the RunImpl of |function| didn't already call SendResponse, run the
208 // message loop until they do. 210 // message loop until they do.
209 if (!response_delegate.HasResponse()) { 211 if (!response_delegate.HasResponse()) {
210 response_delegate.set_should_post_quit(true); 212 response_delegate.set_should_post_quit(true);
211 ui_test_utils::RunMessageLoop(); 213 ui_test_utils::RunMessageLoop();
212 } 214 }
213 215
214 EXPECT_TRUE(response_delegate.HasResponse()); 216 EXPECT_TRUE(response_delegate.HasResponse());
215 return response_delegate.GetResponse(); 217 return response_delegate.GetResponse();
216 } 218 }
217 219
218 } // namespace extension_function_test_utils 220 } // namespace extension_function_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698