blob: 7c0db6d29e56237c3e09cdf34e613d928fb5ca72 [file] [log] [blame]
[email protected]637bf322011-10-01 20:46:321// Copyright (c) 2011 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.
4
5#include "chrome/browser/extensions/extension_function_test_utils.h"
6
7#include <string>
8
[email protected]4e3ce3b2011-10-14 23:25:179#include "base/file_path.h"
[email protected]637bf322011-10-01 20:46:3210#include "base/json/json_reader.h"
11#include "base/values.h"
[email protected]4e3ce3b2011-10-14 23:25:1712#include "chrome/browser/extensions/extension_function.h"
13#include "chrome/browser/extensions/extension_function_dispatcher.h"
[email protected]637bf322011-10-01 20:46:3214#include "chrome/browser/ui/browser.h"
[email protected]4e3ce3b2011-10-14 23:25:1715#include "chrome/common/extensions/extension.h"
[email protected]637bf322011-10-01 20:46:3216#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]4e3ce3b2011-10-14 23:25:1718namespace {
19
20class TestFunctionDispatcherDelegate
21 : public ExtensionFunctionDispatcher::Delegate {
22 public:
23 explicit TestFunctionDispatcherDelegate(Browser* browser) :
24 browser_(browser) {}
25 virtual ~TestFunctionDispatcherDelegate() {}
26
27 private:
28 virtual Browser* GetBrowser() OVERRIDE {
29 return browser_;
30 }
31
32 virtual gfx::NativeView GetNativeViewOfHost() OVERRIDE {
33 return NULL;
34 }
35
36 virtual TabContents* GetAssociatedTabContents() const OVERRIDE {
37 return NULL;
38 }
39
40 Browser* browser_;
41};
42
43} // namespace
44
[email protected]637bf322011-10-01 20:46:3245namespace extension_function_test_utils {
46
47base::Value* ParseJSON(const std::string& data) {
48 const bool kAllowTrailingComma = false;
49 return base::JSONReader::Read(data, kAllowTrailingComma);
50}
51
52base::ListValue* ParseList(const std::string& data) {
53 scoped_ptr<base::Value> result(ParseJSON(data));
54 if (result.get() && result->IsType(base::Value::TYPE_LIST))
55 return static_cast<base::ListValue*>(result.release());
56 else
57 return NULL;
58}
59
60base::DictionaryValue* ParseDictionary(
61 const std::string& data) {
62 scoped_ptr<base::Value> result(ParseJSON(data));
63 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY))
64 return static_cast<base::DictionaryValue*>(result.release());
65 else
66 return NULL;
67}
68
69bool GetBoolean(base::DictionaryValue* val, const std::string& key) {
70 bool result = false;
71 if (!val->GetBoolean(key, &result))
72 ADD_FAILURE() << key << " does not exist or is not a boolean.";
73 return result;
74}
75
76int GetInteger(base::DictionaryValue* val, const std::string& key) {
77 int result = 0;
78 if (!val->GetInteger(key, &result))
79 ADD_FAILURE() << key << " does not exist or is not an integer.";
80 return result;
81}
82
83std::string GetString(base::DictionaryValue* val, const std::string& key) {
84 std::string result;
85 if (!val->GetString(key, &result))
86 ADD_FAILURE() << key << " does not exist or is not a string.";
87 return result;
88}
89
90base::DictionaryValue* ToDictionary(base::Value* val) {
91 if (!val || !val->IsType(base::Value::TYPE_DICTIONARY))
92 ADD_FAILURE() << "value is null or not a dictionary.";
93 return static_cast<base::DictionaryValue*>(val);
94}
95
[email protected]4e3ce3b2011-10-14 23:25:1796scoped_refptr<Extension> CreateEmptyExtension() {
97 std::string error;
98 const FilePath test_extension_path;
99 scoped_ptr<base::DictionaryValue> test_extension_value(
100 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
101 scoped_refptr<Extension> extension(Extension::Create(
102 test_extension_path,
103 Extension::INTERNAL,
104 *test_extension_value.get(),
105 Extension::NO_FLAGS,
106 &error));
107 if (!error.empty())
108 ADD_FAILURE() << "Could not parse test extension " << error;
109 return extension;
110}
111
[email protected]637bf322011-10-01 20:46:32112std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
113 const std::string& args,
114 Browser* browser) {
115 return RunFunctionAndReturnError(function, args, browser, NONE);
116}
117std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
118 const std::string& args,
119 Browser* browser,
120 RunFunctionFlags flags) {
121 scoped_refptr<ExtensionFunction> function_owner(function);
122 RunFunction(function, args, browser, flags);
123 if (function->GetResultValue())
124 ADD_FAILURE() << function->GetResult();
125 return function->GetError();
126}
127
128base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
129 const std::string& args,
130 Browser* browser) {
131 return RunFunctionAndReturnResult(function, args, browser, NONE);
132}
133base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
134 const std::string& args,
135 Browser* browser,
136 RunFunctionFlags flags) {
137 scoped_refptr<ExtensionFunction> function_owner(function);
138 RunFunction(function, args, browser, flags);
139 if (!function->GetError().empty())
[email protected]4e3ce3b2011-10-14 23:25:17140 ADD_FAILURE() << "Unexpected error: " << function->GetError();
141 if (!function->GetResultValue())
142 ADD_FAILURE() << "No result value found";
[email protected]637bf322011-10-01 20:46:32143 return function->GetResultValue()->DeepCopy();
144}
145
146void RunFunction(UIThreadExtensionFunction* function,
147 const std::string& args,
148 Browser* browser,
149 RunFunctionFlags flags) {
150 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
[email protected]4e3ce3b2011-10-14 23:25:17151 if (!parsed_args.get()) {
152 ADD_FAILURE() << "Could not parse extension function arguments: " << args;
153 }
[email protected]637bf322011-10-01 20:46:32154 function->SetArgs(parsed_args.get());
[email protected]4e3ce3b2011-10-14 23:25:17155
156 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
157 ExtensionFunctionDispatcher dispatcher(
158 browser->profile(), &dispatcher_delegate);
159 function->set_dispatcher(dispatcher.AsWeakPtr());
160
[email protected]637bf322011-10-01 20:46:32161 function->set_profile(browser->profile());
162 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
163 function->Run();
164}
165
166} // namespace extension_function_test_utils