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 "chrome/browser/extensions/extension_api_unittest.h" 6 7 #include "base/values.h" 8 #include "chrome/browser/extensions/extension_function_test_utils.h" 9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 #include "content/public/browser/web_contents.h" 12 #include "content/public/common/url_constants.h" 13 #include "extensions/browser/extension_function.h" 14 #include "extensions/common/extension.h" 15 #include "extensions/common/manifest.h" 16 #include "extensions/common/manifest_handlers/background_info.h" 17 18 namespace utils = extension_function_test_utils; 19 20 namespace extensions { 21 22 ExtensionApiUnittest::ExtensionApiUnittest() : contents_(NULL) { 23 } 24 25 ExtensionApiUnittest::~ExtensionApiUnittest() { 26 } 27 28 void ExtensionApiUnittest::SetUp() { 29 BrowserWithTestWindowTest::SetUp(); 30 extension_ = utils::CreateEmptyExtensionWithLocation(Manifest::UNPACKED); 31 } 32 33 void ExtensionApiUnittest::CreateBackgroundPage() { 34 if (!contents_) { 35 GURL url = BackgroundInfo::GetBackgroundURL(extension()); 36 if (url.is_empty()) 37 url = GURL(url::kAboutBlankURL); 38 AddTab(browser(), url); 39 contents_ = browser()->tab_strip_model()->GetActiveWebContents(); 40 } 41 } 42 43 scoped_ptr<base::Value> ExtensionApiUnittest::RunFunctionAndReturnValue( 44 UIThreadExtensionFunction* function, const std::string& args) { 45 function->set_extension(extension()); 46 if (contents_) 47 function->SetRenderViewHost(contents_->GetRenderViewHost()); 48 return scoped_ptr<base::Value>( 49 utils::RunFunctionAndReturnSingleResult(function, args, browser())); 50 } 51 52 scoped_ptr<base::DictionaryValue> 53 ExtensionApiUnittest::RunFunctionAndReturnDictionary( 54 UIThreadExtensionFunction* function, const std::string& args) { 55 base::Value* value = RunFunctionAndReturnValue(function, args).release(); 56 base::DictionaryValue* dict = NULL; 57 58 if (value && !value->GetAsDictionary(&dict)) 59 delete value; 60 61 // We expect to either have successfuly retrieved a dictionary from the value, 62 // or the value to have been NULL. 63 EXPECT_TRUE(dict || !value); 64 return scoped_ptr<base::DictionaryValue>(dict); 65 } 66 67 scoped_ptr<base::ListValue> ExtensionApiUnittest::RunFunctionAndReturnList( 68 UIThreadExtensionFunction* function, const std::string& args) { 69 base::Value* value = RunFunctionAndReturnValue(function, args).release(); 70 base::ListValue* list = NULL; 71 72 if (value && !value->GetAsList(&list)) 73 delete value; 74 75 // We expect to either have successfuly retrieved a list from the value, 76 // or the value to have been NULL. 77 EXPECT_TRUE(list); 78 return scoped_ptr<base::ListValue>(list); 79 } 80 81 std::string ExtensionApiUnittest::RunFunctionAndReturnError( 82 UIThreadExtensionFunction* function, const std::string& args) { 83 function->set_extension(extension()); 84 if (contents_) 85 function->SetRenderViewHost(contents_->GetRenderViewHost()); 86 return utils::RunFunctionAndReturnError(function, args, browser()); 87 } 88 89 void ExtensionApiUnittest::RunFunction( 90 UIThreadExtensionFunction* function, const std::string& args) { 91 RunFunctionAndReturnValue(function, args); 92 } 93 94 } // namespace extensions 95