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 // Run |function| with |args| and return the result. Adds an error to the
61 // current test if |function| returns an error. Takes ownership of
62 // |function|. The caller takes ownership of the result.
63 base::Value
* RunFunctionWithDelegateAndReturnSingleResult(
64 UIThreadExtensionFunction
* function
,
65 const std::string
& args
,
66 content::BrowserContext
* context
,
67 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
);
68 base::Value
* RunFunctionWithDelegateAndReturnSingleResult(
69 UIThreadExtensionFunction
* function
,
70 const std::string
& args
,
71 content::BrowserContext
* context
,
72 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
,
73 RunFunctionFlags flags
);
75 // RunFunctionWithDelegateAndReturnSingleResult, except with a NULL
76 // implementation of the Delegate.
77 base::Value
* RunFunctionAndReturnSingleResult(
78 UIThreadExtensionFunction
* function
,
79 const std::string
& args
,
80 content::BrowserContext
* context
);
81 base::Value
* RunFunctionAndReturnSingleResult(
82 UIThreadExtensionFunction
* function
,
83 const std::string
& args
,
84 content::BrowserContext
* context
,
85 RunFunctionFlags flags
);
87 // Run |function| with |args| and return the resulting error. Adds an error to
88 // the current test if |function| returns a result. Takes ownership of
90 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
91 const std::string
& args
,
92 content::BrowserContext
* context
,
93 RunFunctionFlags flags
);
94 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
95 const std::string
& args
,
96 content::BrowserContext
* context
);
98 // Create and run |function| with |args|. Works with both synchronous and async
99 // functions. Ownership of |function| remains with the caller.
101 // TODO(aa): It would be nice if |args| could be validated against the schema
102 // that |function| expects. That way, we know that we are testing something
103 // close to what the bindings would actually send.
105 // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
106 // we're going to need to frob for all the different extension functions. But
107 // we can refactor when we see what is needed.
108 bool RunFunction(UIThreadExtensionFunction
* function
,
109 const std::string
& args
,
110 content::BrowserContext
* context
);
111 bool RunFunction(UIThreadExtensionFunction
* function
,
112 const std::string
& args
,
113 content::BrowserContext
* context
,
114 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
,
115 RunFunctionFlags flags
);
116 bool RunFunction(UIThreadExtensionFunction
* function
,
117 scoped_ptr
<base::ListValue
> args
,
118 content::BrowserContext
* context
,
119 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher
,
120 RunFunctionFlags flags
);
122 } // namespace api_test_utils
123 } // namespace extensions
125 #endif // EXTENSIONS_BROWSER_API_TEST_UTILS_H_