3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
Code distributed by Google as part of the polymer project is also
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
14
<meta charset="utf-8">
16
<script src="../../webcomponentsjs/webcomponents.js"></script>
17
<script src="../../web-component-tester/browser.js"></script>
18
<script src="../../test-fixture/test-fixture-mocha.js"></script>
20
<link rel="import" href="../../polymer/polymer.html">
21
<link rel="import" href="../../promise-polyfill/promise-polyfill.html">
22
<link rel="import" href="../../test-fixture/test-fixture.html">
23
<link rel="import" href="../iron-location.html">
30
function getIframe() {
31
return new Promise(function(resolve, reject) {
32
var iframe = document.createElement('iframe');
33
var result = getMessageMatching(iframe, function(message) {
34
return message.type === 'ready';
36
iframe.src = './initialization-iframe.html';
37
document.body.appendChild(iframe);
38
iframe.addEventListener('error', reject);
39
result.then(function() {resolve(iframe)}, reject);
43
function onMessage(iframe, callback) {
44
var f = function(event) {
45
if (event.source === iframe.contentWindow) {
49
window.addEventListener('message', f, false);
51
window.removeEventListener('message', f);
55
function getMessageMatching(iframe, predicate) {
56
var revoke = function() {};
57
var result = new Promise(function(resolve, reject) {
58
revoke = onMessage(iframe, function(message) {
59
if (predicate(message)) {
64
result.then(revoke, revoke);
68
function getUrl(iframe) {
69
var result = getMessageMatching(iframe, function(message) {
70
return message.type === 'urlQueryResponse';
72
var revoke = function() {};
73
var result = new Promise(function(resolve, reject) {
74
revoke = onMessage(iframe, resolve);
76
result.then(revoke, revoke);
77
iframe.contentWindow.postMessage({type: 'urlQuery'}, '*');
81
function timePasses(ms) {
82
return new Promise(function(resolve) {
83
window.setTimeout(function() {
89
suite('<iron-location>\'s initialization', function() {
93
return getIframe().then(function(i) {
95
function isError(m) {return m.type === 'error'}
96
getMessageMatching(iframe, isError).then(function(m) {
101
teardown(function() {
103
document.body.removeChild(iframe);
109
throw new Error('Error message from contained iframe: ' + e);
113
'default-before', 'attached-before', 'ready-before',
114
'default-after', 'attached-after', 'ready-after',
115
'default-below', 'attached-below', 'ready-below',
116
'default-above', 'attached-above', 'ready-above',
117
'default-container', 'attached-container', 'ready-container'
119
cases.forEach(function(caseName) {
120
test('the url takes priority in ' + caseName + ' initialization', function() {
121
return getUrl(iframe).then(function(url) {
122
expect(url.search).to.be.eq('');
123
iframe.contentWindow.postMessage({type: 'appendBody', tagName: caseName}, '*');
124
return timePasses(10).then(function() {return getUrl(iframe)});
125
}).then(function(url) {
126
expect(url.search).to.be.eq('');
130
var expectedFailureCases = ['timeout-before', 'timeout-after', 'timeout-below', 'timeout-above', 'timeout-container'];
131
expectedFailureCases.forEach(function(caseName) {
132
test('the url does not take priority in ' + caseName + ' initialization', function() {
133
return getUrl(iframe).then(function(url) {
134
expect(url.search).to.be.eq('');
135
iframe.contentWindow.postMessage({type: 'appendBody', tagName: caseName}, '*');
136
return timePasses(60).then(function() {return getUrl(iframe)});
137
}).then(function(url) {
138
expect(url.search).to.be.eq('?on-timeout');