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.javascript.host;
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 {@link ApplicationCache}.
25   *
26   * @author Daniel Gredler
27   * @author Frank Danek
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class ApplicationCacheTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("undefined")
38      public void scriptableToString() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + LOG_TITLE_FUNCTION
43              + "  function test() {\n"
44              + "    log(window.applicationCache);\n"
45              + "  }\n"
46              + "</script></head>\n"
47              + "<body onload='test()'>\n"
48              + "</body></html>";
49  
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts("no applicationCache")
58      public void onchecking() throws Exception {
59          eventHandler("onchecking");
60      }
61  
62      /**
63       * @throws Exception if the test fails
64       */
65      @Test
66      @Alerts("no applicationCache")
67      public void onerror() throws Exception {
68          eventHandler("onerror");
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      @Alerts("no applicationCache")
76      public void onnoupdate() throws Exception {
77          eventHandler("onnoupdate");
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts("no applicationCache")
85      public void ondownloading() throws Exception {
86          eventHandler("ondownloading");
87      }
88  
89      /**
90       * @throws Exception if the test fails
91       */
92      @Test
93      @Alerts("no applicationCache")
94      public void onprogress() throws Exception {
95          eventHandler("onprogress");
96      }
97  
98      /**
99       * @throws Exception if the test fails
100      */
101     @Test
102     @Alerts("no applicationCache")
103     public void onupdateready() throws Exception {
104         eventHandler("onupdateready");
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts("no applicationCache")
112     public void oncached() throws Exception {
113         eventHandler("oncached");
114     }
115 
116     private void eventHandler(final String handler) throws Exception {
117         final String html = DOCTYPE_HTML
118             + "<html><head>\n"
119             + "<script>\n"
120             + LOG_TITLE_FUNCTION
121             + "  function test() {\n"
122             + "    if (window.applicationCache) {\n"
123             + "      window.applicationCache." + handler + " = function(e) {};\n"
124             + "      var handler = window.applicationCache." + handler + ".toString();\n"
125 
126                      // normalize, testing function.toString() is done somewhere else
127             + "      log(handler.replace(/(\\r|\\n|\\r\\n| )/gm, ''));\n"
128             + "    } else {\n"
129             + "      log('no applicationCache');\n"
130             + "    }\n"
131             + "  }\n"
132             + "</script></head>\n"
133             + "<body onload='test()'>\n"
134             + "</body></html>";
135 
136         loadPageVerifyTitle2(html);
137     }
138 
139     /**
140      * @throws Exception if the test fails
141      */
142     @Test
143     @Alerts("no applicationCache")
144     public void eventListener() throws Exception {
145         final String html = DOCTYPE_HTML
146             + "<html><head>\n"
147             + "<script>\n"
148             + LOG_TITLE_FUNCTION
149             + "  function test() {\n"
150             + "    if (window.applicationCache) {\n"
151             + "      log(window.applicationCache.addEventListener == null);\n"
152             + "      log(window.applicationCache.removeEventListener == null);\n"
153             + "      log(window.applicationCache.dispatchEvent == null);\n"
154 
155             + "      log(window.applicationCache.attachEvent == null);\n"
156             + "      log(window.applicationCache.detachEvent == null);\n"
157             + "    } else {\n"
158             + "      log('no applicationCache');\n"
159             + "    }\n"
160             + "  }\n"
161             + "</script></head>\n"
162             + "<body onload='test()'>\n"
163             + "</body></html>";
164 
165         loadPageVerifyTitle2(html);
166     }
167 }