blob: 85df0ff0179308ad6a4198df7e1befd4af7f6ad4 [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
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
14namespace base {
15class Value;
16class DictionaryValue;
17class ListValue;
18}
19
[email protected]628c93f2014-01-27 22:15:2020class UIThreadExtensionFunction;
21
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(); }
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.
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.
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]628c93f2014-01-27 22:15:2079 // 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_