yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 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" |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 12 | #include "content/public/browser/site_instance.h" |
| 13 | #include "content/public/browser/web_contents.h" |
| 14 | #include "content/public/common/content_client.h" |
| 15 | #include "content/public/common/url_constants.h" |
| 16 | #include "content/public/test/test_browser_context.h" |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 17 | #include "content/public/test/web_contents_tester.h" |
| 18 | #include "extensions/browser/api_test_utils.h" |
| 19 | #include "extensions/browser/extension_function.h" |
| 20 | #include "extensions/browser/test_extensions_browser_client.h" |
| 21 | #include "extensions/common/extension.h" |
| 22 | #include "extensions/common/extension_builder.h" |
| 23 | #include "extensions/common/manifest.h" |
| 24 | #include "extensions/common/manifest_handlers/background_info.h" |
| 25 | #include "extensions/common/value_builder.h" |
| 26 | |
| 27 | namespace utils = extensions::api_test_utils; |
| 28 | |
| 29 | namespace extensions { |
| 30 | |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 31 | ApiUnitTest::ApiUnitTest() {} |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 32 | |
rockot | 8cba036 | 2016-08-09 21:43:43 | [diff] [blame] | 33 | ApiUnitTest::~ApiUnitTest() {} |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 34 | |
| 35 | void ApiUnitTest::SetUp() { |
| 36 | ExtensionsTest::SetUp(); |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 37 | |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 38 | user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_); |
| 39 | |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 40 | extension_ = ExtensionBuilder("Test").Build(); |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 41 | } |
| 42 | |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 43 | void ApiUnitTest::TearDown() { |
| 44 | extension_ = nullptr; |
| 45 | contents_.reset(); |
| 46 | ExtensionsTest::TearDown(); |
| 47 | } |
| 48 | |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 49 | void ApiUnitTest::CreateBackgroundPage() { |
| 50 | if (!contents_) { |
| 51 | GURL url = BackgroundInfo::GetBackgroundURL(extension()); |
| 52 | if (url.is_empty()) |
| 53 | url = GURL(url::kAboutBlankURL); |
Erik Chen | bb8e738e | 2018-04-28 14:10:43 | [diff] [blame] | 54 | contents_ = content::WebContents::Create(content::WebContents::CreateParams( |
| 55 | browser_context(), |
| 56 | content::SiteInstance::CreateForURL(browser_context(), url))); |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 60 | std::unique_ptr<base::Value> ApiUnitTest::RunFunctionAndReturnValue( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 61 | ExtensionFunction* function, |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 62 | const std::string& args) { |
| 63 | function->set_extension(extension()); |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 64 | if (contents_) |
rdevlin.cronin | 92503ba | 2015-06-12 17:00:56 | [diff] [blame] | 65 | function->SetRenderFrameHost(contents_->GetMainFrame()); |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 66 | return std::unique_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult( |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 67 | function, args, browser_context())); |
| 68 | } |
| 69 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 70 | std::unique_ptr<base::DictionaryValue> |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 71 | ApiUnitTest::RunFunctionAndReturnDictionary(ExtensionFunction* function, |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 72 | const std::string& args) { |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 73 | base::Value* value = RunFunctionAndReturnValue(function, args).release(); |
| 74 | base::DictionaryValue* dict = NULL; |
| 75 | |
| 76 | if (value && !value->GetAsDictionary(&dict)) |
| 77 | delete value; |
| 78 | |
| 79 | // We expect to either have successfuly retrieved a dictionary from the value, |
| 80 | // or the value to have been NULL. |
| 81 | EXPECT_TRUE(dict || !value); |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 82 | return std::unique_ptr<base::DictionaryValue>(dict); |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 83 | } |
| 84 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 85 | std::unique_ptr<base::ListValue> ApiUnitTest::RunFunctionAndReturnList( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 86 | ExtensionFunction* function, |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 87 | const std::string& args) { |
| 88 | base::Value* value = RunFunctionAndReturnValue(function, args).release(); |
| 89 | base::ListValue* list = NULL; |
| 90 | |
| 91 | if (value && !value->GetAsList(&list)) |
| 92 | delete value; |
| 93 | |
| 94 | // We expect to either have successfuly retrieved a list from the value, |
| 95 | // or the value to have been NULL. |
| 96 | EXPECT_TRUE(list || !value); |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 97 | return std::unique_ptr<base::ListValue>(list); |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 98 | } |
| 99 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 100 | std::string ApiUnitTest::RunFunctionAndReturnError(ExtensionFunction* function, |
| 101 | const std::string& args) { |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 102 | function->set_extension(extension()); |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 103 | if (contents_) |
rdevlin.cronin | 92503ba | 2015-06-12 17:00:56 | [diff] [blame] | 104 | function->SetRenderFrameHost(contents_->GetMainFrame()); |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 105 | return utils::RunFunctionAndReturnError(function, args, browser_context()); |
| 106 | } |
| 107 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 108 | void ApiUnitTest::RunFunction(ExtensionFunction* function, |
yoz | b6272ef | 2014-08-28 02:23:05 | [diff] [blame] | 109 | const std::string& args) { |
| 110 | RunFunctionAndReturnValue(function, args); |
| 111 | } |
| 112 | |
| 113 | } // namespace extensions |