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