OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 var embedder = {}; | 5 var embedder = {}; |
6 | 6 |
7 // TODO(lfg) Move these functions to a common js. | 7 // TODO(lfg) Move these functions to a common js. |
8 window.runTest = function(testName) { | 8 window.runTest = function(testName) { |
9 if (!embedder.test.testList[testName]) { | 9 if (!embedder.test.testList[testName]) { |
10 window.console.warn('Incorrect testName: ' + testName); | 10 window.console.warn('Incorrect testName: ' + testName); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // Check contentWindow. | 104 // Check contentWindow. |
105 embedder.test.assertEq('object', typeof webview.contentWindow); | 105 embedder.test.assertEq('object', typeof webview.contentWindow); |
106 embedder.test.assertEq('function', | 106 embedder.test.assertEq('function', |
107 typeof webview.contentWindow.postMessage); | 107 typeof webview.contentWindow.postMessage); |
108 embedder.test.succeed(); | 108 embedder.test.succeed(); |
109 }); | 109 }); |
110 webview.setAttribute('src', 'data:text/html,webview check api'); | 110 webview.setAttribute('src', 'data:text/html,webview check api'); |
111 document.body.appendChild(webview); | 111 document.body.appendChild(webview); |
112 } | 112 } |
113 | 113 |
| 114 // This test verifies that assigning the src attribute the same value it had |
| 115 // prior to a crash spawns off a new guest process. |
| 116 function testAssignSrcAfterCrash() { |
| 117 var webview = document.createElement('webview'); |
| 118 webview.setAttribute('partition', arguments.callee.name); |
| 119 var terminated = false; |
| 120 webview.addEventListener('loadstop', function(evt) { |
| 121 if (!terminated) { |
| 122 webview.terminate(); |
| 123 return; |
| 124 } |
| 125 // The guest has recovered after being terminated. |
| 126 embedder.test.succeed(); |
| 127 }); |
| 128 webview.addEventListener('exit', function(evt) { |
| 129 terminated = true; |
| 130 webview.setAttribute('src', 'data:text/html,test page'); |
| 131 }); |
| 132 webview.setAttribute('src', 'data:text/html,test page'); |
| 133 document.body.appendChild(webview); |
| 134 } |
| 135 |
114 // Makes sure 'sizechanged' event is fired only if autosize attribute is | 136 // Makes sure 'sizechanged' event is fired only if autosize attribute is |
115 // specified. | 137 // specified. |
116 // After loading <webview> without autosize attribute and a size, say size1, | 138 // After loading <webview> without autosize attribute and a size, say size1, |
117 // we set autosize attribute and new min size with size2. We would get (only | 139 // we set autosize attribute and new min size with size2. We would get (only |
118 // one) sizechanged event with size1 as old size and size2 as new size. | 140 // one) sizechanged event with size1 as old size and size2 as new size. |
119 function testAutosizeAfterNavigation() { | 141 function testAutosizeAfterNavigation() { |
120 var webview = document.createElement('webview'); | 142 var webview = document.createElement('webview'); |
121 | 143 |
122 var step = 1; | 144 var step = 1; |
123 var sizeChangeHandler = function(e) { | 145 var sizeChangeHandler = function(e) { |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 webview.maxheight = 600; | 351 webview.maxheight = 600; |
330 webview.autosize = true; | 352 webview.autosize = true; |
331 }); | 353 }); |
332 | 354 |
333 webview.style.width = '640px'; | 355 webview.style.width = '640px'; |
334 webview.style.height = '480px'; | 356 webview.style.height = '480px'; |
335 webview.setAttribute('src', 'data:text/html,webview check autosize'); | 357 webview.setAttribute('src', 'data:text/html,webview check autosize'); |
336 document.body.appendChild(webview); | 358 document.body.appendChild(webview); |
337 } | 359 } |
338 | 360 |
| 361 // This test registers two event listeners on a same event (loadcommit). |
| 362 // Each of the listener tries to change some properties on the event param, |
| 363 // which should not be possible. |
| 364 function testCannotMutateEventName() { |
| 365 var webview = document.createElement('webview'); |
| 366 var url = 'data:text/html,<body>Two</body>'; |
| 367 var loadCommitACalled = false; |
| 368 var loadCommitBCalled = false; |
| 369 |
| 370 var maybeFinishTest = function(e) { |
| 371 if (loadCommitACalled && loadCommitBCalled) { |
| 372 embedder.test.assertEq('loadcommit', e.type); |
| 373 embedder.test.succeed(); |
| 374 } |
| 375 }; |
| 376 |
| 377 var onLoadCommitA = function(e) { |
| 378 if (e.url == url) { |
| 379 embedder.test.assertEq('loadcommit', e.type); |
| 380 embedder.test.assertTrue(e.isTopLevel); |
| 381 embedder.test.assertFalse(loadCommitACalled); |
| 382 loadCommitACalled = true; |
| 383 // Try mucking with properities inside |e|. |
| 384 e.type = 'modified'; |
| 385 maybeFinishTest(e); |
| 386 } |
| 387 }; |
| 388 var onLoadCommitB = function(e) { |
| 389 if (e.url == url) { |
| 390 embedder.test.assertEq('loadcommit', e.type); |
| 391 embedder.test.assertTrue(e.isTopLevel); |
| 392 embedder.test.assertFalse(loadCommitBCalled); |
| 393 loadCommitBCalled = true; |
| 394 // Try mucking with properities inside |e|. |
| 395 e.type = 'modified'; |
| 396 maybeFinishTest(e); |
| 397 } |
| 398 }; |
| 399 |
| 400 // The test starts from here, by setting the src to |url|. Event |
| 401 // listener registration works because we already have a (dummy) src set |
| 402 // on the <webview> tag. |
| 403 webview.addEventListener('loadcommit', onLoadCommitA); |
| 404 webview.addEventListener('loadcommit', onLoadCommitB); |
| 405 webview.setAttribute('src', url); |
| 406 document.body.appendChild(webview); |
| 407 } |
| 408 |
| 409 // This test verifies that the load event fires when the a new page is |
| 410 // loaded. |
| 411 // TODO(fsamuel): Add a test to verify that subframe loads within a guest |
| 412 // do not fire the 'contentload' event. |
| 413 function testContentLoadEvent() { |
| 414 var webview = document.createElement('webview'); |
| 415 webview.addEventListener('contentload', function(e) { |
| 416 embedder.test.succeed(); |
| 417 }); |
| 418 webview.setAttribute('src', 'data:text/html,trigger navigation'); |
| 419 document.body.appendChild(webview); |
| 420 } |
| 421 |
| 422 // This test registers two listeners on an event (loadcommit) and removes |
| 423 // the <webview> tag when the first listener fires. |
| 424 // Current expected behavior is that the second event listener will still |
| 425 // fire without crashing. |
| 426 function testDestroyOnEventListener() { |
| 427 var webview = document.createElement('webview'); |
| 428 var url = 'data:text/html,<body>Destroy test</body>'; |
| 429 |
| 430 var loadCommitCount = 0; |
| 431 function loadCommitCommon(e) { |
| 432 embedder.test.assertEq('loadcommit', e.type); |
| 433 if (url != e.url) |
| 434 return; |
| 435 ++loadCommitCount; |
| 436 if (loadCommitCount == 2) { |
| 437 // Pass in a timeout so that we can catch if any additional loadcommit |
| 438 // occurs. |
| 439 setTimeout(function() { |
| 440 embedder.test.succeed(); |
| 441 }, 0); |
| 442 } else if (loadCommitCount > 2) { |
| 443 embedder.test.fail(); |
| 444 } |
| 445 }; |
| 446 |
| 447 // The test starts from here, by setting the src to |url|. |
| 448 webview.addEventListener('loadcommit', function(e) { |
| 449 window.console.log('loadcommit1'); |
| 450 webview.parentNode.removeChild(webview); |
| 451 loadCommitCommon(e); |
| 452 }); |
| 453 webview.addEventListener('loadcommit', function(e) { |
| 454 window.console.log('loadcommit2'); |
| 455 loadCommitCommon(e); |
| 456 }); |
| 457 webview.setAttribute('src', url); |
| 458 document.body.appendChild(webview); |
| 459 } |
| 460 |
| 461 // Tests that a <webview> that starts with "display: none" style loads |
| 462 // properly. |
| 463 function testDisplayNoneWebviewLoad() { |
| 464 var webview = document.createElement('webview'); |
| 465 var visible = false; |
| 466 webview.style.display = 'none'; |
| 467 // foobar is a privileged partition according to the manifest file. |
| 468 webview.partition = 'foobar'; |
| 469 webview.addEventListener('loadabort', function(e) { |
| 470 embedder.test.fail(); |
| 471 }); |
| 472 webview.addEventListener('loadstop', function(e) { |
| 473 embedder.test.assertTrue(visible); |
| 474 embedder.test.succeed(); |
| 475 }); |
| 476 // Set the .src while we are "display: none". |
| 477 webview.setAttribute('src', 'about:blank'); |
| 478 document.body.appendChild(webview); |
| 479 |
| 480 setTimeout(function() { |
| 481 visible = true; |
| 482 // This should trigger loadstop. |
| 483 webview.style.display = ''; |
| 484 }, 0); |
| 485 } |
| 486 |
| 487 function testDisplayNoneWebviewRemoveChild() { |
| 488 var webview = document.createElement('webview'); |
| 489 var visibleAndInDOM = false; |
| 490 webview.style.display = 'none'; |
| 491 // foobar is a privileged partition according to the manifest file. |
| 492 webview.partition = 'foobar'; |
| 493 webview.addEventListener('loadabort', function(e) { |
| 494 embedder.test.fail(); |
| 495 }); |
| 496 webview.addEventListener('loadstop', function(e) { |
| 497 embedder.test.assertTrue(visibleAndInDOM); |
| 498 embedder.test.succeed(); |
| 499 }); |
| 500 // Set the .src while we are "display: none". |
| 501 webview.setAttribute('src', 'about:blank'); |
| 502 document.body.appendChild(webview); |
| 503 |
| 504 setTimeout(function() { |
| 505 webview.parentNode.removeChild(webview); |
| 506 webview.style.display = ''; |
| 507 visibleAndInDOM = true; |
| 508 // This should trigger loadstop. |
| 509 document.body.appendChild(webview); |
| 510 }, 0); |
| 511 } |
| 512 |
| 513 function testExecuteScript() { |
| 514 var webview = document.createElement('webview'); |
| 515 webview.addEventListener('loadstop', function() { |
| 516 webview.executeScript( |
| 517 {code:'document.body.style.backgroundColor = "red";'}, |
| 518 function(results) { |
| 519 embedder.test.assertEq(1, results.length); |
| 520 embedder.test.assertEq('red', results[0]); |
| 521 embedder.test.succeed(); |
| 522 }); |
| 523 }); |
| 524 webview.setAttribute('src', 'data:text/html,trigger navigation'); |
| 525 document.body.appendChild(webview); |
| 526 } |
| 527 |
| 528 function testExecuteScriptFail() { |
| 529 var webview = document.createElement('webview'); |
| 530 try { |
| 531 webview.executeScript( |
| 532 {code: 'document.body.style.backgroundColor = "red";'}, |
| 533 function(results) { embedder.test.fail(); }); |
| 534 } |
| 535 catch (e) { |
| 536 embedder.test.succeed(); |
| 537 } |
| 538 } |
| 539 |
| 540 |
339 | 541 |
340 // Tests end. | 542 // Tests end. |
341 | 543 |
342 embedder.test.testList = { | 544 embedder.test.testList = { |
343 'testAllowTransparencyAttribute': testAllowTransparencyAttribute, | 545 'testAllowTransparencyAttribute': testAllowTransparencyAttribute, |
344 'testAPIMethodExistence': testAPIMethodExistence, | 546 'testAPIMethodExistence': testAPIMethodExistence, |
| 547 'testAssignSrcAfterCrash': testAssignSrcAfterCrash, |
345 'testAutosizeAfterNavigation': testAutosizeAfterNavigation, | 548 'testAutosizeAfterNavigation': testAutosizeAfterNavigation, |
346 'testAutosizeBeforeNavigation': testAutosizeBeforeNavigation, | 549 'testAutosizeBeforeNavigation': testAutosizeBeforeNavigation, |
347 'testAutosizeHeight': testAutosizeHeight, | 550 'testAutosizeHeight': testAutosizeHeight, |
348 'testAutosizeRemoveAttributes': testAutosizeRemoveAttributes, | 551 'testAutosizeRemoveAttributes': testAutosizeRemoveAttributes, |
349 'testAutosizeWithPartialAttributes': testAutosizeWithPartialAttributes | 552 'testAutosizeWithPartialAttributes': testAutosizeWithPartialAttributes, |
| 553 'testCannotMutateEventName': testCannotMutateEventName, |
| 554 'testContentLoadEvent': testContentLoadEvent, |
| 555 'testDestroyOnEventListener': testDestroyOnEventListener, |
| 556 'testDisplayNoneWebviewLoad': testDisplayNoneWebviewLoad, |
| 557 'testDisplayNoneWebviewRemoveChild': testDisplayNoneWebviewRemoveChild, |
| 558 'testExecuteScript': testExecuteScript, |
| 559 'testExecuteScriptFail': testExecuteScriptFail |
350 }; | 560 }; |
351 | 561 |
352 onload = function() { | 562 onload = function() { |
353 chrome.test.sendMessage('LAUNCHED'); | 563 chrome.test.sendMessage('LAUNCHED'); |
354 }; | 564 }; |
OLD | NEW |