|
1 /* Any copyright is dedicated to the public domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // Test that the onmozbrowsericonchange 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, sizes) { |
|
16 var s = sizes ? 'sizes="' + sizes + '"' : ''; |
|
17 return '<link rel="icon" type="image/png" ' + s + ' href="http://example.com/' + name + '.png">'; |
|
18 } |
|
19 |
|
20 function runTest() { |
|
21 var iframe1 = document.createElement('iframe'); |
|
22 SpecialPowers.wrap(iframe1).mozbrowser = true; |
|
23 document.body.appendChild(iframe1); |
|
24 |
|
25 // iframe2 is a red herring; we modify its favicon but don't listen for |
|
26 // iconchanges; we want to make sure that its iconchange events aren't |
|
27 // picked up by the listener on iframe1. |
|
28 var iframe2 = document.createElement('iframe'); |
|
29 SpecialPowers.wrap(iframe2).mozbrowser = true; |
|
30 document.body.appendChild(iframe2); |
|
31 |
|
32 // iframe3 is another red herring. It's not a mozbrowser, so we shouldn't |
|
33 // get any iconchange events on it. |
|
34 var iframe3 = document.createElement('iframe'); |
|
35 document.body.appendChild(iframe3); |
|
36 |
|
37 var numIconChanges = 0; |
|
38 |
|
39 iframe1.addEventListener('mozbrowsericonchange', function(e) { |
|
40 |
|
41 numIconChanges++; |
|
42 |
|
43 if (numIconChanges == 1) { |
|
44 is(e.detail.href, 'http://example.com/myicon.png'); |
|
45 |
|
46 // We should recieve iconchange events when the user creates new links |
|
47 // to a favicon, 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=ICON href=http://example.com/newicon.png>')", |
|
54 /* allowDelayedLoad = */ false); |
|
55 |
|
56 SpecialPowers.getBrowserFrameMessageManager(iframe2) |
|
57 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=ICON href=http://example.com/newicon.png>')", |
|
58 /* allowDelayedLoad = */ false); |
|
59 } |
|
60 else if (numIconChanges == 2) { |
|
61 is(e.detail.href, 'http://example.com/newicon.png'); |
|
62 |
|
63 // Full new pages should trigger iconchange events |
|
64 iframe1.src = createHtml(createLink('3rdicon')); |
|
65 } |
|
66 else if (numIconChanges == 3) { |
|
67 is(e.detail.href, 'http://example.com/3rdicon.png'); |
|
68 |
|
69 // the rel attribute can have various space seperated values, make |
|
70 // sure we only pick up correct values for 'icon' |
|
71 SpecialPowers.getBrowserFrameMessageManager(iframe1) |
|
72 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=shortcuticon href=http://example.com/newicon.png>')", |
|
73 /* allowDelayedLoad = */ false); |
|
74 // Test setting a page with multiple links elements |
|
75 iframe1.src = createHtml(createLink('another') + createLink('icon')); |
|
76 } |
|
77 else if (numIconChanges == 4) { |
|
78 is(e.detail.href, 'http://example.com/another.png'); |
|
79 // 2 events will be triggered by previous test, wait for next |
|
80 } |
|
81 else if (numIconChanges == 5) { |
|
82 is(e.detail.href, 'http://example.com/icon.png'); |
|
83 |
|
84 // Make sure icon check is case insensitive |
|
85 SpecialPowers.getBrowserFrameMessageManager(iframe1) |
|
86 .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<link rel=ICON href=http://example.com/ucaseicon.png>')", |
|
87 /* allowDelayedLoad = */ false); |
|
88 } |
|
89 else if (numIconChanges == 6) { |
|
90 is(e.detail.href, 'http://example.com/ucaseicon.png'); |
|
91 iframe1.src = createHtml(createLink('testsize', '50x50')); |
|
92 } |
|
93 else if (numIconChanges == 7) { |
|
94 is(e.detail.href, 'http://example.com/testsize.png'); |
|
95 is(e.detail.sizes, '50x50'); |
|
96 SimpleTest.finish(); |
|
97 } else { |
|
98 ok(false, 'Too many iconchange events.'); |
|
99 } |
|
100 }); |
|
101 |
|
102 iframe3.addEventListener('mozbrowsericonchange', function(e) { |
|
103 ok(false, 'Should not get a iconchange event for iframe3.'); |
|
104 }); |
|
105 |
|
106 |
|
107 iframe1.src = createHtml(createLink('myicon')); |
|
108 // We should not recieve icon change events for either of the below iframes |
|
109 iframe2.src = createHtml(createLink('myicon')); |
|
110 iframe3.src = createHtml(createLink('myicon')); |
|
111 |
|
112 } |
|
113 |
|
114 addEventListener('testready', runTest); |