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