Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_function_test_utils.h
blob7bd111c7912e2f156523d3b41505941230edce1c
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_
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "extensions/common/manifest.h"
13 class AsyncExtensionFunction;
14 class Browser;
15 class UIThreadExtensionFunction;
17 namespace base {
18 class Value;
19 class DictionaryValue;
20 class ListValue;
23 namespace extensions {
24 class Extension;
27 namespace extension_function_test_utils {
29 // Parse JSON and return as the specified type, or NULL if the JSON is invalid
30 // or not the specified type.
31 base::Value* ParseJSON(const std::string& data);
32 base::ListValue* ParseList(const std::string& data);
33 base::DictionaryValue* ParseDictionary(const std::string& data);
35 // Get |key| from |val| as the specified type. If |key| does not exist, or is
36 // not of the specified type, adds a failure to the current test and returns
37 // false, 0, empty string, etc.
38 bool GetBoolean(base::DictionaryValue* val, const std::string& key);
39 int GetInteger(base::DictionaryValue* val, const std::string& key);
40 std::string GetString(base::DictionaryValue* val, const std::string& key);
42 // If |val| is a dictionary, return it as one, otherwise NULL.
43 base::DictionaryValue* ToDictionary(base::Value* val);
45 // If |val| is a list, return it as one, otherwise NULL.
46 base::ListValue* ToList(base::Value* val);
48 // Creates an extension instance that can be attached to an ExtensionFunction
49 // before running it.
50 scoped_refptr<extensions::Extension> CreateEmptyExtension();
52 // Creates an extension instance with a specified location that can be attached
53 // to an ExtensionFunction before running.
54 scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation(
55 extensions::Manifest::Location location);
57 // Creates an empty extension with a variable ID, for tests that require
58 // multiple extensions side-by-side having distinct IDs. If not empty, then
59 // id_input is passed directly to Extension::CreateId() and thus has the same
60 // behavior as that method. If id_input is empty, then Extension::Create()
61 // receives an empty explicit ID and generates an appropriate ID for a blank
62 // extension.
63 scoped_refptr<extensions::Extension> CreateEmptyExtension(
64 const std::string& id_input);
66 scoped_refptr<extensions::Extension> CreateExtension(
67 extensions::Manifest::Location location,
68 base::DictionaryValue* test_extension_value,
69 const std::string& id_input);
71 // Creates an extension instance with a specified extension value that can be
72 // attached to an ExtensionFunction before running.
73 scoped_refptr<extensions::Extension> CreateExtension(
74 base::DictionaryValue* test_extension_value);
76 // Returns true if |val| contains privacy information, e.g. url,
77 // title, and faviconUrl.
78 bool HasPrivacySensitiveFields(base::DictionaryValue* val);
80 enum RunFunctionFlags {
81 NONE = 0,
82 INCLUDE_INCOGNITO = 1 << 0
85 // Run |function| with |args| and return the resulting error. Adds an error to
86 // the current test if |function| returns a result. Takes ownership of
87 // |function|.
88 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
89 const std::string& args,
90 Browser* browser,
91 RunFunctionFlags flags);
92 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
93 const std::string& args,
94 Browser* browser);
96 // Run |function| with |args| and return the result. Adds an error to the
97 // current test if |function| returns an error. Takes ownership of
98 // |function|. The caller takes ownership of the result.
99 base::Value* RunFunctionAndReturnSingleResult(
100 UIThreadExtensionFunction* function,
101 const std::string& args,
102 Browser* browser,
103 RunFunctionFlags flags);
104 base::Value* RunFunctionAndReturnSingleResult(
105 UIThreadExtensionFunction* function,
106 const std::string& args,
107 Browser* browser);
109 // Create and run |function| with |args|. Works with both synchronous and async
110 // functions. Ownership of |function| remains with the caller.
112 // TODO(aa): It would be nice if |args| could be validated against the schema
113 // that |function| expects. That way, we know that we are testing something
114 // close to what the bindings would actually send.
116 // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
117 // we're going to need to frob for all the different extension functions. But
118 // we can refactor when we see what is needed.
119 bool RunFunction(UIThreadExtensionFunction* function,
120 const std::string& args,
121 Browser* browser,
122 RunFunctionFlags flags);
124 } // namespace extension_function_test_utils
126 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_