15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "extensions/common/manifest.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Browser; 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UIThreadExtensionFunction; 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value; 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue; 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListValue; 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Extension; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// TODO(ckehoe): Accept args as scoped_ptr<base::Value>, 2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// and migrate existing users to the new API. 281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// This file is DEPRECATED. New tests should use the versions in 291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// extensions/browser/api_test_utils.h. 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extension_function_test_utils { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Parse JSON and return as the specified type, or NULL if the JSON is invalid 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or not the specified type. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Value* ParseJSON(const std::string& data); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::ListValue* ParseList(const std::string& data); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::DictionaryValue* ParseDictionary(const std::string& data); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Get |key| from |val| as the specified type. If |key| does not exist, or is 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not of the specified type, adds a failure to the current test and returns 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// false, 0, empty string, etc. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GetBoolean(base::DictionaryValue* val, const std::string& key); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int GetInteger(base::DictionaryValue* val, const std::string& key); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetString(base::DictionaryValue* val, const std::string& key); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If |val| is a dictionary, return it as one, otherwise NULL. 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::DictionaryValue* ToDictionary(base::Value* val); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If |val| is a list, return it as one, otherwise NULL. 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::ListValue* ToList(base::Value* val); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates an extension instance with a specified location that can be attached 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to an ExtensionFunction before running. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation( 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) extensions::Manifest::Location location); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<extensions::Extension> CreateExtension( 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) extensions::Manifest::Location location, 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::DictionaryValue* test_extension_value, 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& id_input); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates an extension instance with a specified extension value that can be 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// attached to an ExtensionFunction before running. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<extensions::Extension> CreateExtension( 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::DictionaryValue* test_extension_value); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |val| contains privacy information, e.g. url, 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// title, and faviconUrl. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool HasPrivacySensitiveFields(base::DictionaryValue* val); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum RunFunctionFlags { 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NONE = 0, 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) INCLUDE_INCOGNITO = 1 << 0 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Run |function| with |args| and return the resulting error. Adds an error to 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the current test if |function| returns a result. Takes ownership of 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// |function|. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& args, 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Browser* browser, 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RunFunctionFlags flags); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& args, 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Browser* browser); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Run |function| with |args| and return the result. Adds an error to the 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// current test if |function| returns an error. Takes ownership of 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// |function|. The caller takes ownership of the result. 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Value* RunFunctionAndReturnSingleResult( 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UIThreadExtensionFunction* function, 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& args, 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Browser* browser, 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RunFunctionFlags flags); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Value* RunFunctionAndReturnSingleResult( 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UIThreadExtensionFunction* function, 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& args, 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Browser* browser); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Create and run |function| with |args|. Works with both synchronous and async 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// functions. Ownership of |function| remains with the caller. 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(aa): It would be nice if |args| could be validated against the schema 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that |function| expects. That way, we know that we are testing something 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// close to what the bindings would actually send. 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(aa): I'm concerned that this style won't scale to all the bits and bobs 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we're going to need to frob for all the different extension functions. But 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we can refactor when we see what is needed. 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RunFunction(UIThreadExtensionFunction* function, 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& args, 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Browser* browser, 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RunFunctionFlags flags); 11303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)bool RunFunction(UIThreadExtensionFunction* function, 11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) scoped_ptr<base::ListValue> args, 11503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) Browser* browser, 11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) RunFunctionFlags flags); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace extension_function_test_utils 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_ 121