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