[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 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" |
| 13 | |
| 14 | namespace base { |
| 15 | class Value; |
| 16 | class DictionaryValue; |
| 17 | class ListValue; |
| 18 | } |
| 19 | |
| 20 | namespace content { |
| 21 | class WebContents; |
| 22 | } |
| 23 | |
| 24 | class UIThreadExtensionFunction; |
| 25 | |
| 26 | namespace extensions { |
| 27 | |
| 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. |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 38 | // TODO(yoz): Move users of this base class to use the equivalent base class |
| 39 | // in extensions/browser/api_unittest.h. |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 40 | class ExtensionApiUnittest : public BrowserWithTestWindowTest { |
| 41 | public: |
| 42 | ExtensionApiUnittest(); |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame^] | 43 | ~ExtensionApiUnittest() override; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 44 | |
| 45 | content::WebContents* contents() { return contents_; } |
| 46 | |
| 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; |
| 51 | } |
| 52 | |
| 53 | protected: |
| 54 | // SetUp creates and loads an empty, unpacked Extension. |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame^] | 55 | void SetUp() override; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 56 | |
| 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(); |
| 61 | |
| 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. |
| 65 | |
| 66 | // Return the function result as a base::Value. |
| 67 | scoped_ptr<base::Value> RunFunctionAndReturnValue( |
| 68 | UIThreadExtensionFunction* function, const std::string& args); |
| 69 | |
| 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); |
| 74 | |
| 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); |
| 79 | |
| 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); |
| 84 | |
| 85 | // Run the function and ignore any result. |
| 86 | void RunFunction( |
| 87 | UIThreadExtensionFunction* function, const std::string& args); |
| 88 | |
| 89 | private: |
| 90 | // The WebContents used to associate a RenderViewHost with API function calls, |
| 91 | // or NULL. |
| 92 | content::WebContents* contents_; |
| 93 | |
| 94 | // The Extension used when running API function calls. |
| 95 | scoped_refptr<Extension> extension_; |
| 96 | }; |
| 97 | |
| 98 | } // namespace extensions |
| 99 | |
| 100 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |