| 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 #include "extensions/browser/api_unittest.h" | 5 #include "extensions/browser/api_unittest.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "components/user_prefs/user_prefs.h" | 8 #include "components/user_prefs/user_prefs.h" |
| 9 #include "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "extensions/common/manifest.h" | 25 #include "extensions/common/manifest.h" |
| 26 #include "extensions/common/manifest_handlers/background_info.h" | 26 #include "extensions/common/manifest_handlers/background_info.h" |
| 27 #include "extensions/common/value_builder.h" | 27 #include "extensions/common/value_builder.h" |
| 28 | 28 |
| 29 namespace utils = extensions::api_test_utils; | 29 namespace utils = extensions::api_test_utils; |
| 30 | 30 |
| 31 namespace extensions { | 31 namespace extensions { |
| 32 | 32 |
| 33 ApiUnitTest::ApiUnitTest() | 33 ApiUnitTest::ApiUnitTest() |
| 34 : notification_service_(content::NotificationService::Create()) { | 34 : notification_service_(content::NotificationService::Create()) { |
| 35 extensions_browser_client()->set_extension_system_factory( |
| 36 &extension_system_factory_); |
| 35 } | 37 } |
| 36 | 38 |
| 37 ApiUnitTest::~ApiUnitTest() { | 39 ApiUnitTest::~ApiUnitTest() { |
| 38 } | 40 } |
| 39 | 41 |
| 40 void ApiUnitTest::SetUp() { | 42 void ApiUnitTest::SetUp() { |
| 41 ExtensionsTest::SetUp(); | 43 ExtensionsTest::SetUp(); |
| 42 extensions_browser_client()->set_extension_system_factory( | |
| 43 &extension_system_factory_); | |
| 44 | 44 |
| 45 thread_bundle_.reset(new content::TestBrowserThreadBundle( | 45 thread_bundle_.reset(new content::TestBrowserThreadBundle( |
| 46 content::TestBrowserThreadBundle::DEFAULT)); | 46 content::TestBrowserThreadBundle::DEFAULT)); |
| 47 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_); | 47 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_); |
| 48 | 48 |
| 49 extension_ = ExtensionBuilder() | 49 extension_ = ExtensionBuilder() |
| 50 .SetManifest(DictionaryBuilder().Set("name", "Test").Set( | 50 .SetManifest(DictionaryBuilder().Set("name", "Test").Set( |
| 51 "version", "1.0")) | 51 "version", "1.0")) |
| 52 .SetLocation(Manifest::UNPACKED) | 52 .SetLocation(Manifest::UNPACKED) |
| 53 .Build(); | 53 .Build(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void ApiUnitTest::CreateBackgroundPage() { |
| 57 if (!contents_) { |
| 58 GURL url = BackgroundInfo::GetBackgroundURL(extension()); |
| 59 if (url.is_empty()) |
| 60 url = GURL(url::kAboutBlankURL); |
| 61 content::SiteInstance* site_instance = |
| 62 content::SiteInstance::CreateForURL(browser_context(), url); |
| 63 contents_.reset(content::WebContents::Create( |
| 64 content::WebContents::CreateParams(browser_context(), site_instance))); |
| 65 } |
| 66 } |
| 67 |
| 56 scoped_ptr<base::Value> ApiUnitTest::RunFunctionAndReturnValue( | 68 scoped_ptr<base::Value> ApiUnitTest::RunFunctionAndReturnValue( |
| 57 UIThreadExtensionFunction* function, | 69 UIThreadExtensionFunction* function, |
| 58 const std::string& args) { | 70 const std::string& args) { |
| 59 function->set_extension(extension()); | 71 function->set_extension(extension()); |
| 72 if (contents_) |
| 73 function->SetRenderViewHost(contents_->GetRenderViewHost()); |
| 60 return scoped_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult( | 74 return scoped_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult( |
| 61 function, args, browser_context())); | 75 function, args, browser_context())); |
| 62 } | 76 } |
| 63 | 77 |
| 64 scoped_ptr<base::DictionaryValue> ApiUnitTest::RunFunctionAndReturnDictionary( | 78 scoped_ptr<base::DictionaryValue> ApiUnitTest::RunFunctionAndReturnDictionary( |
| 65 UIThreadExtensionFunction* function, | 79 UIThreadExtensionFunction* function, |
| 66 const std::string& args) { | 80 const std::string& args) { |
| 67 base::Value* value = RunFunctionAndReturnValue(function, args).release(); | 81 base::Value* value = RunFunctionAndReturnValue(function, args).release(); |
| 68 base::DictionaryValue* dict = NULL; | 82 base::DictionaryValue* dict = NULL; |
| 69 | 83 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 88 // We expect to either have successfuly retrieved a list from the value, | 102 // We expect to either have successfuly retrieved a list from the value, |
| 89 // or the value to have been NULL. | 103 // or the value to have been NULL. |
| 90 EXPECT_TRUE(list || !value); | 104 EXPECT_TRUE(list || !value); |
| 91 return scoped_ptr<base::ListValue>(list); | 105 return scoped_ptr<base::ListValue>(list); |
| 92 } | 106 } |
| 93 | 107 |
| 94 std::string ApiUnitTest::RunFunctionAndReturnError( | 108 std::string ApiUnitTest::RunFunctionAndReturnError( |
| 95 UIThreadExtensionFunction* function, | 109 UIThreadExtensionFunction* function, |
| 96 const std::string& args) { | 110 const std::string& args) { |
| 97 function->set_extension(extension()); | 111 function->set_extension(extension()); |
| 112 if (contents_) |
| 113 function->SetRenderViewHost(contents_->GetRenderViewHost()); |
| 98 return utils::RunFunctionAndReturnError(function, args, browser_context()); | 114 return utils::RunFunctionAndReturnError(function, args, browser_context()); |
| 99 } | 115 } |
| 100 | 116 |
| 101 void ApiUnitTest::RunFunction(UIThreadExtensionFunction* function, | 117 void ApiUnitTest::RunFunction(UIThreadExtensionFunction* function, |
| 102 const std::string& args) { | 118 const std::string& args) { |
| 103 RunFunctionAndReturnValue(function, args); | 119 RunFunctionAndReturnValue(function, args); |
| 104 } | 120 } |
| 105 | 121 |
| 106 } // namespace extensions | 122 } // namespace extensions |
| OLD | NEW |