OLD | NEW |
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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 RunFunction(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, | |
146 const std::string& args, | |
147 Browser* browser, | |
148 RunFunctionFlags flags) { | |
149 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); | |
150 ASSERT_TRUE(parsed_args.get()) << | |
151 "Could not parse extension function arguments: " << args; | |
152 function->SetArgs(parsed_args.get()); | |
153 | |
154 TestFunctionDispatcherDelegate dispatcher_delegate(browser); | |
155 ExtensionFunctionDispatcher dispatcher( | |
156 browser->profile(), &dispatcher_delegate); | |
157 function->set_dispatcher(dispatcher.AsWeakPtr()); | |
158 | |
159 function->set_profile(browser->profile()); | |
160 function->set_include_incognito(flags & INCLUDE_INCOGNITO); | |
161 function->Run(); | |
162 } | |
163 | |
164 // This helps us be able to wait until an AsyncExtensionFunction calls | 145 // This helps us be able to wait until an AsyncExtensionFunction calls |
165 // SendResponse. | 146 // SendResponse. |
166 class SendResponseDelegate : public AsyncExtensionFunction::DelegateForTests { | 147 class SendResponseDelegate |
| 148 : public UIThreadExtensionFunction::DelegateForTests { |
167 public: | 149 public: |
168 SendResponseDelegate() : should_post_quit_(false) {} | 150 SendResponseDelegate() : should_post_quit_(false) {} |
169 | 151 |
170 virtual ~SendResponseDelegate() {} | 152 virtual ~SendResponseDelegate() {} |
171 | 153 |
172 void set_should_post_quit(bool should_quit) { | 154 void set_should_post_quit(bool should_quit) { |
173 should_post_quit_ = should_quit; | 155 should_post_quit_ = should_quit; |
174 } | 156 } |
175 | 157 |
176 bool HasResponse() { | 158 bool HasResponse() { |
177 return response_.get() != NULL; | 159 return response_.get() != NULL; |
178 } | 160 } |
179 | 161 |
180 bool GetResponse() { | 162 bool GetResponse() { |
181 EXPECT_TRUE(HasResponse()); | 163 EXPECT_TRUE(HasResponse()); |
182 return *response_.get(); | 164 return *response_.get(); |
183 } | 165 } |
184 | 166 |
185 virtual void OnSendResponse(AsyncExtensionFunction* function, bool success) { | 167 virtual void OnSendResponse(UIThreadExtensionFunction* function, |
| 168 bool success) { |
186 ASSERT_FALSE(HasResponse()); | 169 ASSERT_FALSE(HasResponse()); |
187 response_.reset(new bool); | 170 response_.reset(new bool); |
188 *response_ = success; | 171 *response_ = success; |
189 if (should_post_quit_) { | 172 if (should_post_quit_) { |
190 MessageLoopForUI::current()->Quit(); | 173 MessageLoopForUI::current()->Quit(); |
191 } | 174 } |
192 } | 175 } |
193 | 176 |
194 private: | 177 private: |
195 scoped_ptr<bool> response_; | 178 scoped_ptr<bool> response_; |
196 bool should_post_quit_; | 179 bool should_post_quit_; |
197 }; | 180 }; |
198 | 181 |
199 bool RunAsyncFunction(AsyncExtensionFunction* function, | 182 bool RunFunction(UIThreadExtensionFunction* function, |
200 const std::string& args, | 183 const std::string& args, |
201 Browser* browser, | 184 Browser* browser, |
202 RunFunctionFlags flags) { | 185 RunFunctionFlags flags) { |
203 SendResponseDelegate response_delegate; | 186 SendResponseDelegate response_delegate; |
204 function->set_test_delegate(&response_delegate); | 187 function->set_test_delegate(&response_delegate); |
205 RunFunction(function, args, browser, flags); | 188 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); |
| 189 EXPECT_TRUE(parsed_args.get()) << |
| 190 "Could not parse extension function arguments: " << args; |
| 191 function->SetArgs(parsed_args.get()); |
| 192 |
| 193 TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
| 194 ExtensionFunctionDispatcher dispatcher( |
| 195 browser->profile(), &dispatcher_delegate); |
| 196 function->set_dispatcher(dispatcher.AsWeakPtr()); |
| 197 |
| 198 function->set_profile(browser->profile()); |
| 199 function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| 200 function->Run(); |
206 | 201 |
207 // If the RunImpl of |function| didn't already call SendResponse, run the | 202 // If the RunImpl of |function| didn't already call SendResponse, run the |
208 // message loop until they do. | 203 // message loop until they do. |
209 if (!response_delegate.HasResponse()) { | 204 if (!response_delegate.HasResponse()) { |
210 response_delegate.set_should_post_quit(true); | 205 response_delegate.set_should_post_quit(true); |
211 ui_test_utils::RunMessageLoop(); | 206 ui_test_utils::RunMessageLoop(); |
212 } | 207 } |
213 | 208 |
214 EXPECT_TRUE(response_delegate.HasResponse()); | 209 EXPECT_TRUE(response_delegate.HasResponse()); |
215 return response_delegate.GetResponse(); | 210 return response_delegate.GetResponse(); |
216 } | 211 } |
217 | 212 |
218 } // namespace extension_function_test_utils | 213 } // namespace extension_function_test_utils |
OLD | NEW |