blob: 0225489d8651b1ab14b4846946fa30f2a5bc10dd [file] [log] [blame]
[email protected]628c93f2014-01-27 22:15:201// 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
dchengc963c7142016-04-08 03:55:228#include <memory>
[email protected]628c93f2014-01-27 22:15:209#include <string>
Owen Min73f36822019-12-04 19:56:5710#include <utility>
[email protected]628c93f2014-01-27 22:15:2011
12#include "base/memory/ref_counted.h"
[email protected]628c93f2014-01-27 22:15:2013#include "chrome/test/base/browser_with_test_window_test.h"
14
15namespace base {
16class Value;
17class DictionaryValue;
18class ListValue;
19}
20
Clark DuVallfd4db3d2019-07-30 19:10:4321class ExtensionFunction;
[email protected]628c93f2014-01-27 22:15:2022
23namespace 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.
yozb6272ef2014-08-28 02:23:0535// TODO(yoz): Move users of this base class to use the equivalent base class
36// in extensions/browser/api_unittest.h.
[email protected]628c93f2014-01-27 22:15:2037class ExtensionApiUnittest : public BrowserWithTestWindowTest {
38 public:
Owen Min73f36822019-12-04 19:56:5739 template <typename... TaskEnvironmentTraits>
40 explicit ExtensionApiUnittest(TaskEnvironmentTraits&&... traits)
41 : BrowserWithTestWindowTest(
42 std::forward<TaskEnvironmentTraits>(traits)...) {}
dcheng72191812014-10-28 20:49:5643 ~ExtensionApiUnittest() override;
[email protected]628c93f2014-01-27 22:15:2044
[email protected]628c93f2014-01-27 22:15:2045 const Extension* extension() const { return extension_.get(); }
Devlin Cronin8e5892f2018-10-04 00:13:4346 scoped_refptr<const Extension> extension_ref() { return extension_; }
47 void set_extension(scoped_refptr<const Extension> extension) {
[email protected]628c93f2014-01-27 22:15:2048 extension_ = extension;
49 }
50
51 protected:
52 // SetUp creates and loads an empty, unpacked Extension.
dcheng72191812014-10-28 20:49:5653 void SetUp() override;
[email protected]628c93f2014-01-27 22:15:2054
[email protected]628c93f2014-01-27 22:15:2055 // 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.
dchengc963c7142016-04-08 03:55:2260 std::unique_ptr<base::Value> RunFunctionAndReturnValue(
Clark DuVallfd4db3d2019-07-30 19:10:4361 ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:2262 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2063
64 // Return the function result as a base::DictionaryValue, or NULL.
65 // This will EXPECT-fail if the result is not a DictionaryValue.
dchengc963c7142016-04-08 03:55:2266 std::unique_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
Clark DuVallfd4db3d2019-07-30 19:10:4367 ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:2268 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2069
70 // Return the function result as a base::ListValue, or NULL.
71 // This will EXPECT-fail if the result is not a ListValue.
dchengc963c7142016-04-08 03:55:2272 std::unique_ptr<base::ListValue> RunFunctionAndReturnList(
Clark DuVallfd4db3d2019-07-30 19:10:4373 ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:2274 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2075
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 DuVallfd4db3d2019-07-30 19:10:4378 std::string RunFunctionAndReturnError(ExtensionFunction* function,
79 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2080
81 // Run the function and ignore any result.
Clark DuVallfd4db3d2019-07-30 19:10:4382 void RunFunction(ExtensionFunction* function, const std::string& args);
[email protected]628c93f2014-01-27 22:15:2083
84 private:
[email protected]628c93f2014-01-27 22:15:2085 // The Extension used when running API function calls.
Devlin Cronin8e5892f2018-10-04 00:13:4386 scoped_refptr<const Extension> extension_;
[email protected]628c93f2014-01-27 22:15:2087};
88
89} // namespace extensions
90
91#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_