[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_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/memory/ref_counted.h" |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame^] | 12 | #include "chrome/common/extensions/extension.h" |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 13 | |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 14 | class AsyncExtensionFunction; |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 15 | class Browser; |
| 16 | class Extension; |
| 17 | class UIThreadExtensionFunction; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | class Value; |
| 21 | class DictionaryValue; |
| 22 | class ListValue; |
| 23 | } |
| 24 | |
| 25 | namespace extension_function_test_utils { |
| 26 | |
| 27 | // Parse JSON and return as the specified type, or NULL if the JSON is invalid |
| 28 | // or not the specified type. |
| 29 | base::Value* ParseJSON(const std::string& data); |
| 30 | base::ListValue* ParseList(const std::string& data); |
| 31 | base::DictionaryValue* ParseDictionary(const std::string& data); |
| 32 | |
| 33 | // Get |key| from |val| as the specified type. If |key| does not exist, or is |
| 34 | // not of the specified type, adds a failure to the current test and returns |
| 35 | // false, 0, empty string, etc. |
| 36 | bool GetBoolean(base::DictionaryValue* val, const std::string& key); |
| 37 | int GetInteger(base::DictionaryValue* val, const std::string& key); |
| 38 | std::string GetString(base::DictionaryValue* val, const std::string& key); |
| 39 | |
| 40 | // If |val| is a dictionary, return it as one, otherwise NULL. |
| 41 | base::DictionaryValue* ToDictionary(base::Value* val); |
| 42 | |
[email protected] | 008ff7fb | 2011-12-19 08:51:17 | [diff] [blame] | 43 | // If |val| is a list, return it as one, otherwise NULL. |
| 44 | base::ListValue* ToList(base::Value* val); |
| 45 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 46 | // Creates an extension instance that can be attached to an ExtensionFunction |
| 47 | // before running it. |
| 48 | scoped_refptr<Extension> CreateEmptyExtension(); |
| 49 | |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame^] | 50 | // Creates an extension instance with a specified location that can be attached |
| 51 | // to an ExtensionFunction before running. |
| 52 | scoped_refptr<Extension> CreateEmptyExtensionWithLocation( |
| 53 | Extension::Location location); |
| 54 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 55 | enum RunFunctionFlags { |
| 56 | NONE = 0, |
| 57 | INCLUDE_INCOGNITO = 1 << 0 |
| 58 | }; |
| 59 | |
| 60 | // Run |function| with |args| and return the resulting error. Adds an error to |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 61 | // the current test if |function| returns a result. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 62 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 63 | const std::string& args, |
| 64 | Browser* browser, |
| 65 | RunFunctionFlags flags); |
| 66 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 67 | const std::string& args, |
| 68 | Browser* browser); |
| 69 | |
| 70 | // Run |function| with |args| and return the result. Adds an error to the |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 71 | // current test if |function| returns an error. The caller takes ownership of |
| 72 | // the result. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 73 | base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
| 74 | const std::string& args, |
| 75 | Browser* browser, |
| 76 | RunFunctionFlags flags); |
| 77 | base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
| 78 | const std::string& args, |
| 79 | Browser* browser); |
| 80 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 81 | // Create and run |function| with |args|. Works with both synchronous and async |
| 82 | // functions. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 83 | // |
| 84 | // TODO(aa): It would be nice if |args| could be validated against the schema |
| 85 | // that |function| expects. That way, we know that we are testing something |
| 86 | // close to what the bindings would actually send. |
| 87 | // |
| 88 | // 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] | 89 | // we're going to need to frob for all the different extension functions. But |
| 90 | // we can refactor when we see what is needed. |
| 91 | bool RunFunction(UIThreadExtensionFunction* function, |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 92 | const std::string& args, |
| 93 | Browser* browser, |
| 94 | RunFunctionFlags flags); |
| 95 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 96 | } // namespace extension_function_test_utils |
| 97 | |
| 98 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_ |