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