|
15 | 15 | package org.htmlunit.cssparser.parser.media; |
16 | 16 |
|
17 | 17 | 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; |
18 | 20 |
|
19 | 21 | import org.htmlunit.cssparser.dom.CSSValueImpl; |
20 | 22 | import org.htmlunit.cssparser.dom.Property; |
21 | 23 | import org.junit.jupiter.api.Test; |
22 | 24 |
|
23 | 25 | /** |
24 | 26 | * Testcases for {@link MediaQuery}. |
| 27 | + * |
25 | 28 | * @author Ronald Brill |
26 | 29 | */ |
27 | 30 | public class MediaQueryTest { |
@@ -76,4 +79,156 @@ public void media() throws Exception { |
76 | 79 | final MediaQuery mq = new MediaQuery("test"); |
77 | 80 | assertEquals("test", mq.getMedia()); |
78 | 81 | } |
| 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 | + } |
79 | 234 | } |
0 commit comments