[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> |
| 10 | |
| 11 | #include "base/memory/ref_counted.h" |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 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 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame^] | 20 | class ExtensionFunction; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 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(); } |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 42 | scoped_refptr<const Extension> extension_ref() { return extension_; } |
| 43 | void set_extension(scoped_refptr<const Extension> extension) { |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 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. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 56 | std::unique_ptr<base::Value> RunFunctionAndReturnValue( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame^] | 57 | ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 58 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 59 | |
| 60 | // Return the function result as a base::DictionaryValue, or NULL. |
| 61 | // This will EXPECT-fail if the result is not a DictionaryValue. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 62 | std::unique_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame^] | 63 | ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 64 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 65 | |
| 66 | // Return the function result as a base::ListValue, or NULL. |
| 67 | // This will EXPECT-fail if the result is not a ListValue. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 68 | std::unique_ptr<base::ListValue> RunFunctionAndReturnList( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame^] | 69 | ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 70 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 71 | |
| 72 | // Return an error thrown from the function, if one exists. |
| 73 | // This will EXPECT-fail if any result is returned from the function. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame^] | 74 | std::string RunFunctionAndReturnError(ExtensionFunction* function, |
| 75 | const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 76 | |
| 77 | // Run the function and ignore any result. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame^] | 78 | void RunFunction(ExtensionFunction* function, const std::string& args); |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 79 | |
| 80 | private: |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 81 | // The Extension used when running API function calls. |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 82 | scoped_refptr<const Extension> extension_; |
[email protected] | 628c93f | 2014-01-27 22:15:20 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace extensions |
| 86 | |
| 87 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |