blob: 8ddb90bdf430e655aa49acbfd09a528490b7f4c5 [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>
10
11#include "base/memory/ref_counted.h"
[email protected]628c93f2014-01-27 22:15:2012#include "chrome/test/base/browser_with_test_window_test.h"
13
14namespace base {
15class Value;
16class DictionaryValue;
17class ListValue;
18}
19
Clark DuVallfd4db3d2019-07-30 19:10:4320class ExtensionFunction;
[email protected]628c93f2014-01-27 22:15:2021
22namespace 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.
yozb6272ef2014-08-28 02:23:0534// TODO(yoz): Move users of this base class to use the equivalent base class
35// in extensions/browser/api_unittest.h.
[email protected]628c93f2014-01-27 22:15:2036class ExtensionApiUnittest : public BrowserWithTestWindowTest {
37 public:
38 ExtensionApiUnittest();
dcheng72191812014-10-28 20:49:5639 ~ExtensionApiUnittest() override;
[email protected]628c93f2014-01-27 22:15:2040
[email protected]628c93f2014-01-27 22:15:2041 const Extension* extension() const { return extension_.get(); }
Devlin Cronin8e5892f2018-10-04 00:13:4342 scoped_refptr<const Extension> extension_ref() { return extension_; }
43 void set_extension(scoped_refptr<const Extension> extension) {
[email protected]628c93f2014-01-27 22:15:2044 extension_ = extension;
45 }
46
47 protected:
48 // SetUp creates and loads an empty, unpacked Extension.
dcheng72191812014-10-28 20:49:5649 void SetUp() override;
[email protected]628c93f2014-01-27 22:15:2050
[email protected]628c93f2014-01-27 22:15:2051 // 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.
dchengc963c7142016-04-08 03:55:2256 std::unique_ptr<base::Value> RunFunctionAndReturnValue(
Clark DuVallfd4db3d2019-07-30 19:10:4357 ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:2258 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2059
60 // Return the function result as a base::DictionaryValue, or NULL.
61 // This will EXPECT-fail if the result is not a DictionaryValue.
dchengc963c7142016-04-08 03:55:2262 std::unique_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
Clark DuVallfd4db3d2019-07-30 19:10:4363 ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:2264 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2065
66 // Return the function result as a base::ListValue, or NULL.
67 // This will EXPECT-fail if the result is not a ListValue.
dchengc963c7142016-04-08 03:55:2268 std::unique_ptr<base::ListValue> RunFunctionAndReturnList(
Clark DuVallfd4db3d2019-07-30 19:10:4369 ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:2270 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2071
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 DuVallfd4db3d2019-07-30 19:10:4374 std::string RunFunctionAndReturnError(ExtensionFunction* function,
75 const std::string& args);
[email protected]628c93f2014-01-27 22:15:2076
77 // Run the function and ignore any result.
Clark DuVallfd4db3d2019-07-30 19:10:4378 void RunFunction(ExtensionFunction* function, const std::string& args);
[email protected]628c93f2014-01-27 22:15:2079
80 private:
[email protected]628c93f2014-01-27 22:15:2081 // The Extension used when running API function calls.
Devlin Cronin8e5892f2018-10-04 00:13:4382 scoped_refptr<const Extension> extension_;
[email protected]628c93f2014-01-27 22:15:2083};
84
85} // namespace extensions
86
87#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_