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_
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"
16 class DictionaryValue;
24 class UIThreadExtensionFunction;
26 namespace extensions {
28 // Use this class to enable calling API functions in a unittest.
29 // By default, this class will create and load an empty unpacked |extension_|,
30 // which will be used in all API function calls. This extension can be
31 // overridden using set_extension().
32 // By default, this class does not create a WebContents for the API functions.
33 // If a WebContents is needed, calling CreateBackgroundPage() will create a
34 // background page for the extension and use it in API function calls. (If
35 // needed, this could be expanded to allow for alternate WebContents).
36 // When calling RunFunction[AndReturn*], |args| should be in JSON format,
37 // wrapped in a list. See also RunFunction* in extension_function_test_utils.h.
38 // TODO(yoz): Move users of this base class to use the equivalent base class
39 // in extensions/browser/api_unittest.h.
40 class ExtensionApiUnittest : public BrowserWithTestWindowTest {
42 ExtensionApiUnittest();
43 virtual ~ExtensionApiUnittest();
45 content::WebContents* contents() { return contents_; }
47 const Extension* extension() const { return extension_.get(); }
48 scoped_refptr<Extension> extension_ref() { return extension_; }
49 void set_extension(scoped_refptr<Extension> extension) {
50 extension_ = extension;
54 // SetUp creates and loads an empty, unpacked Extension.
55 virtual void SetUp() OVERRIDE;
57 // Creates a background page for |extension_|, and sets it for the WebContents
58 // to be used in API calls.
59 // If |contents_| is already set, this does nothing.
60 void CreateBackgroundPage();
62 // Various ways of running an API function. These methods take ownership of
63 // |function|. |args| should be in JSON format, wrapped in a list.
64 // See also the RunFunction* methods in extension_function_test_utils.h.
66 // Return the function result as a base::Value.
67 scoped_ptr<base::Value> RunFunctionAndReturnValue(
68 UIThreadExtensionFunction* function, const std::string& args);
70 // Return the function result as a base::DictionaryValue, or NULL.
71 // This will EXPECT-fail if the result is not a DictionaryValue.
72 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
73 UIThreadExtensionFunction* function, const std::string& args);
75 // Return the function result as a base::ListValue, or NULL.
76 // This will EXPECT-fail if the result is not a ListValue.
77 scoped_ptr<base::ListValue> RunFunctionAndReturnList(
78 UIThreadExtensionFunction* function, const std::string& args);
80 // Return an error thrown from the function, if one exists.
81 // This will EXPECT-fail if any result is returned from the function.
82 std::string RunFunctionAndReturnError(
83 UIThreadExtensionFunction* function, const std::string& args);
85 // Run the function and ignore any result.
87 UIThreadExtensionFunction* function, const std::string& args);
90 // The WebContents used to associate a RenderViewHost with API function calls,
92 content::WebContents* contents_;
94 // The Extension used when running API function calls.
95 scoped_refptr<Extension> extension_;
98 } // namespace extensions
100 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_