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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
// Copyright (c) 2012 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 "chrome/browser/extensions/extension_function_test_utils.h"
#include <string>
#include "base/files/file_path.h"
#include "base/json/json_reader.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "components/crx_file/id_util.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_function_dispatcher.h"
#include "extensions/common/extension.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::WebContents;
using extensions::Extension;
using extensions::Manifest;
namespace keys = extensions::tabs_constants;
namespace {
class TestFunctionDispatcherDelegate
: public extensions::ExtensionFunctionDispatcher::Delegate {
public:
explicit TestFunctionDispatcherDelegate(Browser* browser) :
browser_(browser) {}
~TestFunctionDispatcherDelegate() override {}
private:
extensions::WindowController* GetExtensionWindowController() const override {
return browser_->extension_window_controller();
}
WebContents* GetAssociatedWebContents() const override { return NULL; }
Browser* browser_;
};
} // namespace
namespace extension_function_test_utils {
base::Value* ParseJSON(const std::string& data) {
return base::JSONReader::Read(data);
}
base::ListValue* ParseList(const std::string& data) {
base::Value* result = ParseJSON(data);
base::ListValue* list = NULL;
result->GetAsList(&list);
return list;
}
base::DictionaryValue* ToDictionary(base::Value* val) {
EXPECT_TRUE(val);
EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
return static_cast<base::DictionaryValue*>(val);
}
base::ListValue* ToList(base::Value* val) {
EXPECT_TRUE(val);
EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
return static_cast<base::ListValue*>(val);
}
scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
Manifest::Location location) {
scoped_ptr<base::DictionaryValue> test_extension_value(
extensions::api_test_utils::ParseDictionary(
"{\"name\": \"Test\", \"version\": \"1.0\"}"));
return extensions::api_test_utils::CreateExtension(
location, test_extension_value.get(), std::string());
}
bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
std::string result;
if (val->GetString(keys::kUrlKey, &result) ||
val->GetString(keys::kTitleKey, &result) ||
val->GetString(keys::kFaviconUrlKey, &result))
return true;
return false;
}
std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
const std::string& args,
Browser* browser) {
return RunFunctionAndReturnError(function, args, browser, NONE);
}
std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
const std::string& args,
Browser* browser,
RunFunctionFlags flags) {
scoped_refptr<ExtensionFunction> function_owner(function);
// Without a callback the function will not generate a result.
function->set_has_callback(true);
RunFunction(function, args, browser, flags);
EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
return function->GetError();
}
base::Value* RunFunctionAndReturnSingleResult(
UIThreadExtensionFunction* function,
const std::string& args,
Browser* browser) {
return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
}
base::Value* RunFunctionAndReturnSingleResult(
UIThreadExtensionFunction* function,
const std::string& args,
Browser* browser,
RunFunctionFlags flags) {
scoped_refptr<ExtensionFunction> function_owner(function);
// Without a callback the function will not generate a result.
function->set_has_callback(true);
RunFunction(function, args, browser, flags);
EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
<< function->GetError();
const base::Value* single_result = NULL;
if (function->GetResultList() != NULL &&
function->GetResultList()->Get(0, &single_result)) {
return single_result->DeepCopy();
}
return NULL;
}
// This helps us be able to wait until an UIThreadExtensionFunction calls
// SendResponse.
class SendResponseDelegate
: public UIThreadExtensionFunction::DelegateForTests {
public:
SendResponseDelegate() : should_post_quit_(false) {}
virtual ~SendResponseDelegate() {}
void set_should_post_quit(bool should_quit) {
should_post_quit_ = should_quit;
}
bool HasResponse() {
return response_.get() != NULL;
}
bool GetResponse() {
EXPECT_TRUE(HasResponse());
return *response_.get();
}
void OnSendResponse(UIThreadExtensionFunction* function,
bool success,
bool bad_message) override {
ASSERT_FALSE(bad_message);
ASSERT_FALSE(HasResponse());
response_.reset(new bool);
*response_ = success;
if (should_post_quit_) {
base::MessageLoopForUI::current()->Quit();
}
}
private:
scoped_ptr<bool> response_;
bool should_post_quit_;
};
bool RunFunction(UIThreadExtensionFunction* function,
const std::string& args,
Browser* browser,
RunFunctionFlags flags) {
scoped_ptr<base::ListValue> parsed_args(ParseList(args));
EXPECT_TRUE(parsed_args.get())
<< "Could not parse extension function arguments: " << args;
return RunFunction(function, parsed_args.Pass(), browser, flags);
}
bool RunFunction(UIThreadExtensionFunction* function,
scoped_ptr<base::ListValue> args,
Browser* browser,
RunFunctionFlags flags) {
TestFunctionDispatcherDelegate dispatcher_delegate(browser);
scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
new extensions::ExtensionFunctionDispatcher(browser->profile(),
&dispatcher_delegate));
// TODO(yoz): The cast is a hack; these flags should be defined in
// only one place. See crbug.com/394840.
return extensions::api_test_utils::RunFunction(
function,
args.Pass(),
browser->profile(),
dispatcher.Pass(),
static_cast<extensions::api_test_utils::RunFunctionFlags>(flags));
}
} // namespace extension_function_test_utils
|