[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] | d42c111 | 2013-08-22 19:36:32 | [diff] [blame] | 11 | #include "extensions/common/manifest.h" |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 12 | |
| 13 | class Browser; |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 14 | class UIThreadExtensionFunction; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | class Value; |
| 18 | class DictionaryValue; |
| 19 | class ListValue; |
| 20 | } |
| 21 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 22 | namespace extensions { |
| 23 | class Extension; |
| 24 | } |
| 25 | |
[email protected] | fc672e1 | 2014-08-16 08:16:15 | [diff] [blame] | 26 | // TODO(ckehoe): Accept args as scoped_ptr<base::Value>, |
| 27 | // and migrate existing users to the new API. |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame^] | 28 | // This file is DEPRECATED. New tests should use the versions in |
| 29 | // extensions/browser/api_test_utils.h. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 30 | namespace extension_function_test_utils { |
| 31 | |
| 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); |
| 37 | |
| 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(base::DictionaryValue* val, const std::string& key); |
| 42 | int GetInteger(base::DictionaryValue* val, const std::string& key); |
| 43 | std::string GetString(base::DictionaryValue* val, const std::string& key); |
| 44 | |
| 45 | // If |val| is a dictionary, return it as one, otherwise NULL. |
| 46 | base::DictionaryValue* ToDictionary(base::Value* val); |
| 47 | |
[email protected] | 008ff7fb | 2011-12-19 08:51:17 | [diff] [blame] | 48 | // If |val| is a list, return it as one, otherwise NULL. |
| 49 | base::ListValue* ToList(base::Value* val); |
| 50 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 51 | // Creates an extension instance that can be attached to an ExtensionFunction |
| 52 | // before running it. |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 53 | scoped_refptr<extensions::Extension> CreateEmptyExtension(); |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 54 | |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 55 | // Creates an extension instance with a specified location that can be attached |
| 56 | // to an ExtensionFunction before running. |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 57 | scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 58 | extensions::Manifest::Location location); |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 59 | |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 60 | // Creates an empty extension with a variable ID, for tests that require |
| 61 | // multiple extensions side-by-side having distinct IDs. If not empty, then |
| 62 | // id_input is passed directly to Extension::CreateId() and thus has the same |
| 63 | // behavior as that method. If id_input is empty, then Extension::Create() |
| 64 | // receives an empty explicit ID and generates an appropriate ID for a blank |
| 65 | // extension. |
| 66 | scoped_refptr<extensions::Extension> CreateEmptyExtension( |
| 67 | const std::string& id_input); |
| 68 | |
| 69 | scoped_refptr<extensions::Extension> CreateExtension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 70 | extensions::Manifest::Location location, |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 71 | base::DictionaryValue* test_extension_value, |
| 72 | const std::string& id_input); |
| 73 | |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 74 | // Creates an extension instance with a specified extension value that can be |
| 75 | // attached to an ExtensionFunction before running. |
| 76 | scoped_refptr<extensions::Extension> CreateExtension( |
| 77 | base::DictionaryValue* test_extension_value); |
| 78 | |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 79 | // Returns true if |val| contains privacy information, e.g. url, |
| 80 | // title, and faviconUrl. |
| 81 | bool HasPrivacySensitiveFields(base::DictionaryValue* val); |
| 82 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 83 | enum RunFunctionFlags { |
| 84 | NONE = 0, |
| 85 | INCLUDE_INCOGNITO = 1 << 0 |
| 86 | }; |
| 87 | |
| 88 | // Run |function| with |args| and return the resulting error. Adds an error to |
[email protected] | 4f9c79ec | 2013-03-21 04:41:37 | [diff] [blame] | 89 | // the current test if |function| returns a result. Takes ownership of |
| 90 | // |function|. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 91 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 92 | const std::string& args, |
| 93 | Browser* browser, |
| 94 | RunFunctionFlags flags); |
| 95 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 96 | const std::string& args, |
| 97 | Browser* browser); |
| 98 | |
| 99 | // Run |function| with |args| and return the result. Adds an error to the |
[email protected] | 4f9c79ec | 2013-03-21 04:41:37 | [diff] [blame] | 100 | // current test if |function| returns an error. Takes ownership of |
| 101 | // |function|. The caller takes ownership of the result. |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 102 | base::Value* RunFunctionAndReturnSingleResult( |
| 103 | UIThreadExtensionFunction* function, |
| 104 | const std::string& args, |
| 105 | Browser* browser, |
| 106 | RunFunctionFlags flags); |
| 107 | base::Value* RunFunctionAndReturnSingleResult( |
| 108 | UIThreadExtensionFunction* function, |
| 109 | const std::string& args, |
| 110 | Browser* browser); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 111 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 112 | // Create and run |function| with |args|. Works with both synchronous and async |
[email protected] | 4f9c79ec | 2013-03-21 04:41:37 | [diff] [blame] | 113 | // functions. Ownership of |function| remains with the caller. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 114 | // |
| 115 | // TODO(aa): It would be nice if |args| could be validated against the schema |
| 116 | // that |function| expects. That way, we know that we are testing something |
| 117 | // close to what the bindings would actually send. |
| 118 | // |
| 119 | // 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] | 120 | // we're going to need to frob for all the different extension functions. But |
| 121 | // we can refactor when we see what is needed. |
| 122 | bool RunFunction(UIThreadExtensionFunction* function, |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 123 | const std::string& args, |
| 124 | Browser* browser, |
| 125 | RunFunctionFlags flags); |
[email protected] | fc672e1 | 2014-08-16 08:16:15 | [diff] [blame] | 126 | bool RunFunction(UIThreadExtensionFunction* function, |
| 127 | scoped_ptr<base::ListValue> args, |
| 128 | Browser* browser, |
| 129 | RunFunctionFlags flags); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 130 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 131 | } // namespace extension_function_test_utils |
| 132 | |
| 133 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_ |