OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/renderer/api_bindings_system.h" | 5 #include "extensions/renderer/api_bindings_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 const std::string& expected_arguments) { | 167 const std::string& expected_arguments) { |
168 ASSERT_TRUE(last_request()); | 168 ASSERT_TRUE(last_request()); |
169 // Note that even if no arguments are provided by the API call, we should | 169 // Note that even if no arguments are provided by the API call, we should |
170 // have an empty list. | 170 // have an empty list. |
171 ASSERT_TRUE(last_request()->arguments); | 171 ASSERT_TRUE(last_request()->arguments); |
172 EXPECT_EQ(expected_name, last_request()->method_name); | 172 EXPECT_EQ(expected_name, last_request()->method_name); |
173 EXPECT_EQ(ReplaceSingleQuotes(expected_arguments), | 173 EXPECT_EQ(ReplaceSingleQuotes(expected_arguments), |
174 ValueToString(*last_request()->arguments)); | 174 ValueToString(*last_request()->arguments)); |
175 } | 175 } |
176 | 176 |
177 void APIBindingsSystemTest::CallFunctionOnObject( | 177 v8::Local<v8::Value> APIBindingsSystemTest::CallFunctionOnObject( |
178 v8::Local<v8::Context> context, | 178 v8::Local<v8::Context> context, |
179 v8::Local<v8::Object> object, | 179 v8::Local<v8::Object> object, |
180 const std::string& script_source) { | 180 const std::string& script_source) { |
181 std::string wrapped_script_source = | 181 std::string wrapped_script_source = |
182 base::StringPrintf("(function(obj) { %s })", script_source.c_str()); | 182 base::StringPrintf("(function(obj) { %s })", script_source.c_str()); |
183 | 183 |
184 v8::Local<v8::Function> func = | 184 v8::Local<v8::Function> func = |
185 FunctionFromString(context, wrapped_script_source); | 185 FunctionFromString(context, wrapped_script_source); |
186 ASSERT_FALSE(func.IsEmpty()); | 186 // Use ADD_FAILURE() to avoid messing up the return type with ASSERT. |
| 187 if (func.IsEmpty()) { |
| 188 ADD_FAILURE() << script_source; |
| 189 return v8::Local<v8::Value>(); |
| 190 } |
187 | 191 |
188 v8::Local<v8::Value> argv[] = {object}; | 192 v8::Local<v8::Value> argv[] = {object}; |
189 RunFunction(func, context, 1, argv); | 193 return RunFunction(func, context, 1, argv); |
190 } | 194 } |
191 | 195 |
192 // Tests API object initialization, calling a method on the supplied APIs, and | 196 // Tests API object initialization, calling a method on the supplied APIs, and |
193 // triggering the callback for the request. | 197 // triggering the callback for the request. |
194 TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) { | 198 TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) { |
195 v8::HandleScope handle_scope(isolate()); | 199 v8::HandleScope handle_scope(isolate()); |
196 v8::Local<v8::Context> context = MainContext(); | 200 v8::Local<v8::Context> context = MainContext(); |
197 | 201 |
198 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 202 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
199 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 203 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 { | 416 { |
413 // Test a simple call -> response. | 417 // Test a simple call -> response. |
414 const char kTestCall[] = "obj.functionWithExternalRef({prop1: 'foo'});"; | 418 const char kTestCall[] = "obj.functionWithExternalRef({prop1: 'foo'});"; |
415 CallFunctionOnObject(context, gamma_api, kTestCall); | 419 CallFunctionOnObject(context, gamma_api, kTestCall); |
416 ValidateLastRequest("gamma.functionWithExternalRef", "[{'prop1':'foo'}]"); | 420 ValidateLastRequest("gamma.functionWithExternalRef", "[{'prop1':'foo'}]"); |
417 reset_last_request(); | 421 reset_last_request(); |
418 } | 422 } |
419 } | 423 } |
420 | 424 |
421 } // namespace extensions | 425 } // namespace extensions |
OLD | NEW |