blob: cea6e78a982f86cb1eff0fa8bd246d18e5d91778 [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]6d2d55b2012-05-05 21:33:4311#include "chrome/common/extensions/extension.h"
[email protected]4e3ce3b2011-10-14 23:25:1712
[email protected]d8c8749b92011-11-16 22:31:3213class AsyncExtensionFunction;
[email protected]4e3ce3b2011-10-14 23:25:1714class Browser;
[email protected]4e3ce3b2011-10-14 23:25:1715class UIThreadExtensionFunction;
[email protected]637bf322011-10-01 20:46:3216
17namespace base {
18class Value;
19class DictionaryValue;
20class ListValue;
21}
22
[email protected]1c321ee2012-05-21 03:02:3423namespace extensions {
24class Extension;
25}
26
[email protected]637bf322011-10-01 20:46:3227namespace 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
[email protected]008ff7fb2011-12-19 08:51:1745// If |val| is a list, return it as one, otherwise NULL.
46base::ListValue* ToList(base::Value* val);
47
[email protected]4e3ce3b2011-10-14 23:25:1748// Creates an extension instance that can be attached to an ExtensionFunction
49// before running it.
[email protected]1c321ee2012-05-21 03:02:3450scoped_refptr<extensions::Extension> CreateEmptyExtension();
[email protected]4e3ce3b2011-10-14 23:25:1751
[email protected]6d2d55b2012-05-05 21:33:4352// Creates an extension instance with a specified location that can be attached
53// to an ExtensionFunction before running.
[email protected]1c321ee2012-05-21 03:02:3454scoped_refptr<extensions::Extension> CreateEmptyExtensionWithLocation(
55 extensions::Extension::Location location);
[email protected]6d2d55b2012-05-05 21:33:4356
[email protected]37bb5822012-09-10 15:09:5757// Creates an extension instance with a specified extension value that can be
58// attached to an ExtensionFunction before running.
59scoped_refptr<extensions::Extension> CreateExtension(
60 base::DictionaryValue* test_extension_value);
61
62scoped_refptr<extensions::Extension> CreateExtension(
63 extensions::Extension::Location location,
64 base::DictionaryValue* test_extension_value);
65
66// Returns true if |val| contains privacy information, e.g. url,
67// title, and faviconUrl.
68bool HasPrivacySensitiveFields(base::DictionaryValue* val);
69
[email protected]637bf322011-10-01 20:46:3270enum RunFunctionFlags {
71 NONE = 0,
72 INCLUDE_INCOGNITO = 1 << 0
73};
74
75// Run |function| with |args| and return the resulting error. Adds an error to
[email protected]bdfc03e2011-11-22 00:20:3376// the current test if |function| returns a result.
[email protected]637bf322011-10-01 20:46:3277std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
78 const std::string& args,
79 Browser* browser,
80 RunFunctionFlags flags);
81std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
82 const std::string& args,
83 Browser* browser);
84
85// Run |function| with |args| and return the result. Adds an error to the
[email protected]bdfc03e2011-11-22 00:20:3386// current test if |function| returns an error. The caller takes ownership of
87// the result.
[email protected]07ff5fd2012-07-12 22:39:0988base::Value* RunFunctionAndReturnSingleResult(
89 UIThreadExtensionFunction* function,
90 const std::string& args,
91 Browser* browser,
92 RunFunctionFlags flags);
93base::Value* RunFunctionAndReturnSingleResult(
94 UIThreadExtensionFunction* function,
95 const std::string& args,
96 Browser* browser);
[email protected]637bf322011-10-01 20:46:3297
[email protected]bdfc03e2011-11-22 00:20:3398// Create and run |function| with |args|. Works with both synchronous and async
99// functions.
[email protected]637bf322011-10-01 20:46:32100//
101// TODO(aa): It would be nice if |args| could be validated against the schema
102// that |function| expects. That way, we know that we are testing something
103// close to what the bindings would actually send.
104//
105// TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
[email protected]bdfc03e2011-11-22 00:20:33106// we're going to need to frob for all the different extension functions. But
107// we can refactor when we see what is needed.
108bool RunFunction(UIThreadExtensionFunction* function,
[email protected]637bf322011-10-01 20:46:32109 const std::string& args,
110 Browser* browser,
111 RunFunctionFlags flags);
112
[email protected]637bf322011-10-01 20:46:32113} // namespace extension_function_test_utils
114
115#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_