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.intl;
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 V8BreakIterator}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   *
27   * @see <a href="https://code.google.com/p/v8/source/browse#svn%2Ftrunk%2Ftest%2Fintl%2Fbreak-iterator">
28   *          https://code.google.com/p/v8/source/browse#svn%2Ftrunk%2Ftest%2Fintl%2Fbreak-iterator</a>
29   */
30  public class V8BreakIteratorTest extends WebDriverTestCase {
31  
32      private static final String LINE_ = "line";
33      private static final String CHARACTER_ = "character";
34      private static final String SENTENCE_ = "sentence";
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts(DEFAULT = "no support",
41              CHROME = "true",
42              EDGE = "true")
43      public void v8BreakIterator() throws Exception {
44          final String html = DOCTYPE_HTML
45                  + "<html><head>\n"
46                  + "<script>\n"
47                  + LOG_TITLE_FUNCTION
48                  + "  function test() {\n"
49                  + "    if (window.Intl && window.Intl.v8BreakIterator) {\n"
50                  + "      var iterator = Intl.v8BreakIterator('en');\n"
51                  + "      log(iterator instanceof Intl.v8BreakIterator);\n"
52                  + "    } else { log('no support'); }\n"
53                  + "  }\n"
54                  + "</script>\n"
55                  + "</head>\n"
56                  + "<body onload='test()'>\n"
57                  + "</body></html>";
58  
59          loadPageVerifyTitle2(html);
60      }
61  
62      /**
63       * @throws Exception if the test fails
64       */
65      @Test
66      @Alerts(DEFAULT = "no support",
67              CHROME = {"0", "none", "0", "none", "0", "none", "4", "letter", "5", "none", "8", "letter", "9", "none",
68                  "13", "letter", "14", "none", "15", "none", "19", "letter", "20", "none", "24", "letter", "25", "none",
69                  "29", "letter", "30", "none", "30", "none"},
70              EDGE = {"0", "none", "0", "none", "0", "none", "4", "letter", "5", "none", "8", "letter", "9", "none",
71                  "13", "letter", "14", "none", "15", "none", "19", "letter", "20", "none", "24", "letter", "25", "none",
72                  "29", "letter", "30", "none", "30", "none"})
73      public void sample() throws Exception {
74          test("en", null, "Jack and Jill, went over hill!");
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts(DEFAULT = "no support",
82              CHROME = {"0", "none", "0", "none", "0", "none", "5", "none", "9", "none", "15", "none", "20", "none", "25",
83                  "none", "30", "none", "30", "none"},
84              EDGE = {"0", "none", "0", "none", "0", "none", "5", "none", "9", "none", "15", "none", "20", "none", "25",
85                  "none", "30", "none", "30", "none"})
86      public void sampleLine() throws Exception {
87          test("en", LINE_, "Jack and Jill, went over hill!");
88      }
89  
90      /**
91       * @throws Exception if the test fails
92       */
93      @Test
94      @Alerts(DEFAULT = "no support",
95              CHROME = {"0", "none", "0", "none", "0", "none", "1", "none", "2", "none", "3", "none", "4", "none", "5",
96                  "none", "6", "none", "7", "none", "8", "none", "9", "none", "10",
97                  "none", "11", "none", "12", "none", "13",
98                  "none", "14", "none", "15", "none", "16",
99                  "none", "17", "none", "18", "none", "19", "none", "20", "none",
100                 "21", "none", "22", "none", "23", "none", "24", "none", "25", "none", "26", "none", "27", "none", "28",
101                 "none", "29", "none", "30", "none", "30", "none"},
102             EDGE = {"0", "none", "0", "none", "0", "none", "1", "none", "2", "none", "3", "none", "4", "none", "5",
103                 "none", "6", "none", "7", "none", "8", "none",
104                 "9", "none", "10", "none", "11", "none", "12", "none", "13",
105                 "none", "14", "none", "15", "none", "16", "none",
106                 "17", "none", "18", "none", "19", "none", "20", "none",
107                 "21", "none", "22", "none", "23", "none", "24", "none", "25", "none", "26", "none", "27", "none", "28",
108                 "none", "29", "none", "30", "none", "30", "none"})
109     public void sampleCharacter() throws Exception {
110         test("en", CHARACTER_, "Jack and Jill, went over hill!");
111     }
112 
113     /**
114      * @throws Exception if the test fails
115      */
116     @Test
117     @Alerts(DEFAULT = "no support",
118             CHROME = {"0", "none", "0", "none", "0", "none", "30", "none", "30", "none"},
119             EDGE = {"0", "none", "0", "none", "0", "none", "30", "none", "30", "none"})
120     public void sampleSentence() throws Exception {
121         test("en", SENTENCE_, "Jack and Jill, went over hill!");
122     }
123 
124     /**
125      * @param type can be null
126      */
127     private void test(final String language, final String type, final String text) throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html><head>\n"
130             + "<script>\n"
131             + LOG_TITLE_FUNCTION
132             + "  function test() {\n"
133             + "    if (window.Intl && window.Intl.v8BreakIterator) {\n"
134             + "      var iterator = new Intl.v8BreakIterator('" + language + "'"
135             + (type == null ? "" : ", {type: '" + type + "'}")
136             + ");\n"
137             + "      log1(iterator);\n"
138             + "      var text = '" + text.replace("'", "\\'") + "';\n"
139             + "      iterator.adoptText(text);\n"
140             + "      log1(iterator);\n"
141 
142             + "      var pos = iterator.first();\n"
143             + "      log1(iterator);\n"
144 
145             + "      while (pos !== -1) {\n"
146             + "        var nextPos = iterator.next();\n"
147             + "        log1(iterator);\n"
148             + "        if (nextPos === -1) {\n"
149             + "          break;\n"
150             + "        }\n"
151             + "      }\n"
152             + "    } else { log('no support'); }\n"
153             + "  }\n"
154             + "  function log1(iterator) {\n"
155             + "    log(iterator.current());\n"
156             + "    log(iterator.breakType());\n"
157             + "  }\n"
158             + "</script>\n"
159             + "</head><body onload='test()'>\n"
160             + "</body></html>";
161 
162         loadPageVerifyTitle2(html);
163     }
164 
165     /**
166      * @throws Exception if the test fails
167      */
168     @Test
169     @Alerts(DEFAULT = "no support",
170             CHROME = "en-US",
171             EDGE = "en-US")
172     public void defaultLocale() throws Exception {
173         final String html = DOCTYPE_HTML
174             + "<html><head>\n"
175             + "<script>\n"
176             + LOG_TITLE_FUNCTION
177             + "  function test() {\n"
178             + "    if (window.Intl && window.Intl.v8BreakIterator) {\n"
179             + "      var iterator = new Intl.v8BreakIterator([]);\n"
180             + "      var options = iterator.resolvedOptions();\n"
181             + "      log(options.locale);\n"
182             + "    } else { log('no support'); }\n"
183             + "  }\n"
184             + "</script>\n"
185             + "</head><body onload='test()'>\n"
186             + "</body></html>";
187 
188         loadPageVerifyTitle2(html);
189     }
190 
191     /**
192      * @throws Exception if the test fails
193      */
194     @Test
195     @Alerts(DEFAULT = "no support",
196             CHROME = {"0", "none", "0", "none", "0", "none", "4", "letter", "5", "none", "8", "letter", "9", "none",
197                 "13", "letter", "14", "none", "15", "none", "19", "letter", "20", "none", "24", "letter", "25", "none",
198                 "29", "letter", "30", "none", "31", "none", "34", "letter", "35", "none", "38", "letter", "39", "none",
199                 "43", "letter", "44", "none", "45", "none", "50", "letter", "51", "none", "51", "none"},
200             EDGE = {"0", "none", "0", "none", "0", "none", "4", "letter", "5", "none", "8", "letter", "9", "none",
201                 "13", "letter", "14", "none", "15", "none", "19", "letter", "20", "none", "24", "letter", "25", "none",
202                 "29", "letter", "30", "none", "31", "none", "34", "letter", "35", "none", "38", "letter", "39", "none",
203                 "43", "letter", "44", "none", "45", "none", "50", "letter", "51", "none", "51", "none"})
204     public void enBreak() throws Exception {
205         test("en", null, "Jack and Jill, went over hill, and got lost. Alert!");
206     }
207 
208     /**
209      * @throws Exception if the test fails
210      */
211     @Test
212     @Alerts(DEFAULT = "no support",
213             CHROME = {"0", "none", "0", "none", "0", "none", "1", "none", "2", "none", "3", "none", "4", "none", "5",
214                 "none", "6", "none", "7", "none", "8", "none", "9",
215                 "none", "10", "none", "11", "none", "12", "none", "13",
216                 "none", "14", "none", "15", "none", "15", "none"},
217             EDGE = {"0", "none", "0", "none", "0", "none", "1", "none", "2", "none", "3", "none", "4", "none", "5",
218                 "none", "6", "none", "7", "none", "8", "none", "9",
219                 "none", "10", "none", "11", "none", "12", "none", "13",
220                 "none", "14", "none", "15", "none", "15", "none"})
221     public void zh() throws Exception {
222         test("zh", null, "\u56FD\u52A1\u9662\u5173\u4E8E\u300A\u571F\u5730\u623F\u5C4B\u7BA1\u7406\u6761\u4F8B\u300B");
223     }
224 
225 }