blob: 3f0c91cb29c2468ca83e27130c3549ebaf116c43 [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
20namespace content {
21class WebContents;
22}
23
24class UIThreadExtensionFunction;
25
26namespace extensions {
27
28// Use this class to enable calling API functions in a unittest.
29// By default, this class will create and load an empty unpacked |extension_|,
30// which will be used in all API function calls. This extension can be
31// overridden using set_extension().
32// By default, this class does not create a WebContents for the API functions.
33// If a WebContents is needed, calling CreateBackgroundPage() will create a
34// background page for the extension and use it in API function calls. (If
35// needed, this could be expanded to allow for alternate WebContents).
36// When calling RunFunction[AndReturn*], |args| should be in JSON format,
37// wrapped in a list. See also RunFunction* in extension_function_test_utils.h.
yozb6272ef2014-08-28 02:23:0538// TODO(yoz): Move users of this base class to use the equivalent base class
39// in extensions/browser/api_unittest.h.
[email protected]628c93f2014-01-27 22:15:2040class ExtensionApiUnittest : public BrowserWithTestWindowTest {
41 public:
42 ExtensionApiUnittest();
dcheng72191812014-10-28 20:49:5643 ~ExtensionApiUnittest() override;
[email protected]628c93f2014-01-27 22:15:2044
45 content::WebContents* contents() { return contents_; }
46
47 const Extension* extension() const { return extension_.get(); }
48 scoped_refptr<Extension> extension_ref() { return extension_; }
49 void set_extension(scoped_refptr<Extension> extension) {
50 extension_ = extension;
51 }
52
53 protected:
54 // SetUp creates and loads an empty, unpacked Extension.
dcheng72191812014-10-28 20:49:5655 void SetUp() override;
[email protected]628c93f2014-01-27 22:15:2056
57 // Creates a background page for |extension_|, and sets it for the WebContents
58 // to be used in API calls.
59 // If |contents_| is already set, this does nothing.
60 void CreateBackgroundPage();
61
62 // Various ways of running an API function. These methods take ownership of
63 // |function|. |args| should be in JSON format, wrapped in a list.
64 // See also the RunFunction* methods in extension_function_test_utils.h.
65
66 // Return the function result as a base::Value.
67 scoped_ptr<base::Value> RunFunctionAndReturnValue(
68 UIThreadExtensionFunction* function, const std::string& args);
69
70 // Return the function result as a base::DictionaryValue, or NULL.
71 // This will EXPECT-fail if the result is not a DictionaryValue.
72 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
73 UIThreadExtensionFunction* function, const std::string& args);
74
75 // Return the function result as a base::ListValue, or NULL.
76 // This will EXPECT-fail if the result is not a ListValue.
77 scoped_ptr<base::ListValue> RunFunctionAndReturnList(
78 UIThreadExtensionFunction* function, const std::string& args);
79
80 // Return an error thrown from the function, if one exists.
81 // This will EXPECT-fail if any result is returned from the function.
82 std::string RunFunctionAndReturnError(
83 UIThreadExtensionFunction* function, const std::string& args);
84
85 // Run the function and ignore any result.
86 void RunFunction(
87 UIThreadExtensionFunction* function, const std::string& args);
88
89 private:
90 // The WebContents used to associate a RenderViewHost with API function calls,
91 // or NULL.
92 content::WebContents* contents_;
93
94 // The Extension used when running API function calls.
95 scoped_refptr<Extension> extension_;
96};
97
98} // namespace extensions
99
100#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_