summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api_unittest.cc
blob: f0c0f614e8f1385dced0a3384237401f08a85d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api_unittest.h"

#include "base/values.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/test_extensions_browser_client.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/value_builder.h"

namespace utils = extensions::api_test_utils;

namespace extensions {

ApiUnitTest::ApiUnitTest()
    : notification_service_(content::NotificationService::Create()) {
}

ApiUnitTest::~ApiUnitTest() {
}

void ApiUnitTest::SetUp() {
  ExtensionsTest::SetUp();

  thread_bundle_.reset(new content::TestBrowserThreadBundle(
      content::TestBrowserThreadBundle::DEFAULT));
  user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_);

  extension_ =
      ExtensionBuilder()
          .SetManifest(std::move(
              DictionaryBuilder().Set("name", "Test").Set("version", "1.0")))
          .SetLocation(Manifest::UNPACKED)
          .Build();
}

void ApiUnitTest::CreateBackgroundPage() {
  if (!contents_) {
    GURL url = BackgroundInfo::GetBackgroundURL(extension());
    if (url.is_empty())
      url = GURL(url::kAboutBlankURL);
    content::SiteInstance* site_instance =
        content::SiteInstance::CreateForURL(browser_context(), url);
    contents_.reset(content::WebContents::Create(
        content::WebContents::CreateParams(browser_context(), site_instance)));
  }
}

scoped_ptr<base::Value> ApiUnitTest::RunFunctionAndReturnValue(
    UIThreadExtensionFunction* function,
    const std::string& args) {
  function->set_extension(extension());
  if (contents_)
    function->SetRenderFrameHost(contents_->GetMainFrame());
  return scoped_ptr<base::Value>(utils::RunFunctionAndReturnSingleResult(
      function, args, browser_context()));
}

scoped_ptr<base::DictionaryValue> ApiUnitTest::RunFunctionAndReturnDictionary(
    UIThreadExtensionFunction* function,
    const std::string& args) {
  base::Value* value = RunFunctionAndReturnValue(function, args).release();
  base::DictionaryValue* dict = NULL;

  if (value && !value->GetAsDictionary(&dict))
    delete value;

  // We expect to either have successfuly retrieved a dictionary from the value,
  // or the value to have been NULL.
  EXPECT_TRUE(dict || !value);
  return scoped_ptr<base::DictionaryValue>(dict);
}

scoped_ptr<base::ListValue> ApiUnitTest::RunFunctionAndReturnList(
    UIThreadExtensionFunction* function,
    const std::string& args) {
  base::Value* value = RunFunctionAndReturnValue(function, args).release();
  base::ListValue* list = NULL;

  if (value && !value->GetAsList(&list))
    delete value;

  // We expect to either have successfuly retrieved a list from the value,
  // or the value to have been NULL.
  EXPECT_TRUE(list || !value);
  return scoped_ptr<base::ListValue>(list);
}

std::string ApiUnitTest::RunFunctionAndReturnError(
    UIThreadExtensionFunction* function,
    const std::string& args) {
  function->set_extension(extension());
  if (contents_)
    function->SetRenderFrameHost(contents_->GetMainFrame());
  return utils::RunFunctionAndReturnError(function, args, browser_context());
}

void ApiUnitTest::RunFunction(UIThreadExtensionFunction* function,
                              const std::string& args) {
  RunFunctionAndReturnValue(function, args);
}

}  // namespace extensions