2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
7 from os
import path
as os_path
9 from sys
import path
as sys_path
12 _HERE
= os_path
.dirname(os_path
.abspath(__file__
))
13 sys_path
.append(os_path
.join(_HERE
, '..', '..', '..', 'tools'))
15 import find_depot_tools
# pylint: disable=W0611
16 from testing_support
.super_mox
import SuperMoxTestBase
19 class CssCheckerTest(SuperMoxTestBase
):
21 SuperMoxTestBase
.setUp(self
)
23 self
.fake_file_name
= 'fake.css'
25 self
.fake_file
= self
.mox
.CreateMockAnything()
26 self
.mox
.StubOutWithMock(self
.fake_file
, 'LocalPath')
27 self
.fake_file
.LocalPath().AndReturn(self
.fake_file_name
)
28 # Actual calls to NewContents() are defined in each test.
29 self
.mox
.StubOutWithMock(self
.fake_file
, 'NewContents')
31 self
.input_api
= self
.mox
.CreateMockAnything()
32 self
.input_api
.re
= re
33 self
.mox
.StubOutWithMock(self
.input_api
, 'AffectedSourceFiles')
34 self
.input_api
.AffectedFiles(
35 include_deletes
=False, file_filter
=None).AndReturn([self
.fake_file
])
37 # Actual creations of PresubmitPromptWarning are defined in each test.
38 self
.output_api
= self
.mox
.CreateMockAnything()
39 self
.mox
.StubOutWithMock(self
.output_api
, 'PresubmitPromptWarning',
40 use_mock_anything
=True)
42 self
.output_api
= self
.mox
.CreateMockAnything()
43 self
.mox
.StubOutWithMock(self
.output_api
, 'PresubmitNotifyResult',
44 use_mock_anything
=True)
46 def VerifyContentsIsValid(self
, contents
):
47 self
.fake_file
.NewContents().AndReturn(contents
.splitlines())
49 css_checker
.CSSChecker(self
.input_api
, self
.output_api
).RunChecks()
51 def VerifyContentsProducesOutput(self
, contents
, output
):
52 self
.fake_file
.NewContents().AndReturn(contents
.splitlines())
53 author_msg
= ('Was the CSS checker useful? '
54 'Send feedback or hate mail to dbeam@chromium.org.')
55 self
.output_api
.PresubmitNotifyResult(author_msg
).AndReturn(None)
56 self
.output_api
.PresubmitPromptWarning(
57 self
.fake_file_name
+ ':\n' + output
.strip()).AndReturn(None)
59 css_checker
.CSSChecker(self
.input_api
, self
.output_api
).RunChecks()
61 def testCssAlphaWithAtBlock(self
):
62 self
.VerifyContentsProducesOutput("""
63 <include src="../shared/css/cr/ui/overlay.css">
64 <include src="chrome://resources/totally-cool.css" />
66 /* A hopefully safely ignored comment and @media statement. /**/
76 <if expr="not is macosx">
77 background-image: url(chrome://resources/BLAH); /* TODO(dbeam): Fix this. */
78 background-color: rgb(235, 239, 249);
81 background-color: white;
82 background-image: url(chrome://resources/BLAH2);
88 .language-options-right {
90 opacity: 1; /* TODO(dbeam): Fix this. */
93 - Alphabetize properties and list vendor specific (i.e. -webkit) above standard.
100 def testCssStringWithAt(self
):
101 self
.VerifyContentsIsValid("""
103 background-image: url(images/google_logo.png@2x);
106 body.alternate-logo #logo {
107 -webkit-mask-image: url(images/google_logo.png@2x);
118 def testCssAlphaWithNonStandard(self
):
119 self
.VerifyContentsProducesOutput("""
121 /* A hopefully safely ignored comment and @media statement. /**/
123 -webkit-margin-start: 5px;
125 - Alphabetize properties and list vendor specific (i.e. -webkit) above standard.
127 -webkit-margin-start: 5px;""")
129 def testCssAlphaWithLongerDashedProps(self
):
130 self
.VerifyContentsProducesOutput("""
132 border-left: 5px; /* A hopefully removed comment. */
133 border: 5px solid red;
135 - Alphabetize properties and list vendor specific (i.e. -webkit) above standard.
137 border: 5px solid red;""")
139 def testCssBracesHaveSpaceBeforeAndNothingAfter(self
):
140 self
.VerifyContentsProducesOutput("""
141 /* Hello! */div/* Comment here*/{
154 }; /* This should be ignored. */
157 .this.is { /* allowed */
160 - Start braces ({) end a selector, have a space before them and no rules after.
164 def testCssClassesUseDashes(self
):
165 self
.VerifyContentsProducesOutput("""
168 .class-name /* We should not catch this. */,
172 - Classes use .dash-form.
177 def testCssCloseBraceOnNewLine(self
):
178 self
.VerifyContentsProducesOutput("""
179 @media { /* TODO(dbeam) Fix this case. */
184 @-webkit-keyframe blah {
185 from { height: rotate(-10turn); }
186 100% { height: 500px; }
189 #id { /* ${TemplateExpressions} should be ignored. */
203 rule: value; }""", """
204 - Always put a rule closing brace (}) on a new line.
207 def testCssColonsHaveSpaceAfter(self
):
208 self
.VerifyContentsProducesOutput("""
209 div:not(.class):not([attr=5]), /* We should not catch this. */
210 div:not(.class):not([attr]) /* Nor this. */ {
211 background: url(data:image/jpeg,asdfasdfsadf); /* Ignore this. */
212 background: -webkit-linear-gradient(left, red,
217 - Colons (:) should have a space after them.
220 - Don't use data URIs in source files. Use grit instead.
221 background: url(data:image/jpeg,asdfasdfsadf);""")
223 def testCssFavorSingleQuotes(self
):
224 self
.VerifyContentsProducesOutput("""
225 html[dir="rtl"] body,
226 html[dir=ltr] body /* TODO(dbeam): Require '' around rtl in future? */ {
227 font-family: "Open Sans";
228 <if expr="is_macosx">
232 - Use single quotes (') instead of double quotes (") in strings.
233 html[dir="rtl"] body,
234 font-family: "Open Sans";""")
236 def testCssHexCouldBeShorter(self
):
237 self
.VerifyContentsProducesOutput("""
245 background-color: #336699; /* Ignore short hex rule if not gray. */
249 - Use abbreviated hex (#rgb) when in form #rrggbb.
250 color: #999999; (replace with #999)
252 - Use rgb() over #hex when not a shade of gray (like #333).
253 background-color: #336699; (replace with rgb(51, 102, 153))""")
255 def testCssUseMillisecondsForSmallTimes(self
):
256 self
.VerifyContentsProducesOutput("""
257 .transition-0s /* This is gross but may happen. */ {
261 transform: four 300ms;
263 - Use milliseconds for time measurements under 1 second.
264 transform: one 0.2s; (replace with 200ms)
265 transform: two .1s; (replace with 100ms)""")
267 def testCssNoDataUrisInSourceFiles(self
):
268 self
.VerifyContentsProducesOutput("""
270 background: url( data:image/jpeg,4\/\/350|\/|3|2 );
272 - Don't use data URIs in source files. Use grit instead.
273 background: url( data:image/jpeg,4\/\/350|\/|3|2 );""")
275 def testCssNoQuotesInUrl(self
):
276 self
.VerifyContentsProducesOutput("""
278 background: url('chrome://resources/images/blah.jpg');
279 background: url("../../folder/hello.png");
281 - Use single quotes (') instead of double quotes (") in strings.
282 background: url("../../folder/hello.png");
284 - Don't use quotes in url().
285 background: url('chrome://resources/images/blah.jpg');
286 background: url("../../folder/hello.png");""")
288 def testCssOneRulePerLine(self
):
289 self
.VerifyContentsProducesOutput("""
290 a:not([hidden]):not(.custom-appearance):not([version=1]):first-of-type,
291 a:not([hidden]):not(.custom-appearance):not([version=1]):first-of-type ~
292 input[type='checkbox']:not([hidden]),
294 background: url(chrome://resources/BLAH);
295 rule: value; /* rule: value; */
296 rule: value; rule: value;
305 - One rule per line (what not to do: color: red; margin: 0;).
306 rule: value; rule: value;""")
308 def testCssOneSelectorPerLine(self
):
309 self
.VerifyContentsProducesOutput("""
312 div,/* Hello! */ span,
313 #id.class([dir=rtl):not(.class):any(a, b, d) {
319 some-other: rule here;
321 - One selector per line (what not to do: a, b {}).
326 def testCssPseudoElementDoubleColon(self
):
327 self
.VerifyContentsProducesOutput("""
330 ::-webkit-scrollbar-thumb,
331 a:not([empty]):hover:focus:active, /* shouldn't catch here and above */
333 .tree-label:empty:after,
338 - Pseudo-elements should use double colon (i.e. ::after).
339 :after (should be ::after)
340 :after (should be ::after)
341 :before (should be ::before)
342 :-WebKit-ScrollBar (should be ::-WebKit-ScrollBar)
345 def testCssRgbIfNotGray(self
):
346 self
.VerifyContentsProducesOutput("""
350 background: -webkit-linear-gradient(left, from(#abc), to(#def));
354 - Use rgb() over #hex when not a shade of gray (like #333).
355 background: -webkit-linear-gradient(left, from(#abc), to(#def)); """
356 """(replace with rgb(170, 187, 204), rgb(221, 238, 255))
357 color: #bad; (replace with rgb(187, 170, 221))
358 color: #bada55; (replace with rgb(186, 218, 85))""")
360 def testWebkitBeforeOrAfter(self
):
361 self
.VerifyContentsProducesOutput("""
363 -webkit-margin-before: 10px;
364 -webkit-margin-start: 20px;
365 -webkit-padding-after: 3px;
366 -webkit-padding-end: 5px;
369 - Use *-top/bottom instead of -webkit-*-before/after.
370 -webkit-margin-before: 10px; (replace with margin-top)
371 -webkit-padding-after: 3px; (replace with padding-bottom)""")
373 def testCssZeroWidthLengths(self
):
374 self
.VerifyContentsProducesOutput("""
375 @-webkit-keyframe anim {
376 0% { /* Ignore key frames */
388 background-image: url(images/google_logo.png@2x);
391 body.alternate-logo #logo {
392 -webkit-mask-image: url(images/google_logo.png@2x);
395 /* http://crbug.com/359682 */
396 #spinner-container #spinner {
397 -webkit-animation-duration: 1.0s;
400 .media-button.play > .state0.active,
401 .media-button[state='0'] > .state0.normal /* blah */, /* blee */
402 .media-button[state='0']:not(.disabled):hover > .state0.hover {
403 -webkit-animation: anim 0s;
404 -webkit-animation-duration: anim 0ms;
405 -webkit-transform: scale(0%);
406 background-position-x: 0em;
407 background-position-y: 0ex;
409 color: hsl(0, 0%, 85%); /* Shouldn't trigger error. */
420 - Use "0" for zero-width lengths (i.e. 0px -> 0)
422 -webkit-transform: scale(0%);
423 background-position-x: 0em;
424 background-position-y: 0ex;
435 if __name__
== '__main__':