blob: 27336a52e1f5f4ddc3ba736ceae3911d4b88b09c [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_
7#pragma once
8
9#include <string>
10
11#include "base/memory/ref_counted.h"
[email protected]6d2d55b2012-05-05 21:33:4312#include "chrome/common/extensions/extension.h"
[email protected]4e3ce3b2011-10-14 23:25:1713
[email protected]d8c8749b92011-11-16 22:31:3214class AsyncExtensionFunction;
[email protected]4e3ce3b2011-10-14 23:25:1715class Browser;
16class Extension;
17class UIThreadExtensionFunction;
[email protected]637bf322011-10-01 20:46:3218
19namespace base {
20class Value;
21class DictionaryValue;
22class ListValue;
23}
24
25namespace extension_function_test_utils {
26
27// Parse JSON and return as the specified type, or NULL if the JSON is invalid
28// or not the specified type.
29base::Value* ParseJSON(const std::string& data);
30base::ListValue* ParseList(const std::string& data);
31base::DictionaryValue* ParseDictionary(const std::string& data);
32
33// Get |key| from |val| as the specified type. If |key| does not exist, or is
34// not of the specified type, adds a failure to the current test and returns
35// false, 0, empty string, etc.
36bool GetBoolean(base::DictionaryValue* val, const std::string& key);
37int GetInteger(base::DictionaryValue* val, const std::string& key);
38std::string GetString(base::DictionaryValue* val, const std::string& key);
39
40// If |val| is a dictionary, return it as one, otherwise NULL.
41base::DictionaryValue* ToDictionary(base::Value* val);
42
[email protected]008ff7fb2011-12-19 08:51:1743// If |val| is a list, return it as one, otherwise NULL.
44base::ListValue* ToList(base::Value* val);
45
[email protected]4e3ce3b2011-10-14 23:25:1746// Creates an extension instance that can be attached to an ExtensionFunction
47// before running it.
48scoped_refptr<Extension> CreateEmptyExtension();
49
[email protected]6d2d55b2012-05-05 21:33:4350// Creates an extension instance with a specified location that can be attached
51// to an ExtensionFunction before running.
52scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
53 Extension::Location location);
54
[email protected]637bf322011-10-01 20:46:3255enum RunFunctionFlags {
56 NONE = 0,
57 INCLUDE_INCOGNITO = 1 << 0
58};
59
60// Run |function| with |args| and return the resulting error. Adds an error to
[email protected]bdfc03e2011-11-22 00:20:3361// the current test if |function| returns a result.
[email protected]637bf322011-10-01 20:46:3262std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
63 const std::string& args,
64 Browser* browser,
65 RunFunctionFlags flags);
66std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
67 const std::string& args,
68 Browser* browser);
69
70// Run |function| with |args| and return the result. Adds an error to the
[email protected]bdfc03e2011-11-22 00:20:3371// current test if |function| returns an error. The caller takes ownership of
72// the result.
[email protected]637bf322011-10-01 20:46:3273base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
74 const std::string& args,
75 Browser* browser,
76 RunFunctionFlags flags);
77base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
78 const std::string& args,
79 Browser* browser);
80
[email protected]bdfc03e2011-11-22 00:20:3381// Create and run |function| with |args|. Works with both synchronous and async
82// functions.
[email protected]637bf322011-10-01 20:46:3283//
84// TODO(aa): It would be nice if |args| could be validated against the schema
85// that |function| expects. That way, we know that we are testing something
86// close to what the bindings would actually send.
87//
88// TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
[email protected]bdfc03e2011-11-22 00:20:3389// we're going to need to frob for all the different extension functions. But
90// we can refactor when we see what is needed.
91bool RunFunction(UIThreadExtensionFunction* function,
[email protected]637bf322011-10-01 20:46:3292 const std::string& args,
93 Browser* browser,
94 RunFunctionFlags flags);
95
[email protected]637bf322011-10-01 20:46:3296} // namespace extension_function_test_utils
97
98#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_