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