| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 EXPECT_EQ(webViewHelper.webView()->mainFrame()->firstChild(), | 328 EXPECT_EQ(webViewHelper.webView()->mainFrame()->firstChild(), |
| 329 WebLocalFrame::frameForContext(webViewHelper.webView() | 329 WebLocalFrame::frameForContext(webViewHelper.webView() |
| 330 ->mainFrame() | 330 ->mainFrame() |
| 331 ->firstChild() | 331 ->firstChild() |
| 332 ->mainWorldScriptContext())); | 332 ->mainWorldScriptContext())); |
| 333 } | 333 } |
| 334 | 334 |
| 335 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback { | 335 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback { |
| 336 public: | 336 public: |
| 337 explicit ScriptExecutionCallbackHelper(v8::Local<v8::Context> context) | 337 explicit ScriptExecutionCallbackHelper(v8::Local<v8::Context> context) |
| 338 : m_didComplete(false), m_context(context) {} | 338 : m_didComplete(false), m_boolValue(false), m_context(context) {} |
| 339 ~ScriptExecutionCallbackHelper() {} | 339 ~ScriptExecutionCallbackHelper() {} |
| 340 | 340 |
| 341 bool didComplete() const { return m_didComplete; } | 341 bool didComplete() const { return m_didComplete; } |
| 342 const String& stringValue() const { return m_stringValue; } | 342 const String& stringValue() const { return m_stringValue; } |
| 343 bool boolValue() { return m_boolValue; } |
| 343 | 344 |
| 344 private: | 345 private: |
| 345 void completed(const WebVector<v8::Local<v8::Value>>& values) override { | 346 void completed(const WebVector<v8::Local<v8::Value>>& values) override { |
| 346 m_didComplete = true; | 347 m_didComplete = true; |
| 347 if (!values.isEmpty() && values[0]->IsString()) { | 348 if (!values.isEmpty()) { |
| 348 m_stringValue = | 349 if (values[0]->IsString()) { |
| 349 toCoreString(values[0]->ToString(m_context).ToLocalChecked()); | 350 m_stringValue = |
| 351 toCoreString(values[0]->ToString(m_context).ToLocalChecked()); |
| 352 } else if (values[0]->IsBoolean()) { |
| 353 m_boolValue = values[0].As<v8::Boolean>()->Value(); |
| 354 } |
| 350 } | 355 } |
| 351 } | 356 } |
| 352 | 357 |
| 353 bool m_didComplete; | 358 bool m_didComplete; |
| 354 String m_stringValue; | 359 String m_stringValue; |
| 360 bool m_boolValue; |
| 355 v8::Local<v8::Context> m_context; | 361 v8::Local<v8::Context> m_context; |
| 356 }; | 362 }; |
| 357 | 363 |
| 358 TEST_P(ParameterizedWebFrameTest, RequestExecuteScript) { | 364 TEST_P(ParameterizedWebFrameTest, RequestExecuteScript) { |
| 359 registerMockedHttpURLLoad("foo.html"); | 365 registerMockedHttpURLLoad("foo.html"); |
| 360 | 366 |
| 361 FrameTestHelpers::WebViewHelper webViewHelper; | 367 FrameTestHelpers::WebViewHelper webViewHelper; |
| 362 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); | 368 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 363 | 369 |
| 364 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 370 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 EXPECT_FALSE(callbackHelper.didComplete()); | 403 EXPECT_FALSE(callbackHelper.didComplete()); |
| 398 | 404 |
| 399 // If the frame navigates, pending scripts should be removed, but the callback | 405 // If the frame navigates, pending scripts should be removed, but the callback |
| 400 // should always be ran. | 406 // should always be ran. |
| 401 FrameTestHelpers::loadFrame(webViewHelper.webView()->mainFrame(), | 407 FrameTestHelpers::loadFrame(webViewHelper.webView()->mainFrame(), |
| 402 m_baseURL + "bar.html"); | 408 m_baseURL + "bar.html"); |
| 403 EXPECT_TRUE(callbackHelper.didComplete()); | 409 EXPECT_TRUE(callbackHelper.didComplete()); |
| 404 EXPECT_EQ(String(), callbackHelper.stringValue()); | 410 EXPECT_EQ(String(), callbackHelper.stringValue()); |
| 405 } | 411 } |
| 406 | 412 |
| 413 TEST_P(ParameterizedWebFrameTest, RequestExecuteV8Function) { |
| 414 registerMockedHttpURLLoad("foo.html"); |
| 415 |
| 416 FrameTestHelpers::WebViewHelper webViewHelper; |
| 417 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 418 |
| 419 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 420 info.GetReturnValue().Set(v8String(info.GetIsolate(), "hello")); |
| 421 }; |
| 422 |
| 423 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 424 v8::Local<v8::Context> context = |
| 425 webViewHelper.webView()->mainFrame()->mainWorldScriptContext(); |
| 426 ScriptExecutionCallbackHelper callbackHelper(context); |
| 427 v8::Local<v8::Function> function = |
| 428 v8::Function::New(context, callback).ToLocalChecked(); |
| 429 webViewHelper.webView() |
| 430 ->mainFrame() |
| 431 ->toWebLocalFrame() |
| 432 ->requestExecuteV8Function(context, function, |
| 433 v8::Undefined(context->GetIsolate()), 0, |
| 434 nullptr, &callbackHelper); |
| 435 runPendingTasks(); |
| 436 EXPECT_TRUE(callbackHelper.didComplete()); |
| 437 EXPECT_EQ("hello", callbackHelper.stringValue()); |
| 438 } |
| 439 |
| 440 TEST_P(ParameterizedWebFrameTest, RequestExecuteV8FunctionWhileSuspended) { |
| 441 registerMockedHttpURLLoad("foo.html"); |
| 442 |
| 443 FrameTestHelpers::WebViewHelper webViewHelper; |
| 444 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 445 |
| 446 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 447 info.GetReturnValue().Set(v8String(info.GetIsolate(), "hello")); |
| 448 }; |
| 449 |
| 450 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 451 v8::Local<v8::Context> context = |
| 452 webViewHelper.webView()->mainFrame()->mainWorldScriptContext(); |
| 453 |
| 454 // Suspend scheduled tasks so the script doesn't run. |
| 455 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl(); |
| 456 mainFrame->frame()->document()->suspendScheduledTasks(); |
| 457 |
| 458 ScriptExecutionCallbackHelper callbackHelper(context); |
| 459 v8::Local<v8::Function> function = |
| 460 v8::Function::New(context, callback).ToLocalChecked(); |
| 461 mainFrame->requestExecuteV8Function(context, function, |
| 462 v8::Undefined(context->GetIsolate()), 0, |
| 463 nullptr, &callbackHelper); |
| 464 runPendingTasks(); |
| 465 EXPECT_FALSE(callbackHelper.didComplete()); |
| 466 |
| 467 mainFrame->frame()->document()->resumeScheduledTasks(); |
| 468 runPendingTasks(); |
| 469 EXPECT_TRUE(callbackHelper.didComplete()); |
| 470 EXPECT_EQ("hello", callbackHelper.stringValue()); |
| 471 } |
| 472 |
| 473 TEST_P(ParameterizedWebFrameTest, |
| 474 RequestExecuteV8FunctionWhileSuspendedWithUserGesture) { |
| 475 registerMockedHttpURLLoad("foo.html"); |
| 476 |
| 477 FrameTestHelpers::WebViewHelper webViewHelper; |
| 478 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 479 |
| 480 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 481 info.GetReturnValue().Set(v8::Boolean::New( |
| 482 info.GetIsolate(), UserGestureIndicator::processingUserGesture())); |
| 483 }; |
| 484 |
| 485 // Suspend scheduled tasks so the script doesn't run. |
| 486 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl(); |
| 487 Document* document = mainFrame->frame()->document(); |
| 488 document->suspendScheduledTasks(); |
| 489 |
| 490 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 491 v8::Local<v8::Context> context = |
| 492 webViewHelper.webView()->mainFrame()->mainWorldScriptContext(); |
| 493 |
| 494 std::unique_ptr<UserGestureIndicator> indicator = |
| 495 wrapUnique(new UserGestureIndicator(DocumentUserGestureToken::create( |
| 496 document, UserGestureToken::NewGesture))); |
| 497 ScriptExecutionCallbackHelper callbackHelper(context); |
| 498 v8::Local<v8::Function> function = |
| 499 v8::Function::New(context, callback).ToLocalChecked(); |
| 500 mainFrame->requestExecuteV8Function( |
| 501 mainFrame->mainWorldScriptContext(), function, |
| 502 v8::Undefined(context->GetIsolate()), 0, nullptr, &callbackHelper); |
| 503 |
| 504 runPendingTasks(); |
| 505 EXPECT_FALSE(callbackHelper.didComplete()); |
| 506 |
| 507 mainFrame->frame()->document()->resumeScheduledTasks(); |
| 508 runPendingTasks(); |
| 509 EXPECT_TRUE(callbackHelper.didComplete()); |
| 510 EXPECT_EQ(true, callbackHelper.boolValue()); |
| 511 } |
| 512 |
| 407 TEST_P(ParameterizedWebFrameTest, IframeScriptRemovesSelf) { | 513 TEST_P(ParameterizedWebFrameTest, IframeScriptRemovesSelf) { |
| 408 registerMockedHttpURLLoad("single_iframe.html"); | 514 registerMockedHttpURLLoad("single_iframe.html"); |
| 409 registerMockedHttpURLLoad("visible_iframe.html"); | 515 registerMockedHttpURLLoad("visible_iframe.html"); |
| 410 | 516 |
| 411 FrameTestHelpers::WebViewHelper webViewHelper; | 517 FrameTestHelpers::WebViewHelper webViewHelper; |
| 412 webViewHelper.initializeAndLoad(m_baseURL + "single_iframe.html", true); | 518 webViewHelper.initializeAndLoad(m_baseURL + "single_iframe.html", true); |
| 413 | 519 |
| 414 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 520 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 415 ScriptExecutionCallbackHelper callbackHelper( | 521 ScriptExecutionCallbackHelper callbackHelper( |
| 416 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); | 522 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); |
| (...skipping 9844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10261 webViewHelper.webView()->handleInputEvent(endEvent); | 10367 webViewHelper.webView()->handleInputEvent(endEvent); |
| 10262 webViewHelper.webView()->handleInputEvent(updateEvent); | 10368 webViewHelper.webView()->handleInputEvent(updateEvent); |
| 10263 | 10369 |
| 10264 // Try a full Begin/Update/End cycle. | 10370 // Try a full Begin/Update/End cycle. |
| 10265 webViewHelper.webView()->handleInputEvent(beginEvent); | 10371 webViewHelper.webView()->handleInputEvent(beginEvent); |
| 10266 webViewHelper.webView()->handleInputEvent(updateEvent); | 10372 webViewHelper.webView()->handleInputEvent(updateEvent); |
| 10267 webViewHelper.webView()->handleInputEvent(endEvent); | 10373 webViewHelper.webView()->handleInputEvent(endEvent); |
| 10268 } | 10374 } |
| 10269 | 10375 |
| 10270 } // namespace blink | 10376 } // namespace blink |
| OLD | NEW |