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/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 ApiUnitTest::ApiUnitTest() 34 : notification_service_(content::NotificationService::Create()) { 35 } 36 37 ApiUnitTest::~ApiUnitTest() { 38 } 39 40 void ApiUnitTest::SetUp() { 41 ExtensionsTest::SetUp(); 42 extensions_browser_client()->set_extension_system_factory( 43 &extension_system_factory_); 44 45 thread_bundle_.reset(new content::TestBrowserThreadBundle( 46 content::TestBrowserThreadBundle::DEFAULT)); 47 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_); 48 49 extension_ = ExtensionBuilder() 50 .SetManifest(DictionaryBuilder().Set("name", "Test").Set( 51 "version", "1.0")) 52 .SetLocation(Manifest::UNPACKED) 53 .Build(); 54 } 55 56 scoped_ptr<base::Value> ApiUnitTest::RunFunctionAndReturnValue( 57 UIThreadExtensionFunction* function, 58 const std::string& args) { 59 function->set_extension(extension()); 60 return scoped_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult( 61 function, args, browser_context())); 62 } 63 64 scoped_ptr<base::DictionaryValue> ApiUnitTest::RunFunctionAndReturnDictionary( 65 UIThreadExtensionFunction* function, 66 const std::string& args) { 67 base::Value* value = RunFunctionAndReturnValue(function, args).release(); 68 base::DictionaryValue* dict = NULL; 69 70 if (value && !value->GetAsDictionary(&dict)) 71 delete value; 72 73 // We expect to either have successfuly retrieved a dictionary from the value, 74 // or the value to have been NULL. 75 EXPECT_TRUE(dict || !value); 76 return scoped_ptr<base::DictionaryValue>(dict); 77 } 78 79 scoped_ptr<base::ListValue> ApiUnitTest::RunFunctionAndReturnList( 80 UIThreadExtensionFunction* function, 81 const std::string& args) { 82 base::Value* value = RunFunctionAndReturnValue(function, args).release(); 83 base::ListValue* list = NULL; 84 85 if (value && !value->GetAsList(&list)) 86 delete value; 87 88 // We expect to either have successfuly retrieved a list from the value, 89 // or the value to have been NULL. 90 EXPECT_TRUE(list || !value); 91 return scoped_ptr<base::ListValue>(list); 92 } 93 94 std::string ApiUnitTest::RunFunctionAndReturnError( 95 UIThreadExtensionFunction* function, 96 const std::string& args) { 97 function->set_extension(extension()); 98 return utils::RunFunctionAndReturnError(function, args, browser_context()); 99 } 100 101 void ApiUnitTest::RunFunction(UIThreadExtensionFunction* function, 102 const std::string& args) { 103 RunFunctionAndReturnValue(function, args); 104 } 105 106 } // namespace extensions 107