[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 1 | // 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] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 9 | #include "base/file_path.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 10 | #include "base/json/json_reader.h" |
| 11 | #include "base/values.h" |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 12 | #include "chrome/browser/extensions/extension_function.h" |
| 13 | #include "chrome/browser/extensions/extension_function_dispatcher.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 15 | #include "chrome/common/extensions/extension.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 18 | namespace { |
| 19 | |
| 20 | class 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] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 45 | namespace extension_function_test_utils { |
| 46 | |
| 47 | base::Value* ParseJSON(const std::string& data) { |
| 48 | const bool kAllowTrailingComma = false; |
| 49 | return base::JSONReader::Read(data, kAllowTrailingComma); |
| 50 | } |
| 51 | |
| 52 | base::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 | |
| 60 | base::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 | |
| 69 | bool 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 | |
| 76 | int 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 | |
| 83 | std::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 | |
| 90 | base::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] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 96 | scoped_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] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 112 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 113 | const std::string& args, |
| 114 | Browser* browser) { |
| 115 | return RunFunctionAndReturnError(function, args, browser, NONE); |
| 116 | } |
| 117 | std::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 | |
| 128 | base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
| 129 | const std::string& args, |
| 130 | Browser* browser) { |
| 131 | return RunFunctionAndReturnResult(function, args, browser, NONE); |
| 132 | } |
| 133 | base::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] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 140 | ADD_FAILURE() << "Unexpected error: " << function->GetError(); |
| 141 | if (!function->GetResultValue()) |
| 142 | ADD_FAILURE() << "No result value found"; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 143 | return function->GetResultValue()->DeepCopy(); |
| 144 | } |
| 145 | |
| 146 | void 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] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 151 | if (!parsed_args.get()) { |
| 152 | ADD_FAILURE() << "Could not parse extension function arguments: " << args; |
| 153 | } |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 154 | function->SetArgs(parsed_args.get()); |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame^] | 155 | |
| 156 | TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
| 157 | ExtensionFunctionDispatcher dispatcher( |
| 158 | browser->profile(), &dispatcher_delegate); |
| 159 | function->set_dispatcher(dispatcher.AsWeakPtr()); |
| 160 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 161 | function->set_profile(browser->profile()); |
| 162 | function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| 163 | function->Run(); |
| 164 | } |
| 165 | |
| 166 | } // namespace extension_function_test_utils |