fontsizemonitor_test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.dom.FontSizeMonitorTest');
  15. goog.setTestOnly('goog.dom.FontSizeMonitorTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.FontSizeMonitor');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.events');
  20. goog.require('goog.events.Event');
  21. goog.require('goog.testing.PropertyReplacer');
  22. goog.require('goog.testing.events');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.userAgent');
  25. function isBuggyGecko() {
  26. return goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9');
  27. }
  28. var monitor;
  29. function setUp() {
  30. monitor = new goog.dom.FontSizeMonitor();
  31. }
  32. function tearDown() {
  33. monitor.dispose();
  34. }
  35. function getResizeTarget() {
  36. return goog.userAgent.IE ?
  37. monitor.sizeElement_ :
  38. goog.dom.getFrameContentWindow(monitor.sizeElement_);
  39. }
  40. function testFontSizeNoChange() {
  41. // This tests that firing the resize event without changing the font-size
  42. // does not trigger the event.
  43. var fired = false;
  44. goog.events.listen(
  45. monitor, goog.dom.FontSizeMonitor.EventType.CHANGE,
  46. function(e) { fired = true; });
  47. var resizeEvent = new goog.events.Event('resize', getResizeTarget());
  48. goog.testing.events.fireBrowserEvent(resizeEvent);
  49. assertFalse('The font size should not have changed', fired);
  50. }
  51. function testFontSizeChanged() {
  52. // One can trigger the iframe resize by changing the
  53. // document.body.style.fontSize but the event is fired asynchronously in
  54. // Firefox. Instead, we just override the lastWidth_ to simulate that the
  55. // size changed.
  56. var fired = false;
  57. goog.events.listen(
  58. monitor, goog.dom.FontSizeMonitor.EventType.CHANGE,
  59. function(e) { fired = true; });
  60. monitor.lastWidth_--;
  61. var resizeEvent = new goog.events.Event('resize', getResizeTarget());
  62. goog.testing.events.fireBrowserEvent(resizeEvent);
  63. assertTrue('The font size should have changed', fired);
  64. }
  65. function testCreateAndDispose() {
  66. var frameCount = window.frames.length;
  67. var iframeElementCount =
  68. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  69. var divElementCount =
  70. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  71. var monitor = new goog.dom.FontSizeMonitor();
  72. monitor.dispose();
  73. var newFrameCount = window.frames.length;
  74. var newIframeElementCount =
  75. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  76. var newDivElementCount =
  77. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  78. assertEquals(
  79. 'There should be no trailing frames', frameCount + isBuggyGecko(),
  80. newFrameCount);
  81. assertEquals(
  82. 'There should be no trailing iframe elements',
  83. iframeElementCount + isBuggyGecko(), newIframeElementCount);
  84. assertEquals(
  85. 'There should be no trailing div elements', divElementCount,
  86. newDivElementCount);
  87. }
  88. function testWithDomHelper() {
  89. var frameCount = window.frames.length;
  90. var iframeElementCount =
  91. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  92. var divElementCount =
  93. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  94. var monitor = new goog.dom.FontSizeMonitor(goog.dom.getDomHelper());
  95. var newFrameCount = window.frames.length;
  96. var newIframeElementCount =
  97. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  98. var newDivElementCount =
  99. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  100. if (goog.userAgent.IE) {
  101. assertEquals(
  102. 'There should be one new div element', divElementCount + 1,
  103. newDivElementCount);
  104. } else {
  105. assertEquals(
  106. 'There should be one new frame', frameCount + 1, newFrameCount);
  107. assertEquals(
  108. 'There should be one new iframe element', iframeElementCount + 1,
  109. newIframeElementCount);
  110. }
  111. // Use the first iframe in the doc. This is added in the HTML markup.
  112. var win = window.frames[0];
  113. var doc = win.document;
  114. doc.open();
  115. doc.write('<html><body></body></html>');
  116. doc.close();
  117. var domHelper = goog.dom.getDomHelper(doc);
  118. var frameCount2 = win.frames.length;
  119. var iframeElementCount2 =
  120. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME, doc).length;
  121. var divElementCount2 =
  122. goog.dom.getElementsByTagName(goog.dom.TagName.DIV, doc).length;
  123. var monitor2 = new goog.dom.FontSizeMonitor(domHelper);
  124. var newFrameCount2 = win.frames.length;
  125. var newIframeElementCount2 =
  126. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME, doc).length;
  127. var newDivElementCount2 =
  128. goog.dom.getElementsByTagName(goog.dom.TagName.DIV, doc).length;
  129. if (goog.userAgent.IE) {
  130. assertEquals(
  131. 'There should be one new div element', divElementCount2 + 1,
  132. newDivElementCount2);
  133. } else {
  134. assertEquals(
  135. 'There should be one new frame', frameCount2 + 1, newFrameCount2);
  136. assertEquals(
  137. 'There should be one new iframe element', iframeElementCount2 + 1,
  138. newIframeElementCount2);
  139. }
  140. monitor.dispose();
  141. monitor2.dispose();
  142. }
  143. function testEnsureThatDocIsOpenedForGecko() {
  144. var pr = new goog.testing.PropertyReplacer();
  145. pr.set(goog.userAgent, 'GECKO', true);
  146. pr.set(goog.userAgent, 'IE', false);
  147. var openCalled = false;
  148. var closeCalled = false;
  149. var instance = {
  150. document: {
  151. open: function() { openCalled = true; },
  152. close: function() { closeCalled = true; }
  153. },
  154. attachEvent: function() {}
  155. };
  156. pr.set(goog.dom, 'getFrameContentWindow', function() { return instance; });
  157. try {
  158. var monitor = new goog.dom.FontSizeMonitor();
  159. assertTrue('doc.open should have been called', openCalled);
  160. assertTrue('doc.close should have been called', closeCalled);
  161. monitor.dispose();
  162. } finally {
  163. pr.reset();
  164. }
  165. }
  166. function testFirefox2WorkAroundFirefox3() {
  167. var pr = new goog.testing.PropertyReplacer();
  168. pr.set(goog.userAgent, 'GECKO', true);
  169. pr.set(goog.userAgent, 'IE', false);
  170. try {
  171. // 1.9 should clear iframes
  172. pr.set(goog.userAgent, 'VERSION', '1.9');
  173. goog.userAgent.isVersionOrHigherCache_ = {};
  174. var frameCount = window.frames.length;
  175. var iframeElementCount =
  176. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  177. var divElementCount =
  178. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  179. var monitor = new goog.dom.FontSizeMonitor();
  180. monitor.dispose();
  181. var newFrameCount = window.frames.length;
  182. var newIframeElementCount =
  183. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  184. var newDivElementCount =
  185. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  186. assertEquals(
  187. 'There should be no trailing frames', frameCount, newFrameCount);
  188. assertEquals(
  189. 'There should be no trailing iframe elements', iframeElementCount,
  190. newIframeElementCount);
  191. assertEquals(
  192. 'There should be no trailing div elements', divElementCount,
  193. newDivElementCount);
  194. } finally {
  195. pr.reset();
  196. }
  197. }
  198. function testFirefox2WorkAroundFirefox2() {
  199. var pr = new goog.testing.PropertyReplacer();
  200. pr.set(goog.userAgent, 'GECKO', true);
  201. pr.set(goog.userAgent, 'IE', false);
  202. try {
  203. // 1.8 should NOT clear iframes
  204. pr.set(goog.userAgent, 'VERSION', '1.8');
  205. goog.userAgent.isVersionOrHigherCache_ = {};
  206. var frameCount = window.frames.length;
  207. var iframeElementCount =
  208. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  209. var divElementCount =
  210. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  211. var monitor = new goog.dom.FontSizeMonitor();
  212. monitor.dispose();
  213. var newFrameCount = window.frames.length;
  214. var newIframeElementCount =
  215. goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME).length;
  216. var newDivElementCount =
  217. goog.dom.getElementsByTagName(goog.dom.TagName.DIV).length;
  218. assertEquals(
  219. 'There should be no trailing frames', frameCount + 1, newFrameCount);
  220. assertEquals(
  221. 'There should be no trailing iframe elements', iframeElementCount + 1,
  222. newIframeElementCount);
  223. assertEquals(
  224. 'There should be no trailing div elements', divElementCount,
  225. newDivElementCount);
  226. } finally {
  227. pr.reset();
  228. }
  229. }