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