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 3 // found in the LICENSE file. 4 5 #ifndef EXTENSIONS_BROWSER_API_TEST_UTILS_H_ 6 #define EXTENSIONS_BROWSER_API_TEST_UTILS_H_ 7 8 #include <string> 9 10 #include "base/memory/scoped_ptr.h" 11 12 class UIThreadExtensionFunction; 13 14 namespace base { 15 class ListValue; 16 class Value; 17 } 18 19 namespace content { 20 class BrowserContext; 21 } 22 23 namespace extensions { 24 class ExtensionFunctionDispatcher; 25 26 // TODO(yoz): crbug.com/394840: Remove duplicate functionality in 27 // chrome/browser/extensions/extension_function_test_utils.h. 28 // 29 // TODO(ckehoe): Accept args as scoped_ptr<base::Value>, 30 // and migrate existing users to the new API. 31 namespace api_test_utils { 32 33 enum RunFunctionFlags { NONE = 0, INCLUDE_INCOGNITO = 1 << 0 }; 34 35 // Run |function| with |args| and return the result. Adds an error to the 36 // current test if |function| returns an error. Takes ownership of 37 // |function|. The caller takes ownership of the result. 38 base::Value* RunFunctionWithDelegateAndReturnSingleResult( 39 UIThreadExtensionFunction* function, 40 const std::string& args, 41 content::BrowserContext* context, 42 scoped_ptr<ExtensionFunctionDispatcher> dispatcher); 43 base::Value* RunFunctionWithDelegateAndReturnSingleResult( 44 UIThreadExtensionFunction* function, 45 const std::string& args, 46 content::BrowserContext* context, 47 scoped_ptr<ExtensionFunctionDispatcher> dispatcher, 48 RunFunctionFlags flags); 49 50 // RunFunctionWithDelegateAndReturnSingleResult, except with a NULL 51 // implementation of the Delegate. 52 base::Value* RunFunctionAndReturnSingleResult( 53 UIThreadExtensionFunction* function, 54 const std::string& args, 55 content::BrowserContext* context); 56 base::Value* RunFunctionAndReturnSingleResult( 57 UIThreadExtensionFunction* function, 58 const std::string& args, 59 content::BrowserContext* context, 60 RunFunctionFlags flags); 61 62 // Run |function| with |args| and return the resulting error. Adds an error to 63 // the current test if |function| returns a result. Takes ownership of 64 // |function|. 65 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 66 const std::string& args, 67 content::BrowserContext* context, 68 RunFunctionFlags flags); 69 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 70 const std::string& args, 71 content::BrowserContext* context); 72 73 // Create and run |function| with |args|. Works with both synchronous and async 74 // functions. Ownership of |function| remains with the caller. 75 // 76 // TODO(aa): It would be nice if |args| could be validated against the schema 77 // that |function| expects. That way, we know that we are testing something 78 // close to what the bindings would actually send. 79 // 80 // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs 81 // we're going to need to frob for all the different extension functions. But 82 // we can refactor when we see what is needed. 83 bool RunFunction(UIThreadExtensionFunction* function, 84 const std::string& args, 85 content::BrowserContext* context); 86 bool RunFunction(UIThreadExtensionFunction* function, 87 const std::string& args, 88 content::BrowserContext* context, 89 scoped_ptr<ExtensionFunctionDispatcher> dispatcher, 90 RunFunctionFlags flags); 91 bool RunFunction(UIThreadExtensionFunction* function, 92 scoped_ptr<base::ListValue> args, 93 content::BrowserContext* context, 94 scoped_ptr<ExtensionFunctionDispatcher> dispatcher, 95 RunFunctionFlags flags); 96 97 } // namespace function_test_utils 98 } // namespace extensions 99 100 #endif // EXTENSIONS_BROWSER_API_TEST_UTILS_H_ 101