View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for HtmlUnit's support of IE conditional comments.
23   * Due to current implementation, conditional comment expressions get evaluated only when the simulated browser is IE.
24   *
25   * @author Marc Guillemot
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   */
29  public class IEConditionalCommentExpressionEvaluatorTest extends WebDriverTestCase {
30  
31      /**
32       * Test for expression {@code if IE}.
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("done")
37      public void IE() throws Exception {
38          doTest("IE");
39      }
40  
41      /**
42       * Test for expression {@code if IE 5}.
43       * @throws Exception if the test fails
44       */
45      @Test
46      @Alerts("done")
47      public void IE_5() throws Exception {
48          doTest("IE 5");
49      }
50  
51      /**
52       * Test for expression {@code if IE 6}.
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts("done")
57      public void IE_6() throws Exception {
58          doTest("IE 6");
59      }
60  
61      /**
62       * Test for expression {@code if IE 7}.
63       * @throws Exception if the test fails
64       */
65      @Test
66      @Alerts("done")
67      public void IE_7() throws Exception {
68          doTest("IE 7");
69      }
70  
71      /**
72       * Test for expression {@code if IE 8}.
73       * @throws Exception if the test fails
74       */
75      @Test
76      @Alerts("done")
77      public void IE_8() throws Exception {
78          doTest("IE 8");
79      }
80  
81      /**
82       * Test for expression {@code if !IE}.
83       * @throws Exception if the test fails
84       */
85      @Test
86      @Alerts("done")
87      public void notIE() throws Exception {
88          doTest("!IE");
89      }
90  
91      /**
92       * Test for expression {@code if lt IE 5.5}.
93       * @throws Exception if the test fails
94       */
95      @Test
96      @Alerts("done")
97      public void lt_IE_5_5() throws Exception {
98          doTest("lt IE 5.5");
99      }
100 
101     /**
102      * Test for expression {@code if lt IE 6}.
103      * @throws Exception if the test fails
104      */
105     @Test
106     @Alerts("done")
107     public void lt_IE_6() throws Exception {
108         doTest("lt IE 6");
109     }
110 
111     /**
112      * Test for expression {@code if lt IE 7}.
113      * @throws Exception if the test fails
114      */
115     @Test
116     @Alerts("done")
117     public void lt_IE_7() throws Exception {
118         doTest("lt IE 7");
119     }
120 
121     /**
122      * Test for expressions {@code if lt IE 8}.
123      * @throws Exception if the test fails
124      */
125     @Test
126     @Alerts("done")
127     public void lt_IE_8() throws Exception {
128         doTest("lt IE 8");
129     }
130 
131     /**
132      * Test for expression {@code if lt IE 9}.
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts("done")
137     public void lt_IE_9() throws Exception {
138         doTest("lt IE 9");
139     }
140 
141     /**
142      * Test for expression {@code if gt IE 5.5}.
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts("done")
147     public void gt_IE_5_5() throws Exception {
148         doTest("gt IE 5.5");
149     }
150 
151     /**
152      * Test for expression {@code if gt IE 6}.
153      * @throws Exception if the test fails
154      */
155     @Test
156     @Alerts("done")
157     public void gt_IE_6() throws Exception {
158         doTest("gt IE 6");
159     }
160 
161     /**
162      * Test for expression {@code if gt IE 7}.
163      * @throws Exception if the test fails
164      */
165     @Test
166     @Alerts("done")
167     public void gt_IE_7() throws Exception {
168         doTest("gt IE 7");
169     }
170 
171     /**
172      * Test for expression {@code if gt IE 8}.
173      * @throws Exception if the test fails
174      */
175     @Test
176     @Alerts("done")
177     public void gt_IE_8() throws Exception {
178         doTest("gt IE 8");
179     }
180 
181     /**
182      * Test for expression {@code if gte IE 5.5}.
183      * @throws Exception if the test fails
184      */
185     @Test
186     @Alerts("done")
187     public void gte_IE_5_5() throws Exception {
188         doTest("gte IE 5.5");
189     }
190 
191     /**
192      * Test for expression {@code if gte IE 6}.
193      * @throws Exception if the test fails
194      */
195     @Test
196     @Alerts("done")
197     public void gte_IE_6() throws Exception {
198         doTest("gte IE 6");
199     }
200 
201     /**
202      * Test for expressions {@code if gte IE 7}.
203      * @throws Exception if the test fails
204      */
205     @Test
206     @Alerts("done")
207     public void gte_IE_7() throws Exception {
208         doTest("gte IE 7");
209     }
210 
211     /**
212      * Test for expressions {@code if gte IE 8}.
213      * @throws Exception if the test fails
214      */
215     @Test
216     @Alerts("done")
217     public void gte_IE_8() throws Exception {
218         doTest("gte IE 8");
219     }
220 
221     /**
222      * Test for expressions {@code if !(IE 5)}.
223      * @throws Exception if the test fails
224      */
225     @Test
226     @Alerts("done")
227     public void parenthese_5() throws Exception {
228         doTest("!(IE 5)");
229     }
230 
231     /**
232      * Test for expressions {@code if !(IE 6)}.
233      * @throws Exception if the test fails
234      */
235     @Test
236     @Alerts("done")
237     public void parenthese_6() throws Exception {
238         doTest("!(IE 6)");
239     }
240 
241     /**
242      * Test for expressions {@code if !(IE 7)}.
243      * @throws Exception if the test fails
244      */
245     @Test
246     @Alerts("done")
247     public void parenthese_7() throws Exception {
248         doTest("!(IE 7)");
249     }
250 
251     /**
252      * Test for expressions {@code if !(IE 8)}.
253      * @throws Exception if the test fails
254      */
255     @Test
256     @Alerts("done")
257     public void parenthese_8() throws Exception {
258         doTest("!(IE 8)");
259     }
260 
261     /**
262      * Test for expressions {@code (gt IE 6)&(lt IE 8)}.
263      * @throws Exception if the test fails
264      */
265     @Test
266     @Alerts("done")
267     public void and() throws Exception {
268         doTest("(gt IE 6)&(lt IE 8)");
269     }
270 
271     /**
272      * Test for expressions {@code if (IE 6)|(IE 7)}.
273      * @throws Exception if the test fails
274      */
275     @Test
276     @Alerts("done")
277     public void or() throws Exception {
278         doTest("(IE 6)|(IE 7)");
279     }
280 
281     /**
282      * Test for expressions {@code if true}.
283      * @throws Exception if the test fails
284      */
285     @Test
286     @Alerts("done")
287     public void true_() throws Exception {
288         doTest("true");
289     }
290 
291     /**
292      * Test for expressions [if false].
293      * @throws Exception if the test fails
294      */
295     @Test
296     @Alerts("done")
297     public void false_() throws Exception {
298         doTest("false");
299     }
300 
301     /**
302      * Test for expressions with "mso" (HTML code generated by MS Office).
303      * @throws Exception if the test fails
304      */
305     @Test
306     @Alerts("done")
307     public void mso_1() throws Exception {
308         doTest("mso 9");
309     }
310 
311     /**
312      * Test for expressions with "mso" (HTML code generated by MS Office).
313      * @throws Exception if the test fails
314      */
315     @Test
316     @Alerts("done")
317     public void mso_2() throws Exception {
318         doTest("gte mso 9");
319     }
320 
321     /**
322      * Test for expressions with "mso" (HTML code generated by MS Office).
323      * @throws Exception if the test fails
324      */
325     @Test
326     @Alerts("done")
327     public void mso_3() throws Exception {
328         doTest("lt mso 9");
329     }
330 
331     /**
332      * Test for expressions with "mso" (HTML code generated by MS Office).
333      * @throws Exception if the test fails
334      */
335     @Test
336     @Alerts("done")
337     public void mso_4() throws Exception {
338         doTest("lt mso 1");
339     }
340 
341     /**
342      * Test for expressions with unexpected identifier.
343      * @throws Exception if the test fails
344      */
345     @Test
346     @Alerts("done")
347     public void unknown_1() throws Exception {
348         doTest("foo 1");
349     }
350 
351     /**
352      * Test for expressions with unexpected identifier.
353      * @throws Exception if the test fails
354      */
355     @Test
356     @Alerts("done")
357     public void unknown_2() throws Exception {
358         doTest("gte foo 1");
359     }
360 
361     /**
362      * Test for expressions with unexpected identifier.
363      * @throws Exception if the test fails
364      */
365     @Test
366     @Alerts("done")
367     public void unknown_3() throws Exception {
368         doTest("gt foo 1");
369     }
370 
371     /**
372      * Test for expressions with unexpected identifier.
373      * @throws Exception if the test fails
374      */
375     @Test
376     @Alerts("done")
377     public void unknown_4() throws Exception {
378         doTest("lt foo 1");
379     }
380 
381     private void doTest(final String expression) throws Exception {
382         final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
383             + "<html><head>\n"
384             + "<!--[if " + expression + "]><script>alert('cond');</script><![endif]-->\n"
385             + "<script>alert('done');</script>\n"
386             + "</head><body></body></html>";
387         loadPageWithAlerts2(html);
388     }
389 }