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