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 WeakSet.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  public class WeakSetTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("true")
34      public void constructorArray() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><head>\n"
37              + "<script>\n"
38              + LOG_TITLE_FUNCTION
39              + "function test() {\n"
40              + "  if (window.WeakSet) {\n"
41              + "    var obj = {};\n"
42              + "    var foo = {};\n"
43              + "    var mySet = new WeakSet([obj, foo]);\n"
44              + "    log(mySet.has(foo));\n"
45              + "  }\n"
46              + "}\n"
47              + "</script></head><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({ "false", "true"})
58      public void constructorSetParam() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><head>\n"
61              + "<script>\n"
62              + LOG_TITLE_FUNCTION
63              + "function test() {\n"
64              + "  if (window.WeakSet) {\n"
65              + "    var obj = {};\n"
66              + "    var foo = {};\n"
67              + "    var mySet = new WeakSet(new Set([foo]));\n"
68              + "    log(mySet.has(obj));\n"
69              + "    log(mySet.has(foo));\n"
70              + "  }\n"
71              + "}\n"
72              + "</script></head><body onload='test()'>\n"
73              + "</body></html>";
74  
75          loadPageVerifyTitle2(html);
76      }
77  
78      /**
79       * @throws Exception if the test fails
80       */
81      @Test
82      @Alerts({"false", "false", "false"})
83      public void constructorMapParam() throws Exception {
84          final String html = DOCTYPE_HTML
85              + "<html><head>\n"
86              + "<script>\n"
87              + LOG_TITLE_FUNCTION
88              + "function test() {\n"
89              + "  if (window.WeakSet) {\n"
90              + "    var obj = {};\n"
91              + "    var foo = {};\n"
92              + "    var kvArray = [['key1', obj], ['key2', foo]];\n"
93              + "    var myMap = new Map(kvArray);\n"
94              + "    var mySet = new WeakSet(myMap);\n"
95              + "    log(mySet.has('key1'));\n"
96              + "    log(mySet.has(obj));\n"
97              + "    log(mySet.has(kvArray[1]));\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", "false"})
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 logElement(value) {\n"
117             + "  log(value);\n"
118             + "}\n"
119             + "function test() {\n"
120             + "  if (window.WeakSet) {\n"
121             + "    var obj = {};\n"
122             + "    var foo = {};\n"
123             + "    var myIterable = {};\n"
124             + "    myIterable[Symbol.iterator] = function() {\n"
125             + "      return {\n"
126             + "        next: function() {\n"
127             + "          if (this._first) {;\n"
128             + "            this._first = false;\n"
129             + "            return { value: foo, done: false };\n"
130             + "          }\n"
131             + "          return { done: true };\n"
132             + "        },\n"
133             + "        _first: true\n"
134             + "      };\n"
135             + "    };\n"
136             + "    var mySet = new WeakSet(myIterable);\n"
137             + "    log(mySet.has(foo));\n"
138             + "    log(mySet.has(obj));\n"
139             + "  }\n"
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", "true"})
153     public void has() throws Exception {
154         final String html = DOCTYPE_HTML
155             + "<html><head>\n"
156             + "<script>\n"
157             + LOG_TITLE_FUNCTION
158             + "  function test() {\n"
159             + "    if (window.WeakSet) {\n"
160             + "      var obj = {};\n"
161             + "      var foo = {};\n"
162             + "      var myArray = [obj, foo];\n"
163             + "      var mySet = new WeakSet(myArray);\n"
164             + "      mySet.add(window);\n"
165             + "      log(mySet.size);\n"
166             + "      log(mySet.has(window));\n"
167             + "    }\n"
168             + "  }\n"
169             + "</script></head><body onload='test()'>\n"
170             + "</body></html>";
171         loadPageVerifyTitle2(html);
172     }
173 
174     /**
175      * @throws Exception if the test fails
176      */
177     @Test
178     @Alerts({"Type error", "Type error", "true"})
179     public void add() throws Exception {
180         final String html = DOCTYPE_HTML
181             + "<html><head>\n"
182             + "<script>\n"
183             + LOG_TITLE_FUNCTION
184             + "  function test() {\n"
185             + "    if (window.WeakSet) {\n"
186             + "      var mySet = new WeakSet();\n"
187 
188             + "      try {\n"
189             + "        mySet.add(7);\n"
190             + "      } catch(e) { log('Type error'); }\n"
191 
192             + "      try {\n"
193             + "        mySet.add('seven');\n"
194             + "      } catch(e) { log('Type error'); }\n"
195 
196             + "      var foo = {};\n"
197             + "      mySet.add(foo);\n"
198             + "      log(mySet.has(foo));\n"
199             + "    }\n"
200             + "  }\n"
201             + "</script></head><body onload='test()'>\n"
202             + "</body></html>";
203         loadPageVerifyTitle2(html);
204     }
205 }