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 java.net.URL;
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.htmlunit.SimpleWebTestCase;
22  import org.htmlunit.WebConsole;
23  import org.htmlunit.WebConsole.Logger;
24  import org.htmlunit.junit.annotation.Alerts;
25  import org.htmlunit.util.MimeType;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests for Console.
30   *
31   * @author Ahmed Ashour
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   */
35  public class Console2Test extends SimpleWebTestCase {
36  
37      private final class LoggerMock implements Logger {
38          private final List<String> messages_;
39  
40          private LoggerMock(final List<String> messages) {
41              messages_ = messages;
42          }
43  
44          @Override
45          public void warn(final Object message) {
46          }
47  
48          @Override
49          public void trace(final Object message) {
50          }
51  
52          @Override
53          public void info(final Object message) {
54              messages_.add("info: " + message);
55          }
56  
57          @Override
58          public void error(final Object message) {
59          }
60  
61          @Override
62          public void debug(final Object message) {
63          }
64  
65          @Override
66          public boolean isTraceEnabled() {
67              return false;
68          }
69  
70          @Override
71          public boolean isDebugEnabled() {
72              return false;
73          }
74  
75          @Override
76          public boolean isInfoEnabled() {
77              return true;
78          }
79  
80          @Override
81          public boolean isWarnEnabled() {
82              return true;
83          }
84  
85          @Override
86          public boolean isErrorEnabled() {
87              return true;
88          }
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts("info: [\"one\",\"two\",\"three\",{}]")
96      public void log() throws Exception {
97          log("['one', 'two', 'three', document.body.children]");
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts("info: string: HtmlUnit; numb: 4, 4; float: 4.2, link: \"http://htmlunit.sourceforge.net/\"")
105     public void logSimplePlaceholder() throws Exception {
106         log("'string: %s; numb: %d, %i; float: %f, link: %o', 'HtmlUnit', 4.2, 4, 4.2,"
107                 + " 'http://htmlunit.sourceforge.net/'");
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts("info: string: %s; %i;")
115     public void logMissingParam() throws Exception {
116         log("'string: %s; %i;'");
117     }
118 
119     /**
120      * @throws Exception if the test fails
121      */
122     @Test
123     @Alerts("info: string: #; %i; %i;")
124     public void logMissingParam2() throws Exception {
125         log("'string: %s; %i; %i;', '#'");
126     }
127 
128     /**
129      * @throws Exception if the test fails
130      */
131     @Test
132     @Alerts("info: string: param1; param2")
133     public void logMissingPlaceholder() throws Exception {
134         log("'string: %s;', 'param1', 'param2'");
135     }
136 
137     /**
138      * @throws Exception if the test fails
139      */
140     @Test
141     @Alerts("info: %i; 1; %i; % 2 3 4")
142     public void logEscaping() throws Exception {
143         log("'%%i; %i; %%i; %', 1, 2, 3, 4");
144     }
145 
146     /**
147      * @throws Exception if the test fails
148      */
149     @Test
150     @Alerts("info: 12%i3; 4")
151     public void logContinous() throws Exception {
152         log("'%i%i%%i%i;', 1, 2, 3, 4");
153     }
154 
155     /**
156      * @throws Exception if the test fails
157      */
158     @Test
159     @Alerts("info: %x 1 10%  % ; 2")
160     public void logPercent() throws Exception {
161         log("'%x %i 10%  %% ;', 1, 2");
162     }
163 
164     /**
165      * Regression test for issue #1711.
166      * @throws Exception if the test fails
167      */
168     @Test
169     @Alerts("info: $Version$")
170     public void logDollar() throws Exception {
171         log("'%s', '$Version$'");
172     }
173 
174     private void log(final String logInput) throws Exception {
175         final WebConsole console = getWebClient().getWebConsole();
176         final List<String> messages = new ArrayList<>();
177         console.setLogger(new LoggerMock(messages));
178 
179         final String html = DOCTYPE_HTML
180             + "<html><head>\n"
181             + "<script>\n"
182             + "function test() {\n"
183             + "  window.console.log(" + logInput + ");\n"
184             + "}\n"
185             + "</script>\n"
186             + "</head>\n"
187             + "<body onload='test()'></body>\n"
188             + "</html>";
189 
190         loadPage(html);
191         assertEquals(getExpectedAlerts(), messages);
192     }
193 
194     /**
195      * @throws Exception if the test fails
196      */
197     @Test
198     @Alerts("info: from worker")
199     public void fromWorker() throws Exception {
200         final WebConsole console = getWebClient().getWebConsole();
201         final List<String> messages = new ArrayList<>();
202         console.setLogger(new LoggerMock(messages));
203 
204         final String workerJs = "console.log('from worker');\n";
205         getMockWebConnection().setResponse(new URL(URL_FIRST, "worker.js"), workerJs, MimeType.TEXT_JAVASCRIPT);
206 
207         final String html = DOCTYPE_HTML
208                 + "<html><body>\n"
209                 + "<script async>\n"
210                 + "try {\n"
211                 + "  var myWorker = new Worker('worker.js');\n"
212                 + "} catch(e) { logEx(e); }\n"
213                 + "</script></body></html>\n";
214 
215         loadPage(html);
216         Thread.sleep(100);
217         assertEquals(getExpectedAlerts(), messages);
218     }
219 }