[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 | |
| 9 | #include "base/json/json_reader.h" |
| 10 | #include "base/values.h" |
| 11 | #include "chrome/browser/ui/browser.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace extension_function_test_utils { |
| 15 | |
| 16 | base::Value* ParseJSON(const std::string& data) { |
| 17 | const bool kAllowTrailingComma = false; |
| 18 | return base::JSONReader::Read(data, kAllowTrailingComma); |
| 19 | } |
| 20 | |
| 21 | base::ListValue* ParseList(const std::string& data) { |
| 22 | scoped_ptr<base::Value> result(ParseJSON(data)); |
| 23 | if (result.get() && result->IsType(base::Value::TYPE_LIST)) |
| 24 | return static_cast<base::ListValue*>(result.release()); |
| 25 | else |
| 26 | return NULL; |
| 27 | } |
| 28 | |
| 29 | base::DictionaryValue* ParseDictionary( |
| 30 | const std::string& data) { |
| 31 | scoped_ptr<base::Value> result(ParseJSON(data)); |
| 32 | if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY)) |
| 33 | return static_cast<base::DictionaryValue*>(result.release()); |
| 34 | else |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | bool GetBoolean(base::DictionaryValue* val, const std::string& key) { |
| 39 | bool result = false; |
| 40 | if (!val->GetBoolean(key, &result)) |
| 41 | ADD_FAILURE() << key << " does not exist or is not a boolean."; |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | int GetInteger(base::DictionaryValue* val, const std::string& key) { |
| 46 | int result = 0; |
| 47 | if (!val->GetInteger(key, &result)) |
| 48 | ADD_FAILURE() << key << " does not exist or is not an integer."; |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | std::string GetString(base::DictionaryValue* val, const std::string& key) { |
| 53 | std::string result; |
| 54 | if (!val->GetString(key, &result)) |
| 55 | ADD_FAILURE() << key << " does not exist or is not a string."; |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | base::DictionaryValue* ToDictionary(base::Value* val) { |
| 60 | if (!val || !val->IsType(base::Value::TYPE_DICTIONARY)) |
| 61 | ADD_FAILURE() << "value is null or not a dictionary."; |
| 62 | return static_cast<base::DictionaryValue*>(val); |
| 63 | } |
| 64 | |
| 65 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 66 | const std::string& args, |
| 67 | Browser* browser) { |
| 68 | return RunFunctionAndReturnError(function, args, browser, NONE); |
| 69 | } |
| 70 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 71 | const std::string& args, |
| 72 | Browser* browser, |
| 73 | RunFunctionFlags flags) { |
| 74 | scoped_refptr<ExtensionFunction> function_owner(function); |
| 75 | RunFunction(function, args, browser, flags); |
| 76 | if (function->GetResultValue()) |
| 77 | ADD_FAILURE() << function->GetResult(); |
| 78 | return function->GetError(); |
| 79 | } |
| 80 | |
| 81 | base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
| 82 | const std::string& args, |
| 83 | Browser* browser) { |
| 84 | return RunFunctionAndReturnResult(function, args, browser, NONE); |
| 85 | } |
| 86 | base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
| 87 | const std::string& args, |
| 88 | Browser* browser, |
| 89 | RunFunctionFlags flags) { |
| 90 | scoped_refptr<ExtensionFunction> function_owner(function); |
| 91 | RunFunction(function, args, browser, flags); |
| 92 | if (!function->GetError().empty()) |
| 93 | ADD_FAILURE() << function->GetError(); |
| 94 | return function->GetResultValue()->DeepCopy(); |
| 95 | } |
| 96 | |
| 97 | void RunFunction(UIThreadExtensionFunction* function, |
| 98 | const std::string& args, |
| 99 | Browser* browser, |
| 100 | RunFunctionFlags flags) { |
| 101 | scoped_ptr<base::ListValue> parsed_args(ParseList(args)); |
| 102 | function->SetArgs(parsed_args.get()); |
| 103 | function->set_profile(browser->profile()); |
| 104 | function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| 105 | function->Run(); |
| 106 | } |
| 107 | |
| 108 | } // namespace extension_function_test_utils |