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 "chrome/browser/extensions/extension_api_unittest.h"
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 "extensions/browser/extension_function.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/manifest.h"
15 #include "extensions/common/manifest_handlers/background_info.h"
17 namespace utils
= extension_function_test_utils
;
19 namespace extensions
{
21 ExtensionApiUnittest::ExtensionApiUnittest() : contents_(NULL
) {
24 ExtensionApiUnittest::~ExtensionApiUnittest() {
27 void ExtensionApiUnittest::SetUp() {
28 BrowserWithTestWindowTest::SetUp();
29 extension_
= utils::CreateEmptyExtensionWithLocation(Manifest::UNPACKED
);
32 void ExtensionApiUnittest::CreateBackgroundPage() {
34 AddTab(browser(), BackgroundInfo::GetBackgroundURL(extension()));
35 contents_
= browser()->tab_strip_model()->GetActiveWebContents();
39 scoped_ptr
<base::Value
> ExtensionApiUnittest::RunFunctionAndReturnValue(
40 UIThreadExtensionFunction
* function
, const std::string
& args
) {
41 function
->set_extension(extension());
43 function
->SetRenderViewHost(contents_
->GetRenderViewHost());
44 return scoped_ptr
<base::Value
>(
45 utils::RunFunctionAndReturnSingleResult(function
, args
, browser()));
48 scoped_ptr
<base::DictionaryValue
>
49 ExtensionApiUnittest::RunFunctionAndReturnDictionary(
50 UIThreadExtensionFunction
* function
, const std::string
& args
) {
51 base::Value
* value
= RunFunctionAndReturnValue(function
, args
).release();
52 base::DictionaryValue
* dict
= NULL
;
54 if (value
&& !value
->GetAsDictionary(&dict
))
57 // We expect to either have successfuly retrieved a dictionary from the value,
58 // or the value to have been NULL.
59 EXPECT_TRUE(dict
|| !value
);
60 return scoped_ptr
<base::DictionaryValue
>(dict
);
63 scoped_ptr
<base::ListValue
> ExtensionApiUnittest::RunFunctionAndReturnList(
64 UIThreadExtensionFunction
* function
, const std::string
& args
) {
65 base::Value
* value
= RunFunctionAndReturnValue(function
, args
).release();
66 base::ListValue
* list
= NULL
;
68 if (value
&& !value
->GetAsList(&list
))
71 // We expect to either have successfuly retrieved a list from the value,
72 // or the value to have been NULL.
74 return scoped_ptr
<base::ListValue
>(list
);
77 std::string
ExtensionApiUnittest::RunFunctionAndReturnError(
78 UIThreadExtensionFunction
* function
, const std::string
& args
) {
79 function
->set_extension(extension());
81 function
->SetRenderViewHost(contents_
->GetRenderViewHost());
82 return utils::RunFunctionAndReturnError(function
, args
, browser());
85 void ExtensionApiUnittest::RunFunction(
86 UIThreadExtensionFunction
* function
, const std::string
& args
) {
87 RunFunctionAndReturnValue(function
, args
);
90 } // namespace extensions