| 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" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_function.h" | 12 #include "chrome/browser/extensions/extension_function.h" |
| 13 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 13 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 class TestFunctionDispatcherDelegate | 20 class TestFunctionDispatcherDelegate |
| 22 : public ExtensionFunctionDispatcher::Delegate { | 21 : public ExtensionFunctionDispatcher::Delegate { |
| 23 public: | 22 public: |
| 24 explicit TestFunctionDispatcherDelegate(Browser* browser) : | 23 explicit TestFunctionDispatcherDelegate(Browser* browser) : |
| 25 browser_(browser) {} | 24 browser_(browser) {} |
| 26 virtual ~TestFunctionDispatcherDelegate() {} | 25 virtual ~TestFunctionDispatcherDelegate() {} |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 TestFunctionDispatcherDelegate dispatcher_delegate(browser); | 153 TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
| 155 ExtensionFunctionDispatcher dispatcher( | 154 ExtensionFunctionDispatcher dispatcher( |
| 156 browser->profile(), &dispatcher_delegate); | 155 browser->profile(), &dispatcher_delegate); |
| 157 function->set_dispatcher(dispatcher.AsWeakPtr()); | 156 function->set_dispatcher(dispatcher.AsWeakPtr()); |
| 158 | 157 |
| 159 function->set_profile(browser->profile()); | 158 function->set_profile(browser->profile()); |
| 160 function->set_include_incognito(flags & INCLUDE_INCOGNITO); | 159 function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| 161 function->Run(); | 160 function->Run(); |
| 162 } | 161 } |
| 163 | 162 |
| 164 // This helps us be able to wait until an AsyncExtensionFunction calls | |
| 165 // SendResponse. | |
| 166 class SendResponseDelegate : public AsyncExtensionFunction::DelegateForTests { | |
| 167 public: | |
| 168 SendResponseDelegate() : should_post_quit_(false) {} | |
| 169 | |
| 170 virtual ~SendResponseDelegate() {} | |
| 171 | |
| 172 void set_should_post_quit(bool should_quit) { | |
| 173 should_post_quit_ = should_quit; | |
| 174 } | |
| 175 | |
| 176 bool HasResponse() { | |
| 177 return response_.get() != NULL; | |
| 178 } | |
| 179 | |
| 180 bool GetResponse() { | |
| 181 EXPECT_TRUE(HasResponse()); | |
| 182 return *response_.get(); | |
| 183 } | |
| 184 | |
| 185 virtual void OnSendResponse(AsyncExtensionFunction* function, bool success) { | |
| 186 ASSERT_FALSE(HasResponse()); | |
| 187 response_.reset(new bool); | |
| 188 *response_ = success; | |
| 189 if (should_post_quit_) { | |
| 190 MessageLoopForUI::current()->Quit(); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 private: | |
| 195 scoped_ptr<bool> response_; | |
| 196 bool should_post_quit_; | |
| 197 }; | |
| 198 | |
| 199 bool RunAsyncFunction(AsyncExtensionFunction* function, | |
| 200 const std::string& args, | |
| 201 Browser* browser, | |
| 202 RunFunctionFlags flags) { | |
| 203 SendResponseDelegate response_delegate; | |
| 204 function->set_test_delegate(&response_delegate); | |
| 205 RunFunction(function, args, browser, flags); | |
| 206 | |
| 207 // If the RunImpl of |function| didn't already call SendResponse, run the | |
| 208 // message loop until they do. | |
| 209 if (!response_delegate.HasResponse()) { | |
| 210 response_delegate.set_should_post_quit(true); | |
| 211 ui_test_utils::RunMessageLoop(); | |
| 212 } | |
| 213 | |
| 214 EXPECT_TRUE(response_delegate.HasResponse()); | |
| 215 return response_delegate.GetResponse(); | |
| 216 } | |
| 217 | |
| 218 } // namespace extension_function_test_utils | 163 } // namespace extension_function_test_utils |
| OLD | NEW |