| 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 EXTENSIONS_BROWSER_API_UNITTEST_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_UNITTEST_H_ |
| 6 #define EXTENSIONS_BROWSER_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 "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
| 13 #include "extensions/browser/extensions_test.h" | 13 #include "extensions/browser/extensions_test.h" |
| 14 #include "extensions/browser/mock_extension_system.h" | 14 #include "extensions/browser/mock_extension_system.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class Value; | 17 class Value; |
| 18 class DictionaryValue; | 18 class DictionaryValue; |
| 19 class ListValue; | 19 class ListValue; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 class NotificationService; | 23 class NotificationService; |
| 24 class TestBrowserThreadBundle; | 24 class TestBrowserThreadBundle; |
| 25 class WebContents; |
| 25 } | 26 } |
| 26 | 27 |
| 27 class UIThreadExtensionFunction; | 28 class UIThreadExtensionFunction; |
| 28 | 29 |
| 29 namespace extensions { | 30 namespace extensions { |
| 30 | 31 |
| 31 // Use this class to enable calling API functions in a unittest. | 32 // Use this class to enable calling API functions in a unittest. |
| 32 // By default, this class will create and load an empty unpacked |extension_|, | 33 // By default, this class will create and load an empty unpacked |extension_|, |
| 33 // which will be used in all API function calls. This extension can be | 34 // which will be used in all API function calls. This extension can be |
| 34 // overridden using set_extension(). | 35 // overridden using set_extension(). |
| 35 // When calling RunFunction[AndReturn*], |args| should be in JSON format, | 36 // When calling RunFunction[AndReturn*], |args| should be in JSON format, |
| 36 // wrapped in a list. See also RunFunction* in api_test_utils.h. | 37 // wrapped in a list. See also RunFunction* in api_test_utils.h. |
| 37 class ApiUnitTest : public ExtensionsTest { | 38 class ApiUnitTest : public ExtensionsTest { |
| 38 public: | 39 public: |
| 39 ApiUnitTest(); | 40 ApiUnitTest(); |
| 40 ~ApiUnitTest() override; | 41 ~ApiUnitTest() override; |
| 41 | 42 |
| 43 content::WebContents* contents() { return contents_.get(); } |
| 42 const Extension* extension() const { return extension_.get(); } | 44 const Extension* extension() const { return extension_.get(); } |
| 43 scoped_refptr<Extension> extension_ref() { return extension_; } | 45 scoped_refptr<Extension> extension_ref() { return extension_; } |
| 44 void set_extension(scoped_refptr<Extension> extension) { | 46 void set_extension(scoped_refptr<Extension> extension) { |
| 45 extension_ = extension; | 47 extension_ = extension; |
| 46 } | 48 } |
| 47 | 49 |
| 48 protected: | 50 protected: |
| 49 // SetUp creates and loads an empty, unpacked Extension. | 51 // SetUp creates and loads an empty, unpacked Extension. |
| 50 void SetUp() override; | 52 void SetUp() override; |
| 51 | 53 |
| 54 // Creates a background page for |extension_|, and sets it for the WebContents |
| 55 // to be used in API calls. |
| 56 // If |contents_| is already set, this does nothing. |
| 57 void CreateBackgroundPage(); |
| 58 |
| 52 // Various ways of running an API function. These methods take ownership of | 59 // Various ways of running an API function. These methods take ownership of |
| 53 // |function|. |args| should be in JSON format, wrapped in a list. | 60 // |function|. |args| should be in JSON format, wrapped in a list. |
| 54 // See also the RunFunction* methods in extension_function_test_utils.h. | 61 // See also the RunFunction* methods in extension_function_test_utils.h. |
| 55 | 62 |
| 56 // Return the function result as a base::Value. | 63 // Return the function result as a base::Value. |
| 57 scoped_ptr<base::Value> RunFunctionAndReturnValue( | 64 scoped_ptr<base::Value> RunFunctionAndReturnValue( |
| 58 UIThreadExtensionFunction* function, | 65 UIThreadExtensionFunction* function, |
| 59 const std::string& args); | 66 const std::string& args); |
| 60 | 67 |
| 61 // Return the function result as a base::DictionaryValue, or NULL. | 68 // Return the function result as a base::DictionaryValue, or NULL. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 80 const std::string& args); | 87 const std::string& args); |
| 81 | 88 |
| 82 private: | 89 private: |
| 83 scoped_ptr<content::NotificationService> notification_service_; | 90 scoped_ptr<content::NotificationService> notification_service_; |
| 84 | 91 |
| 85 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | 92 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; |
| 86 TestingPrefServiceSimple testing_pref_service_; | 93 TestingPrefServiceSimple testing_pref_service_; |
| 87 | 94 |
| 88 MockExtensionSystemFactory<MockExtensionSystem> extension_system_factory_; | 95 MockExtensionSystemFactory<MockExtensionSystem> extension_system_factory_; |
| 89 | 96 |
| 97 // The WebContents used to associate a RenderViewHost with API function calls, |
| 98 // or null. |
| 99 scoped_ptr<content::WebContents> contents_; |
| 100 |
| 90 // The Extension used when running API function calls. | 101 // The Extension used when running API function calls. |
| 91 scoped_refptr<Extension> extension_; | 102 scoped_refptr<Extension> extension_; |
| 92 }; | 103 }; |
| 93 | 104 |
| 94 } // namespace extensions | 105 } // namespace extensions |
| 95 | 106 |
| 96 #endif // EXTENSIONS_BROWSER_API_UNITTEST_H_ | 107 #endif // EXTENSIONS_BROWSER_API_UNITTEST_H_ |
| OLD | NEW |