Skip to content

Commit cff9a5d

Browse files
committed
more tests
1 parent 824467c commit cff9a5d

File tree

4 files changed

+745
-1
lines changed

4 files changed

+745
-1
lines changed

src/test/java/org/htmlunit/cssparser/dom/PropteryTest.java renamed to src/test/java/org/htmlunit/cssparser/dom/PropertyTest.java

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @author Ronald Brill
3030
*/
31-
public class PropteryTest {
31+
public class PropertyTest {
3232

3333
/**
3434
* @throws Exception if any error occurs
@@ -94,4 +94,82 @@ public void constructWithParams() throws Exception {
9494
assertEquals("13.2cm", prop.getValue().toString());
9595
assertFalse(prop.isImportant());
9696
}
97+
98+
/**
99+
* Test equals method with same object.
100+
* @throws Exception if any error occurs
101+
*/
102+
@Test
103+
public void equalsSameObject() throws Exception {
104+
final LexicalUnit lu = LexicalUnitImpl.createPixel(null, 10);
105+
final Property prop = new Property("color", new CSSValueImpl(lu, true), false);
106+
107+
assertTrue(prop.equals(prop));
108+
}
109+
110+
/**
111+
* Test equals method with different names.
112+
* @throws Exception if any error occurs
113+
*/
114+
@Test
115+
public void equalsDifferentNames() throws Exception {
116+
final LexicalUnit lu1 = LexicalUnitImpl.createPixel(null, 10);
117+
final Property prop1 = new Property("color", new CSSValueImpl(lu1, true), false);
118+
119+
final LexicalUnit lu2 = LexicalUnitImpl.createPixel(null, 10);
120+
final Property prop2 = new Property("background", new CSSValueImpl(lu2, true), false);
121+
122+
assertFalse(prop1.equals(prop2));
123+
}
124+
125+
/**
126+
* Test equals method with different important flags.
127+
* @throws Exception if any error occurs
128+
*/
129+
@Test
130+
public void equalsDifferentImportant() throws Exception {
131+
final Property prop1 = new Property("color", null, false);
132+
final Property prop2 = new Property("color", null, true);
133+
134+
assertFalse(prop1.equals(prop2));
135+
}
136+
137+
/**
138+
* Test equals method with null.
139+
* @throws Exception if any error occurs
140+
*/
141+
@Test
142+
public void equalsWithNull() throws Exception {
143+
final LexicalUnit lu = LexicalUnitImpl.createPixel(null, 10);
144+
final Property prop = new Property("color", new CSSValueImpl(lu, true), false);
145+
146+
assertFalse(prop.equals(null));
147+
}
148+
149+
/**
150+
* Test equals method with different class.
151+
* @throws Exception if any error occurs
152+
*/
153+
@Test
154+
public void equalsWithDifferentClass() throws Exception {
155+
final LexicalUnit lu = LexicalUnitImpl.createPixel(null, 10);
156+
final Property prop = new Property("color", new CSSValueImpl(lu, true), false);
157+
158+
assertFalse(prop.equals("not a property"));
159+
}
160+
161+
/**
162+
* Test hashCode consistency.
163+
* @throws Exception if any error occurs
164+
*/
165+
@Test
166+
public void hashCodeConsistency() throws Exception {
167+
final LexicalUnit lu = LexicalUnitImpl.createPixel(null, 10);
168+
final Property prop = new Property("color", new CSSValueImpl(lu, true), false);
169+
170+
final int hash1 = prop.hashCode();
171+
final int hash2 = prop.hashCode();
172+
173+
assertEquals(hash1, hash2);
174+
}
97175
}

src/test/java/org/htmlunit/cssparser/parser/condition/PseudoClassConditionTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
/**
2323
* Testcases for {@link PseudoClassCondition}.
24+
*
2425
* @author Ronald Brill
2526
*/
2627
public class PseudoClassConditionTest {
@@ -60,4 +61,58 @@ public void withValue() throws Exception {
6061

6162
assertEquals(":value", c.toString());
6263
}
64+
65+
/**
66+
* @throws Exception if any error occurs
67+
*/
68+
@Test
69+
public void doubleColonPrefix() throws Exception {
70+
final PseudoClassCondition c = new PseudoClassCondition("hover", null, true);
71+
assertEquals("hover", c.getValue());
72+
assertEquals("::hover", c.toString());
73+
}
74+
75+
/**
76+
* @throws Exception if any error occurs
77+
*/
78+
@Test
79+
public void singleColonPrefix() throws Exception {
80+
final PseudoClassCondition c = new PseudoClassCondition("hover", null, false);
81+
assertEquals("hover", c.getValue());
82+
assertEquals(":hover", c.toString());
83+
}
84+
85+
/**
86+
* @throws Exception if any error occurs
87+
*/
88+
@Test
89+
public void doubleColonWithNullValue() throws Exception {
90+
final PseudoClassCondition c = new PseudoClassCondition(null, null, true);
91+
assertNull(c.getValue());
92+
assertNull(c.toString());
93+
}
94+
95+
/**
96+
* @throws Exception if any error occurs
97+
*/
98+
@Test
99+
public void conditionType() throws Exception {
100+
final PseudoClassCondition c = new PseudoClassCondition("active", null, false);
101+
assertEquals(Condition.ConditionType.PSEUDO_CLASS_CONDITION, c.getConditionType());
102+
}
103+
104+
/**
105+
* @throws Exception if any error occurs
106+
*/
107+
@Test
108+
public void variousPseudoClassesDoubleColon() throws Exception {
109+
final PseudoClassCondition c1 = new PseudoClassCondition("first-child", null, true);
110+
assertEquals("::first-child", c1.toString());
111+
112+
final PseudoClassCondition c2 = new PseudoClassCondition("last-child", null, true);
113+
assertEquals("::last-child", c2.toString());
114+
115+
final PseudoClassCondition c3 = new PseudoClassCondition("nth-child(2n)", null, true);
116+
assertEquals("::nth-child(2n)", c3.toString());
117+
}
63118
}

src/test/java/org/htmlunit/cssparser/parser/media/MediaQueryTest.java

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
package org.htmlunit.cssparser.parser.media;
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
1820

1921
import org.htmlunit.cssparser.dom.CSSValueImpl;
2022
import org.htmlunit.cssparser.dom.Property;
2123
import org.junit.jupiter.api.Test;
2224

2325
/**
2426
* Testcases for {@link MediaQuery}.
27+
*
2528
* @author Ronald Brill
2629
*/
2730
public class MediaQueryTest {
@@ -76,4 +79,156 @@ public void media() throws Exception {
7679
final MediaQuery mq = new MediaQuery("test");
7780
assertEquals("test", mq.getMedia());
7881
}
82+
83+
/**
84+
* @throws Exception if any error occurs
85+
*/
86+
@Test
87+
public void isOnly() throws Exception {
88+
MediaQuery mq = new MediaQuery("screen", false, false);
89+
assertFalse(mq.isOnly());
90+
91+
mq = new MediaQuery("screen", true, false);
92+
assertTrue(mq.isOnly());
93+
}
94+
95+
/**
96+
* @throws Exception if any error occurs
97+
*/
98+
@Test
99+
public void isNot() throws Exception {
100+
MediaQuery mq = new MediaQuery("screen", false, false);
101+
assertFalse(mq.isNot());
102+
103+
mq = new MediaQuery("screen", false, true);
104+
assertTrue(mq.isNot());
105+
}
106+
107+
/**
108+
* @throws Exception if any error occurs
109+
*/
110+
@Test
111+
public void onlyAndNot() throws Exception {
112+
// If both only and not are true, only takes precedence
113+
final MediaQuery mq = new MediaQuery("screen", true, true);
114+
assertEquals("only screen", mq.toString());
115+
}
116+
117+
/**
118+
* @throws Exception if any error occurs
119+
*/
120+
@Test
121+
public void nullMediaType() throws Exception {
122+
final MediaQuery mq = new MediaQuery(null);
123+
// null media is converted to "all" but implicitAll is set
124+
// so toString doesn't show it unless there are properties
125+
assertEquals("", mq.toString());
126+
assertEquals("all", mq.getMedia());
127+
}
128+
129+
/**
130+
* @throws Exception if any error occurs
131+
*/
132+
@Test
133+
public void emptyMediaType() throws Exception {
134+
final MediaQuery mq = new MediaQuery("");
135+
assertEquals("", mq.toString());
136+
assertEquals("", mq.getMedia());
137+
}
138+
139+
/**
140+
* @throws Exception if any error occurs
141+
*/
142+
@Test
143+
public void multipleProperties() throws Exception {
144+
final MediaQuery mq = new MediaQuery("screen");
145+
146+
final CSSValueImpl value1 = new CSSValueImpl(null);
147+
value1.setCssText("800px");
148+
final Property prop1 = new Property("min-width", value1, false);
149+
mq.addMediaProperty(prop1);
150+
151+
final CSSValueImpl value2 = new CSSValueImpl(null);
152+
value2.setCssText("1200px");
153+
final Property prop2 = new Property("max-width", value2, false);
154+
mq.addMediaProperty(prop2);
155+
156+
final CSSValueImpl value3 = new CSSValueImpl(null);
157+
value3.setCssText("landscape");
158+
final Property prop3 = new Property("orientation", value3, false);
159+
mq.addMediaProperty(prop3);
160+
161+
assertEquals("screen and (min-width: 800px) and (max-width: 1200px) and (orientation: landscape)",
162+
mq.toString());
163+
assertEquals(3, mq.getProperties().size());
164+
165+
Property prop = mq.getProperties().get(0);
166+
assertEquals("min-width: 800px", prop.toString());
167+
168+
prop = mq.getProperties().get(1);
169+
assertEquals("max-width: 1200px", prop.toString());
170+
171+
prop = mq.getProperties().get(2);
172+
assertEquals("orientation: landscape", prop.toString());
173+
}
174+
175+
/**
176+
* @throws Exception if any error occurs
177+
*/
178+
@Test
179+
public void commonMediaTypes() throws Exception {
180+
MediaQuery mq = new MediaQuery("all");
181+
assertEquals("all", mq.toString());
182+
183+
mq = new MediaQuery("screen");
184+
assertEquals("screen", mq.toString());
185+
186+
mq = new MediaQuery("print");
187+
assertEquals("print", mq.toString());
188+
189+
mq = new MediaQuery("speech");
190+
assertEquals("speech", mq.toString());
191+
}
192+
193+
/**
194+
* @throws Exception if any error occurs
195+
*/
196+
@Test
197+
public void onlyWithProperties() throws Exception {
198+
final MediaQuery mq = new MediaQuery("print", true, false);
199+
200+
final CSSValueImpl value = new CSSValueImpl(null);
201+
value.setCssText("300dpi");
202+
final Property prop1 = new Property("resolution", value, false);
203+
mq.addMediaProperty(prop1);
204+
205+
assertEquals("only print and (resolution: 300dpi)", mq.toString());
206+
assertTrue(mq.isOnly());
207+
assertFalse(mq.isNot());
208+
assertEquals(1, mq.getProperties().size());
209+
210+
final Property prop = mq.getProperties().get(0);
211+
assertEquals("resolution: 300dpi", prop.toString());
212+
}
213+
214+
/**
215+
* @throws Exception if any error occurs
216+
*/
217+
@Test
218+
public void notWithProperties() throws Exception {
219+
final MediaQuery mq = new MediaQuery("screen", false, true);
220+
221+
final CSSValueImpl value = new CSSValueImpl(null);
222+
value.setCssText("600px");
223+
final Property prop1 = new Property("max-width", value, false);
224+
mq.addMediaProperty(prop1);
225+
226+
assertEquals("not screen and (max-width: 600px)", mq.toString());
227+
assertFalse(mq.isOnly());
228+
assertTrue(mq.isNot());
229+
assertEquals(1, mq.getProperties().size());
230+
231+
final Property prop = mq.getProperties().get(0);
232+
assertEquals("max-width: 600px", prop.toString());
233+
}
79234
}

0 commit comments

Comments
 (0)