Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / extension_api_unittest.h
blob85df0ff0179308ad6a4198df7e1befd4af7f6ad4
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/test/base/browser_with_test_window_test.h"
14 namespace base {
15 class Value;
16 class DictionaryValue;
17 class ListValue;
20 class UIThreadExtensionFunction;
22 namespace extensions {
24 // Use this class to enable calling API functions in a unittest.
25 // By default, this class will create and load an empty unpacked |extension_|,
26 // which will be used in all API function calls. This extension can be
27 // overridden using set_extension().
28 // By default, this class does not create a WebContents for the API functions.
29 // If a WebContents is needed, calling CreateBackgroundPage() will create a
30 // background page for the extension and use it in API function calls. (If
31 // needed, this could be expanded to allow for alternate WebContents).
32 // When calling RunFunction[AndReturn*], |args| should be in JSON format,
33 // wrapped in a list. See also RunFunction* in extension_function_test_utils.h.
34 // TODO(yoz): Move users of this base class to use the equivalent base class
35 // in extensions/browser/api_unittest.h.
36 class ExtensionApiUnittest : public BrowserWithTestWindowTest {
37 public:
38 ExtensionApiUnittest();
39 ~ExtensionApiUnittest() override;
41 const Extension* extension() const { return extension_.get(); }
42 scoped_refptr<Extension> extension_ref() { return extension_; }
43 void set_extension(scoped_refptr<Extension> extension) {
44 extension_ = extension;
47 protected:
48 // SetUp creates and loads an empty, unpacked Extension.
49 void SetUp() override;
51 // Various ways of running an API function. These methods take ownership of
52 // |function|. |args| should be in JSON format, wrapped in a list.
53 // See also the RunFunction* methods in extension_function_test_utils.h.
55 // Return the function result as a base::Value.
56 scoped_ptr<base::Value> RunFunctionAndReturnValue(
57 UIThreadExtensionFunction* function, const std::string& args);
59 // Return the function result as a base::DictionaryValue, or NULL.
60 // This will EXPECT-fail if the result is not a DictionaryValue.
61 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
62 UIThreadExtensionFunction* function, const std::string& args);
64 // Return the function result as a base::ListValue, or NULL.
65 // This will EXPECT-fail if the result is not a ListValue.
66 scoped_ptr<base::ListValue> RunFunctionAndReturnList(
67 UIThreadExtensionFunction* function, const std::string& args);
69 // Return an error thrown from the function, if one exists.
70 // This will EXPECT-fail if any result is returned from the function.
71 std::string RunFunctionAndReturnError(
72 UIThreadExtensionFunction* function, const std::string& args);
74 // Run the function and ignore any result.
75 void RunFunction(
76 UIThreadExtensionFunction* function, const std::string& args);
78 private:
79 // The Extension used when running API function calls.
80 scoped_refptr<Extension> extension_;
83 } // namespace extensions
85 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_