| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Framework for running JavaScript tests of Polymer elements. | 6 * @fileoverview Framework for running JavaScript tests of Polymer elements. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Test fixture for Polymer element testing. | 10 * Test fixture for Polymer element testing. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 this.preloadTime = window.performance.now(); | 53 this.preloadTime = window.performance.now(); |
| 54 testing.Test.prototype.preLoad.call(this); | 54 testing.Test.prototype.preLoad.call(this); |
| 55 }, | 55 }, |
| 56 | 56 |
| 57 /** @override */ | 57 /** @override */ |
| 58 setUp: function() { | 58 setUp: function() { |
| 59 testing.Test.prototype.setUp.call(this); | 59 testing.Test.prototype.setUp.call(this); |
| 60 | 60 |
| 61 // List of imported URLs for debugging purposes. | 61 // List of imported URLs for debugging purposes. |
| 62 PolymerTest.importUrls_ = []; | 62 PolymerTest.importUrls_ = []; |
| 63 PolymerTest.scriptUrls_ = []; |
| 63 | 64 |
| 64 // Importing a URL like "chrome://md-settings/foo" redirects to the base | 65 // Importing a URL like "chrome://md-settings/foo" redirects to the base |
| 65 // ("chrome://md-settings") page, which due to how browsePreload works can | 66 // ("chrome://md-settings") page, which due to how browsePreload works can |
| 66 // result in duplicate imports. Wrap document.registerElement so failures | 67 // result in duplicate imports. Wrap document.registerElement so failures |
| 67 // caused by re-registering Polymer elements are caught; otherwise Chrome | 68 // caused by re-registering Polymer elements are caught; otherwise Chrome |
| 68 // simply throws "Script error" which is unhelpful. | 69 // simply throws "Script error" which is unhelpful. |
| 69 var originalRegisterElement = document.registerElement; | 70 var originalRegisterElement = document.registerElement; |
| 70 document.registerElement = function() { | 71 document.registerElement = function() { |
| 71 try { | 72 try { |
| 72 return originalRegisterElement.apply(document, arguments); | 73 return originalRegisterElement.apply(document, arguments); |
| 73 } catch (e) { | 74 } catch (e) { |
| 74 var msg = | 75 var msg = |
| 75 'If the call to document.registerElement failed because a type ' + | 76 'If the call to document.registerElement failed because a type ' + |
| 76 'is already registered, perhaps you have loaded a script twice. ' + | 77 'is already registered, perhaps you have loaded a script twice. ' + |
| 77 'Incorrect resource URLs can redirect to base WebUI pages; make ' + | 78 'Incorrect resource URLs can redirect to base WebUI pages; make ' + |
| 78 'sure the following URLs are correct and unique:\n'; | 79 'sure the following URLs are correct and unique:\n'; |
| 79 for (var i = 0; i < PolymerTest.importUrls_.length; i++) | 80 for (var i = 0; i < PolymerTest.importUrls_.length; i++) |
| 80 msg += ' ' + PolymerTest.importUrls_[i] + '\n'; | 81 msg += ' ' + PolymerTest.importUrls_[i] + '\n'; |
| 82 for (var i = 0; i < PolymerTest.scriptUrls_.length; i++) |
| 83 msg += ' ' + PolymerTest.scriptUrls_[i] + '\n'; |
| 81 console.error(msg); | 84 console.error(msg); |
| 82 | 85 |
| 83 // Mocha will handle the error. | 86 // Mocha will handle the error. |
| 84 throw e; | 87 throw e; |
| 85 } | 88 } |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 // Import Polymer and iron-test-helpers before running tests. | 91 // Import Polymer and iron-test-helpers before running tests. |
| 89 suiteSetup(function() { | 92 suiteSetup(function() { |
| 90 var promises = []; | 93 var promises = []; |
| 91 if (typeof Polymer != 'function') { | 94 if (typeof Polymer != 'function') { |
| 92 promises.push( | 95 promises.push( |
| 93 PolymerTest.importHtml( | 96 PolymerTest.importHtml( |
| 94 'chrome://resources/polymer/v1_0/polymer/polymer.html')); | 97 'chrome://resources/polymer/v1_0/polymer/polymer.html')); |
| 95 } | 98 } |
| 96 if (typeof TestHelpers != 'object') { | 99 if (typeof MockInteractions != 'object') { |
| 100 // Avoid importing the HTML file because iron-test-helpers assumes it is |
| 101 // not being imported separately alongside a vulcanized Polymer. |
| 97 promises.push( | 102 promises.push( |
| 98 PolymerTest.importHtml( | 103 PolymerTest.loadScript( |
| 99 'chrome://resources/polymer/v1_0/iron-test-helpers/' + | 104 'chrome://resources/polymer/v1_0/iron-test-helpers/' + |
| 100 'iron-test-helpers.html')); | 105 'mock-interactions.js')); |
| 101 } | 106 } |
| 102 return Promise.all(promises); | 107 return Promise.all(promises); |
| 103 }); | 108 }); |
| 104 }, | 109 }, |
| 105 | 110 |
| 106 /** @override */ | 111 /** @override */ |
| 107 runTest: function(testBody) { | 112 runTest: function(testBody) { |
| 108 this.runTime = window.performance.now(); | 113 this.runTime = window.performance.now(); |
| 109 testing.Test.prototype.runTest.call(this, testBody); | 114 testing.Test.prototype.runTest.call(this, testBody); |
| 110 }, | 115 }, |
| 111 | 116 |
| 112 /** @override */ | 117 /** @override */ |
| 113 tearDown: function() { | 118 tearDown: function() { |
| 114 var endTime = window.performance.now(); | 119 var endTime = window.performance.now(); |
| 115 var delta = this.runTime - this.preloadTime; | 120 var delta = this.runTime - this.preloadTime; |
| 116 console.log('Page load time: ' + delta.toFixed(0) + " ms"); | 121 console.log('Page load time: ' + delta.toFixed(0) + " ms"); |
| 117 delta = endTime - this.runTime; | 122 delta = endTime - this.runTime; |
| 118 console.log('Test run time: ' + delta.toFixed(0) + " ms"); | 123 console.log('Test run time: ' + delta.toFixed(0) + " ms"); |
| 119 delta = endTime - this.preloadTime; | 124 delta = endTime - this.preloadTime; |
| 120 console.log('Total time: ' + delta.toFixed(0) + " ms"); | 125 console.log('Total time: ' + delta.toFixed(0) + " ms"); |
| 121 testing.Test.prototype.tearDown.call(this); | 126 testing.Test.prototype.tearDown.call(this); |
| 122 } | 127 } |
| 123 }; | 128 }; |
| 124 | 129 |
| 125 /** | 130 /** |
| 126 * Imports the HTML file. | 131 * Imports the HTML file. |
| 127 * @param {string} src The URL to load. | 132 * @param {string} src The URL to load. |
| 128 * @return {Promise} A promise that is resolved/rejected on success/failure. | 133 * @return {!Promise} A promise that is resolved/rejected on success/failure. |
| 129 */ | 134 */ |
| 130 PolymerTest.importHtml = function(src) { | 135 PolymerTest.importHtml = function(src) { |
| 131 PolymerTest.importUrls_.push(src); | 136 PolymerTest.importUrls_.push(src); |
| 132 var link = document.createElement('link'); | 137 var link = document.createElement('link'); |
| 133 link.rel = 'import'; | 138 link.rel = 'import'; |
| 134 var promise = new Promise(function(resolve, reject) { | 139 var promise = new Promise(function(resolve, reject) { |
| 135 link.onload = resolve; | 140 link.onload = resolve; |
| 136 link.onerror = reject; | 141 link.onerror = reject; |
| 137 }); | 142 }); |
| 138 link.href = src; | 143 link.href = src; |
| 139 document.head.appendChild(link); | 144 document.head.appendChild(link); |
| 140 return promise; | 145 return promise; |
| 141 }; | 146 }; |
| 142 | 147 |
| 143 /** | 148 /** |
| 149 * Loads the script file. |
| 150 * @param {string} src The URL to load. |
| 151 * @return {!Promise} A promise that is resolved/rejected on success/failure. |
| 152 */ |
| 153 PolymerTest.loadScript = function(src) { |
| 154 PolymerTest.scriptUrls_.push(src); |
| 155 var script = document.createElement('script'); |
| 156 var promise = new Promise(function(resolve, reject) { |
| 157 script.onload = resolve; |
| 158 script.onerror = reject; |
| 159 }); |
| 160 script.src = src; |
| 161 document.head.appendChild(script); |
| 162 return promise; |
| 163 }; |
| 164 |
| 165 /** |
| 144 * Removes all content from the body. | 166 * Removes all content from the body. |
| 145 */ | 167 */ |
| 146 PolymerTest.clearBody = function() { | 168 PolymerTest.clearBody = function() { |
| 147 document.body.innerHTML = ''; | 169 document.body.innerHTML = ''; |
| 148 }; | 170 }; |
| 149 | 171 |
| 150 /** | 172 /** |
| 151 * Helper function to return the list of extra libraries relative to basePath. | 173 * Helper function to return the list of extra libraries relative to basePath. |
| 152 */ | 174 */ |
| 153 PolymerTest.getLibraries = function(basePath) { | 175 PolymerTest.getLibraries = function(basePath) { |
| 154 // Ensure basePath ends in '/'. | 176 // Ensure basePath ends in '/'. |
| 155 if (basePath.length && basePath[basePath.length - 1] != '/') | 177 if (basePath.length && basePath[basePath.length - 1] != '/') |
| 156 basePath += '/'; | 178 basePath += '/'; |
| 157 | 179 |
| 158 return PolymerTest.prototype.extraLibraries.map(function(library) { | 180 return PolymerTest.prototype.extraLibraries.map(function(library) { |
| 159 return basePath + library; | 181 return basePath + library; |
| 160 }); | 182 }); |
| 161 }; | 183 }; |
| OLD | NEW |