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 WeakMap.
25   *
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class WeakMapTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts({"true", "one"})
37      public void constructorArray() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html><head>\n"
40              + "<script>\n"
41              + LOG_TITLE_FUNCTION
42              + "function test() {\n"
43              + "  var obj = {};\n"
44              + "  var foo = {};\n"
45              + "  var myMap = new WeakMap([[ obj, 'one' ],[ foo, 'two' ]]);\n"
46              + "  log(myMap.has(foo));\n"
47              + "  log(myMap.get(obj));\n"
48              + "}\n"
49              + "</script></head><body onload='test()'>\n"
50              + "</body></html>";
51  
52          loadPageVerifyTitle2(html);
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts("TypeError")
60      public void constructorSetParam() throws Exception {
61          final String html = DOCTYPE_HTML
62              + "<html><head>\n"
63              + "<script>\n"
64              + LOG_TITLE_FUNCTION
65              + "function test() {\n"
66              + "  try {\n"
67              + "    var myMap = new WeakMap(new Set('test'));\n"
68              + "    log(myMap.has('test'));\n"
69              + "  } catch(e) {\n"
70              + "    logEx(e);\n"
71              + "  }\n"
72              + "}\n"
73              + "</script></head><body onload='test()'>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts("true")
84      public void constructorMapParam() throws Exception {
85          final String html = DOCTYPE_HTML
86              + "<html><head>\n"
87              + "<script>\n"
88              + LOG_TITLE_FUNCTION
89              + "function test() {\n"
90              + "  var obj = {};\n"
91              + "  var foo = {};\n"
92              + "  var testMap = new Map([[ obj, 'one' ],[ foo, 'two' ]]);\n"
93              + "  try {\n"
94              + "    var myMap = new WeakMap(testMap);\n"
95              + "    log(myMap.has(foo));\n"
96              + "  } catch(e) {\n"
97              + "    logEx(e);\n"
98              + "  }\n"
99              + "}\n"
100             + "</script></head><body onload='test()'>\n"
101             + "</body></html>";
102 
103         loadPageVerifyTitle2(html);
104     }
105 
106     /**
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts("true")
111     public void constructorIteratorParam() throws Exception {
112         final String html = DOCTYPE_HTML
113             + "<html><head>\n"
114             + "<script>\n"
115             + LOG_TITLE_FUNCTION
116             + "function test() {\n"
117             + "  if (window.WeakSet) {\n"
118             + "    var obj = {};\n"
119             + "    var foo = {};\n"
120             + "    var myIterable = {};\n"
121             + "    myIterable[Symbol.iterator] = function() {\n"
122             + "      return {\n"
123             + "        next: function() {\n"
124             + "          if (this._first) {;\n"
125             + "            this._first = false;\n"
126             + "            return { value: [ foo, 'one' ], done: false };\n"
127             + "          }\n"
128             + "          return { done: true };\n"
129             + "        },\n"
130             + "        _first: true\n"
131             + "      };\n"
132             + "    };\n"
133             + "    try {\n"
134             + "      var myMap = new WeakMap(myIterable);\n"
135             + "      log(myMap.has(foo));\n"
136             + "    } catch(e) {\n"
137             + "      logEx(e);\n"
138             + "    }\n"
139             + "  }"
140             + "}\n"
141             + "</script></head>\n"
142             + "<body onload='test()'>\n"
143             + "</body></html>";
144 
145         loadPageVerifyTitle2(html);
146     }
147 
148     /**
149      * @throws Exception if the test fails
150      */
151     @Test
152     @Alerts({"undefined", "value2"})
153     public void get() throws Exception {
154         final String html = DOCTYPE_HTML
155             + "<html><head>\n"
156             + "<script>\n"
157             + LOG_TITLE_FUNCTION
158             + "  function test() {\n"
159             + "    var kvArray = [[{}, 'value1'], [window, 'value2']];\n"
160             + "    var myMap = new WeakMap(kvArray);\n"
161             + "    log(myMap.size);\n"
162             + "    log(myMap.get(window));\n"
163             + "  }\n"
164             + "</script></head><body onload='test()'>\n"
165             + "</body></html>";
166         loadPageVerifyTitle2(html);
167     }
168 
169     /**
170      * @throws Exception if the test fails
171      */
172     @Test
173     @Alerts("TypeError")
174     public void setNonObject() throws Exception {
175         final String html = DOCTYPE_HTML
176             + "<html><head>\n"
177             + "<script>\n"
178             + LOG_TITLE_FUNCTION
179             + "  function test() {\n"
180             + "    if (window.WeakMap) {\n"
181             + "      var kvArray = [[{}, 'value1'], [window, 'value2']];\n"
182             + "      var myMap = new WeakMap(kvArray);\n"
183             + "      try {\n"
184             + "        myMap.set(1, 2);\n"
185             + "      } catch(e) { logEx(e) }\n"
186             + "    }\n"
187             + "  }\n"
188             + "</script></head><body onload='test()'>\n"
189             + "</body></html>";
190         loadPageVerifyTitle2(html);
191     }
192 }