| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_function_test_utils.h" | 5 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 base::ListValue* ParseList(const std::string& data) { | 54 base::ListValue* ParseList(const std::string& data) { |
| 55 base::Value* result = ParseJSON(data); | 55 base::Value* result = ParseJSON(data); |
| 56 base::ListValue* list = NULL; | 56 base::ListValue* list = NULL; |
| 57 result->GetAsList(&list); | 57 result->GetAsList(&list); |
| 58 return list; | 58 return list; |
| 59 } | 59 } |
| 60 | 60 |
| 61 base::DictionaryValue* ParseDictionary( | 61 base::DictionaryValue* ParseDictionary( |
| 62 const std::string& data) { | 62 const std::string& data) { |
| 63 base::Value* result = ParseJSON(data); | 63 return extensions::api_test_utils::ParseDictionary(data); |
| 64 base::DictionaryValue* dict = NULL; | |
| 65 result->GetAsDictionary(&dict); | |
| 66 return dict; | |
| 67 } | 64 } |
| 68 | 65 |
| 69 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) { | 66 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) { |
| 70 bool result = false; | 67 return extensions::api_test_utils::GetBoolean(val, key); |
| 71 if (!val->GetBoolean(key, &result)) | |
| 72 ADD_FAILURE() << key << " does not exist or is not a boolean."; | |
| 73 return result; | |
| 74 } | 68 } |
| 75 | 69 |
| 76 int GetInteger(const base::DictionaryValue* val, const std::string& key) { | 70 int GetInteger(const base::DictionaryValue* val, const std::string& key) { |
| 77 int result = 0; | 71 return extensions::api_test_utils::GetInteger(val, key); |
| 78 if (!val->GetInteger(key, &result)) | |
| 79 ADD_FAILURE() << key << " does not exist or is not an integer."; | |
| 80 return result; | |
| 81 } | 72 } |
| 82 | 73 |
| 83 std::string GetString(const base::DictionaryValue* val, | 74 std::string GetString(const base::DictionaryValue* val, |
| 84 const std::string& key) { | 75 const std::string& key) { |
| 85 std::string result; | 76 return extensions::api_test_utils::GetString(val, key); |
| 86 if (!val->GetString(key, &result)) | |
| 87 ADD_FAILURE() << key << " does not exist or is not a string."; | |
| 88 return result; | |
| 89 } | 77 } |
| 90 | 78 |
| 91 base::DictionaryValue* ToDictionary(base::Value* val) { | 79 base::DictionaryValue* ToDictionary(base::Value* val) { |
| 92 EXPECT_TRUE(val); | 80 EXPECT_TRUE(val); |
| 93 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType()); | 81 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType()); |
| 94 return static_cast<base::DictionaryValue*>(val); | 82 return static_cast<base::DictionaryValue*>(val); |
| 95 } | 83 } |
| 96 | 84 |
| 97 base::ListValue* ToList(base::Value* val) { | 85 base::ListValue* ToList(base::Value* val) { |
| 98 EXPECT_TRUE(val); | 86 EXPECT_TRUE(val); |
| 99 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType()); | 87 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType()); |
| 100 return static_cast<base::ListValue*>(val); | 88 return static_cast<base::ListValue*>(val); |
| 101 } | 89 } |
| 102 | 90 |
| 103 scoped_refptr<Extension> CreateEmptyExtensionWithLocation( | 91 scoped_refptr<Extension> CreateEmptyExtensionWithLocation( |
| 104 Manifest::Location location) { | 92 Manifest::Location location) { |
| 105 scoped_ptr<base::DictionaryValue> test_extension_value( | 93 scoped_ptr<base::DictionaryValue> test_extension_value( |
| 106 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}")); | 94 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}")); |
| 107 return CreateExtension(location, test_extension_value.get(), std::string()); | 95 return extensions::api_test_utils::CreateExtension( |
| 96 location, test_extension_value.get(), std::string()); |
| 108 } | 97 } |
| 109 | 98 |
| 110 scoped_refptr<Extension> CreateExtension( | 99 scoped_refptr<Extension> CreateExtension( |
| 111 base::DictionaryValue* test_extension_value) { | 100 base::DictionaryValue* test_extension_value) { |
| 112 return CreateExtension(Manifest::INTERNAL, test_extension_value, | 101 return extensions::api_test_utils::CreateExtension( |
| 113 std::string()); | 102 Manifest::INTERNAL, test_extension_value, std::string()); |
| 114 } | 103 } |
| 115 | 104 |
| 116 scoped_refptr<Extension> CreateExtension( | 105 scoped_refptr<Extension> CreateExtension( |
| 117 Manifest::Location location, | 106 Manifest::Location location, |
| 118 base::DictionaryValue* test_extension_value, | 107 base::DictionaryValue* test_extension_value, |
| 119 const std::string& id_input) { | 108 const std::string& id_input) { |
| 120 std::string error; | 109 return extensions::api_test_utils::CreateExtension( |
| 121 const base::FilePath test_extension_path; | 110 location, test_extension_value, id_input); |
| 122 std::string id; | |
| 123 if (!id_input.empty()) | |
| 124 id = crx_file::id_util::GenerateId(id_input); | |
| 125 scoped_refptr<Extension> extension(Extension::Create( | |
| 126 test_extension_path, | |
| 127 location, | |
| 128 *test_extension_value, | |
| 129 Extension::NO_FLAGS, | |
| 130 id, | |
| 131 &error)); | |
| 132 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error; | |
| 133 return extension; | |
| 134 } | 111 } |
| 135 | 112 |
| 136 bool HasPrivacySensitiveFields(base::DictionaryValue* val) { | 113 bool HasPrivacySensitiveFields(base::DictionaryValue* val) { |
| 137 std::string result; | 114 std::string result; |
| 138 if (val->GetString(keys::kUrlKey, &result) || | 115 if (val->GetString(keys::kUrlKey, &result) || |
| 139 val->GetString(keys::kTitleKey, &result) || | 116 val->GetString(keys::kTitleKey, &result) || |
| 140 val->GetString(keys::kFaviconUrlKey, &result)) | 117 val->GetString(keys::kFaviconUrlKey, &result)) |
| 141 return true; | 118 return true; |
| 142 return false; | 119 return false; |
| 143 } | 120 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 // only one place. See crbug.com/394840. | 222 // only one place. See crbug.com/394840. |
| 246 return extensions::api_test_utils::RunFunction( | 223 return extensions::api_test_utils::RunFunction( |
| 247 function, | 224 function, |
| 248 args.Pass(), | 225 args.Pass(), |
| 249 browser->profile(), | 226 browser->profile(), |
| 250 dispatcher.Pass(), | 227 dispatcher.Pass(), |
| 251 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags)); | 228 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags)); |
| 252 } | 229 } |
| 253 | 230 |
| 254 } // namespace extension_function_test_utils | 231 } // namespace extension_function_test_utils |
| OLD | NEW |