1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_
10 #include "base/memory/ref_counted.h"
11 #include "extensions/common/manifest.h"
14 class UIThreadExtensionFunction
;
18 class DictionaryValue
;
22 namespace extensions
{
26 // TODO(ckehoe): Accept args as scoped_ptr<base::Value>,
27 // and migrate existing users to the new API.
28 // This file is DEPRECATED. New tests should use the versions in
29 // extensions/browser/api_test_utils.h.
30 namespace extension_function_test_utils
{
32 // Parse JSON and return as the specified type, or NULL if the JSON is invalid
33 // or not the specified type.
34 base::Value
* ParseJSON(const std::string
& data
);
35 base::ListValue
* ParseList(const std::string
& data
);
36 base::DictionaryValue
* ParseDictionary(const std::string
& data
);
38 // Get |key| from |val| as the specified type. If |key| does not exist, or is
39 // not of the specified type, adds a failure to the current test and returns
40 // false, 0, empty string, etc.
41 bool GetBoolean(const base::DictionaryValue
* val
, const std::string
& key
);
42 int GetInteger(const base::DictionaryValue
* val
, const std::string
& key
);
43 std::string
GetString(const base::DictionaryValue
* val
, const std::string
& key
);
45 // If |val| is a dictionary, return it as one, otherwise NULL.
46 base::DictionaryValue
* ToDictionary(base::Value
* val
);
48 // If |val| is a list, return it as one, otherwise NULL.
49 base::ListValue
* ToList(base::Value
* val
);
51 // Creates an extension instance with a specified location that can be attached
52 // to an ExtensionFunction before running.
53 scoped_refptr
<extensions::Extension
> CreateEmptyExtensionWithLocation(
54 extensions::Manifest::Location location
);
56 scoped_refptr
<extensions::Extension
> CreateExtension(
57 extensions::Manifest::Location location
,
58 base::DictionaryValue
* test_extension_value
,
59 const std::string
& id_input
);
61 // Creates an extension instance with a specified extension value that can be
62 // attached to an ExtensionFunction before running.
63 scoped_refptr
<extensions::Extension
> CreateExtension(
64 base::DictionaryValue
* test_extension_value
);
66 // Returns true if |val| contains privacy information, e.g. url,
67 // title, and faviconUrl.
68 bool HasPrivacySensitiveFields(base::DictionaryValue
* val
);
70 enum RunFunctionFlags
{
72 INCLUDE_INCOGNITO
= 1 << 0
75 // Run |function| with |args| and return the resulting error. Adds an error to
76 // the current test if |function| returns a result. Takes ownership of
78 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
79 const std::string
& args
,
81 RunFunctionFlags flags
);
82 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
83 const std::string
& args
,
86 // Run |function| with |args| and return the result. Adds an error to the
87 // current test if |function| returns an error. Takes ownership of
88 // |function|. The caller takes ownership of the result.
89 base::Value
* RunFunctionAndReturnSingleResult(
90 UIThreadExtensionFunction
* function
,
91 const std::string
& args
,
93 RunFunctionFlags flags
);
94 base::Value
* RunFunctionAndReturnSingleResult(
95 UIThreadExtensionFunction
* function
,
96 const std::string
& args
,
99 // Create and run |function| with |args|. Works with both synchronous and async
100 // functions. Ownership of |function| remains with the caller.
102 // TODO(aa): It would be nice if |args| could be validated against the schema
103 // that |function| expects. That way, we know that we are testing something
104 // close to what the bindings would actually send.
106 // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
107 // we're going to need to frob for all the different extension functions. But
108 // we can refactor when we see what is needed.
109 bool RunFunction(UIThreadExtensionFunction
* function
,
110 const std::string
& args
,
112 RunFunctionFlags flags
);
113 bool RunFunction(UIThreadExtensionFunction
* function
,
114 scoped_ptr
<base::ListValue
> args
,
116 RunFunctionFlags flags
);
118 } // namespace extension_function_test_utils
120 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_