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_request_handler.h" | 5 #include "extensions/renderer/api_request_handler.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/optional.h" | 8 #include "base/optional.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "extensions/renderer/api_binding_test.h" | 10 #include "extensions/renderer/api_binding_test.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 // Tests adding a request to the request handler, and then triggering the | 61 // Tests adding a request to the request handler, and then triggering the |
62 // response. | 62 // response. |
63 TEST_F(APIRequestHandlerTest, AddRequestAndCompleteRequestTest) { | 63 TEST_F(APIRequestHandlerTest, AddRequestAndCompleteRequestTest) { |
64 v8::HandleScope handle_scope(isolate()); | 64 v8::HandleScope handle_scope(isolate()); |
65 v8::Local<v8::Context> context = MainContext(); | 65 v8::Local<v8::Context> context = MainContext(); |
66 | 66 |
67 APIRequestHandler request_handler( | 67 APIRequestHandler request_handler( |
68 base::Bind(&DoNothingWithRequest), | 68 base::Bind(&DoNothingWithRequest), |
69 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 69 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
70 APILastError(APILastError::GetParent())); | 70 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
71 | 71 |
72 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 72 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
73 | 73 |
74 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); | 74 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); |
75 ASSERT_FALSE(function.IsEmpty()); | 75 ASSERT_FALSE(function.IsEmpty()); |
76 | 76 |
77 int request_id = request_handler.StartRequest( | 77 int request_id = request_handler.StartRequest( |
78 context, kMethod, base::MakeUnique<base::ListValue>(), function, | 78 context, kMethod, base::MakeUnique<base::ListValue>(), function, |
79 v8::Local<v8::Function>(), binding::RequestThread::UI); | 79 v8::Local<v8::Function>(), binding::RequestThread::UI); |
80 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), | 80 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), |
(...skipping 14 matching lines...) Expand all Loading... |
95 } | 95 } |
96 | 96 |
97 // Tests that trying to run non-existent or invalided requests is a no-op. | 97 // Tests that trying to run non-existent or invalided requests is a no-op. |
98 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { | 98 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { |
99 v8::HandleScope handle_scope(isolate()); | 99 v8::HandleScope handle_scope(isolate()); |
100 v8::Local<v8::Context> context = MainContext(); | 100 v8::Local<v8::Context> context = MainContext(); |
101 | 101 |
102 APIRequestHandler request_handler( | 102 APIRequestHandler request_handler( |
103 base::Bind(&DoNothingWithRequest), | 103 base::Bind(&DoNothingWithRequest), |
104 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 104 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
105 APILastError(APILastError::GetParent())); | 105 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
106 | 106 |
107 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); | 107 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); |
108 ASSERT_FALSE(function.IsEmpty()); | 108 ASSERT_FALSE(function.IsEmpty()); |
109 | 109 |
110 int request_id = request_handler.StartRequest( | 110 int request_id = request_handler.StartRequest( |
111 context, kMethod, base::MakeUnique<base::ListValue>(), function, | 111 context, kMethod, base::MakeUnique<base::ListValue>(), function, |
112 v8::Local<v8::Function>(), binding::RequestThread::UI); | 112 v8::Local<v8::Function>(), binding::RequestThread::UI); |
113 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), | 113 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), |
114 testing::UnorderedElementsAre(request_id)); | 114 testing::UnorderedElementsAre(request_id)); |
115 | 115 |
(...skipping 15 matching lines...) Expand all Loading... |
131 } | 131 } |
132 | 132 |
133 TEST_F(APIRequestHandlerTest, MultipleRequestsAndContexts) { | 133 TEST_F(APIRequestHandlerTest, MultipleRequestsAndContexts) { |
134 v8::HandleScope handle_scope(isolate()); | 134 v8::HandleScope handle_scope(isolate()); |
135 v8::Local<v8::Context> context_a = MainContext(); | 135 v8::Local<v8::Context> context_a = MainContext(); |
136 v8::Local<v8::Context> context_b = AddContext(); | 136 v8::Local<v8::Context> context_b = AddContext(); |
137 | 137 |
138 APIRequestHandler request_handler( | 138 APIRequestHandler request_handler( |
139 base::Bind(&DoNothingWithRequest), | 139 base::Bind(&DoNothingWithRequest), |
140 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 140 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
141 APILastError(APILastError::GetParent())); | 141 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
142 | 142 |
143 // By having both different arguments and different behaviors in the | 143 // By having both different arguments and different behaviors in the |
144 // callbacks, we can easily verify that the right function is called in the | 144 // callbacks, we can easily verify that the right function is called in the |
145 // right context. | 145 // right context. |
146 v8::Local<v8::Function> function_a = FunctionFromString( | 146 v8::Local<v8::Function> function_a = FunctionFromString( |
147 context_a, "(function(res) { this.result = res + 'alpha'; })"); | 147 context_a, "(function(res) { this.result = res + 'alpha'; })"); |
148 v8::Local<v8::Function> function_b = FunctionFromString( | 148 v8::Local<v8::Function> function_b = FunctionFromString( |
149 context_b, "(function(res) { this.result = res + 'beta'; })"); | 149 context_b, "(function(res) { this.result = res + 'beta'; })"); |
150 | 150 |
151 int request_a = request_handler.StartRequest( | 151 int request_a = request_handler.StartRequest( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 GetStringPropertyFromObject(context_b->Global(), context_b, "result")); | 183 GetStringPropertyFromObject(context_b->Global(), context_b, "result")); |
184 } | 184 } |
185 | 185 |
186 TEST_F(APIRequestHandlerTest, CustomCallbackArguments) { | 186 TEST_F(APIRequestHandlerTest, CustomCallbackArguments) { |
187 v8::HandleScope handle_scope(isolate()); | 187 v8::HandleScope handle_scope(isolate()); |
188 v8::Local<v8::Context> context = MainContext(); | 188 v8::Local<v8::Context> context = MainContext(); |
189 | 189 |
190 APIRequestHandler request_handler( | 190 APIRequestHandler request_handler( |
191 base::Bind(&DoNothingWithRequest), | 191 base::Bind(&DoNothingWithRequest), |
192 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 192 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
193 APILastError(APILastError::GetParent())); | 193 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
194 | 194 |
195 v8::Local<v8::Function> custom_callback = | 195 v8::Local<v8::Function> custom_callback = |
196 FunctionFromString(context, kEchoArgs); | 196 FunctionFromString(context, kEchoArgs); |
197 v8::Local<v8::Function> callback = | 197 v8::Local<v8::Function> callback = |
198 FunctionFromString(context, "(function() {})"); | 198 FunctionFromString(context, "(function() {})"); |
199 ASSERT_FALSE(callback.IsEmpty()); | 199 ASSERT_FALSE(callback.IsEmpty()); |
200 ASSERT_FALSE(custom_callback.IsEmpty()); | 200 ASSERT_FALSE(custom_callback.IsEmpty()); |
201 | 201 |
202 int request_id = request_handler.StartRequest( | 202 int request_id = request_handler.StartRequest( |
203 context, "method", base::MakeUnique<base::ListValue>(), callback, | 203 context, "method", base::MakeUnique<base::ListValue>(), callback, |
(...skipping 26 matching lines...) Expand all Loading... |
230 | 230 |
231 // Test that having a custom callback without an extension-provided callback | 231 // Test that having a custom callback without an extension-provided callback |
232 // doesn't crash. | 232 // doesn't crash. |
233 TEST_F(APIRequestHandlerTest, CustomCallbackArgumentsWithEmptyCallback) { | 233 TEST_F(APIRequestHandlerTest, CustomCallbackArgumentsWithEmptyCallback) { |
234 v8::HandleScope handle_scope(isolate()); | 234 v8::HandleScope handle_scope(isolate()); |
235 v8::Local<v8::Context> context = MainContext(); | 235 v8::Local<v8::Context> context = MainContext(); |
236 | 236 |
237 APIRequestHandler request_handler( | 237 APIRequestHandler request_handler( |
238 base::Bind(&DoNothingWithRequest), | 238 base::Bind(&DoNothingWithRequest), |
239 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 239 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
240 APILastError(APILastError::GetParent())); | 240 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
241 | 241 |
242 v8::Local<v8::Function> custom_callback = | 242 v8::Local<v8::Function> custom_callback = |
243 FunctionFromString(context, kEchoArgs); | 243 FunctionFromString(context, kEchoArgs); |
244 ASSERT_FALSE(custom_callback.IsEmpty()); | 244 ASSERT_FALSE(custom_callback.IsEmpty()); |
245 | 245 |
246 v8::Local<v8::Function> empty_callback; | 246 v8::Local<v8::Function> empty_callback; |
247 int request_id = request_handler.StartRequest( | 247 int request_id = request_handler.StartRequest( |
248 context, "method", base::MakeUnique<base::ListValue>(), empty_callback, | 248 context, "method", base::MakeUnique<base::ListValue>(), empty_callback, |
249 custom_callback, binding::RequestThread::UI); | 249 custom_callback, binding::RequestThread::UI); |
250 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), | 250 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), |
(...skipping 17 matching lines...) Expand all Loading... |
268 } | 268 } |
269 | 269 |
270 // Test user gestures being curried around for API requests. | 270 // Test user gestures being curried around for API requests. |
271 TEST_F(APIRequestHandlerTest, UserGestureTest) { | 271 TEST_F(APIRequestHandlerTest, UserGestureTest) { |
272 v8::HandleScope handle_scope(isolate()); | 272 v8::HandleScope handle_scope(isolate()); |
273 v8::Local<v8::Context> context = MainContext(); | 273 v8::Local<v8::Context> context = MainContext(); |
274 | 274 |
275 APIRequestHandler request_handler( | 275 APIRequestHandler request_handler( |
276 base::Bind(&DoNothingWithRequest), | 276 base::Bind(&DoNothingWithRequest), |
277 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 277 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
278 APILastError(APILastError::GetParent())); | 278 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
279 | 279 |
280 auto callback = [](base::Optional<bool>* ran_with_user_gesture) { | 280 auto callback = [](base::Optional<bool>* ran_with_user_gesture) { |
281 *ran_with_user_gesture = | 281 *ran_with_user_gesture = |
282 blink::WebUserGestureIndicator::IsProcessingUserGestureThreadSafe(); | 282 blink::WebUserGestureIndicator::IsProcessingUserGestureThreadSafe(); |
283 }; | 283 }; |
284 | 284 |
285 // Set up a callback to be used with the request so we can check if a user | 285 // Set up a callback to be used with the request so we can check if a user |
286 // gesture was active. | 286 // gesture was active. |
287 base::Optional<bool> ran_with_user_gesture; | 287 base::Optional<bool> ran_with_user_gesture; |
288 v8::Local<v8::FunctionTemplate> function_template = | 288 v8::Local<v8::FunctionTemplate> function_template = |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 base::Optional<binding::RequestThread> thread; | 332 base::Optional<binding::RequestThread> thread; |
333 auto on_request = [](base::Optional<binding::RequestThread>* thread_out, | 333 auto on_request = [](base::Optional<binding::RequestThread>* thread_out, |
334 std::unique_ptr<APIRequestHandler::Request> request, | 334 std::unique_ptr<APIRequestHandler::Request> request, |
335 v8::Local<v8::Context> context) { | 335 v8::Local<v8::Context> context) { |
336 *thread_out = request->thread; | 336 *thread_out = request->thread; |
337 }; | 337 }; |
338 | 338 |
339 APIRequestHandler request_handler( | 339 APIRequestHandler request_handler( |
340 base::Bind(on_request, &thread), | 340 base::Bind(on_request, &thread), |
341 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), | 341 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)), |
342 APILastError(APILastError::GetParent())); | 342 APILastError(APILastError::GetParent(), APILastError::AddConsoleError())); |
343 | 343 |
344 request_handler.StartRequest( | 344 request_handler.StartRequest( |
345 context, kMethod, base::MakeUnique<base::ListValue>(), | 345 context, kMethod, base::MakeUnique<base::ListValue>(), |
346 v8::Local<v8::Function>(), v8::Local<v8::Function>(), | 346 v8::Local<v8::Function>(), v8::Local<v8::Function>(), |
347 binding::RequestThread::UI); | 347 binding::RequestThread::UI); |
348 ASSERT_TRUE(thread); | 348 ASSERT_TRUE(thread); |
349 EXPECT_EQ(binding::RequestThread::UI, *thread); | 349 EXPECT_EQ(binding::RequestThread::UI, *thread); |
350 thread.reset(); | 350 thread.reset(); |
351 | 351 |
352 request_handler.StartRequest( | 352 request_handler.StartRequest( |
353 context, kMethod, base::MakeUnique<base::ListValue>(), | 353 context, kMethod, base::MakeUnique<base::ListValue>(), |
354 v8::Local<v8::Function>(), v8::Local<v8::Function>(), | 354 v8::Local<v8::Function>(), v8::Local<v8::Function>(), |
355 binding::RequestThread::IO); | 355 binding::RequestThread::IO); |
356 ASSERT_TRUE(thread); | 356 ASSERT_TRUE(thread); |
357 EXPECT_EQ(binding::RequestThread::IO, *thread); | 357 EXPECT_EQ(binding::RequestThread::IO, *thread); |
358 thread.reset(); | 358 thread.reset(); |
359 } | 359 } |
360 | 360 |
361 } // namespace extensions | 361 } // namespace extensions |
OLD | NEW |