OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/extension_api_unittest.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "components/user_prefs/user_prefs.h" |
| 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/content_browser_client.h" |
| 12 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/site_instance.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/content_client.h" |
| 16 #include "content/public/common/url_constants.h" |
| 17 #include "content/public/test/test_browser_context.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "content/public/test/web_contents_tester.h" |
| 20 #include "extensions/browser/api_test_utils.h" |
| 21 #include "extensions/browser/extension_function.h" |
| 22 #include "extensions/browser/test_extensions_browser_client.h" |
| 23 #include "extensions/common/extension.h" |
| 24 #include "extensions/common/extension_builder.h" |
| 25 #include "extensions/common/manifest.h" |
| 26 #include "extensions/common/manifest_handlers/background_info.h" |
| 27 #include "extensions/common/value_builder.h" |
| 28 |
| 29 namespace utils = extensions::api_test_utils; |
| 30 |
| 31 namespace extensions { |
| 32 |
| 33 ExtensionApiUnittest::ExtensionApiUnittest() |
| 34 : notification_service_(content::NotificationService::Create()) { |
| 35 // TODO(yoz): TestExtensionsBrowserClient->set_extension_system_factory |
| 36 } |
| 37 |
| 38 ExtensionApiUnittest::~ExtensionApiUnittest() { |
| 39 } |
| 40 |
| 41 void ExtensionApiUnittest::SetUp() { |
| 42 ExtensionsTest::SetUp(); |
| 43 extensions_browser_client()->set_extension_system_factory( |
| 44 &extension_system_factory_); |
| 45 |
| 46 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
| 47 content::TestBrowserThreadBundle::DEFAULT)); |
| 48 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_); |
| 49 |
| 50 extension_ = ExtensionBuilder() |
| 51 .SetManifest(DictionaryBuilder().Set("name", "Test").Set( |
| 52 "version", "1.0")) |
| 53 .SetLocation(Manifest::UNPACKED) |
| 54 .Build(); |
| 55 } |
| 56 |
| 57 scoped_ptr<base::Value> ExtensionApiUnittest::RunFunctionAndReturnValue( |
| 58 UIThreadExtensionFunction* function, |
| 59 const std::string& args) { |
| 60 function->set_extension(extension()); |
| 61 return scoped_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult( |
| 62 function, args, browser_context())); |
| 63 } |
| 64 |
| 65 scoped_ptr<base::DictionaryValue> |
| 66 ExtensionApiUnittest::RunFunctionAndReturnDictionary( |
| 67 UIThreadExtensionFunction* function, |
| 68 const std::string& args) { |
| 69 base::Value* value = RunFunctionAndReturnValue(function, args).release(); |
| 70 base::DictionaryValue* dict = NULL; |
| 71 |
| 72 if (value && !value->GetAsDictionary(&dict)) |
| 73 delete value; |
| 74 |
| 75 // We expect to either have successfuly retrieved a dictionary from the value, |
| 76 // or the value to have been NULL. |
| 77 EXPECT_TRUE(dict || !value); |
| 78 return scoped_ptr<base::DictionaryValue>(dict); |
| 79 } |
| 80 |
| 81 scoped_ptr<base::ListValue> ExtensionApiUnittest::RunFunctionAndReturnList( |
| 82 UIThreadExtensionFunction* function, |
| 83 const std::string& args) { |
| 84 base::Value* value = RunFunctionAndReturnValue(function, args).release(); |
| 85 base::ListValue* list = NULL; |
| 86 |
| 87 if (value && !value->GetAsList(&list)) |
| 88 delete value; |
| 89 |
| 90 // We expect to either have successfuly retrieved a list from the value, |
| 91 // or the value to have been NULL. |
| 92 EXPECT_TRUE(list || !value); |
| 93 return scoped_ptr<base::ListValue>(list); |
| 94 } |
| 95 |
| 96 std::string ExtensionApiUnittest::RunFunctionAndReturnError( |
| 97 UIThreadExtensionFunction* function, |
| 98 const std::string& args) { |
| 99 function->set_extension(extension()); |
| 100 return utils::RunFunctionAndReturnError(function, args, browser_context()); |
| 101 } |
| 102 |
| 103 void ExtensionApiUnittest::RunFunction(UIThreadExtensionFunction* function, |
| 104 const std::string& args) { |
| 105 RunFunctionAndReturnValue(function, args); |
| 106 } |
| 107 |
| 108 } // namespace extensions |
OLD | NEW |