Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / extensions / browser / api_unittest.cc
blobd50a98b689b07532ff5570cf404fd45a7e26c199
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();
43 thread_bundle_.reset(new content::TestBrowserThreadBundle(
44 content::TestBrowserThreadBundle::DEFAULT));
45 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_);
47 extension_ = ExtensionBuilder()
48 .SetManifest(DictionaryBuilder().Set("name", "Test").Set(
49 "version", "1.0"))
50 .SetLocation(Manifest::UNPACKED)
51 .Build();
54 void ApiUnitTest::CreateBackgroundPage() {
55 if (!contents_) {
56 GURL url = BackgroundInfo::GetBackgroundURL(extension());
57 if (url.is_empty())
58 url = GURL(url::kAboutBlankURL);
59 content::SiteInstance* site_instance =
60 content::SiteInstance::CreateForURL(browser_context(), url);
61 contents_.reset(content::WebContents::Create(
62 content::WebContents::CreateParams(browser_context(), site_instance)));
66 scoped_ptr<base::Value> ApiUnitTest::RunFunctionAndReturnValue(
67 UIThreadExtensionFunction* function,
68 const std::string& args) {
69 function->set_extension(extension());
70 if (contents_)
71 function->SetRenderFrameHost(contents_->GetMainFrame());
72 return scoped_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult(
73 function, args, browser_context()));
76 scoped_ptr<base::DictionaryValue> ApiUnitTest::RunFunctionAndReturnDictionary(
77 UIThreadExtensionFunction* function,
78 const std::string& args) {
79 base::Value* value = RunFunctionAndReturnValue(function, args).release();
80 base::DictionaryValue* dict = NULL;
82 if (value && !value->GetAsDictionary(&dict))
83 delete value;
85 // We expect to either have successfuly retrieved a dictionary from the value,
86 // or the value to have been NULL.
87 EXPECT_TRUE(dict || !value);
88 return scoped_ptr<base::DictionaryValue>(dict);
91 scoped_ptr<base::ListValue> ApiUnitTest::RunFunctionAndReturnList(
92 UIThreadExtensionFunction* function,
93 const std::string& args) {
94 base::Value* value = RunFunctionAndReturnValue(function, args).release();
95 base::ListValue* list = NULL;
97 if (value && !value->GetAsList(&list))
98 delete value;
100 // We expect to either have successfuly retrieved a list from the value,
101 // or the value to have been NULL.
102 EXPECT_TRUE(list || !value);
103 return scoped_ptr<base::ListValue>(list);
106 std::string ApiUnitTest::RunFunctionAndReturnError(
107 UIThreadExtensionFunction* function,
108 const std::string& args) {
109 function->set_extension(extension());
110 if (contents_)
111 function->SetRenderFrameHost(contents_->GetMainFrame());
112 return utils::RunFunctionAndReturnError(function, args, browser_context());
115 void ApiUnitTest::RunFunction(UIThreadExtensionFunction* function,
116 const std::string& args) {
117 RunFunctionAndReturnValue(function, args);
120 } // namespace extensions