| 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()); |
| 365 ScriptExecutionCallbackHelper callbackHelper( | 371 ScriptExecutionCallbackHelper callbackHelper( |
| 366 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); | 372 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); |
| 367 webViewHelper.webView() | 373 webViewHelper.webView() |
| 368 ->mainFrame() | 374 ->mainFrame() |
| 369 ->toWebLocalFrame() | 375 ->toWebLocalFrame() |
| 370 ->requestExecuteScriptAndReturnValue( | 376 ->requestExecuteScriptAndReturnValue( |
| 371 WebScriptSource(WebString("'hello';")), false, &callbackHelper); | 377 WebScriptSource(WebString("'hello';")), false, &callbackHelper); |
| 372 runPendingTasks(); | 378 runPendingTasks(); |
| 373 EXPECT_TRUE(callbackHelper.didComplete()); | 379 EXPECT_TRUE(callbackHelper.didComplete()); |
| 374 EXPECT_EQ("hello", callbackHelper.stringValue()); | 380 EXPECT_EQ("hello", callbackHelper.stringValue()); |
| 375 } | 381 } |
| 376 | 382 |
| 383 TEST_P(ParameterizedWebFrameTest, RequestExecuteScriptWithUserGesture) { |
| 384 registerMockedHttpURLLoad("foo.html"); |
| 385 |
| 386 FrameTestHelpers::WebViewHelper webViewHelper; |
| 387 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 388 |
| 389 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 390 ScriptExecutionCallbackHelper callbackHelper( |
| 391 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); |
| 392 |
| 393 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl(); |
| 394 Document* document = mainFrame->frame()->document(); |
| 395 UserGestureIndicator gesture(DocumentUserGestureToken::create(document)); |
| 396 mainFrame->requestExecuteScriptAndReturnValue( |
| 397 WebScriptSource(WebString("document.body.webkitRequestFullscreen();")), |
| 398 true, &callbackHelper); |
| 399 runPendingTasks(); |
| 400 EXPECT_TRUE(callbackHelper.didComplete()); |
| 401 EXPECT_EQ(document->body(), |
| 402 Fullscreen::currentFullScreenElementFrom(*document)); |
| 403 } |
| 404 |
| 377 TEST_P(ParameterizedWebFrameTest, SuspendedRequestExecuteScript) { | 405 TEST_P(ParameterizedWebFrameTest, SuspendedRequestExecuteScript) { |
| 378 registerMockedHttpURLLoad("foo.html"); | 406 registerMockedHttpURLLoad("foo.html"); |
| 379 registerMockedHttpURLLoad("bar.html"); | 407 registerMockedHttpURLLoad("bar.html"); |
| 380 | 408 |
| 381 FrameTestHelpers::WebViewHelper webViewHelper; | 409 FrameTestHelpers::WebViewHelper webViewHelper; |
| 382 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); | 410 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 383 | 411 |
| 384 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 412 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 385 ScriptExecutionCallbackHelper callbackHelper( | 413 ScriptExecutionCallbackHelper callbackHelper( |
| 386 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); | 414 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 397 EXPECT_FALSE(callbackHelper.didComplete()); | 425 EXPECT_FALSE(callbackHelper.didComplete()); |
| 398 | 426 |
| 399 // If the frame navigates, pending scripts should be removed, but the callback | 427 // If the frame navigates, pending scripts should be removed, but the callback |
| 400 // should always be ran. | 428 // should always be ran. |
| 401 FrameTestHelpers::loadFrame(webViewHelper.webView()->mainFrame(), | 429 FrameTestHelpers::loadFrame(webViewHelper.webView()->mainFrame(), |
| 402 m_baseURL + "bar.html"); | 430 m_baseURL + "bar.html"); |
| 403 EXPECT_TRUE(callbackHelper.didComplete()); | 431 EXPECT_TRUE(callbackHelper.didComplete()); |
| 404 EXPECT_EQ(String(), callbackHelper.stringValue()); | 432 EXPECT_EQ(String(), callbackHelper.stringValue()); |
| 405 } | 433 } |
| 406 | 434 |
| 435 TEST_P(ParameterizedWebFrameTest, RequestExecuteV8Function) { |
| 436 registerMockedHttpURLLoad("foo.html"); |
| 437 |
| 438 FrameTestHelpers::WebViewHelper webViewHelper; |
| 439 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 440 |
| 441 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 442 info.GetReturnValue().Set(v8String(info.GetIsolate(), "hello")); |
| 443 }; |
| 444 |
| 445 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 446 v8::Local<v8::Context> context = |
| 447 webViewHelper.webView()->mainFrame()->mainWorldScriptContext(); |
| 448 ScriptExecutionCallbackHelper callbackHelper(context); |
| 449 v8::Local<v8::Function> function = |
| 450 v8::Function::New(context, callback).ToLocalChecked(); |
| 451 webViewHelper.webView() |
| 452 ->mainFrame() |
| 453 ->toWebLocalFrame() |
| 454 ->requestExecuteV8Function(context, function, |
| 455 v8::Undefined(context->GetIsolate()), 0, |
| 456 nullptr, &callbackHelper); |
| 457 runPendingTasks(); |
| 458 EXPECT_TRUE(callbackHelper.didComplete()); |
| 459 EXPECT_EQ("hello", callbackHelper.stringValue()); |
| 460 } |
| 461 |
| 462 TEST_P(ParameterizedWebFrameTest, RequestExecuteV8FunctionWhileSuspended) { |
| 463 registerMockedHttpURLLoad("foo.html"); |
| 464 |
| 465 FrameTestHelpers::WebViewHelper webViewHelper; |
| 466 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 467 |
| 468 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 469 info.GetReturnValue().Set(v8String(info.GetIsolate(), "hello")); |
| 470 }; |
| 471 |
| 472 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 473 v8::Local<v8::Context> context = |
| 474 webViewHelper.webView()->mainFrame()->mainWorldScriptContext(); |
| 475 |
| 476 // Suspend scheduled tasks so the script doesn't run. |
| 477 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl(); |
| 478 mainFrame->frame()->document()->suspendScheduledTasks(); |
| 479 |
| 480 ScriptExecutionCallbackHelper callbackHelper(context); |
| 481 v8::Local<v8::Function> function = |
| 482 v8::Function::New(context, callback).ToLocalChecked(); |
| 483 mainFrame->requestExecuteV8Function(context, function, |
| 484 v8::Undefined(context->GetIsolate()), 0, |
| 485 nullptr, &callbackHelper); |
| 486 runPendingTasks(); |
| 487 EXPECT_FALSE(callbackHelper.didComplete()); |
| 488 |
| 489 mainFrame->frame()->document()->resumeScheduledTasks(); |
| 490 runPendingTasks(); |
| 491 EXPECT_TRUE(callbackHelper.didComplete()); |
| 492 EXPECT_EQ("hello", callbackHelper.stringValue()); |
| 493 } |
| 494 |
| 495 TEST_P(ParameterizedWebFrameTest, |
| 496 RequestExecuteV8FunctionWhileSuspendedWithUserGesture) { |
| 497 registerMockedHttpURLLoad("foo.html"); |
| 498 |
| 499 FrameTestHelpers::WebViewHelper webViewHelper; |
| 500 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); |
| 501 |
| 502 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 503 info.GetReturnValue().Set(v8::Boolean::New( |
| 504 info.GetIsolate(), UserGestureIndicator::processingUserGesture())); |
| 505 }; |
| 506 |
| 507 // Suspend scheduled tasks so the script doesn't run. |
| 508 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl(); |
| 509 Document* document = mainFrame->frame()->document(); |
| 510 document->suspendScheduledTasks(); |
| 511 |
| 512 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 513 v8::Local<v8::Context> context = |
| 514 webViewHelper.webView()->mainFrame()->mainWorldScriptContext(); |
| 515 |
| 516 std::unique_ptr<UserGestureIndicator> indicator = |
| 517 wrapUnique(new UserGestureIndicator(DocumentUserGestureToken::create( |
| 518 document, UserGestureToken::NewGesture))); |
| 519 ScriptExecutionCallbackHelper callbackHelper(context); |
| 520 v8::Local<v8::Function> function = |
| 521 v8::Function::New(context, callback).ToLocalChecked(); |
| 522 mainFrame->requestExecuteV8Function( |
| 523 mainFrame->mainWorldScriptContext(), function, |
| 524 v8::Undefined(context->GetIsolate()), 0, nullptr, &callbackHelper); |
| 525 |
| 526 runPendingTasks(); |
| 527 EXPECT_FALSE(callbackHelper.didComplete()); |
| 528 |
| 529 mainFrame->frame()->document()->resumeScheduledTasks(); |
| 530 runPendingTasks(); |
| 531 EXPECT_TRUE(callbackHelper.didComplete()); |
| 532 EXPECT_EQ(true, callbackHelper.boolValue()); |
| 533 } |
| 534 |
| 407 TEST_P(ParameterizedWebFrameTest, IframeScriptRemovesSelf) { | 535 TEST_P(ParameterizedWebFrameTest, IframeScriptRemovesSelf) { |
| 408 registerMockedHttpURLLoad("single_iframe.html"); | 536 registerMockedHttpURLLoad("single_iframe.html"); |
| 409 registerMockedHttpURLLoad("visible_iframe.html"); | 537 registerMockedHttpURLLoad("visible_iframe.html"); |
| 410 | 538 |
| 411 FrameTestHelpers::WebViewHelper webViewHelper; | 539 FrameTestHelpers::WebViewHelper webViewHelper; |
| 412 webViewHelper.initializeAndLoad(m_baseURL + "single_iframe.html", true); | 540 webViewHelper.initializeAndLoad(m_baseURL + "single_iframe.html", true); |
| 413 | 541 |
| 414 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 542 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 415 ScriptExecutionCallbackHelper callbackHelper( | 543 ScriptExecutionCallbackHelper callbackHelper( |
| 416 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); | 544 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); |
| (...skipping 9844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10261 webViewHelper.webView()->handleInputEvent(endEvent); | 10389 webViewHelper.webView()->handleInputEvent(endEvent); |
| 10262 webViewHelper.webView()->handleInputEvent(updateEvent); | 10390 webViewHelper.webView()->handleInputEvent(updateEvent); |
| 10263 | 10391 |
| 10264 // Try a full Begin/Update/End cycle. | 10392 // Try a full Begin/Update/End cycle. |
| 10265 webViewHelper.webView()->handleInputEvent(beginEvent); | 10393 webViewHelper.webView()->handleInputEvent(beginEvent); |
| 10266 webViewHelper.webView()->handleInputEvent(updateEvent); | 10394 webViewHelper.webView()->handleInputEvent(updateEvent); |
| 10267 webViewHelper.webView()->handleInputEvent(endEvent); | 10395 webViewHelper.webView()->handleInputEvent(endEvent); |
| 10268 } | 10396 } |
| 10269 | 10397 |
| 10270 } // namespace blink | 10398 } // namespace blink |
| OLD | NEW |