1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/browserElement_Iconchange.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 1.4 +/* Any copyright is dedicated to the public domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// Test that the onmozbrowsericonchange event works. 1.8 +"use strict"; 1.9 + 1.10 +SimpleTest.waitForExplicitFinish(); 1.11 +browserElementTestHelpers.setEnabledPref(true); 1.12 +browserElementTestHelpers.addPermission(); 1.13 + 1.14 +function createHtml(link) { 1.15 + return 'data:text/html,<html><head>' + link + '<body></body></html>'; 1.16 +} 1.17 + 1.18 +function createLink(name, sizes) { 1.19 + var s = sizes ? 'sizes="' + sizes + '"' : ''; 1.20 + return '<link rel="icon" type="image/png" ' + s + ' href="http://example.com/' + name + '.png">'; 1.21 +} 1.22 + 1.23 +function runTest() { 1.24 + var iframe1 = document.createElement('iframe'); 1.25 + SpecialPowers.wrap(iframe1).mozbrowser = true; 1.26 + document.body.appendChild(iframe1); 1.27 + 1.28 + // iframe2 is a red herring; we modify its favicon but don't listen for 1.29 + // iconchanges; we want to make sure that its iconchange events aren't 1.30 + // picked up by the listener on iframe1. 1.31 + var iframe2 = document.createElement('iframe'); 1.32 + SpecialPowers.wrap(iframe2).mozbrowser = true; 1.33 + document.body.appendChild(iframe2); 1.34 + 1.35 + // iframe3 is another red herring. It's not a mozbrowser, so we shouldn't 1.36 + // get any iconchange events on it. 1.37 + var iframe3 = document.createElement('iframe'); 1.38 + document.body.appendChild(iframe3); 1.39 + 1.40 + var numIconChanges = 0; 1.41 + 1.42 + iframe1.addEventListener('mozbrowsericonchange', function(e) { 1.43 + 1.44 + numIconChanges++; 1.45 + 1.46 + if (numIconChanges == 1) { 1.47 + is(e.detail.href, 'http://example.com/myicon.png'); 1.48 + 1.49 + // We should recieve iconchange events when the user creates new links 1.50 + // to a favicon, but only when we listen for them 1.51 + SpecialPowers.getBrowserFrameMessageManager(iframe1) 1.52 + .loadFrameScript("data:,content.document.title='New title';", 1.53 + /* allowDelayedLoad = */ false); 1.54 + 1.55 + SpecialPowers.getBrowserFrameMessageManager(iframe1) 1.56 + .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=ICON href=http://example.com/newicon.png>')", 1.57 + /* allowDelayedLoad = */ false); 1.58 + 1.59 + SpecialPowers.getBrowserFrameMessageManager(iframe2) 1.60 + .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=ICON href=http://example.com/newicon.png>')", 1.61 + /* allowDelayedLoad = */ false); 1.62 + } 1.63 + else if (numIconChanges == 2) { 1.64 + is(e.detail.href, 'http://example.com/newicon.png'); 1.65 + 1.66 + // Full new pages should trigger iconchange events 1.67 + iframe1.src = createHtml(createLink('3rdicon')); 1.68 + } 1.69 + else if (numIconChanges == 3) { 1.70 + is(e.detail.href, 'http://example.com/3rdicon.png'); 1.71 + 1.72 + // the rel attribute can have various space seperated values, make 1.73 + // sure we only pick up correct values for 'icon' 1.74 + SpecialPowers.getBrowserFrameMessageManager(iframe1) 1.75 + .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=shortcuticon href=http://example.com/newicon.png>')", 1.76 + /* allowDelayedLoad = */ false); 1.77 + // Test setting a page with multiple links elements 1.78 + iframe1.src = createHtml(createLink('another') + createLink('icon')); 1.79 + } 1.80 + else if (numIconChanges == 4) { 1.81 + is(e.detail.href, 'http://example.com/another.png'); 1.82 + // 2 events will be triggered by previous test, wait for next 1.83 + } 1.84 + else if (numIconChanges == 5) { 1.85 + is(e.detail.href, 'http://example.com/icon.png'); 1.86 + 1.87 + // Make sure icon check is case insensitive 1.88 + SpecialPowers.getBrowserFrameMessageManager(iframe1) 1.89 + .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=ICON href=http://example.com/ucaseicon.png>')", 1.90 + /* allowDelayedLoad = */ false); 1.91 + } 1.92 + else if (numIconChanges == 6) { 1.93 + is(e.detail.href, 'http://example.com/ucaseicon.png'); 1.94 + iframe1.src = createHtml(createLink('testsize', '50x50')); 1.95 + } 1.96 + else if (numIconChanges == 7) { 1.97 + is(e.detail.href, 'http://example.com/testsize.png'); 1.98 + is(e.detail.sizes, '50x50'); 1.99 + SimpleTest.finish(); 1.100 + } else { 1.101 + ok(false, 'Too many iconchange events.'); 1.102 + } 1.103 + }); 1.104 + 1.105 + iframe3.addEventListener('mozbrowsericonchange', function(e) { 1.106 + ok(false, 'Should not get a iconchange event for iframe3.'); 1.107 + }); 1.108 + 1.109 + 1.110 + iframe1.src = createHtml(createLink('myicon')); 1.111 + // We should not recieve icon change events for either of the below iframes 1.112 + iframe2.src = createHtml(createLink('myicon')); 1.113 + iframe3.src = createHtml(createLink('myicon')); 1.114 + 1.115 +} 1.116 + 1.117 +addEventListener('testready', runTest);