[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 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 9 | #include <string> |
Owen Min | 73f3682 | 2019-12-04 19:56:57 | [diff] [blame] | 10 | #include <utility> |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 11 | |
| 12 | #include "base/memory/ref_counted.h" |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 13 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 14 | |
| 15 | namespace base { |
| 16 | class Value; |
| 17 | class DictionaryValue; |
| 18 | class ListValue; |
| 19 | } |
| 20 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 21 | class ExtensionFunction; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 22 | |
| 23 | namespace extensions { |
| 24 | |
| 25 | // Use this class to enable calling API functions in a unittest. |
| 26 | // By default, this class will create and load an empty unpacked |extension_|, |
| 27 | // which will be used in all API function calls. This extension can be |
| 28 | // overridden using set_extension(). |
| 29 | // By default, this class does not create a WebContents for the API functions. |
| 30 | // If a WebContents is needed, calling CreateBackgroundPage() will create a |
| 31 | // background page for the extension and use it in API function calls. (If |
| 32 | // needed, this could be expanded to allow for alternate WebContents). |
| 33 | // When calling RunFunction[AndReturn*], |args| should be in JSON format, |
| 34 | // wrapped in a list. See also RunFunction* in extension_function_test_utils.h. |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 35 | // TODO(yoz): Move users of this base class to use the equivalent base class |
| 36 | // in extensions/browser/api_unittest.h. |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 37 | class ExtensionApiUnittest : public BrowserWithTestWindowTest { |
| 38 | public: |
Owen Min | 73f3682 | 2019-12-04 19:56:57 | [diff] [blame] | 39 | template <typename... TaskEnvironmentTraits> |
| 40 | explicit ExtensionApiUnittest(TaskEnvironmentTraits&&... traits) |
| 41 | : BrowserWithTestWindowTest( |
| 42 | std::forward<TaskEnvironmentTraits>(traits)...) {} |
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 | |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 45 | const Extension* extension() const { return extension_.get(); } |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 46 | scoped_refptr<const Extension> extension_ref() { return extension_; } |
| 47 | void set_extension(scoped_refptr<const Extension> extension) { |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 48 | extension_ = extension; |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | // SetUp creates and loads an empty, unpacked Extension. |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 53 | void SetUp() override; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 54 | |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 55 | // Various ways of running an API function. These methods take ownership of |
| 56 | // |function|. |args| should be in JSON format, wrapped in a list. |
| 57 | // See also the RunFunction* methods in extension_function_test_utils.h. |
| 58 | |
| 59 | // Return the function result as a base::Value. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 60 | std::unique_ptr<base::Value> RunFunctionAndReturnValue( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 61 | ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 62 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 63 | |
| 64 | // Return the function result as a base::DictionaryValue, or NULL. |
| 65 | // This will EXPECT-fail if the result is not a DictionaryValue. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 66 | std::unique_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 67 | ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 68 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 69 | |
| 70 | // Return the function result as a base::ListValue, or NULL. |
| 71 | // This will EXPECT-fail if the result is not a ListValue. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 72 | std::unique_ptr<base::ListValue> RunFunctionAndReturnList( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 73 | ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 74 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 75 | |
| 76 | // Return an error thrown from the function, if one exists. |
| 77 | // This will EXPECT-fail if any result is returned from the function. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 78 | std::string RunFunctionAndReturnError(ExtensionFunction* function, |
| 79 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 80 | |
| 81 | // Run the function and ignore any result. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 82 | void RunFunction(ExtensionFunction* function, const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 83 | |
| 84 | private: |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 85 | // The Extension used when running API function calls. |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 86 | scoped_refptr<const Extension> extension_; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace extensions |
| 90 | |
| 91 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |