blob: 9a726d2aaa623632b0bc8ca87cad9dceefe1c4a4 [file] [log] [blame]
[email protected]6d2d55b2012-05-05 21:33:431// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]637bf322011-10-01 20:46:322// 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]637bf322011-10-01 20:46:327
8#include <string>
9
10#include "base/memory/ref_counted.h"
[email protected]d42c1112013-08-22 19:36:3211#include "extensions/common/manifest.h"
[email protected]4e3ce3b2011-10-14 23:25:1712
13class Browser;
[email protected]4e3ce3b2011-10-14 23:25:1714class UIThreadExtensionFunction;
[email protected]637bf322011-10-01 20:46:3215
16namespace base {
17class Value;
18class DictionaryValue;
19class ListValue;
20}
21
[email protected]1c321ee2012-05-21 03:02:3422namespace extensions {
23class Extension;
24}
25
[email protected]fc672e12014-08-16 08:16:1526// TODO(ckehoe): Accept args as scoped_ptr<base::Value>,
27// and migrate existing users to the new API.
yozb6272ef2014-08-28 02:23:0528// This file is DEPRECATED. New tests should use the versions in
29// extensions/browser/api_test_utils.h.
[email protected]637bf322011-10-01 20:46:3230namespace 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.
34base::Value* ParseJSON(const std::string& data);
35base::ListValue* ParseList(const std::string& data);
36base::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.
41bool GetBoolean(base::DictionaryValue* val, const std::string& key);
42int GetInteger(base::DictionaryValue* val, const std::string& key);
43std::string GetString(base::DictionaryValue* val, const std::string& key);
44
45// If |val| is a dictionary, return it as one, otherwise NULL.
46base::DictionaryValue* ToDictionary(base::Value* val);
47
[email protected]008ff7fb2011-12-19 08:51:1748// If |val| is a list, return it as one, otherwise NULL.
49base::ListValue* ToList(base::Value* val);
50
[email protected]4e3ce3b2011-10-14 23:25:1751// Creates an extension instance that can be attached to an ExtensionFunction
52// before running it.
[email protected]1c321ee2012-05-21 03:02:3453scoped_refptr<extensions::Extension> CreateEmptyExtension();
[email protected]4e3ce3b2011-10-14 23:25:1754
[email protected]6d2d55b2012-05-05 21:33:4355// Creates an extension instance with a specified location that can be attached
56// to an ExtensionFunction before running.
[email protected]1c321ee2012-05-21 03:02:3457scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation(
[email protected]1d5e58b2013-01-31 08:41:4058 extensions::Manifest::Location location);
[email protected]6d2d55b2012-05-05 21:33:4359
[email protected]09426852012-09-11 22:39:0760// 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.
66scoped_refptr<extensions::Extension> CreateEmptyExtension(
67 const std::string& id_input);
68
69scoped_refptr<extensions::Extension> CreateExtension(
[email protected]1d5e58b2013-01-31 08:41:4070 extensions::Manifest::Location location,
[email protected]09426852012-09-11 22:39:0771 base::DictionaryValue* test_extension_value,
72 const std::string& id_input);
73
[email protected]37bb5822012-09-10 15:09:5774// Creates an extension instance with a specified extension value that can be
75// attached to an ExtensionFunction before running.
76scoped_refptr<extensions::Extension> CreateExtension(
77 base::DictionaryValue* test_extension_value);
78
[email protected]37bb5822012-09-10 15:09:5779// Returns true if |val| contains privacy information, e.g. url,
80// title, and faviconUrl.
81bool HasPrivacySensitiveFields(base::DictionaryValue* val);
82
[email protected]637bf322011-10-01 20:46:3283enum 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]4f9c79ec2013-03-21 04:41:3789// the current test if |function| returns a result. Takes ownership of
90// |function|.
[email protected]637bf322011-10-01 20:46:3291std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
92 const std::string& args,
93 Browser* browser,
94 RunFunctionFlags flags);
95std::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]4f9c79ec2013-03-21 04:41:37100// current test if |function| returns an error. Takes ownership of
101// |function|. The caller takes ownership of the result.
[email protected]07ff5fd2012-07-12 22:39:09102base::Value* RunFunctionAndReturnSingleResult(
103 UIThreadExtensionFunction* function,
104 const std::string& args,
105 Browser* browser,
106 RunFunctionFlags flags);
107base::Value* RunFunctionAndReturnSingleResult(
108 UIThreadExtensionFunction* function,
109 const std::string& args,
110 Browser* browser);
[email protected]637bf322011-10-01 20:46:32111
[email protected]bdfc03e2011-11-22 00:20:33112// Create and run |function| with |args|. Works with both synchronous and async
[email protected]4f9c79ec2013-03-21 04:41:37113// functions. Ownership of |function| remains with the caller.
[email protected]637bf322011-10-01 20:46:32114//
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]bdfc03e2011-11-22 00:20:33120// we're going to need to frob for all the different extension functions. But
121// we can refactor when we see what is needed.
122bool RunFunction(UIThreadExtensionFunction* function,
[email protected]637bf322011-10-01 20:46:32123 const std::string& args,
124 Browser* browser,
125 RunFunctionFlags flags);
[email protected]fc672e12014-08-16 08:16:15126bool RunFunction(UIThreadExtensionFunction* function,
127 scoped_ptr<base::ListValue> args,
128 Browser* browser,
129 RunFunctionFlags flags);
[email protected]637bf322011-10-01 20:46:32130
[email protected]637bf322011-10-01 20:46:32131} // namespace extension_function_test_utils
132
133#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_