devcss_test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.debug.DevCssTest');
  15. goog.setTestOnly('goog.debug.DevCssTest');
  16. goog.require('goog.debug.DevCss');
  17. goog.require('goog.style');
  18. goog.require('goog.testing.jsunit');
  19. var el;
  20. function setUpPage() {
  21. el = document.getElementById('devcss-test-2');
  22. }
  23. // Since background color sometimes comes back like rgb(xxx, xxx, xxx)
  24. // or rgb(xxx,xxx,xxx) depending on browser.
  25. function spaceless(foo) {
  26. return foo.replace(/\s/g, '');
  27. }
  28. function testGetIe6CombinedSelectorText() {
  29. var devcssInstance = new goog.debug.DevCss();
  30. devcssInstance.ie6CombinedMatches_ = [];
  31. var css = '.class2 { -goog-ie6-selector:".class1_class2"; prop: val; }';
  32. var newCss = devcssInstance.getIe6CombinedSelectorText_(css);
  33. assertEquals('.class1_class2', newCss);
  34. assertArrayEquals(
  35. ['class1', 'class2'], devcssInstance.ie6CombinedMatches_[0].classNames);
  36. assertEquals(
  37. 'class1_class2', devcssInstance.ie6CombinedMatches_[0].combinedClassName);
  38. devcssInstance = new goog.debug.DevCss();
  39. devcssInstance.ie6CombinedMatches_ = [];
  40. css = '.class3 { prop: val; -goog-ie6-selector:".class1_class2_class3";' +
  41. 'prop: val; }';
  42. newCss = devcssInstance.getIe6CombinedSelectorText_(css);
  43. assertEquals('.class1_class2_class3', newCss);
  44. assertArrayEquals(
  45. ['class1', 'class2', 'class3'],
  46. devcssInstance.ie6CombinedMatches_[0].classNames);
  47. assertEquals(
  48. 'class1_class2_class3',
  49. devcssInstance.ie6CombinedMatches_[0].combinedClassName);
  50. devcssInstance = new goog.debug.DevCss();
  51. devcssInstance.ie6CombinedMatches_ = [];
  52. css = '.class3, .class5 {' +
  53. '-goog-ie6-selector:".class1_class2_class3, .class4_class5";' +
  54. 'prop: val; }';
  55. newCss = devcssInstance.getIe6CombinedSelectorText_(css);
  56. assertEquals('.class1_class2_class3, .class4_class5', newCss);
  57. assertArrayEquals(
  58. ['class1', 'class2', 'class3'],
  59. devcssInstance.ie6CombinedMatches_[0].classNames);
  60. assertEquals(
  61. 'class1_class2_class3',
  62. devcssInstance.ie6CombinedMatches_[0].combinedClassName);
  63. assertArrayEquals(
  64. ['class4', 'class5'], devcssInstance.ie6CombinedMatches_[1].classNames);
  65. assertEquals(
  66. 'class4_class5', devcssInstance.ie6CombinedMatches_[1].combinedClassName);
  67. }
  68. function testAddIe6CombinedClassNames() {
  69. var el_combined1 = document.getElementById('devcss-test-combined1');
  70. var el_combined2 = document.getElementById('devcss-test-combined2');
  71. var el_notcombined1 = document.getElementById('devcss-test-notcombined1');
  72. var el_notcombined2 = document.getElementById('devcss-test-notcombined2');
  73. var el_notcombined3 = document.getElementById('devcss-test-notcombined3');
  74. var devcssInstance = new goog.debug.DevCss();
  75. devcssInstance.ie6CombinedMatches_ = [
  76. {classNames: ['ie6-2', 'ie6-1'], combinedClassName: 'ie6-1_ie6-2', els: []},
  77. {
  78. classNames: ['ie6-2', 'ie6-3', 'ie6-1'],
  79. combinedClassName: 'ie6-1_ie6-2_ie6-3',
  80. els: []
  81. }
  82. ];
  83. devcssInstance.addIe6CombinedClassNames_();
  84. assertEquals(-1, el_notcombined1.className.indexOf('ie6-1_ie6-2'));
  85. assertEquals(-1, el_notcombined2.className.indexOf('ie6-1_ie6-2'));
  86. assertEquals(-1, el_notcombined3.className.indexOf('ie6-1_ie6-2_ie6-3'));
  87. assertTrue(el_combined1.className.indexOf('ie6-1_ie6-2') != -1);
  88. assertTrue(el_combined2.className.indexOf('ie6-1_ie6-2_ie6-3') != -1);
  89. }
  90. function testActivateBrowserSpecificCssALL() {
  91. // equals GECKO
  92. var devcssInstance = new goog.debug.DevCss('GECKO');
  93. devcssInstance.activateBrowserSpecificCssRules(false);
  94. var backgroundColor = goog.style.getBackgroundColor(el);
  95. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  96. // GECKO test case w/ two selectors joined by a commma.
  97. var elGeckoOne = document.getElementById('devcss-gecko-1');
  98. backgroundColor = goog.style.getBackgroundColor(elGeckoOne);
  99. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  100. var elGeckoTwo = document.getElementById('devcss-gecko-2');
  101. backgroundColor = goog.style.getBackgroundColor(elGeckoTwo);
  102. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  103. }
  104. function testActivateBrowserSpecificCssWithVersion() {
  105. // equals IE 6
  106. var devcssInstance = new goog.debug.DevCss('IE', '6');
  107. devcssInstance.activateBrowserSpecificCssRules(false);
  108. var elIe6 = document.getElementById('devcss-test-ie6');
  109. var backgroundColor = goog.style.getBackgroundColor(elIe6);
  110. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  111. // IE8 test case w/ two selectors joined by a commma.
  112. var devCssInstanceTwo = new goog.debug.DevCss('IE', '8');
  113. devCssInstanceTwo.activateBrowserSpecificCssRules(false);
  114. var elIe8One = document.getElementById('devcss-ie8-1');
  115. backgroundColor = goog.style.getBackgroundColor(elIe8One);
  116. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  117. var elIe8Two = document.getElementById('devcss-ie8-2');
  118. backgroundColor = goog.style.getBackgroundColor(elIe8Two);
  119. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  120. }
  121. function testActivateBrowserSpecificCssGteInvalid() {
  122. // WEBKIT_GTE255
  123. var marginBox = goog.style.getMarginBox(el);
  124. assertEquals(1, marginBox.top); // should still be 1
  125. var devcssInstance = new goog.debug.DevCss('WEBKIT', 254);
  126. devcssInstance.activateBrowserSpecificCssRules(false);
  127. var marginBox = goog.style.getMarginBox(el);
  128. assertEquals(1, marginBox.top); // should still be 1
  129. }
  130. function testActivateBrowserSpecificCssGteValid() {
  131. var devcssInstance = new goog.debug.DevCss('WEBKIT', 255);
  132. devcssInstance.activateBrowserSpecificCssRules(false);
  133. var marginBox = goog.style.getMarginBox(el);
  134. assertEquals(20, marginBox.top);
  135. }
  136. function testActivateBrowserSpecificCssLteInvalid() {
  137. // IE_LTE6
  138. var marginBox = goog.style.getMarginBox(el);
  139. assertEquals(1, marginBox.left); // should still be 1
  140. var devcssInstance = new goog.debug.DevCss('WEBKIT', 202);
  141. devcssInstance.activateBrowserSpecificCssRules(false);
  142. var marginBox = goog.style.getMarginBox(el);
  143. assertEquals(1, marginBox.left); // should still be 1
  144. }
  145. function testActivateBrowserSpecificCssLteValid() {
  146. var devcssInstance = new goog.debug.DevCss('WEBKIT', 199);
  147. devcssInstance.activateBrowserSpecificCssRules(false);
  148. var marginBox = goog.style.getMarginBox(el);
  149. assertEquals(15, marginBox.left);
  150. }
  151. function testReplaceIe6Selectors() {
  152. var devcssInstance = new goog.debug.DevCss('IE', 6);
  153. devcssInstance.activateBrowserSpecificCssRules(false);
  154. // It should correctly be transparent, even in IE6.
  155. var compound2El = document.getElementById('devcss-test-compound2');
  156. var backgroundColor = spaceless(goog.style.getBackgroundColor(compound2El));
  157. assertTrue(
  158. 'Unexpected background color: ' + backgroundColor,
  159. 'transparent' == backgroundColor || 'rgba(0,0,0,0)' == backgroundColor);
  160. // And this one should have the combined selector working, even in
  161. // IE6.
  162. backgroundColor = goog.style.getBackgroundColor(
  163. document.getElementById('devcss-test-compound1-2'));
  164. assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
  165. }
  166. /*
  167. * TODO(user): Re-enable if we ever come up with a way to make imports
  168. * work.
  169. function testDisableDuplicateStyleSheetImports() {
  170. var el1 = document.getElementById('devcss-test-importfixer-1');
  171. var el2 = document.getElementById('devcss-test-importfixer-2');
  172. var backgroundColor = goog.style.getBackgroundColor(el1);
  173. assertEquals('rgb(255,255,0)', spaceless(backgroundColor));
  174. var backgroundColor = goog.style.getBackgroundColor(el2);
  175. assertEquals('rgb(255,0,0)', spaceless(backgroundColor));
  176. // This should disable the second coming of devcss_test_import_1.css.
  177. var devcssInstance = new goog.debug.DevCss();
  178. devcssInstance.disableDuplicateStyleSheetImports();
  179. var backgroundColor = goog.style.getBackgroundColor(el1);
  180. assertEquals('rgb(255,255,0)', spaceless(backgroundColor));
  181. var backgroundColor = goog.style.getBackgroundColor(el2);
  182. assertEquals('rgb(173,216,230)', spaceless(backgroundColor));
  183. }
  184. */