1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/browserElement_Metachange.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 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 onmozbrowsermetachange 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(meta) { 1.15 + return 'data:text/html,<html xmlns:xml="http://www.w3.org/XML/1998/namespace"><head>' + meta + '<body></body></html>'; 1.16 +} 1.17 + 1.18 +function createHtmlWithLang(meta, lang) { 1.19 + return 'data:text/html,<html xmlns:xml="http://www.w3.org/XML/1998/namespace" lang="' + lang + '"><head>' + meta + '<body></body></html>'; 1.20 +} 1.21 + 1.22 +function createMeta(name, content) { 1.23 + return '<meta name="' + name + '" content="' + content + '">'; 1.24 +} 1.25 + 1.26 +function createMetaWithLang(name, content, lang) { 1.27 + return '<meta name="' + name + '" content="' + content + '" lang="' + lang + '">'; 1.28 +} 1.29 + 1.30 +function runTest() { 1.31 + var iframe1 = document.createElement('iframe'); 1.32 + SpecialPowers.wrap(iframe1).mozbrowser = true; 1.33 + document.body.appendChild(iframe1); 1.34 + 1.35 + // iframe2 is a red herring; we modify its meta elements but don't listen for 1.36 + // metachanges; we want to make sure that its metachange events aren't 1.37 + // picked up by the listener on iframe1. 1.38 + var iframe2 = document.createElement('iframe'); 1.39 + SpecialPowers.wrap(iframe2).mozbrowser = true; 1.40 + document.body.appendChild(iframe2); 1.41 + 1.42 + // iframe3 is another red herring. It's not a mozbrowser, so we shouldn't 1.43 + // get any metachange events on it. 1.44 + var iframe3 = document.createElement('iframe'); 1.45 + document.body.appendChild(iframe3); 1.46 + 1.47 + var numMetaChanges = 0; 1.48 + 1.49 + iframe1.addEventListener('mozbrowsermetachange', function(e) { 1.50 + 1.51 + numMetaChanges++; 1.52 + 1.53 + if (numMetaChanges == 1) { 1.54 + is(e.detail.name, 'application-name'); 1.55 + is(e.detail.content, 'foobar'); 1.56 + 1.57 + // We should recieve metachange events when the user creates new metas 1.58 + SpecialPowers.getBrowserFrameMessageManager(iframe1) 1.59 + .loadFrameScript("data:,content.document.title='New title';", 1.60 + /* allowDelayedLoad = */ false); 1.61 + 1.62 + SpecialPowers.getBrowserFrameMessageManager(iframe1) 1.63 + .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<meta name=application-name content=new_foobar>')", 1.64 + /* allowDelayedLoad = */ false); 1.65 + 1.66 + SpecialPowers.getBrowserFrameMessageManager(iframe2) 1.67 + .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<meta name=application-name content=new_foobar>')", 1.68 + /* allowDelayedLoad = */ false); 1.69 + } 1.70 + else if (numMetaChanges == 2) { 1.71 + is(e.detail.name, 'application-name', 'name matches'); 1.72 + is(e.detail.content, 'new_foobar', 'content matches'); 1.73 + ok(!("lang" in e.detail), 'lang not present'); 1.74 + 1.75 + // Full new pages should trigger metachange events 1.76 + iframe1.src = createHtml(createMeta('application-name', '3rd_foobar')); 1.77 + } 1.78 + else if (numMetaChanges == 3) { 1.79 + is(e.detail.name, 'application-name', 'name matches'); 1.80 + is(e.detail.content, '3rd_foobar', 'content matches'); 1.81 + ok(!("lang" in e.detail), 'lang not present'); 1.82 + 1.83 + // Test setting a page with multiple meta elements 1.84 + iframe1.src = createHtml(createMeta('application-name', 'foobar_1') + createMeta('application-name', 'foobar_2')); 1.85 + } 1.86 + else if (numMetaChanges == 4) { 1.87 + is(e.detail.name, 'application-name', 'name matches'); 1.88 + is(e.detail.content, 'foobar_1', 'content matches'); 1.89 + ok(!("lang" in e.detail), 'lang not present'); 1.90 + // 2 events will be triggered by previous test, wait for next 1.91 + } 1.92 + else if (numMetaChanges == 5) { 1.93 + is(e.detail.name, 'application-name', 'name matches'); 1.94 + is(e.detail.content, 'foobar_2', 'content matches'); 1.95 + ok(!("lang" in e.detail), 'lang not present'); 1.96 + 1.97 + // Test the language 1.98 + iframe1.src = createHtml(createMetaWithLang('application-name', 'foobar_lang_1', 'en')); 1.99 + } 1.100 + else if (numMetaChanges == 6) { 1.101 + is(e.detail.name, 'application-name', 'name matches'); 1.102 + is(e.detail.content, 'foobar_lang_1', 'content matches'); 1.103 + is(e.detail.lang, 'en', 'language matches'); 1.104 + 1.105 + // Test the language in the ancestor element 1.106 + iframe1.src = createHtmlWithLang(createMeta('application-name', 'foobar_lang_2'), 'es'); 1.107 + } 1.108 + else if (numMetaChanges == 7) { 1.109 + is(e.detail.name, 'application-name', 'name matches'); 1.110 + is(e.detail.content, 'foobar_lang_2', 'content matches'); 1.111 + is(e.detail.lang, 'es', 'language matches'); 1.112 + 1.113 + // Test the language in the ancestor element 1.114 + iframe1.src = createHtmlWithLang(createMetaWithLang('application-name', 'foobar_lang_3', 'it'), 'fi'); 1.115 + } 1.116 + else if (numMetaChanges == 8) { 1.117 + is(e.detail.name, 'application-name', 'name matches'); 1.118 + is(e.detail.content, 'foobar_lang_3', 'content matches'); 1.119 + is(e.detail.lang, 'it', 'language matches'); 1.120 + 1.121 + // Test the content-language 1.122 + iframe1.src = "http://test/tests/dom/browser-element/mochitest/file_browserElement_Metachange.sjs?ru"; 1.123 + } 1.124 + else if (numMetaChanges == 9) { 1.125 + is(e.detail.name, 'application-name', 'name matches'); 1.126 + is(e.detail.content, 'sjs', 'content matches'); 1.127 + is(e.detail.lang, 'ru', 'language matches'); 1.128 + 1.129 + // Test the content-language 1.130 + iframe1.src = "http://test/tests/dom/browser-element/mochitest/file_browserElement_Metachange.sjs?ru|dk"; 1.131 + } 1.132 + else if (numMetaChanges == 10) { 1.133 + is(e.detail.name, 'application-name', 'name matches'); 1.134 + is(e.detail.content, 'sjs', 'content matches'); 1.135 + is(e.detail.lang, 'dk', 'language matches'); 1.136 + 1.137 + // Test the language 1.138 + SimpleTest.finish(); 1.139 + } else { 1.140 + ok(false, 'Too many metachange events.'); 1.141 + } 1.142 + }); 1.143 + 1.144 + iframe3.addEventListener('mozbrowsermetachange', function(e) { 1.145 + ok(false, 'Should not get a metachange event for iframe3.'); 1.146 + }); 1.147 + 1.148 + 1.149 + iframe1.src = createHtml(createMeta('application-name', 'foobar')); 1.150 + // We should not recieve meta change events for either of the below iframes 1.151 + iframe2.src = createHtml(createMeta('application-name', 'foobar')); 1.152 + iframe3.src = createHtml(createMeta('application-name', 'foobar')); 1.153 + 1.154 +} 1.155 + 1.156 +addEventListener('testready', runTest);