diff options
| author | Stefan Winkler | 2014-04-02 12:02:45 +0000 |
|---|---|---|
| committer | Paul Webster | 2014-04-04 20:00:09 +0000 |
| commit | bbcd42522c3f7fb76f0ec98666ef20786297a58e (patch) | |
| tree | 8cb0ea33c92c3b4f3143bb91b6eb748f4ad431b6 | |
| parent | cf7be5c8f93e003afa54c4928cda1d57130ce10f (diff) | |
| download | eclipse.platform.ui-bbcd42522c3f7fb76f0ec98666ef20786297a58e.tar.gz eclipse.platform.ui-bbcd42522c3f7fb76f0ec98666ef20786297a58e.tar.xz eclipse.platform.ui-bbcd42522c3f7fb76f0ec98666ef20786297a58e.zip | |
Bug 419482 - [CSS] Cascading policy used to apply rules is broken
- added testcases for different scenarios
Change-Id: I6192e8b9c1eb8476fad9cb57e0d7e5ee74f9a3e8
Signed-off-by: Stefan Winkler <stefan@winklerweb.net>
4 files changed, 228 insertions, 7 deletions
diff --git a/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/parser/ViewCSSTest.java b/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/parser/ViewCSSTest.java index 92da1f4bf23..e6cc6c40d7f 100644 --- a/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/parser/ViewCSSTest.java +++ b/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/parser/ViewCSSTest.java @@ -1,11 +1,12 @@ /******************************************************************************* * Copyright (c) 2009 EclipseSource and others. All rights reserved. * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * EclipseSource - initial API and implementation + * Stefan Winkler <stefan@winklerweb.net> - Bug 419482 ******************************************************************************/ package org.eclipse.e4.ui.tests.css.core.parser; @@ -23,12 +24,12 @@ import org.w3c.dom.css.CSSStyleDeclaration; import org.w3c.dom.css.CSSStyleSheet; import org.w3c.dom.css.ViewCSS; - public class ViewCSSTest extends TestCase { private Display display; private CSSSWTEngineImpl engine; + @Override protected void setUp() throws Exception { display = Display.getDefault(); engine = new CSSSWTEngineImpl(display); @@ -38,8 +39,8 @@ public class ViewCSSTest extends TestCase { // Two rules with the same specificity, the second rule should take // precedence because of its position in the stylesheet String css = "Label { color: black; }" - + "Button { color: blue; font-weight: bold; }\n" - + "Button { color: green; }\n"; + + "Button { color: blue; font-weight: bold; }\n" + + "Button { color: green; }\n"; ViewCSS viewCSS = createViewCss(css); TestElement shell = new TestElement("Shell", engine); @@ -57,6 +58,57 @@ public class ViewCSSTest extends TestCase { assertEquals( 2, buttonStyle.getLength() ); } + public void testBug419482_order1() throws Exception + { + String css = "Shell > * > * { color: red; }\n" + + "Button { color: blue; }\n"; + ViewCSS viewCSS = createViewCss(css); + + final TestElement shell = new TestElement("Shell", engine); + final TestElement composite = new TestElement("Composite", shell, + engine); + final TestElement button = new TestElement("Button", composite, engine); + + CSSStyleDeclaration buttonStyle = viewCSS.getComputedStyle(button, null); + assertNotNull( buttonStyle ); + assertEquals( 1, buttonStyle.getLength() ); + assertEquals("color: blue;", buttonStyle.getCssText()); + } + + public void testBug419482_order2() throws Exception { + String css = "Button { color: blue; }\n" + + "Shell > * > * { color: red; }\n"; + ViewCSS viewCSS = createViewCss(css); + + final TestElement shell = new TestElement("Shell", engine); + final TestElement composite = new TestElement("Composite", shell, + engine); + final TestElement button = new TestElement("Button", composite, engine); + + CSSStyleDeclaration buttonStyle = viewCSS + .getComputedStyle(button, null); + assertNotNull(buttonStyle); + assertEquals(1, buttonStyle.getLength()); + assertEquals("color: red;", buttonStyle.getCssText()); + } + + public void testBug419482_higherSpecificity() throws Exception { + String css = "Shell > * > Button { color: blue; }\n" + + "Shell > * > * { color: red; }\n"; + ViewCSS viewCSS = createViewCss(css); + + final TestElement shell = new TestElement("Shell", engine); + final TestElement composite = new TestElement("Composite", shell, + engine); + final TestElement button = new TestElement("Button", composite, engine); + + CSSStyleDeclaration buttonStyle = viewCSS + .getComputedStyle(button, null); + assertNotNull(buttonStyle); + assertEquals(1, buttonStyle.getLength()); + assertEquals("color: blue;", buttonStyle.getCssText()); + } + private static ViewCSS createViewCss(String css) throws IOException { CSSStyleSheet styleSheet = ParserTestUtil.parseCss(css); DocumentCSSImpl docCss = new DocumentCSSImpl(); diff --git a/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/util/TestElement.java b/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/util/TestElement.java index a5f46bc712f..b1f02c76773 100644 --- a/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/util/TestElement.java +++ b/tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/util/TestElement.java @@ -1,11 +1,12 @@ /******************************************************************************* * Copyright (c) 2009 EclipseSource and others. All rights reserved. * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * EclipseSource - initial API and implementation + * Stefan Winkler <stefan@winklerweb.net> - Bug 419482 ******************************************************************************/ package org.eclipse.e4.ui.tests.css.core.util; @@ -14,6 +15,7 @@ import java.util.Map; import org.eclipse.e4.ui.css.core.dom.ElementAdapter; import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -26,53 +28,68 @@ public class TestElement extends ElementAdapter { private String className; private String id; private Map attrs = new HashMap(); + private Node parentNode = null; public TestElement(String type, CSSEngine engine) { super(null, engine); this.typeName = type; } + public TestElement(String type, TestElement parent, CSSSWTEngineImpl engine) { + this(type, engine); + this.parentNode = parent; + } + public void setClass(String className) { this.className = className; } - + public void setId(String id) { this.id = id; } + @Override public void setAttribute(String name, String value) { attrs.put(name, value); } + @Override public String getAttribute(String name) { String value = (String) attrs.get(name); return value == null ? "" : value; } + @Override public String getLocalName() { return typeName; } + @Override public NodeList getChildNodes() { return null; } + @Override public String getNamespaceURI() { return null; } + @Override public Node getParentNode() { - return null; + return parentNode; } + @Override public String getCSSClass() { return className; } + @Override public String getCSSId() { return id; } + @Override public String getCSSStyle() { return null; } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/Bug419482Test.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/Bug419482Test.java new file mode 100644 index 00000000000..2eb5d0d02c4 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/Bug419482Test.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2014 Stefan Winkler and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Stefan Winkler <stefan@winklerweb.net> - initial contribution + *******************************************************************************/ +package org.eclipse.e4.ui.tests.css.swt; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.RowLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.ToolBar; + +public class Bug419482Test extends CSSSWTTestCase { + + private static final RGB RGB_BLUE = new RGB(0, 0, 255); + private static final RGB RGB_RED = new RGB(255, 0, 0); + + private CSSEngine engine; + private ToolBar toolbar1; + private ToolBar toolbar2; + private ToolBar toolbar3; + + public void testTwoLevelsWildcard() throws Exception { + String cssString = "Shell > * > * { color: red; } \n" + + "Label { color: blue; }"; + + Label label = createTestLabel(cssString); + + RGB rgb = label.getForeground().getRGB(); + assertEquals(RGB_BLUE, rgb); + } + + public void testOneLevelWildcardOneSpecific() throws Exception { + String cssString = "Shell > * > Label { color: red; } \n" + + "Label { color: blue; }"; + + Label label = createTestLabel(cssString); + + RGB rgb = label.getForeground().getRGB(); + assertEquals(RGB_RED, rgb); + } + + public void testDescendentsWildcard() throws Exception { + String cssString = "Shell * { color: red; } \n" + + "Label { color: blue; }"; + + Label label = createTestLabel(cssString); + + RGB rgb = label.getForeground().getRGB(); + assertEquals(RGB_BLUE, rgb); + } + + public void testDescendentsSpecific() throws Exception { + String cssString = "Shell Label { color: red; } \n" + + "Label { color: blue; }"; + + Label label = createTestLabel(cssString); + + RGB rgb = label.getForeground().getRGB(); + assertEquals(RGB_RED, rgb); + } + + + private Label createTestLabel(String styleSheet) { + Display display = Display.getDefault(); + engine = createEngine(styleSheet, display); + + // Create widgets + Shell shell = new Shell(display, SWT.SHELL_TRIM); + FillLayout layout = new FillLayout(); + shell.setLayout(layout); + + Composite composite = new Composite(shell, SWT.NONE); + composite.setLayout(new FillLayout()); + + Label labelToTest = new Label(composite, SWT.NONE); + labelToTest.setText("Some label text"); + + // Apply styles + engine.applyStyles(labelToTest, true); + return labelToTest; + } + + public void testOriginalBugReport() { + String css = "Shell, Shell > *, Shell > * > * {\n" + + " background-color: red;\n" + + "}\n" + + "ToolBar {\n" + + " background-color: blue;\n" + + "}"; + + Display display = Display.getDefault(); + engine = createEngine(css, display); + + Shell shell = createShellWithToolbars(display); + + // Apply styles + engine.applyStyles(shell, true); + + assertEquals(RGB_BLUE, toolbar1.getBackground().getRGB()); + assertEquals(RGB_BLUE, toolbar2.getBackground().getRGB()); + assertEquals(RGB_BLUE, toolbar3.getBackground().getRGB()); + } + + public void testOriginalBugReportDifferentOrder() { + String css = "ToolBar {\n" + " background-color: blue;\n" + "}" + + "Shell, Shell > *, Shell > * > * {\n" + + " background-color: red;\n" + "}\n"; + + Display display = Display.getDefault(); + engine = createEngine(css, display); + + // Create widgets + Shell shell = createShellWithToolbars(display); + + // Apply styles + engine.applyStyles(shell, true); + + assertEquals(RGB_RED, toolbar1.getBackground().getRGB()); + assertEquals(RGB_RED, toolbar2.getBackground().getRGB()); + assertEquals(RGB_BLUE, toolbar3.getBackground().getRGB()); + } + + private Shell createShellWithToolbars(Display display) { + Shell shell = new Shell(display, SWT.SHELL_TRIM); + shell.setLayout(new RowLayout(SWT.VERTICAL)); + + toolbar1 = new ToolBar(shell, SWT.BORDER); + Composite composite1 = new Composite(shell, SWT.NONE); + composite1.setLayout(new RowLayout(SWT.VERTICAL)); + + toolbar2 = new ToolBar(composite1, SWT.BORDER); + Composite composite2 = new Composite(composite1, SWT.NONE); + composite2.setLayout(new RowLayout(SWT.VERTICAL)); + + toolbar3 = new ToolBar(composite2, SWT.BORDER); + return shell; + } +} diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java index f070201168c..f6b870e2d85 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java @@ -6,6 +6,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Stefan Winkler <stefan@winklerweb.net> - Bug 419482 *******************************************************************************/ package org.eclipse.e4.ui.tests.css.swt; @@ -61,5 +62,6 @@ public class CssSwtTestSuite extends TestSuite { addTestSuite(DescendentTest.class); addTestSuite(ThemeTest.class); + addTestSuite(Bug419482Test.class); } } |
