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;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.htmlunit.util.MimeType;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  
25  /**
26   * Tests for general Rhino problems.
27   *
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class RhinoTest extends WebDriverTestCase {
32  
33      /**
34       * Some samples from
35       * <a href="https://stackoverflow.com/questions/10480108/is-there-any-way-to-check-if-strict-mode-is-enforced">
36       * https://stackoverflow.com/questions/10480108/is-there-any-way-to-check-if-strict-mode-is-enforced</a>.
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts({"true", "true"})
41      @HtmlUnitNYI(CHROME = {"false", "false"},
42              EDGE = {"false", "false"},
43              FF = {"false", "false"},
44              FF_ESR = {"false", "false"})
45      public void isStrict_GlobalThis() throws Exception {
46          final String html = DOCTYPE_HTML
47              + "<html>\n"
48              + "<body>\n"
49              + "<script>\n"
50              + "  'use strict'\n"
51              + LOG_TITLE_FUNCTION
52  
53              + "  var isStrict = (function() { return !this; })();\n"
54              + "  log(isStrict);\n"
55  
56              + "  isStrict = (function() { return !!!this; })();\n"
57              + "  log(isStrict);\n"
58              + "</script>\n"
59              + "</body></html>";
60  
61          loadPageVerifyTitle2(html);
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      @Alerts("true")
69      @HtmlUnitNYI(CHROME = "false",
70              EDGE = "false",
71              FF = "false",
72              FF_ESR = "false")
73      public void isStrict_evalVar() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html>\n"
76              + "<body>\n"
77              + "<script>\n"
78              + "  'use strict'\n"
79              + LOG_TITLE_FUNCTION
80  
81              + "  function isStrict() {\n"
82              + "    var x = true;\n"
83              + "    eval('var x = false');\n"
84              + "    return x;\n"
85              + "  }\n"
86  
87              + "  log(isStrict());\n"
88              + "</script>\n"
89              + "</body></html>";
90  
91          loadPageVerifyTitle2(html);
92      }
93  
94      /**
95       * @throws Exception if the test fails
96       */
97      @Test
98      @Alerts("true")
99      public void isStrict_argumentsCallee() throws Exception {
100         final String html = DOCTYPE_HTML
101             + "<html>\n"
102             + "<body>\n"
103             + "<script>\n"
104             + "  'use strict'\n"
105             + LOG_TITLE_FUNCTION
106 
107             + "  function isStrict() {\n"
108             + "    try { arguments.callee } catch(e) { return true; };"
109             + "    return false;\n"
110             + "  }\n"
111 
112             + "  log(isStrict());\n"
113             + "</script>\n"
114             + "</body></html>";
115 
116         loadPageVerifyTitle2(html);
117     }
118 
119     /**
120      * @throws Exception if the test fails
121      */
122     @Test
123     @Alerts({"true", "true", "true"})
124     public void isStrict_constructor() throws Exception {
125         final String html = DOCTYPE_HTML
126             + "<html>\n"
127             + "<body>\n"
128             + "<script>\n"
129             + "  'use strict'\n"
130             + LOG_TITLE_FUNCTION
131 
132             + "  function Foo () {}\n"
133 
134             + "  try {\n"
135             + "    true.constructor = Foo;\n"
136             + "    log('true.constructor');\n"
137             + "  } catch(e) { log(e instanceof TypeError); }\n"
138 
139             + "  try {\n"
140             + "    var o = 1;\n"
141             + "    o.constructor = Foo;\n"
142             + "    log('1.constructor');\n"
143             + "  } catch(e) { log(e instanceof TypeError); }\n"
144 
145             + "  try {\n"
146             + "    'test'.constructor = Foo;\n"
147             + "    log('test.constructor');\n"
148             + "  } catch(e) { log(e instanceof TypeError); }\n"
149 
150             + "</script>\n"
151             + "</body></html>";
152 
153         loadPageVerifyTitle2(html);
154     }
155 
156     /**
157      * @throws Exception if the test fails
158      */
159     @Test
160     @Alerts("from script")
161     public void consStringAsSetterFunctionParam() throws Exception {
162         final String html = DOCTYPE_HTML
163                 + "<html>\n"
164                 + "  <head>\n"
165                 + "    <script>\n"
166                 + LOG_TITLE_FUNCTION
167                 + "      function test() {\n"
168 
169                 + "        var script = document.createElement('script');\n"
170                 + "        script.id = 'b';\n"
171                 + "        script.type = 'text/javascript';\n"
172                 + "        Object.defineProperty(script, 'source', {\n"
173                 + "          get: function() { return this.src },\n"
174                 + "          set: function(source) {\n"
175                 + "            var srcDesc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(script), 'src');\n"
176                 + "            srcDesc.set.call(this, source);\n"
177                 + "          }\n"
178                 + "        });\n"
179 
180                 + "        var part1 = 'sc';\n"
181                 + "        script.source = part1 + 'r' + 'ipt.js';\n"
182                 + "        document.body.appendChild(script);\n"
183                 + "      }\n"
184                 + "    </script>\n"
185                 + "  </head>\n"
186                 + "  <body onload='test()'>\n"
187                 + "  </body></html>";
188 
189         final String js = "log('from script');";
190         getMockWebConnection().setDefaultResponse(js, MimeType.TEXT_JAVASCRIPT);
191 
192         loadPageVerifyTitle2(html);
193     }
194 }