michael@0: /* Any copyright is dedicated to the public domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test that the onmozbrowseropensearch event works. michael@0: "use strict"; michael@0: michael@0: SimpleTest.waitForExplicitFinish(); michael@0: browserElementTestHelpers.setEnabledPref(true); michael@0: browserElementTestHelpers.addPermission(); michael@0: michael@0: function createHtml(link) { michael@0: return 'data:text/html,' + link + ''; michael@0: } michael@0: michael@0: function createLink(name) { michael@0: return ''; michael@0: } michael@0: michael@0: function runTest() { michael@0: var iframe1 = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe1).mozbrowser = true; michael@0: document.body.appendChild(iframe1); michael@0: michael@0: // iframe2 is a red herring; we modify its link but don't listen for michael@0: // opensearch; we want to make sure that its opensearch events aren't michael@0: // picked up by the listener on iframe1. michael@0: var iframe2 = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe2).mozbrowser = true; michael@0: document.body.appendChild(iframe2); michael@0: michael@0: // iframe3 is another red herring. It's not a mozbrowser, so we shouldn't michael@0: // get any opensearch events on it. michael@0: var iframe3 = document.createElement('iframe'); michael@0: document.body.appendChild(iframe3); michael@0: michael@0: var numLinkChanges = 0; michael@0: michael@0: iframe1.addEventListener('mozbrowseropensearch', function(e) { michael@0: michael@0: numLinkChanges++; michael@0: michael@0: if (numLinkChanges == 1) { michael@0: is(e.detail.title, 'Test OpenSearch'); michael@0: is(e.detail.href, 'http://example.com/mysearch.xml'); michael@0: michael@0: // We should recieve opensearch events when the user creates new links michael@0: // to a search engine, but only when we listen for them michael@0: SpecialPowers.getBrowserFrameMessageManager(iframe1) michael@0: .loadFrameScript("data:,content.document.title='New title';", michael@0: /* allowDelayedLoad = */ false); michael@0: michael@0: SpecialPowers.getBrowserFrameMessageManager(iframe1) michael@0: .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '')", michael@0: /* allowDelayedLoad = */ false); michael@0: michael@0: SpecialPowers.getBrowserFrameMessageManager(iframe2) michael@0: .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '')", michael@0: /* allowDelayedLoad = */ false); michael@0: } michael@0: else if (numLinkChanges == 2) { michael@0: is(e.detail.href, 'http://example.com/newsearch.xml'); michael@0: michael@0: // Full new pages should trigger opensearch events michael@0: iframe1.src = createHtml(createLink('3rdsearch')); michael@0: } michael@0: else if (numLinkChanges == 3) { michael@0: is(e.detail.href, 'http://example.com/3rdsearch.xml'); michael@0: michael@0: // the rel attribute can have various space seperated values, make michael@0: // sure we only pick up correct values for 'opensearch' michael@0: SpecialPowers.getBrowserFrameMessageManager(iframe1) michael@0: .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '')", michael@0: /* allowDelayedLoad = */ false); michael@0: // Test setting a page with multiple links elements michael@0: iframe1.src = createHtml(createLink('another') + createLink('search')); michael@0: } michael@0: else if (numLinkChanges == 4) { michael@0: is(e.detail.href, 'http://example.com/another.xml'); michael@0: // 2 events will be triggered by previous test, wait for next michael@0: } michael@0: else if (numLinkChanges == 5) { michael@0: is(e.detail.href, 'http://example.com/search.xml'); michael@0: michael@0: // Make sure opensearch check is case insensitive michael@0: SpecialPowers.getBrowserFrameMessageManager(iframe1) michael@0: .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '')", michael@0: /* allowDelayedLoad = */ false); michael@0: } michael@0: else if (numLinkChanges == 6) { michael@0: is(e.detail.href, 'http://example.com/ucasesearch.xml'); michael@0: SimpleTest.finish(); michael@0: } else { michael@0: ok(false, 'Too many opensearch events.'); michael@0: } michael@0: }); michael@0: michael@0: iframe3.addEventListener('mozbrowseropensearch', function(e) { michael@0: ok(false, 'Should not get a opensearch event for iframe3.'); michael@0: }); michael@0: michael@0: michael@0: iframe1.src = createHtml(createLink('mysearch')); michael@0: // We should not recieve opensearch change events for either of the below iframes michael@0: iframe2.src = createHtml(createLink('mysearch')); michael@0: iframe3.src = createHtml(createLink('mysearch')); michael@0: michael@0: } michael@0: michael@0: addEventListener('testready', runTest);