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.
5 #include "extensions/browser/api_unittest.h"
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"
29 namespace utils
= extensions::api_test_utils
;
31 namespace extensions
{
33 ApiUnitTest::ApiUnitTest()
34 : notification_service_(content::NotificationService::Create()) {
37 ApiUnitTest::~ApiUnitTest() {
40 void ApiUnitTest::SetUp() {
41 ExtensionsTest::SetUp();
42 extensions_browser_client()->set_extension_system_factory(
43 &extension_system_factory_
);
45 thread_bundle_
.reset(new content::TestBrowserThreadBundle(
46 content::TestBrowserThreadBundle::DEFAULT
));
47 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_
);
49 extension_
= ExtensionBuilder()
50 .SetManifest(DictionaryBuilder().Set("name", "Test").Set(
52 .SetLocation(Manifest::UNPACKED
)
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()));
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
;
70 if (value
&& !value
->GetAsDictionary(&dict
))
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
);
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
;
85 if (value
&& !value
->GetAsList(&list
))
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
);
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());
101 void ApiUnitTest::RunFunction(UIThreadExtensionFunction
* function
,
102 const std::string
& args
) {
103 RunFunctionAndReturnValue(function
, args
);
106 } // namespace extensions