| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 81 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 82 const std::string& args, | 82 const std::string& args, |
| 83 Browser* browser) { | 83 Browser* browser) { |
| 84 return RunFunctionAndReturnError(function, args, browser, NONE); | 84 return RunFunctionAndReturnError(function, args, browser, NONE); |
| 85 } | 85 } |
| 86 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 86 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 87 const std::string& args, | 87 const std::string& args, |
| 88 Browser* browser, | 88 Browser* browser, |
| 89 RunFunctionFlags flags) { | 89 RunFunctionFlags flags) { |
| 90 scoped_refptr<ExtensionFunction> function_owner(function); | 90 scoped_refptr<ExtensionFunction> function_owner(function); |
| 91 // Without a callback the function will not generate a result. | |
| 92 function->set_has_callback(true); | |
| 93 RunFunction(function, args, browser, flags); | 91 RunFunction(function, args, browser, flags); |
| 94 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; | 92 // When sending a response, the function will set an empty list value if there |
| 93 // is no specified result. |
| 94 const base::ListValue* results = function->GetResultList(); |
| 95 CHECK(results); |
| 96 EXPECT_TRUE(results->empty()) << "Did not expect a result"; |
| 97 CHECK(function->response()); |
| 98 EXPECT_EQ(ExtensionFunction::FAILED, *function->response()); |
| 95 return function->GetError(); | 99 return function->GetError(); |
| 96 } | 100 } |
| 97 | 101 |
| 98 base::Value* RunFunctionAndReturnSingleResult( | 102 base::Value* RunFunctionAndReturnSingleResult( |
| 99 UIThreadExtensionFunction* function, | 103 UIThreadExtensionFunction* function, |
| 100 const std::string& args, | 104 const std::string& args, |
| 101 Browser* browser) { | 105 Browser* browser) { |
| 102 return RunFunctionAndReturnSingleResult(function, args, browser, NONE); | 106 return RunFunctionAndReturnSingleResult(function, args, browser, NONE); |
| 103 } | 107 } |
| 104 base::Value* RunFunctionAndReturnSingleResult( | 108 base::Value* RunFunctionAndReturnSingleResult( |
| 105 UIThreadExtensionFunction* function, | 109 UIThreadExtensionFunction* function, |
| 106 const std::string& args, | 110 const std::string& args, |
| 107 Browser* browser, | 111 Browser* browser, |
| 108 RunFunctionFlags flags) { | 112 RunFunctionFlags flags) { |
| 109 scoped_refptr<ExtensionFunction> function_owner(function); | 113 scoped_refptr<ExtensionFunction> function_owner(function); |
| 110 // Without a callback the function will not generate a result. | |
| 111 function->set_has_callback(true); | |
| 112 RunFunction(function, args, browser, flags); | 114 RunFunction(function, args, browser, flags); |
| 113 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " | 115 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " |
| 114 << function->GetError(); | 116 << function->GetError(); |
| 115 const base::Value* single_result = NULL; | 117 const base::Value* single_result = NULL; |
| 116 if (function->GetResultList() != NULL && | 118 if (function->GetResultList() != NULL && |
| 117 function->GetResultList()->Get(0, &single_result)) { | 119 function->GetResultList()->Get(0, &single_result)) { |
| 118 return single_result->DeepCopy(); | 120 return single_result->DeepCopy(); |
| 119 } | 121 } |
| 120 return NULL; | 122 return NULL; |
| 121 } | 123 } |
| 122 | 124 |
| 123 // This helps us be able to wait until an UIThreadExtensionFunction calls | |
| 124 // SendResponse. | |
| 125 class SendResponseDelegate | |
| 126 : public UIThreadExtensionFunction::DelegateForTests { | |
| 127 public: | |
| 128 SendResponseDelegate() : should_post_quit_(false) {} | |
| 129 | |
| 130 virtual ~SendResponseDelegate() {} | |
| 131 | |
| 132 void set_should_post_quit(bool should_quit) { | |
| 133 should_post_quit_ = should_quit; | |
| 134 } | |
| 135 | |
| 136 bool HasResponse() { | |
| 137 return response_.get() != NULL; | |
| 138 } | |
| 139 | |
| 140 bool GetResponse() { | |
| 141 EXPECT_TRUE(HasResponse()); | |
| 142 return *response_; | |
| 143 } | |
| 144 | |
| 145 void OnSendResponse(UIThreadExtensionFunction* function, | |
| 146 bool success, | |
| 147 bool bad_message) override { | |
| 148 ASSERT_FALSE(bad_message); | |
| 149 ASSERT_FALSE(HasResponse()); | |
| 150 response_.reset(new bool); | |
| 151 *response_ = success; | |
| 152 if (should_post_quit_) { | |
| 153 base::MessageLoopForUI::current()->QuitWhenIdle(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 private: | |
| 158 std::unique_ptr<bool> response_; | |
| 159 bool should_post_quit_; | |
| 160 }; | |
| 161 | |
| 162 bool RunFunction(UIThreadExtensionFunction* function, | 125 bool RunFunction(UIThreadExtensionFunction* function, |
| 163 const std::string& args, | 126 const std::string& args, |
| 164 Browser* browser, | 127 Browser* browser, |
| 165 RunFunctionFlags flags) { | 128 RunFunctionFlags flags) { |
| 166 std::unique_ptr<base::ListValue> parsed_args(ParseList(args)); | 129 std::unique_ptr<base::ListValue> parsed_args(ParseList(args)); |
| 167 EXPECT_TRUE(parsed_args.get()) | 130 EXPECT_TRUE(parsed_args.get()) |
| 168 << "Could not parse extension function arguments: " << args; | 131 << "Could not parse extension function arguments: " << args; |
| 169 return RunFunction(function, std::move(parsed_args), browser, flags); | 132 return RunFunction(function, std::move(parsed_args), browser, flags); |
| 170 } | 133 } |
| 171 | 134 |
| 172 bool RunFunction(UIThreadExtensionFunction* function, | 135 bool RunFunction(UIThreadExtensionFunction* function, |
| 173 std::unique_ptr<base::ListValue> args, | 136 std::unique_ptr<base::ListValue> args, |
| 174 Browser* browser, | 137 Browser* browser, |
| 175 RunFunctionFlags flags) { | 138 RunFunctionFlags flags) { |
| 176 TestFunctionDispatcherDelegate dispatcher_delegate(browser); | 139 TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
| 177 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher( | 140 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher( |
| 178 new extensions::ExtensionFunctionDispatcher(browser->profile())); | 141 new extensions::ExtensionFunctionDispatcher(browser->profile())); |
| 179 dispatcher->set_delegate(&dispatcher_delegate); | 142 dispatcher->set_delegate(&dispatcher_delegate); |
| 180 // TODO(yoz): The cast is a hack; these flags should be defined in | 143 // TODO(yoz): The cast is a hack; these flags should be defined in |
| 181 // only one place. See crbug.com/394840. | 144 // only one place. See crbug.com/394840. |
| 182 return extensions::api_test_utils::RunFunction( | 145 return extensions::api_test_utils::RunFunction( |
| 183 function, std::move(args), browser->profile(), std::move(dispatcher), | 146 function, std::move(args), browser->profile(), std::move(dispatcher), |
| 184 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags)); | 147 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags)); |
| 185 } | 148 } |
| 186 | 149 |
| 187 } // namespace extension_function_test_utils | 150 } // namespace extension_function_test_utils |
| OLD | NEW |