blob: 3b24b5d440bbf1ef9aee92832192f96b4b192c2e [file] [log] [blame]
[email protected]637bf322011-10-01 20:46:321// Copyright (c) 2011 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#pragma once
8
9#include <string>
10
11#include "base/memory/ref_counted.h"
[email protected]4e3ce3b2011-10-14 23:25:1712
13class Browser;
14class Extension;
15class UIThreadExtensionFunction;
[email protected]637bf322011-10-01 20:46:3216
17namespace base {
18class Value;
19class DictionaryValue;
20class ListValue;
21}
22
23namespace extension_function_test_utils {
24
25// Parse JSON and return as the specified type, or NULL if the JSON is invalid
26// or not the specified type.
27base::Value* ParseJSON(const std::string& data);
28base::ListValue* ParseList(const std::string& data);
29base::DictionaryValue* ParseDictionary(const std::string& data);
30
31// Get |key| from |val| as the specified type. If |key| does not exist, or is
32// not of the specified type, adds a failure to the current test and returns
33// false, 0, empty string, etc.
34bool GetBoolean(base::DictionaryValue* val, const std::string& key);
35int GetInteger(base::DictionaryValue* val, const std::string& key);
36std::string GetString(base::DictionaryValue* val, const std::string& key);
37
38// If |val| is a dictionary, return it as one, otherwise NULL.
39base::DictionaryValue* ToDictionary(base::Value* val);
40
[email protected]4e3ce3b2011-10-14 23:25:1741// Creates an extension instance that can be attached to an ExtensionFunction
42// before running it.
43scoped_refptr<Extension> CreateEmptyExtension();
44
[email protected]637bf322011-10-01 20:46:3245enum RunFunctionFlags {
46 NONE = 0,
47 INCLUDE_INCOGNITO = 1 << 0
48};
49
50// Run |function| with |args| and return the resulting error. Adds an error to
51// the current test if |function| returns a result. The caller releases
52// ownership of |function|.
53std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
54 const std::string& args,
55 Browser* browser,
56 RunFunctionFlags flags);
57std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
58 const std::string& args,
59 Browser* browser);
60
61// Run |function| with |args| and return the result. Adds an error to the
62// current test if |function| returns an error. The caller releases ownership of
63// |function|. the caller takes ownership of the result.
64base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
65 const std::string& args,
66 Browser* browser,
67 RunFunctionFlags flags);
68base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
69 const std::string& args,
70 Browser* browser);
71
72// Create and run |function| with |args|. The caller retains ownership of
73// |function|.
74//
75// TODO(aa): It would be nice if |args| could be validated against the schema
76// that |function| expects. That way, we know that we are testing something
77// close to what the bindings would actually send.
78//
79// TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
80// we're going to need to frob for all the different extension functions. But we
81// can refactor when we see what is needed.
82void RunFunction(UIThreadExtensionFunction* function,
83 const std::string& args,
84 Browser* browser,
85 RunFunctionFlags flags);
86
87} // namespace extension_function_test_utils
88
89#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_TEST_UTILS_H_