[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 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_ |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/memory/ref_counted.h" |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 11 | #include "chrome/common/extensions/extension.h" |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 12 | |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 13 | class AsyncExtensionFunction; |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 14 | class Browser; |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 15 | class UIThreadExtensionFunction; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | class Value; |
| 19 | class DictionaryValue; |
| 20 | class ListValue; |
| 21 | } |
| 22 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 23 | namespace extensions { |
| 24 | class Extension; |
| 25 | } |
| 26 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 27 | namespace extension_function_test_utils { |
| 28 | |
| 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); |
| 34 | |
| 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); |
| 41 | |
| 42 | // If |val| is a dictionary, return it as one, otherwise NULL. |
| 43 | base::DictionaryValue* ToDictionary(base::Value* val); |
| 44 | |
[email protected] | 008ff7fb | 2011-12-19 08:51:17 | [diff] [blame] | 45 | // If |val| is a list, return it as one, otherwise NULL. |
| 46 | base::ListValue* ToList(base::Value* val); |
| 47 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 48 | // Creates an extension instance that can be attached to an ExtensionFunction |
| 49 | // before running it. |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 50 | scoped_refptr<extensions::Extension> CreateEmptyExtension(); |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 51 | |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 52 | // Creates an extension instance with a specified location that can be attached |
| 53 | // to an ExtensionFunction before running. |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 54 | scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation( |
| 55 | extensions::Extension::Location location); |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 56 | |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame^] | 57 | // Creates an extension instance with a specified extension value that can be |
| 58 | // attached to an ExtensionFunction before running. |
| 59 | scoped_refptr<extensions::Extension> CreateExtension( |
| 60 | base::DictionaryValue* test_extension_value); |
| 61 | |
| 62 | scoped_refptr<extensions::Extension> CreateExtension( |
| 63 | extensions::Extension::Location location, |
| 64 | base::DictionaryValue* test_extension_value); |
| 65 | |
| 66 | // Returns true if |val| contains privacy information, e.g. url, |
| 67 | // title, and faviconUrl. |
| 68 | bool HasPrivacySensitiveFields(base::DictionaryValue* val); |
| 69 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 70 | enum RunFunctionFlags { |
| 71 | NONE = 0, |
| 72 | INCLUDE_INCOGNITO = 1 << 0 |
| 73 | }; |
| 74 | |
| 75 | // Run |function| with |args| and return the resulting error. Adds an error to |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 76 | // the current test if |function| returns a result. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 77 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 78 | const std::string& args, |
| 79 | Browser* browser, |
| 80 | RunFunctionFlags flags); |
| 81 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 82 | const std::string& args, |
| 83 | Browser* browser); |
| 84 | |
| 85 | // Run |function| with |args| and return the result. Adds an error to the |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 86 | // current test if |function| returns an error. The caller takes ownership of |
| 87 | // the result. |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 88 | base::Value* RunFunctionAndReturnSingleResult( |
| 89 | UIThreadExtensionFunction* function, |
| 90 | const std::string& args, |
| 91 | Browser* browser, |
| 92 | RunFunctionFlags flags); |
| 93 | base::Value* RunFunctionAndReturnSingleResult( |
| 94 | UIThreadExtensionFunction* function, |
| 95 | const std::string& args, |
| 96 | Browser* browser); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 97 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 98 | // Create and run |function| with |args|. Works with both synchronous and async |
| 99 | // functions. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 100 | // |
| 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. |
| 104 | // |
| 105 | // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 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, |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 109 | const std::string& args, |
| 110 | Browser* browser, |
| 111 | RunFunctionFlags flags); |
| 112 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 113 | } // namespace extension_function_test_utils |
| 114 | |
| 115 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_ |