dom/browser-element/mochitest/browserElement_Opensearch.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the public domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Test that the onmozbrowseropensearch event works.
michael@0 5 "use strict";
michael@0 6
michael@0 7 SimpleTest.waitForExplicitFinish();
michael@0 8 browserElementTestHelpers.setEnabledPref(true);
michael@0 9 browserElementTestHelpers.addPermission();
michael@0 10
michael@0 11 function createHtml(link) {
michael@0 12 return 'data:text/html,<html><head>' + link + '<body></body></html>';
michael@0 13 }
michael@0 14
michael@0 15 function createLink(name) {
michael@0 16 return '<link rel="search" title="Test OpenSearch" type="application/opensearchdescription+xml" href="http://example.com/' + name + '.xml">';
michael@0 17 }
michael@0 18
michael@0 19 function runTest() {
michael@0 20 var iframe1 = document.createElement('iframe');
michael@0 21 SpecialPowers.wrap(iframe1).mozbrowser = true;
michael@0 22 document.body.appendChild(iframe1);
michael@0 23
michael@0 24 // iframe2 is a red herring; we modify its link but don't listen for
michael@0 25 // opensearch; we want to make sure that its opensearch events aren't
michael@0 26 // picked up by the listener on iframe1.
michael@0 27 var iframe2 = document.createElement('iframe');
michael@0 28 SpecialPowers.wrap(iframe2).mozbrowser = true;
michael@0 29 document.body.appendChild(iframe2);
michael@0 30
michael@0 31 // iframe3 is another red herring. It's not a mozbrowser, so we shouldn't
michael@0 32 // get any opensearch events on it.
michael@0 33 var iframe3 = document.createElement('iframe');
michael@0 34 document.body.appendChild(iframe3);
michael@0 35
michael@0 36 var numLinkChanges = 0;
michael@0 37
michael@0 38 iframe1.addEventListener('mozbrowseropensearch', function(e) {
michael@0 39
michael@0 40 numLinkChanges++;
michael@0 41
michael@0 42 if (numLinkChanges == 1) {
michael@0 43 is(e.detail.title, 'Test OpenSearch');
michael@0 44 is(e.detail.href, 'http://example.com/mysearch.xml');
michael@0 45
michael@0 46 // We should recieve opensearch events when the user creates new links
michael@0 47 // to a search engine, but only when we listen for them
michael@0 48 SpecialPowers.getBrowserFrameMessageManager(iframe1)
michael@0 49 .loadFrameScript("data:,content.document.title='New title';",
michael@0 50 /* allowDelayedLoad = */ false);
michael@0 51
michael@0 52 SpecialPowers.getBrowserFrameMessageManager(iframe1)
michael@0 53 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=SEARCH type=application/opensearchdescription+xml href=http://example.com/newsearch.xml>')",
michael@0 54 /* allowDelayedLoad = */ false);
michael@0 55
michael@0 56 SpecialPowers.getBrowserFrameMessageManager(iframe2)
michael@0 57 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=SEARCH type=application/opensearchdescription+xml href=http://example.com/newsearch.xml>')",
michael@0 58 /* allowDelayedLoad = */ false);
michael@0 59 }
michael@0 60 else if (numLinkChanges == 2) {
michael@0 61 is(e.detail.href, 'http://example.com/newsearch.xml');
michael@0 62
michael@0 63 // Full new pages should trigger opensearch events
michael@0 64 iframe1.src = createHtml(createLink('3rdsearch'));
michael@0 65 }
michael@0 66 else if (numLinkChanges == 3) {
michael@0 67 is(e.detail.href, 'http://example.com/3rdsearch.xml');
michael@0 68
michael@0 69 // the rel attribute can have various space seperated values, make
michael@0 70 // sure we only pick up correct values for 'opensearch'
michael@0 71 SpecialPowers.getBrowserFrameMessageManager(iframe1)
michael@0 72 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=someopensearch type=application/opensearchdescription+xml href=http://example.com/newsearch.xml>')",
michael@0 73 /* allowDelayedLoad = */ false);
michael@0 74 // Test setting a page with multiple links elements
michael@0 75 iframe1.src = createHtml(createLink('another') + createLink('search'));
michael@0 76 }
michael@0 77 else if (numLinkChanges == 4) {
michael@0 78 is(e.detail.href, 'http://example.com/another.xml');
michael@0 79 // 2 events will be triggered by previous test, wait for next
michael@0 80 }
michael@0 81 else if (numLinkChanges == 5) {
michael@0 82 is(e.detail.href, 'http://example.com/search.xml');
michael@0 83
michael@0 84 // Make sure opensearch check is case insensitive
michael@0 85 SpecialPowers.getBrowserFrameMessageManager(iframe1)
michael@0 86 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=SEARCH type=application/opensearchdescription+xml href=http://example.com/ucasesearch.xml>')",
michael@0 87 /* allowDelayedLoad = */ false);
michael@0 88 }
michael@0 89 else if (numLinkChanges == 6) {
michael@0 90 is(e.detail.href, 'http://example.com/ucasesearch.xml');
michael@0 91 SimpleTest.finish();
michael@0 92 } else {
michael@0 93 ok(false, 'Too many opensearch events.');
michael@0 94 }
michael@0 95 });
michael@0 96
michael@0 97 iframe3.addEventListener('mozbrowseropensearch', function(e) {
michael@0 98 ok(false, 'Should not get a opensearch event for iframe3.');
michael@0 99 });
michael@0 100
michael@0 101
michael@0 102 iframe1.src = createHtml(createLink('mysearch'));
michael@0 103 // We should not recieve opensearch change events for either of the below iframes
michael@0 104 iframe2.src = createHtml(createLink('mysearch'));
michael@0 105 iframe3.src = createHtml(createLink('mysearch'));
michael@0 106
michael@0 107 }
michael@0 108
michael@0 109 addEventListener('testready', runTest);

mercurial