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