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