| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api_test_utils.h" | 5 #include "extensions/browser/api_test_utils.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "components/crx_file/id_util.h" | 12 #include "components/crx_file/id_util.h" |
| 13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 15 #include "extensions/browser/extension_function.h" | 15 #include "extensions/browser/extension_function.h" |
| 16 #include "extensions/browser/extension_function_dispatcher.h" | 16 #include "extensions/browser/extension_function_dispatcher.h" |
| 17 #include "extensions/common/extension_builder.h" | 17 #include "extensions/common/extension_builder.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using extensions::ExtensionFunctionDispatcher; | 20 using extensions::ExtensionFunctionDispatcher; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 scoped_ptr<base::Value> ParseJSON(const std::string& data) { | 24 std::unique_ptr<base::Value> ParseJSON(const std::string& data) { |
| 25 return base::JSONReader::Read(data); | 25 return base::JSONReader::Read(data); |
| 26 } | 26 } |
| 27 | 27 |
| 28 scoped_ptr<base::ListValue> ParseList(const std::string& data) { | 28 std::unique_ptr<base::ListValue> ParseList(const std::string& data) { |
| 29 return base::ListValue::From(ParseJSON(data)); | 29 return base::ListValue::From(ParseJSON(data)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // This helps us be able to wait until an UIThreadExtensionFunction calls | 32 // This helps us be able to wait until an UIThreadExtensionFunction calls |
| 33 // SendResponse. | 33 // SendResponse. |
| 34 class SendResponseDelegate | 34 class SendResponseDelegate |
| 35 : public UIThreadExtensionFunction::DelegateForTests { | 35 : public UIThreadExtensionFunction::DelegateForTests { |
| 36 public: | 36 public: |
| 37 SendResponseDelegate() : should_post_quit_(false) {} | 37 SendResponseDelegate() : should_post_quit_(false) {} |
| 38 | 38 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 55 ASSERT_FALSE(bad_message); | 55 ASSERT_FALSE(bad_message); |
| 56 ASSERT_FALSE(HasResponse()); | 56 ASSERT_FALSE(HasResponse()); |
| 57 response_.reset(new bool); | 57 response_.reset(new bool); |
| 58 *response_ = success; | 58 *response_ = success; |
| 59 if (should_post_quit_) { | 59 if (should_post_quit_) { |
| 60 base::MessageLoopForUI::current()->QuitWhenIdle(); | 60 base::MessageLoopForUI::current()->QuitWhenIdle(); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 scoped_ptr<bool> response_; | 65 std::unique_ptr<bool> response_; |
| 66 bool should_post_quit_; | 66 bool should_post_quit_; |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 } // namespace | 69 } // namespace |
| 70 | 70 |
| 71 namespace extensions { | 71 namespace extensions { |
| 72 | 72 |
| 73 namespace api_test_utils { | 73 namespace api_test_utils { |
| 74 | 74 |
| 75 scoped_ptr<base::DictionaryValue> ParseDictionary(const std::string& data) { | 75 std::unique_ptr<base::DictionaryValue> ParseDictionary( |
| 76 const std::string& data) { |
| 76 return base::DictionaryValue::From(ParseJSON(data)); | 77 return base::DictionaryValue::From(ParseJSON(data)); |
| 77 } | 78 } |
| 78 | 79 |
| 79 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) { | 80 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) { |
| 80 bool result = false; | 81 bool result = false; |
| 81 if (!val->GetBoolean(key, &result)) | 82 if (!val->GetBoolean(key, &result)) |
| 82 ADD_FAILURE() << key << " does not exist or is not a boolean."; | 83 ADD_FAILURE() << key << " does not exist or is not a boolean."; |
| 83 return result; | 84 return result; |
| 84 } | 85 } |
| 85 | 86 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 115 } | 116 } |
| 116 | 117 |
| 117 scoped_refptr<Extension> CreateExtension( | 118 scoped_refptr<Extension> CreateExtension( |
| 118 base::DictionaryValue* test_extension_value) { | 119 base::DictionaryValue* test_extension_value) { |
| 119 return CreateExtension(Manifest::INTERNAL, test_extension_value, | 120 return CreateExtension(Manifest::INTERNAL, test_extension_value, |
| 120 std::string()); | 121 std::string()); |
| 121 } | 122 } |
| 122 | 123 |
| 123 scoped_refptr<Extension> CreateEmptyExtensionWithLocation( | 124 scoped_refptr<Extension> CreateEmptyExtensionWithLocation( |
| 124 Manifest::Location location) { | 125 Manifest::Location location) { |
| 125 scoped_ptr<base::DictionaryValue> test_extension_value = | 126 std::unique_ptr<base::DictionaryValue> test_extension_value = |
| 126 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"); | 127 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"); |
| 127 return CreateExtension(location, test_extension_value.get(), std::string()); | 128 return CreateExtension(location, test_extension_value.get(), std::string()); |
| 128 } | 129 } |
| 129 | 130 |
| 130 base::Value* RunFunctionWithDelegateAndReturnSingleResult( | 131 base::Value* RunFunctionWithDelegateAndReturnSingleResult( |
| 131 UIThreadExtensionFunction* function, | 132 UIThreadExtensionFunction* function, |
| 132 const std::string& args, | 133 const std::string& args, |
| 133 content::BrowserContext* context, | 134 content::BrowserContext* context, |
| 134 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { | 135 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
| 135 return RunFunctionWithDelegateAndReturnSingleResult( | 136 return RunFunctionWithDelegateAndReturnSingleResult( |
| 136 function, args, context, std::move(dispatcher), NONE); | 137 function, args, context, std::move(dispatcher), NONE); |
| 137 } | 138 } |
| 138 | 139 |
| 139 base::Value* RunFunctionWithDelegateAndReturnSingleResult( | 140 base::Value* RunFunctionWithDelegateAndReturnSingleResult( |
| 140 UIThreadExtensionFunction* function, | 141 UIThreadExtensionFunction* function, |
| 141 const std::string& args, | 142 const std::string& args, |
| 142 content::BrowserContext* context, | 143 content::BrowserContext* context, |
| 143 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 144 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 144 RunFunctionFlags flags) { | 145 RunFunctionFlags flags) { |
| 145 scoped_refptr<ExtensionFunction> function_owner(function); | 146 scoped_refptr<ExtensionFunction> function_owner(function); |
| 146 // Without a callback the function will not generate a result. | 147 // Without a callback the function will not generate a result. |
| 147 function->set_has_callback(true); | 148 function->set_has_callback(true); |
| 148 RunFunction(function, args, context, std::move(dispatcher), flags); | 149 RunFunction(function, args, context, std::move(dispatcher), flags); |
| 149 EXPECT_TRUE(function->GetError().empty()) | 150 EXPECT_TRUE(function->GetError().empty()) |
| 150 << "Unexpected error: " << function->GetError(); | 151 << "Unexpected error: " << function->GetError(); |
| 151 const base::Value* single_result = NULL; | 152 const base::Value* single_result = NULL; |
| 152 if (function->GetResultList() != NULL && | 153 if (function->GetResultList() != NULL && |
| 153 function->GetResultList()->Get(0, &single_result)) { | 154 function->GetResultList()->Get(0, &single_result)) { |
| 154 return single_result->DeepCopy(); | 155 return single_result->DeepCopy(); |
| 155 } | 156 } |
| 156 return NULL; | 157 return NULL; |
| 157 } | 158 } |
| 158 | 159 |
| 159 base::Value* RunFunctionAndReturnSingleResult( | 160 base::Value* RunFunctionAndReturnSingleResult( |
| 160 UIThreadExtensionFunction* function, | 161 UIThreadExtensionFunction* function, |
| 161 const std::string& args, | 162 const std::string& args, |
| 162 content::BrowserContext* context) { | 163 content::BrowserContext* context) { |
| 163 return RunFunctionAndReturnSingleResult(function, args, context, NONE); | 164 return RunFunctionAndReturnSingleResult(function, args, context, NONE); |
| 164 } | 165 } |
| 165 | 166 |
| 166 base::Value* RunFunctionAndReturnSingleResult( | 167 base::Value* RunFunctionAndReturnSingleResult( |
| 167 UIThreadExtensionFunction* function, | 168 UIThreadExtensionFunction* function, |
| 168 const std::string& args, | 169 const std::string& args, |
| 169 content::BrowserContext* context, | 170 content::BrowserContext* context, |
| 170 RunFunctionFlags flags) { | 171 RunFunctionFlags flags) { |
| 171 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | 172 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 172 new ExtensionFunctionDispatcher(context)); | 173 new ExtensionFunctionDispatcher(context)); |
| 173 | 174 |
| 174 return RunFunctionWithDelegateAndReturnSingleResult( | 175 return RunFunctionWithDelegateAndReturnSingleResult( |
| 175 function, args, context, std::move(dispatcher), flags); | 176 function, args, context, std::move(dispatcher), flags); |
| 176 } | 177 } |
| 177 | 178 |
| 178 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 179 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 179 const std::string& args, | 180 const std::string& args, |
| 180 content::BrowserContext* context) { | 181 content::BrowserContext* context) { |
| 181 return RunFunctionAndReturnError(function, args, context, NONE); | 182 return RunFunctionAndReturnError(function, args, context, NONE); |
| 182 } | 183 } |
| 183 | 184 |
| 184 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 185 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 185 const std::string& args, | 186 const std::string& args, |
| 186 content::BrowserContext* context, | 187 content::BrowserContext* context, |
| 187 RunFunctionFlags flags) { | 188 RunFunctionFlags flags) { |
| 188 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | 189 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 189 new ExtensionFunctionDispatcher(context)); | 190 new ExtensionFunctionDispatcher(context)); |
| 190 scoped_refptr<ExtensionFunction> function_owner(function); | 191 scoped_refptr<ExtensionFunction> function_owner(function); |
| 191 // Without a callback the function will not generate a result. | 192 // Without a callback the function will not generate a result. |
| 192 function->set_has_callback(true); | 193 function->set_has_callback(true); |
| 193 RunFunction(function, args, context, std::move(dispatcher), flags); | 194 RunFunction(function, args, context, std::move(dispatcher), flags); |
| 194 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; | 195 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; |
| 195 return function->GetError(); | 196 return function->GetError(); |
| 196 } | 197 } |
| 197 | 198 |
| 198 bool RunFunction(UIThreadExtensionFunction* function, | 199 bool RunFunction(UIThreadExtensionFunction* function, |
| 199 const std::string& args, | 200 const std::string& args, |
| 200 content::BrowserContext* context) { | 201 content::BrowserContext* context) { |
| 201 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | 202 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 202 new ExtensionFunctionDispatcher(context)); | 203 new ExtensionFunctionDispatcher(context)); |
| 203 return RunFunction(function, args, context, std::move(dispatcher), NONE); | 204 return RunFunction(function, args, context, std::move(dispatcher), NONE); |
| 204 } | 205 } |
| 205 | 206 |
| 206 bool RunFunction(UIThreadExtensionFunction* function, | 207 bool RunFunction( |
| 207 const std::string& args, | 208 UIThreadExtensionFunction* function, |
| 208 content::BrowserContext* context, | 209 const std::string& args, |
| 209 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 210 content::BrowserContext* context, |
| 210 RunFunctionFlags flags) { | 211 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 211 scoped_ptr<base::ListValue> parsed_args = ParseList(args); | 212 RunFunctionFlags flags) { |
| 213 std::unique_ptr<base::ListValue> parsed_args = ParseList(args); |
| 212 EXPECT_TRUE(parsed_args.get()) | 214 EXPECT_TRUE(parsed_args.get()) |
| 213 << "Could not parse extension function arguments: " << args; | 215 << "Could not parse extension function arguments: " << args; |
| 214 return RunFunction(function, std::move(parsed_args), context, | 216 return RunFunction(function, std::move(parsed_args), context, |
| 215 std::move(dispatcher), flags); | 217 std::move(dispatcher), flags); |
| 216 } | 218 } |
| 217 | 219 |
| 218 bool RunFunction(UIThreadExtensionFunction* function, | 220 bool RunFunction( |
| 219 scoped_ptr<base::ListValue> args, | 221 UIThreadExtensionFunction* function, |
| 220 content::BrowserContext* context, | 222 std::unique_ptr<base::ListValue> args, |
| 221 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 223 content::BrowserContext* context, |
| 222 RunFunctionFlags flags) { | 224 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 225 RunFunctionFlags flags) { |
| 223 SendResponseDelegate response_delegate; | 226 SendResponseDelegate response_delegate; |
| 224 function->set_test_delegate(&response_delegate); | 227 function->set_test_delegate(&response_delegate); |
| 225 function->SetArgs(args.get()); | 228 function->SetArgs(args.get()); |
| 226 | 229 |
| 227 CHECK(dispatcher); | 230 CHECK(dispatcher); |
| 228 function->set_dispatcher(dispatcher->AsWeakPtr()); | 231 function->set_dispatcher(dispatcher->AsWeakPtr()); |
| 229 | 232 |
| 230 function->set_browser_context(context); | 233 function->set_browser_context(context); |
| 231 function->set_include_incognito(flags & INCLUDE_INCOGNITO); | 234 function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| 232 function->Run()->Execute(); | 235 function->Run()->Execute(); |
| 233 | 236 |
| 234 // If the RunAsync of |function| didn't already call SendResponse, run the | 237 // If the RunAsync of |function| didn't already call SendResponse, run the |
| 235 // message loop until they do. | 238 // message loop until they do. |
| 236 if (!response_delegate.HasResponse()) { | 239 if (!response_delegate.HasResponse()) { |
| 237 response_delegate.set_should_post_quit(true); | 240 response_delegate.set_should_post_quit(true); |
| 238 content::RunMessageLoop(); | 241 content::RunMessageLoop(); |
| 239 } | 242 } |
| 240 | 243 |
| 241 EXPECT_TRUE(response_delegate.HasResponse()); | 244 EXPECT_TRUE(response_delegate.HasResponse()); |
| 242 return response_delegate.GetResponse(); | 245 return response_delegate.GetResponse(); |
| 243 } | 246 } |
| 244 | 247 |
| 245 } // namespace api_test_utils | 248 } // namespace api_test_utils |
| 246 } // namespace extensions | 249 } // namespace extensions |
| OLD | NEW |