OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "chrome/test/base/browser_with_test_window_test.h" | 12 #include "chrome/test/base/browser_with_test_window_test.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 class Value; | 15 class Value; |
16 class DictionaryValue; | 16 class DictionaryValue; |
17 class ListValue; | 17 class ListValue; |
18 } | 18 } |
19 | 19 |
20 namespace content { | |
21 class WebContents; | |
22 } | |
23 | |
24 class UIThreadExtensionFunction; | 20 class UIThreadExtensionFunction; |
25 | 21 |
26 namespace extensions { | 22 namespace extensions { |
27 | 23 |
28 // Use this class to enable calling API functions in a unittest. | 24 // Use this class to enable calling API functions in a unittest. |
29 // By default, this class will create and load an empty unpacked |extension_|, | 25 // 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 | 26 // which will be used in all API function calls. This extension can be |
31 // overridden using set_extension(). | 27 // overridden using set_extension(). |
32 // By default, this class does not create a WebContents for the API functions. | 28 // By default, this class does not create a WebContents for the API functions. |
33 // If a WebContents is needed, calling CreateBackgroundPage() will create a | 29 // If a WebContents is needed, calling CreateBackgroundPage() will create a |
34 // background page for the extension and use it in API function calls. (If | 30 // background page for the extension and use it in API function calls. (If |
35 // needed, this could be expanded to allow for alternate WebContents). | 31 // needed, this could be expanded to allow for alternate WebContents). |
36 // When calling RunFunction[AndReturn*], |args| should be in JSON format, | 32 // When calling RunFunction[AndReturn*], |args| should be in JSON format, |
37 // wrapped in a list. See also RunFunction* in extension_function_test_utils.h. | 33 // wrapped in a list. See also RunFunction* in extension_function_test_utils.h. |
38 // TODO(yoz): Move users of this base class to use the equivalent base class | 34 // TODO(yoz): Move users of this base class to use the equivalent base class |
39 // in extensions/browser/api_unittest.h. | 35 // in extensions/browser/api_unittest.h. |
40 class ExtensionApiUnittest : public BrowserWithTestWindowTest { | 36 class ExtensionApiUnittest : public BrowserWithTestWindowTest { |
41 public: | 37 public: |
42 ExtensionApiUnittest(); | 38 ExtensionApiUnittest(); |
43 ~ExtensionApiUnittest() override; | 39 ~ExtensionApiUnittest() override; |
44 | 40 |
45 content::WebContents* contents() { return contents_; } | |
46 | |
47 const Extension* extension() const { return extension_.get(); } | 41 const Extension* extension() const { return extension_.get(); } |
48 scoped_refptr<Extension> extension_ref() { return extension_; } | 42 scoped_refptr<Extension> extension_ref() { return extension_; } |
49 void set_extension(scoped_refptr<Extension> extension) { | 43 void set_extension(scoped_refptr<Extension> extension) { |
50 extension_ = extension; | 44 extension_ = extension; |
51 } | 45 } |
52 | 46 |
53 protected: | 47 protected: |
54 // SetUp creates and loads an empty, unpacked Extension. | 48 // SetUp creates and loads an empty, unpacked Extension. |
55 void SetUp() override; | 49 void SetUp() override; |
56 | 50 |
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 | 51 // Various ways of running an API function. These methods take ownership of |
63 // |function|. |args| should be in JSON format, wrapped in a list. | 52 // |function|. |args| should be in JSON format, wrapped in a list. |
64 // See also the RunFunction* methods in extension_function_test_utils.h. | 53 // See also the RunFunction* methods in extension_function_test_utils.h. |
65 | 54 |
66 // Return the function result as a base::Value. | 55 // Return the function result as a base::Value. |
67 scoped_ptr<base::Value> RunFunctionAndReturnValue( | 56 scoped_ptr<base::Value> RunFunctionAndReturnValue( |
68 UIThreadExtensionFunction* function, const std::string& args); | 57 UIThreadExtensionFunction* function, const std::string& args); |
69 | 58 |
70 // Return the function result as a base::DictionaryValue, or NULL. | 59 // Return the function result as a base::DictionaryValue, or NULL. |
71 // This will EXPECT-fail if the result is not a DictionaryValue. | 60 // This will EXPECT-fail if the result is not a DictionaryValue. |
72 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary( | 61 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary( |
73 UIThreadExtensionFunction* function, const std::string& args); | 62 UIThreadExtensionFunction* function, const std::string& args); |
74 | 63 |
75 // Return the function result as a base::ListValue, or NULL. | 64 // Return the function result as a base::ListValue, or NULL. |
76 // This will EXPECT-fail if the result is not a ListValue. | 65 // This will EXPECT-fail if the result is not a ListValue. |
77 scoped_ptr<base::ListValue> RunFunctionAndReturnList( | 66 scoped_ptr<base::ListValue> RunFunctionAndReturnList( |
78 UIThreadExtensionFunction* function, const std::string& args); | 67 UIThreadExtensionFunction* function, const std::string& args); |
79 | 68 |
80 // Return an error thrown from the function, if one exists. | 69 // Return an error thrown from the function, if one exists. |
81 // This will EXPECT-fail if any result is returned from the function. | 70 // This will EXPECT-fail if any result is returned from the function. |
82 std::string RunFunctionAndReturnError( | 71 std::string RunFunctionAndReturnError( |
83 UIThreadExtensionFunction* function, const std::string& args); | 72 UIThreadExtensionFunction* function, const std::string& args); |
84 | 73 |
85 // Run the function and ignore any result. | 74 // Run the function and ignore any result. |
86 void RunFunction( | 75 void RunFunction( |
87 UIThreadExtensionFunction* function, const std::string& args); | 76 UIThreadExtensionFunction* function, const std::string& args); |
88 | 77 |
89 private: | 78 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. | 79 // The Extension used when running API function calls. |
95 scoped_refptr<Extension> extension_; | 80 scoped_refptr<Extension> extension_; |
96 }; | 81 }; |
97 | 82 |
98 } // namespace extensions | 83 } // namespace extensions |
99 | 84 |
100 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ | 85 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ |
OLD | NEW |