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.performance;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  
26  /**
27   * Tests for {@link PerformanceTiming}.
28   *
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class PerformanceTimingTest extends WebDriverTestCase {
33      private static final long NOE = System.currentTimeMillis();
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts("[object PerformanceTiming]")
40      public void available() throws Exception {
41          final String html = DOCTYPE_HTML
42                  + "<html>\n"
43                  + "<head>\n"
44                  + "<script>\n"
45                  + LOG_TITLE_FUNCTION
46                  + "  function test() {\n"
47                  + "    var performanceTiming = performance.timing;\n"
48                  + "    log(performanceTiming);\n"
49                  + "  }\n"
50                  + "  test();\n"
51                  + "</script>\n"
52                  + "</head>\n"
53                  + "<body></body>\n"
54                  + "</html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts("true")
64      public void navigationStart() throws Exception {
65          final String html = DOCTYPE_HTML
66                  + "<html>\n"
67                  + "<head>\n"
68                  + "<script>\n"
69                  + LOG_TITLE_FUNCTION
70                  + "  function test() {\n"
71                  + "    var performanceTiming = performance.timing;\n"
72                  + "    log(performanceTiming.navigationStart > " + NOE + ");\n"
73                  + "  }\n"
74                  + "  test();\n"
75                  + "</script>\n"
76                  + "</head>\n"
77                  + "<body></body>\n"
78                  + "</html>";
79  
80          loadPageVerifyTitle2(html);
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts("0")
88      public void secureConnectionStart() throws Exception {
89          final String html = DOCTYPE_HTML
90                  + "<html>\n"
91                  + "<head>\n"
92                  + "<script>\n"
93                  + LOG_TITLE_FUNCTION
94                  + "  function test() {\n"
95                  + "    var performanceTiming = performance.timing;\n"
96                  + "    log(performanceTiming.secureConnectionStart);\n"
97                  + "  }\n"
98                  + "  test();\n"
99                  + "</script>\n"
100                 + "</head>\n"
101                 + "<body></body>\n"
102                 + "</html>";
103 
104         loadPageVerifyTitle2(html);
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts({"0", "0"})
112     public void unloadEvent() throws Exception {
113         final String html = DOCTYPE_HTML
114                 + "<html>\n"
115                 + "<head>\n"
116                 + "<script>\n"
117                 + LOG_TITLE_FUNCTION
118                 + "  function test() {\n"
119                 + "    var performanceTiming = performance.timing;\n"
120                 + "    log(performanceTiming.unloadEventStart);\n"
121                 + "    log(performanceTiming.unloadEventEnd);\n"
122                 + "  }\n"
123                 + "  test();\n"
124                 + "</script>\n"
125                 + "</head>\n"
126                 + "<body></body>\n"
127                 + "</html>";
128 
129         loadPageVerifyTitle2(html);
130     }
131 
132     /**
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts({"0", "0"})
137     public void redirect() throws Exception {
138         final String html = DOCTYPE_HTML
139                 + "<html>\n"
140                 + "<head>\n"
141                 + "<script>\n"
142                 + LOG_TITLE_FUNCTION
143                 + "  function test() {\n"
144                 + "    var performanceTiming = performance.timing;\n"
145                 + "    log(performanceTiming.redirectStart);\n"
146                 + "    log(performanceTiming.redirectEnd);\n"
147                 + "  }\n"
148                 + "  test();\n"
149                 + "</script>\n"
150                 + "</head>\n"
151                 + "<body></body>\n"
152                 + "</html>";
153 
154         loadPageVerifyTitle2(html);
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts({"true", "true"})
162     public void domainLookup() throws Exception {
163         final String html = DOCTYPE_HTML
164                 + "<html>\n"
165                 + "<head>\n"
166                 + "<script>\n"
167                 + LOG_TITLE_FUNCTION
168                 + "  function test() {\n"
169                 + "    var performanceTiming = performance.timing;\n"
170                 + "    var start = performanceTiming.domainLookupStart;\n"
171                 + "    log(start > " + NOE + ");\n"
172                 + "    log(performanceTiming.domainLookupEnd >= start);\n"
173                 + "  }\n"
174                 + "  test();\n"
175                 + "</script>\n"
176                 + "</head>\n"
177                 + "<body></body>\n"
178                 + "</html>";
179 
180         loadPageVerifyTitle2(html);
181     }
182 
183     /**
184      * @throws Exception if the test fails
185      */
186     @Test
187     @Alerts({"true", "true"})
188     public void response() throws Exception {
189         final String html = DOCTYPE_HTML
190                 + "<html>\n"
191                 + "<head>\n"
192                 + "<script>\n"
193                 + LOG_TITLE_FUNCTION
194                 + "  function test() {\n"
195                 + "    var performanceTiming = performance.timing;\n"
196                 + "    var start = performanceTiming.responseStart;\n"
197                 + "    log(start > " + NOE + ");\n"
198                 + "    log(performanceTiming.responseEnd >= start);\n"
199                 + "  }\n"
200                 + "  test();\n"
201                 + "</script>\n"
202                 + "</head>\n"
203                 + "<body></body>\n"
204                 + "</html>";
205 
206         loadPageVerifyTitle2(html);
207     }
208 
209     /**
210      * @throws Exception if the test fails
211      */
212     @Test
213     @Alerts({"true", "true"})
214     public void loadEvent() throws Exception {
215         final String html = DOCTYPE_HTML
216                 + "<html>\n"
217                 + "<head>\n"
218                 + "<script>\n"
219                 + LOG_TITLE_FUNCTION
220                 + "  function test() {\n"
221                 + "    var performanceTiming = performance.timing;\n"
222                 + "    var start = performanceTiming.loadEventStart;\n"
223                 + "    log(start > " + NOE + ");\n"
224                 + "    log(performanceTiming.loadEventEnd >= start);\n"
225                 + "  }\n"
226                 + "</script>\n"
227                 + "</head>\n"
228                 + "<body>\n"
229                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
230                 + "</body>\n"
231                 + "</html>";
232 
233         final WebDriver driver = loadPage2(html);
234         driver.findElement(By.id("clickMe")).click();
235         verifyTitle2(driver, getExpectedAlerts());
236     }
237 
238     /**
239      * @throws Exception if the test fails
240      */
241     @Test
242     @Alerts({"true", "true"})
243     public void connect() throws Exception {
244         final String html = DOCTYPE_HTML
245                 + "<html>\n"
246                 + "<head>\n"
247                 + "<script>\n"
248                 + LOG_TITLE_FUNCTION
249                 + "  function test() {\n"
250                 + "    var performanceTiming = performance.timing;\n"
251                 + "    var start = performanceTiming.connectStart;\n"
252                 + "    log(start > " + NOE + ");\n"
253                 + "    log(performanceTiming.connectEnd >= start);\n"
254                 + "  }\n"
255                 + "  test();\n"
256                 + "</script>\n"
257                 + "</head>\n"
258                 + "<body></body>\n"
259                 + "</html>";
260 
261         loadPageVerifyTitle2(html);
262     }
263 
264     /**
265      * @throws Exception if the test fails
266      */
267     @Test
268     @Alerts("true")
269     public void fetchStart() throws Exception {
270         final String html = DOCTYPE_HTML
271                 + "<html>\n"
272                 + "<head>\n"
273                 + "<script>\n"
274                 + LOG_TITLE_FUNCTION
275                 + "  function test() {\n"
276                 + "    var performanceTiming = performance.timing;\n"
277                 + "    log(performanceTiming.fetchStart > " + NOE + ");\n"
278                 + "  }\n"
279                 + "  test();\n"
280                 + "</script>\n"
281                 + "</head>\n"
282                 + "<body></body>\n"
283                 + "</html>";
284 
285         loadPageVerifyTitle2(html);
286     }
287 
288     /**
289      * @throws Exception if the test fails
290      */
291     @Test
292     @Alerts({"true", "true"})
293     public void domContentLoadedEvent() throws Exception {
294         final String html = DOCTYPE_HTML
295                 + "<html>\n"
296                 + "<head>\n"
297                 + "<script>\n"
298                 + LOG_TITLE_FUNCTION
299                 + "  function test() {\n"
300                 + "    var performanceTiming = performance.timing;\n"
301                 + "    var start = performanceTiming.domContentLoadedEventStart;\n"
302                 + "    log(start > " + NOE + ");\n"
303                 + "    log(performanceTiming.domContentLoadedEventEnd >= start);\n"
304                 + "  }\n"
305                 + "</script>\n"
306                 + "</head>\n"
307                 + "<body>\n"
308                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
309                 + "</body>\n"
310                 + "</html>";
311 
312         final WebDriver driver = loadPage2(html);
313         driver.findElement(By.id("clickMe")).click();
314         verifyTitle2(driver, getExpectedAlerts());
315     }
316 
317     /**
318      * @throws Exception if the test fails
319      */
320     @Test
321     @Alerts({"true", "true", "true", "true", "true"})
322     public void dom() throws Exception {
323         final String html = DOCTYPE_HTML
324                 + "<html>\n"
325                 + "<head>\n"
326                 + "<script>\n"
327                 + LOG_TITLE_FUNCTION
328                 + "  function test() {\n"
329                 + "    var performanceTiming = performance.timing;\n"
330                 + "    log(performanceTiming.domLoading > " + NOE + ");\n"
331                 + "    log(performanceTiming.domInteractive > " + NOE + ");\n"
332                 + "    log(performanceTiming.domContentLoadedEventStart > " + NOE + ");\n"
333                 + "    log(performanceTiming.domContentLoadedEventEnd > " + NOE + ");\n"
334                 + "    log(performanceTiming.domComplete > " + NOE + ");\n"
335                 + "  }\n"
336                 + "</script>\n"
337                 + "</head>\n"
338                 + "<body>\n"
339                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
340                 + "</body>\n"
341                 + "</html>";
342 
343         final WebDriver driver = loadPage2(html);
344         driver.findElement(By.id("clickMe")).click();
345         verifyTitle2(driver, getExpectedAlerts());
346     }
347 
348     /**
349      * @throws Exception if the test fails
350      */
351     @Test
352     @Alerts("[object Object]")
353     @HtmlUnitNYI(CHROME = {},
354             EDGE = {},
355             FF = {},
356             FF_ESR = {})
357     public void toJSON() throws Exception {
358         final String html = DOCTYPE_HTML
359                 + "<html>\n"
360                 + "<head>\n"
361                 + "<script>\n"
362                 + LOG_TITLE_FUNCTION
363                 + "  function test() {\n"
364                 + "    var performanceTiming = performance.timing;\n"
365                 + "    log(performanceTiming.toJSON());\n"
366                 + "  }\n"
367                 + "</script>\n"
368                 + "</head>\n"
369                 + "<body>\n"
370                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
371                 + "</body>\n"
372                 + "</html>";
373 
374         final WebDriver driver = loadPage2(html);
375         driver.findElement(By.id("clickMe")).click();
376         verifyTitle2(driver, getExpectedAlerts());
377     }
378 }