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