summaryrefslogtreecommitdiff
path: root/dom/browser-element/mochitest/browserElement_Metachange.js
blob: 7789a3e18c0c02261b3749b17e5e591c8c794530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that the onmozbrowsermetachange event works.
"use strict";

SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();

function createHtml(meta) {
  return 'data:text/html,<html xmlns:xml="http://www.w3.org/XML/1998/namespace"><head>' + meta + '<body></body></html>';
}

function createHtmlWithLang(meta, lang) {
  return 'data:text/html,<html xmlns:xml="http://www.w3.org/XML/1998/namespace" lang="' + lang + '"><head>' + meta + '<body></body></html>';
}

function createMeta(name, content) {
  return '<meta name="' + name + '" content="' + content + '">';
}

function createMetaWithLang(name, content, lang) {
  return '<meta name="' + name + '" content="' + content + '" lang="' + lang + '">';
}

function createMetaWithProperty(property, content) {
  return '<meta property="' + property + '" content="' + content + '">';
}

function runTest() {
  var iframe1 = document.createElement('iframe');
  iframe1.setAttribute('mozbrowser', 'true');
  document.body.appendChild(iframe1);

  // iframe2 is a red herring; we modify its meta elements but don't listen for
  // metachanges; we want to make sure that its metachange events aren't
  // picked up by the listener on iframe1.
  var iframe2 = document.createElement('iframe');
  iframe2.setAttribute('mozbrowser', 'true');
  document.body.appendChild(iframe2);

  // iframe3 is another red herring.  It's not a mozbrowser, so we shouldn't
  // get any metachange events on it.
  var iframe3 = document.createElement('iframe');
  document.body.appendChild(iframe3);

  var numMetaChanges = 0;

  iframe1.addEventListener('mozbrowsermetachange', function(e) {

    numMetaChanges++;

    if (numMetaChanges == 1) {
      is(e.detail.name, 'application-name');
      is(e.detail.content, 'foobar');

      // We should recieve metachange events when the user creates new metas
      SpecialPowers.getBrowserFrameMessageManager(iframe1)
                   .loadFrameScript("data:,content.document.title='New title';",
                                    /* allowDelayedLoad = */ false);

      SpecialPowers.getBrowserFrameMessageManager(iframe1)
                   .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<meta name=application-name content=new_foobar>')",
                                    /* allowDelayedLoad = */ false);

      SpecialPowers.getBrowserFrameMessageManager(iframe2)
                   .loadFrameScript("data:,content.document.head.insertAdjacentHTML('beforeend', '<meta name=application-name content=new_foobar>')",
                                    /* allowDelayedLoad = */ false);
    }
    else if (numMetaChanges == 2) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'new_foobar', 'content matches');
      ok(!("lang" in e.detail), 'lang not present');

      // Full new pages should trigger metachange events
      iframe1.src = createHtml(createMeta('application-name', '3rd_foobar'));
    }
    else if (numMetaChanges == 3) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, '3rd_foobar', 'content matches');
      ok(!("lang" in e.detail), 'lang not present');

      // Test setting a page with multiple meta elements
      iframe1.src = createHtml(createMeta('application-name', 'foobar_1') + createMeta('application-name', 'foobar_2'));
    }
    else if (numMetaChanges == 4) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'foobar_1', 'content matches');
      ok(!("lang" in e.detail), 'lang not present');
      // 2 events will be triggered by previous test, wait for next
    }
    else if (numMetaChanges == 5) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'foobar_2', 'content matches');
      ok(!("lang" in e.detail), 'lang not present');

      // Test the language
      iframe1.src = createHtml(createMetaWithLang('application-name', 'foobar_lang_1', 'en'));
    }
    else if (numMetaChanges == 6) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'foobar_lang_1', 'content matches');
      is(e.detail.lang, 'en', 'language matches');

      // Test the language in the ancestor element
      iframe1.src = createHtmlWithLang(createMeta('application-name', 'foobar_lang_2'), 'es');
    }
    else if (numMetaChanges == 7) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'foobar_lang_2', 'content matches');
      is(e.detail.lang, 'es', 'language matches');

      // Test the language in the ancestor element
      iframe1.src = createHtmlWithLang(createMetaWithLang('application-name', 'foobar_lang_3', 'it'), 'fi');
    }
    else if (numMetaChanges == 8) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'foobar_lang_3', 'content matches');
      is(e.detail.lang, 'it', 'language matches');

      // Test the content-language
      iframe1.src = "http://test/tests/dom/browser-element/mochitest/file_browserElement_Metachange.sjs?ru";
    }
    else if (numMetaChanges == 9) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'sjs', 'content matches');
      is(e.detail.lang, 'ru', 'language matches');

      // Test the content-language
      iframe1.src = "http://test/tests/dom/browser-element/mochitest/file_browserElement_Metachange.sjs?ru|dk";
    }
    else if (numMetaChanges == 10) {
      is(e.detail.name, 'application-name', 'name matches');
      is(e.detail.content, 'sjs', 'content matches');
      is(e.detail.lang, 'dk', 'language matches');

      // Test Open Graph property
      iframe1.src = createHtml(createMetaWithProperty('og:description', 'Fascinating article'));

      // We should not get event if property doesn't start with 'og:'
      iframe3.src = createHtml(createMetaWithProperty('go:description', 'Fascinating article'));
    }
    else if (numMetaChanges == 11) {
      is(e.detail.name, 'og:description', 'property name matches');
      is(e.detail.content, 'Fascinating article', 'content matches');

      // Sometimes 'name' is used instead of 'property'. Verify that works.
      iframe1.src = createHtml(createMeta('og:title', 'One weird trick!'));

      // We should not get event if property doesn't start with 'og:'
      iframe3.src = createHtml(createMeta('go:title', 'One weird trick!'));
    }
    else if (numMetaChanges == 12) {
      is(e.detail.name, 'og:title', 'property name matches');
      is(e.detail.content, 'One weird trick!', 'content matches');

      // Test the language
      SimpleTest.finish();
    } else {
      ok(false, 'Too many metachange events.');
    }
  });

  iframe3.addEventListener('mozbrowsermetachange', function(e) {
    ok(false, 'Should not get a metachange event for iframe3.');
  });


  iframe1.src = createHtml(createMeta('application-name', 'foobar'));
  // We should not recieve meta change events for either of the below iframes
  iframe2.src = createHtml(createMeta('application-name', 'foobar'));
  iframe3.src = createHtml(createMeta('application-name', 'foobar'));

}

addEventListener('testready', runTest);