1 /*******************************************************************************
2  * Copyright (c) 2008, 2014 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Thibault Le Ouay <thibaultleouay@gmail.com> - Bug 443094
14  *******************************************************************************/
15 package org.eclipse.e4.ui.tests.css.swt;
16 
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertNotSame;
19 
20 import java.util.HashSet;
21 
22 import org.eclipse.e4.ui.css.swt.dom.WidgetElement;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.FontData;
25 import org.eclipse.swt.graphics.RGB;
26 import org.eclipse.swt.layout.FillLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Shell;
29 import org.junit.Test;
30 
31 public class ShellTest extends CSSSWTTestCase {
32 
33 	static final RGB RED = new RGB(255, 0, 0);
34 	static final RGB GREEN = new RGB(0, 255, 0);
35 	static final RGB BLUE = new RGB(0, 0, 255);
36 
createTestShell(String styleSheet)37 	protected Shell createTestShell(String styleSheet) {
38 		engine = createEngine(styleSheet, display);
39 
40 		// Create widgets
41 		Shell shell = new Shell(display, SWT.SHELL_TRIM);
42 		FillLayout layout = new FillLayout();
43 		shell.setLayout(layout);
44 
45 		Composite panel = new Composite(shell, SWT.NONE);
46 		panel.setLayout(new FillLayout());
47 
48 		// Apply styles
49 		engine.applyStyles(shell, true);
50 
51 		shell.pack();
52 		return shell;
53 	}
54 
55 
56 	@Test
testColor()57 	public void testColor() {
58 		Shell shellToTest = createTestShell("Shell { background-color: #FF0000; color: #0000FF }");
59 		assertEquals(RED, shellToTest.getBackground().getRGB());
60 		assertEquals(BLUE, shellToTest.getForeground().getRGB());
61 	}
62 
63 	@Test
testFontRegular()64 	public void testFontRegular() {
65 		Shell shellToTest = createTestShell("Shell { font: Verdana 16px }");
66 		assertEquals(1, shellToTest.getFont().getFontData().length);
67 		FontData fontData = shellToTest.getFont().getFontData()[0];
68 		assertEquals("Verdana", fontData.getName());
69 		assertEquals(16, fontData.getHeight());
70 		assertEquals(SWT.NORMAL, fontData.getStyle());
71 	}
72 
73 	@Test
testFontBold()74 	public void testFontBold() {
75 		Shell shellToTest = createTestShell("Shell { font: Arial 12px; font-weight: bold }");
76 		assertEquals(1, shellToTest.getFont().getFontData().length);
77 		FontData fontData = shellToTest.getFont().getFontData()[0];
78 		assertEquals("Arial", fontData.getName());
79 		assertEquals(12, fontData.getHeight());
80 		assertEquals(SWT.BOLD, fontData.getStyle());
81 	}
82 
83 	@Test
testFontItalic()84 	public void testFontItalic() {
85 		Shell shellToTest = createTestShell("Shell { font-style: italic }");
86 		assertEquals(1, shellToTest.getFont().getFontData().length);
87 		FontData fontData = shellToTest.getFont().getFontData()[0];
88 		assertEquals(SWT.ITALIC, fontData.getStyle());
89 	}
90 
91 	// bug 375069: ensure children aren't caught up in parent
92 	@Test
test375069ChildShellDifferentiation()93 	public void test375069ChildShellDifferentiation() {
94 		engine = createEngine("Shell.parent { font-style: italic; }", display);
95 
96 		Shell parent = new Shell(display, SWT.NONE);
97 		WidgetElement.setCSSClass(parent, "parent");
98 		Shell child = new Shell(parent, SWT.NONE);
99 		WidgetElement.setCSSClass(child, "child");
100 		parent.open();
101 		child.open();
102 		engine.applyStyles(parent, true);
103 		engine.applyStyles(child, true);
104 
105 
106 		assertEquals(1, parent.getFont().getFontData().length);
107 		FontData fontData = parent.getFont().getFontData()[0];
108 		assertEquals(SWT.ITALIC, fontData.getStyle());
109 
110 		assertEquals(1, child.getFont().getFontData().length);
111 		fontData = child.getFont().getFontData()[0];
112 		assertNotSame(SWT.ITALIC, fontData.getStyle());
113 	}
114 
115 	// bug 375069: ensure children shells are still captured by Shell
116 	@Test
test375069AllShell()117 	public void test375069AllShell() {
118 		engine = createEngine("Shell { font-style: italic; }", display);
119 
120 		Shell parent = new Shell(display, SWT.NONE);
121 		WidgetElement.setCSSClass(parent, "parent");
122 		Shell child = new Shell(parent, SWT.NONE);
123 		WidgetElement.setCSSClass(child, "child");
124 		parent.open();
125 		child.open();
126 		engine.applyStyles(parent, true);
127 		engine.applyStyles(child, true);
128 
129 		assertEquals(1, parent.getFont().getFontData().length);
130 		FontData fontData = parent.getFont().getFontData()[0];
131 		assertEquals(SWT.ITALIC, fontData.getStyle());
132 
133 		assertEquals(1, child.getFont().getFontData().length);
134 		fontData = child.getFont().getFontData()[0];
135 		assertEquals(SWT.ITALIC, fontData.getStyle());
136 	}
137 
138 	// bug 375069: ensure children shells are still captured by Shell
139 	@Test
testShellParentage()140 	public void testShellParentage() {
141 		engine = createEngine(
142 				"Shell[parentage='parent'] { font-style: italic; }", display);
143 
144 		Shell parent = new Shell(display, SWT.NONE);
145 		WidgetElement.setID(parent, "parent");
146 		Shell child = new Shell(parent, SWT.NONE);
147 		WidgetElement.setID(child, "child");
148 		parent.open();
149 		child.open();
150 		engine.applyStyles(parent, true);
151 		engine.applyStyles(child, true);
152 
153 		assertEquals(1, parent.getFont().getFontData().length);
154 		FontData fontData = parent.getFont().getFontData()[0];
155 		assertNotSame(SWT.ITALIC, fontData.getStyle());
156 
157 		assertEquals(1, child.getFont().getFontData().length);
158 		fontData = child.getFont().getFontData()[0];
159 		assertEquals(SWT.ITALIC, fontData.getStyle());
160 	}
161 
162 	@Test
testShellUnparentedPseudoelement()163 	public void testShellUnparentedPseudoelement() {
164 		engine = createEngine(
165 				"Shell:swt-unparented { font-style: italic; }", display);
166 
167 		Shell parent = new Shell(display, SWT.NONE);
168 		WidgetElement.setCSSClass(parent, "parent");
169 		Shell child = new Shell(parent, SWT.NONE);
170 		WidgetElement.setCSSClass(child, "child");
171 		parent.open();
172 		child.open();
173 		engine.applyStyles(parent, true);
174 		engine.applyStyles(child, true);
175 
176 		assertEquals(1, parent.getFont().getFontData().length);
177 		FontData fontData = parent.getFont().getFontData()[0];
178 		assertEquals(SWT.ITALIC, fontData.getStyle());
179 
180 		assertEquals(1, child.getFont().getFontData().length);
181 		fontData = child.getFont().getFontData()[0];
182 		assertNotSame(SWT.ITALIC, fontData.getStyle());
183 	}
184 
185 	@Test
testShellParentedPseudoelement()186 	public void testShellParentedPseudoelement() {
187 		engine = createEngine(
188 				"Shell:swt-parented { font-style: italic; }", display);
189 
190 		Shell parent = new Shell(display, SWT.NONE);
191 		WidgetElement.setCSSClass(parent, "parent");
192 		Shell child = new Shell(parent, SWT.NONE);
193 		WidgetElement.setCSSClass(child, "child");
194 		parent.open();
195 		child.open();
196 		engine.applyStyles(parent, true);
197 		engine.applyStyles(child, true);
198 
199 		assertEquals(1, parent.getFont().getFontData().length);
200 		FontData fontData = parent.getFont().getFontData()[0];
201 		assertNotSame(SWT.ITALIC, fontData.getStyle());
202 
203 		assertEquals(1, child.getFont().getFontData().length);
204 		fontData = child.getFont().getFontData()[0];
205 		assertEquals(SWT.ITALIC, fontData.getStyle());
206 	}
207 
208 	@Test
testSwtDataClassAttribute()209 	public void testSwtDataClassAttribute() {
210 		engine = createEngine(
211 				"Shell[swt-data-class ~= 'java.util.HashSet'] { font-style: italic; }",
212 				display);
213 
214 		Shell parent = new Shell(display, SWT.NONE);
215 		parent.setData(new HashSet<>());
216 		parent.open();
217 		engine.applyStyles(parent, true);
218 
219 		assertEquals(1, parent.getFont().getFontData().length);
220 		FontData fontData = parent.getFont().getFontData()[0];
221 		assertEquals(SWT.ITALIC, fontData.getStyle());
222 	}
223 
224 	@Test
testBackgroundMode()225 	public void testBackgroundMode() {
226 		Shell shellToTest = createTestShell("Shell { swt-background-mode: force; }");
227 		assertEquals(SWT.INHERIT_FORCE, shellToTest.getBackgroundMode());
228 	}
229 
230 }