OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api_test_utils.h" | 5 #include "extensions/browser/api_test_utils.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "components/crx_file/id_util.h" |
10 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
11 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
12 #include "extensions/browser/extension_function.h" | 13 #include "extensions/browser/extension_function.h" |
13 #include "extensions/browser/extension_function_dispatcher.h" | 14 #include "extensions/browser/extension_function_dispatcher.h" |
14 #include "extensions/common/extension_builder.h" | 15 #include "extensions/common/extension_builder.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using extensions::ExtensionFunctionDispatcher; | 18 using extensions::ExtensionFunctionDispatcher; |
18 | 19 |
19 namespace { | 20 namespace { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 scoped_ptr<bool> response_; | 77 scoped_ptr<bool> response_; |
77 bool should_post_quit_; | 78 bool should_post_quit_; |
78 }; | 79 }; |
79 | 80 |
80 } // namespace | 81 } // namespace |
81 | 82 |
82 namespace extensions { | 83 namespace extensions { |
83 | 84 |
84 namespace api_test_utils { | 85 namespace api_test_utils { |
85 | 86 |
| 87 base::DictionaryValue* ParseDictionary(const std::string& data) { |
| 88 base::Value* result = ParseJSON(data); |
| 89 base::DictionaryValue* dict = NULL; |
| 90 result->GetAsDictionary(&dict); |
| 91 return dict; |
| 92 } |
| 93 |
| 94 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) { |
| 95 bool result = false; |
| 96 if (!val->GetBoolean(key, &result)) |
| 97 ADD_FAILURE() << key << " does not exist or is not a boolean."; |
| 98 return result; |
| 99 } |
| 100 |
| 101 int GetInteger(const base::DictionaryValue* val, const std::string& key) { |
| 102 int result = 0; |
| 103 if (!val->GetInteger(key, &result)) |
| 104 ADD_FAILURE() << key << " does not exist or is not an integer."; |
| 105 return result; |
| 106 } |
| 107 |
| 108 std::string GetString(const base::DictionaryValue* val, |
| 109 const std::string& key) { |
| 110 std::string result; |
| 111 if (!val->GetString(key, &result)) |
| 112 ADD_FAILURE() << key << " does not exist or is not a string."; |
| 113 return result; |
| 114 } |
| 115 |
| 116 scoped_refptr<Extension> CreateExtension( |
| 117 Manifest::Location location, |
| 118 base::DictionaryValue* test_extension_value, |
| 119 const std::string& id_input) { |
| 120 std::string error; |
| 121 const base::FilePath test_extension_path; |
| 122 std::string id; |
| 123 if (!id_input.empty()) |
| 124 id = crx_file::id_util::GenerateId(id_input); |
| 125 scoped_refptr<Extension> extension( |
| 126 Extension::Create(test_extension_path, location, *test_extension_value, |
| 127 Extension::NO_FLAGS, id, &error)); |
| 128 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error; |
| 129 return extension; |
| 130 } |
| 131 |
| 132 scoped_refptr<Extension> CreateExtension( |
| 133 base::DictionaryValue* test_extension_value) { |
| 134 return CreateExtension(Manifest::INTERNAL, test_extension_value, |
| 135 std::string()); |
| 136 } |
| 137 |
86 base::Value* RunFunctionWithDelegateAndReturnSingleResult( | 138 base::Value* RunFunctionWithDelegateAndReturnSingleResult( |
87 UIThreadExtensionFunction* function, | 139 UIThreadExtensionFunction* function, |
88 const std::string& args, | 140 const std::string& args, |
89 content::BrowserContext* context, | 141 content::BrowserContext* context, |
90 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { | 142 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
91 return RunFunctionWithDelegateAndReturnSingleResult( | 143 return RunFunctionWithDelegateAndReturnSingleResult( |
92 function, args, context, dispatcher.Pass(), NONE); | 144 function, args, context, dispatcher.Pass(), NONE); |
93 } | 145 } |
94 | 146 |
95 base::Value* RunFunctionWithDelegateAndReturnSingleResult( | 147 base::Value* RunFunctionWithDelegateAndReturnSingleResult( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 response_delegate.set_should_post_quit(true); | 248 response_delegate.set_should_post_quit(true); |
197 content::RunMessageLoop(); | 249 content::RunMessageLoop(); |
198 } | 250 } |
199 | 251 |
200 EXPECT_TRUE(response_delegate.HasResponse()); | 252 EXPECT_TRUE(response_delegate.HasResponse()); |
201 return response_delegate.GetResponse(); | 253 return response_delegate.GetResponse(); |
202 } | 254 } |
203 | 255 |
204 } // namespace api_test_utils | 256 } // namespace api_test_utils |
205 } // namespace extensions | 257 } // namespace extensions |
OLD | NEW |