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.
5 #ifndef EXTENSIONS_BROWSER_API_TEST_UTILS_H_
6 #define EXTENSIONS_BROWSER_API_TEST_UTILS_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "extensions/common/manifest.h"
14 class UIThreadExtensionFunction
;
17 class DictionaryValue
;
26 namespace extensions
{
28 class ExtensionFunctionDispatcher
;
30 // TODO(yoz): crbug.com/394840: Remove duplicate functionality in
31 // chrome/browser/extensions/extension_function_test_utils.h.
33 // TODO(ckehoe): Accept args as scoped_ptr<base::Value>,
34 // and migrate existing users to the new API.
35 namespace api_test_utils
{
37 enum RunFunctionFlags
{ NONE
= 0, INCLUDE_INCOGNITO
= 1 << 0 };
39 // Parse JSON and return as the specified type, or NULL if the JSON is invalid
40 // or not the specified type.
41 base::DictionaryValue
* ParseDictionary(const std::string
& data
);
43 // Get |key| from |val| as the specified type. If |key| does not exist, or is
44 // not of the specified type, adds a failure to the current test and returns
45 // false, 0, empty string, etc.
46 bool GetBoolean(const base::DictionaryValue
* val
, const std::string
& key
);
47 int GetInteger(const base::DictionaryValue
* val
, const std::string
& key
);
48 std::string
GetString(const base::DictionaryValue
* val
, const std::string
& key
);
50 // Creates an extension instance with a specified extension value that can be
51 // attached to an ExtensionFunction before running.
52 scoped_refptr
<extensions::Extension
> CreateExtension(
53 base::DictionaryValue
* test_extension_value
);
55 scoped_refptr
<extensions::Extension
> CreateExtension(
56 extensions::Manifest::Location location
,
57 base::DictionaryValue
* test_extension_value
,
58 const std::string
& id_input
);
60 // Creates an extension instance with a specified location that can be attached
61 // to an ExtensionFunction before running.
62 scoped_refptr
<extensions::Extension
> CreateEmptyExtensionWithLocation(
63 extensions::Manifest::Location location
);
65 // Run |function| with |args| and return the result. Adds an error to the
66 // current test if |function| returns an error. Takes ownership of
67 // |function|. The caller takes ownership of the result.
68 base::Value
* RunFunctionWithDelegateAndReturnSingleResult(
69 UIThreadExtensionFunction
* function
,
70 const std::string
& args
,
71 content::BrowserContext
* context
,
72 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
);
73 base::Value
* RunFunctionWithDelegateAndReturnSingleResult(
74 UIThreadExtensionFunction
* function
,
75 const std::string
& args
,
76 content::BrowserContext
* context
,
77 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
,
78 RunFunctionFlags flags
);
80 // RunFunctionWithDelegateAndReturnSingleResult, except with a NULL
81 // implementation of the Delegate.
82 base::Value
* RunFunctionAndReturnSingleResult(
83 UIThreadExtensionFunction
* function
,
84 const std::string
& args
,
85 content::BrowserContext
* context
);
86 base::Value
* RunFunctionAndReturnSingleResult(
87 UIThreadExtensionFunction
* function
,
88 const std::string
& args
,
89 content::BrowserContext
* context
,
90 RunFunctionFlags flags
);
92 // Run |function| with |args| and return the resulting error. Adds an error to
93 // the current test if |function| returns a result. Takes ownership of
95 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
96 const std::string
& args
,
97 content::BrowserContext
* context
,
98 RunFunctionFlags flags
);
99 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
100 const std::string
& args
,
101 content::BrowserContext
* context
);
103 // Create and run |function| with |args|. Works with both synchronous and async
104 // functions. Ownership of |function| remains with the caller.
106 // TODO(aa): It would be nice if |args| could be validated against the schema
107 // that |function| expects. That way, we know that we are testing something
108 // close to what the bindings would actually send.
110 // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
111 // we're going to need to frob for all the different extension functions. But
112 // we can refactor when we see what is needed.
113 bool RunFunction(UIThreadExtensionFunction
* function
,
114 const std::string
& args
,
115 content::BrowserContext
* context
);
116 bool RunFunction(UIThreadExtensionFunction
* function
,
117 const std::string
& args
,
118 content::BrowserContext
* context
,
119 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
,
120 RunFunctionFlags flags
);
121 bool RunFunction(UIThreadExtensionFunction
* function
,
122 scoped_ptr
<base::ListValue
> args
,
123 content::BrowserContext
* context
,
124 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
,
125 RunFunctionFlags flags
);
127 } // namespace api_test_utils
128 } // namespace extensions
130 #endif // EXTENSIONS_BROWSER_API_TEST_UTILS_H_