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