[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 | |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 20 | class UIThreadExtensionFunction; |
| 21 | |
| 22 | namespace extensions { |
| 23 | |
| 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. |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 34 | // TODO(yoz): Move users of this base class to use the equivalent base class |
| 35 | // in extensions/browser/api_unittest.h. |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 36 | class ExtensionApiUnittest : public BrowserWithTestWindowTest { |
| 37 | public: |
| 38 | ExtensionApiUnittest(); |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 39 | ~ExtensionApiUnittest() override; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 40 | |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 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; |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | // SetUp creates and loads an empty, unpacked Extension. |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 49 | void SetUp() override; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 50 | |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 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. |
| 54 | |
| 55 | // Return the function result as a base::Value. |
| 56 | scoped_ptr<base::Value> RunFunctionAndReturnValue( |
| 57 | UIThreadExtensionFunction* function, const std::string& args); |
| 58 | |
| 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); |
| 63 | |
| 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); |
| 68 | |
| 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); |
| 73 | |
| 74 | // Run the function and ignore any result. |
| 75 | void RunFunction( |
| 76 | UIThreadExtensionFunction* function, const std::string& args); |
| 77 | |
| 78 | private: |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 79 | // The Extension used when running API function calls. |
| 80 | scoped_refptr<Extension> extension_; |
| 81 | }; |
| 82 | |
| 83 | } // namespace extensions |
| 84 | |
| 85 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |