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
12class UIThreadExtensionFunction;
13
14namespace base {
15class ListValue;
16class Value;
17}
18
19namespace content {
20class BrowserContext;
21}
22
23namespace extensions {
24class 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.
31namespace api_test_utils {
32
33enum 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.
38base::Value* RunFunctionWithDelegateAndReturnSingleResult(
39    UIThreadExtensionFunction* function,
40    const std::string& args,
41    content::BrowserContext* context,
42    scoped_ptr<ExtensionFunctionDispatcher> dispatcher);
43base::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.
52base::Value* RunFunctionAndReturnSingleResult(
53    UIThreadExtensionFunction* function,
54    const std::string& args,
55    content::BrowserContext* context);
56base::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|.
65std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
66                                      const std::string& args,
67                                      content::BrowserContext* context,
68                                      RunFunctionFlags flags);
69std::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.
83bool RunFunction(UIThreadExtensionFunction* function,
84                 const std::string& args,
85                 content::BrowserContext* context);
86bool RunFunction(UIThreadExtensionFunction* function,
87                 const std::string& args,
88                 content::BrowserContext* context,
89                 scoped_ptr<ExtensionFunctionDispatcher> dispatcher,
90                 RunFunctionFlags flags);
91bool 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