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