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 EXTENSIONS_BROWSER_API_UNITTEST_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ | 6 #define EXTENSIONS_BROWSER_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 "base/prefs/testing_pref_service.h" |
13 #include "extensions/browser/extensions_test.h" | |
14 #include "extensions/browser/mock_extension_system.h" | |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 class Value; | 17 class Value; |
16 class DictionaryValue; | 18 class DictionaryValue; |
17 class ListValue; | 19 class ListValue; |
18 } | 20 } |
19 | 21 |
20 namespace content { | 22 namespace content { |
21 class WebContents; | 23 class NotificationService; |
24 class TestBrowserThreadBundle; | |
22 } | 25 } |
23 | 26 |
24 class UIThreadExtensionFunction; | 27 class UIThreadExtensionFunction; |
25 | 28 |
26 namespace extensions { | 29 namespace extensions { |
27 | 30 |
28 // Use this class to enable calling API functions in a unittest. | 31 // Use this class to enable calling API functions in a unittest. |
29 // By default, this class will create and load an empty unpacked |extension_|, | 32 // 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 | 33 // which will be used in all API function calls. This extension can be |
31 // overridden using set_extension(). | 34 // 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, | 35 // When calling RunFunction[AndReturn*], |args| should be in JSON format, |
37 // wrapped in a list. See also RunFunction* in extension_function_test_utils.h. | 36 // wrapped in a list. See also RunFunction* in api_test_utils.h. |
38 class ExtensionApiUnittest : public BrowserWithTestWindowTest { | 37 class ApiUnitTest : public ExtensionsTest { |
James Cook
2014/08/13 23:14:56
Yeah, I like ApiUnitTest.
| |
39 public: | 38 public: |
40 ExtensionApiUnittest(); | 39 ApiUnitTest(); |
41 virtual ~ExtensionApiUnittest(); | 40 virtual ~ApiUnitTest(); |
42 | |
43 content::WebContents* contents() { return contents_; } | |
44 | 41 |
45 const Extension* extension() const { return extension_.get(); } | 42 const Extension* extension() const { return extension_.get(); } |
46 scoped_refptr<Extension> extension_ref() { return extension_; } | 43 scoped_refptr<Extension> extension_ref() { return extension_; } |
47 void set_extension(scoped_refptr<Extension> extension) { | 44 void set_extension(scoped_refptr<Extension> extension) { |
48 extension_ = extension; | 45 extension_ = extension; |
49 } | 46 } |
50 | 47 |
51 protected: | 48 protected: |
52 // SetUp creates and loads an empty, unpacked Extension. | 49 // SetUp creates and loads an empty, unpacked Extension. |
53 virtual void SetUp() OVERRIDE; | 50 virtual void SetUp() OVERRIDE; |
54 | 51 |
55 // Creates a background page for |extension_|, and sets it for the WebContents | |
56 // to be used in API calls. | |
57 // If |contents_| is already set, this does nothing. | |
58 void CreateBackgroundPage(); | |
59 | |
60 // Various ways of running an API function. These methods take ownership of | 52 // Various ways of running an API function. These methods take ownership of |
61 // |function|. |args| should be in JSON format, wrapped in a list. | 53 // |function|. |args| should be in JSON format, wrapped in a list. |
62 // See also the RunFunction* methods in extension_function_test_utils.h. | 54 // See also the RunFunction* methods in extension_function_test_utils.h. |
63 | 55 |
64 // Return the function result as a base::Value. | 56 // Return the function result as a base::Value. |
65 scoped_ptr<base::Value> RunFunctionAndReturnValue( | 57 scoped_ptr<base::Value> RunFunctionAndReturnValue( |
66 UIThreadExtensionFunction* function, const std::string& args); | 58 UIThreadExtensionFunction* function, |
59 const std::string& args); | |
67 | 60 |
68 // Return the function result as a base::DictionaryValue, or NULL. | 61 // Return the function result as a base::DictionaryValue, or NULL. |
69 // This will EXPECT-fail if the result is not a DictionaryValue. | 62 // This will EXPECT-fail if the result is not a DictionaryValue. |
70 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary( | 63 scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary( |
71 UIThreadExtensionFunction* function, const std::string& args); | 64 UIThreadExtensionFunction* function, |
65 const std::string& args); | |
72 | 66 |
73 // Return the function result as a base::ListValue, or NULL. | 67 // Return the function result as a base::ListValue, or NULL. |
74 // This will EXPECT-fail if the result is not a ListValue. | 68 // This will EXPECT-fail if the result is not a ListValue. |
75 scoped_ptr<base::ListValue> RunFunctionAndReturnList( | 69 scoped_ptr<base::ListValue> RunFunctionAndReturnList( |
76 UIThreadExtensionFunction* function, const std::string& args); | 70 UIThreadExtensionFunction* function, |
71 const std::string& args); | |
77 | 72 |
78 // Return an error thrown from the function, if one exists. | 73 // Return an error thrown from the function, if one exists. |
79 // This will EXPECT-fail if any result is returned from the function. | 74 // This will EXPECT-fail if any result is returned from the function. |
80 std::string RunFunctionAndReturnError( | 75 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
81 UIThreadExtensionFunction* function, const std::string& args); | 76 const std::string& args); |
82 | 77 |
83 // Run the function and ignore any result. | 78 // Run the function and ignore any result. |
84 void RunFunction( | 79 void RunFunction(UIThreadExtensionFunction* function, |
85 UIThreadExtensionFunction* function, const std::string& args); | 80 const std::string& args); |
86 | 81 |
87 private: | 82 private: |
88 // The WebContents used to associate a RenderViewHost with API function calls, | 83 scoped_ptr<content::NotificationService> notification_service_; |
89 // or NULL. | 84 |
90 content::WebContents* contents_; | 85 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
86 TestingPrefServiceSimple testing_pref_service_; | |
87 | |
88 MockExtensionSystemFactory<MockExtensionSystem> extension_system_factory_; | |
91 | 89 |
92 // The Extension used when running API function calls. | 90 // The Extension used when running API function calls. |
93 scoped_refptr<Extension> extension_; | 91 scoped_refptr<Extension> extension_; |
94 }; | 92 }; |
95 | 93 |
96 } // namespace extensions | 94 } // namespace extensions |
97 | 95 |
98 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_ | 96 #endif // EXTENSIONS_BROWSER_API_UNITTEST_H_ |
OLD | NEW |